You are viewing a plain text version of this content. The canonical link for it is here.
Posted to sanselan-commits@incubator.apache.org by cm...@apache.org on 2007/11/29 01:34:18 UTC
svn commit: r599241 - in /incubator/sanselan/trunk/src/test/java/org:
apache/sanselan/ apache/sanselan/common/byteSources/
apache/sanselan/formats/png/ apache/sanselan/test/util/ cmc/ cmc/sanselan/
cmc/sanselan/common/ cmc/sanselan/common/byteSources/ ...
Author: cmchen
Date: Wed Nov 28 17:34:16 2007
New Revision: 599241
URL: http://svn.apache.org/viewvc?rev=599241&view=rev
Log:
part 1 of reverting package structure.
Added:
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java (with props)
incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java (with props)
incubator/sanselan/trunk/src/test/java/org/cmc/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/common/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/common/byteSources/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/formats/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/formats/png/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/test/
incubator/sanselan/trunk/src/test/java/org/cmc/sanselan/test/util/
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest(String testName)
+ {
+ super(testName);
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite(AppTest.class);
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue(true);
+ }
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/AppTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.sanselan.test.util.FSTraversal;
+import org.apache.sanselan.util.Debug;
+
+public abstract class SanselanTest extends TestCase
+{
+ public SanselanTest(String name)
+ {
+ super(name);
+ }
+
+ public void compareByteArrays(byte a[], byte b[])
+ {
+ assertTrue(a.length == b.length);
+ for (int i = 0; i < b.length; i++)
+ assertTrue(b[i] == a[i]);
+ }
+
+ protected void purgeMemory()
+ {
+ try
+ {
+ System.gc();
+ Thread.sleep(50);
+ System.runFinalization();
+ Thread.sleep(50);
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+ }
+
+ protected List getAllTestImages()
+ {
+ File srcFolder = new File(".", "src");
+ File testFolder = new File(srcFolder, "test");
+ File dataFolder = new File(testFolder, "data");
+ File imagesFolder = new File(dataFolder, "images");
+
+ final List result = new ArrayList();
+
+ FSTraversal.Visitor visitor = new FSTraversal.Visitor()
+ {
+ public boolean visit(File file, double progressEstimate)
+ {
+ if (Sanselan.hasImageFileExtension(file))
+ result.add(file);
+ return false;
+ }
+ };
+ new FSTraversal().traverseFiles(imagesFolder, visitor);
+
+ return result;
+ }
+
+ protected File getTestImage()
+ {
+ List images = getAllTestImages();
+ if(images.size()<1)
+ return null;
+ return (File) images.get(0);
+ }
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/SanselanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan.common.byteSources;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.sanselan.util.Debug;
+import org.apache.sanselan.util.IOUtils;
+
+public class ByteSourceDataTest extends ByteSourceTest
+{
+ public ByteSourceDataTest()
+ {
+ super(ByteSourceDataTest.class.getName());
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite(ByteSourceDataTest.class);
+ }
+
+ private interface ByteSourceFactory
+ {
+ public ByteSource getByteSource(byte src[]) throws IOException;
+ }
+
+ private class ByteSourceFileFactory implements ByteSourceFactory
+ {
+ public ByteSource getByteSource(byte src[]) throws IOException
+ {
+ File file = createTempFile(src);
+
+ // test that all bytes written to file.
+ assertTrue(src.length == file.length());
+
+ ByteSource byteSource = new ByteSourceFile(file);
+ return byteSource;
+ }
+ }
+
+ private class ByteSourceInputStreamFileFactory implements ByteSourceFactory
+ {
+ public ByteSource getByteSource(byte src[]) throws IOException
+ {
+ File file = createTempFile(src);
+
+ FileInputStream is = new FileInputStream(file);
+
+ ByteSource byteSource = new ByteSourceInputStream(is, null);
+ return byteSource;
+ }
+ }
+
+ private class ByteSourceInputStreamRawFactory implements ByteSourceFactory
+ {
+ public ByteSource getByteSource(byte src[]) throws IOException
+ {
+ ByteArrayInputStream is = new ByteArrayInputStream(src);
+
+ ByteSource byteSource = new ByteSourceInputStream(is, null);
+ return byteSource;
+ }
+
+ }
+
+ protected void writeAndReadBytes(ByteSourceFactory byteSourceFactory,
+ byte src[]) throws IOException
+ {
+ ByteSource byteSource = byteSourceFactory.getByteSource(src);
+
+ // test cache during interrupted read cache by reading only first N bytes.
+ {
+ InputStream is = byteSource.getInputStream();
+ byte prefix[] = new byte[256];
+ int read = is.read(prefix);
+
+ assertTrue(read <= src.length);
+ for (int i = 0; i < read; i++)
+ assertTrue(src[i] == prefix[i]);
+ }
+
+ // test cache by completely reading InputStream N times.
+ for (int j = 0; j < 5; j++)
+ {
+ InputStream is = byteSource.getInputStream();
+ byte dst[] = IOUtils.getInputStreamBytes(is);
+
+ compareByteArrays(src, dst);
+ }
+
+ {
+ // test getAll() method.
+ byte all[] = byteSource.getAll();
+ compareByteArrays(src, all);
+ }
+
+ if (src.length > 2)
+ {
+ // test optional start param to getInputStream()
+
+ int start = src.length / 2;
+
+ InputStream is = byteSource.getInputStream(start);
+ byte dst[] = IOUtils.getInputStreamBytes(is);
+
+ assertTrue(src.length == dst.length + start);
+ for (int i = 0; i < dst.length; i++)
+ assertTrue(dst[i] == src[i + start]);
+ }
+
+ }
+
+ public void test() throws IOException
+ {
+ ByteSourceFactory byteSourceFactories[] = {
+ new ByteSourceFileFactory(),
+ new ByteSourceInputStreamFileFactory(),
+ new ByteSourceInputStreamRawFactory(),
+ };
+
+ byte testByteArrays[][] = getTestByteArrays();
+
+ for (int i = 0; i < testByteArrays.length; i++)
+ {
+ byte testByteArray[] = testByteArrays[i];
+
+ for (int j = 0; j < byteSourceFactories.length; j++)
+ {
+ ByteSourceFactory byteSourceFactory = byteSourceFactories[j];
+
+ writeAndReadBytes(byteSourceFactory, testByteArray);
+ }
+ }
+ }
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceDataTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan.common.byteSources;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.sanselan.ImageFormat;
+import org.apache.sanselan.ImageInfo;
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.Sanselan;
+import org.apache.sanselan.util.Debug;
+import org.apache.sanselan.util.IOUtils;
+
+public class ByteSourceImageTest extends ByteSourceTest
+{
+ public ByteSourceImageTest()
+ {
+ super(ByteSourceImageTest.class.getName());
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite(ByteSourceImageTest.class);
+ }
+
+ public void test() throws IOException, ImageReadException,
+ IllegalAccessException, IllegalArgumentException,
+ InvocationTargetException
+ {
+ File imageFile = getTestImage();
+ Debug.debug("imageFile", imageFile);
+ assertTrue(imageFile != null);
+
+ byte imageFileBytes[] = IOUtils.getFileBytes(imageFile);
+ assertTrue(imageFileBytes != null);
+ assertTrue(imageFileBytes.length == imageFile.length());
+
+ checkGuessFormat(imageFile, imageFileBytes);
+
+ checkGetICCProfileBytes(imageFile, imageFileBytes);
+
+ checkGetImageInfo(imageFile, imageFileBytes);
+
+ checkGetImageSize(imageFile, imageFileBytes);
+
+ checkGetBufferedImage(imageFile, imageFileBytes);
+ }
+
+ public void checkGetBufferedImage(File file, byte[] bytes)
+ throws IOException, ImageReadException
+ {
+ BufferedImage imageFile = Sanselan.getBufferedImage(file);
+ assertTrue(imageFile != null);
+ assertTrue(imageFile.getWidth() > 0);
+ assertTrue(imageFile.getHeight() > 0);
+
+ BufferedImage imageBytes = Sanselan.getBufferedImage(bytes);
+ assertTrue(imageBytes != null);
+ assertTrue(imageFile.getWidth() == imageBytes.getWidth());
+ assertTrue(imageFile.getHeight() == imageBytes.getHeight());
+ }
+
+ public void checkGetImageSize(File imageFile, byte[] imageFileBytes)
+ throws IOException, ImageReadException
+ {
+ Dimension imageSizeFile = Sanselan.getImageSize(imageFile);
+ assertTrue(imageSizeFile != null);
+ assertTrue(imageSizeFile.width > 0);
+ assertTrue(imageSizeFile.height > 0);
+
+ Dimension imageSizeBytes = Sanselan.getImageSize(imageFileBytes);
+ assertTrue(imageSizeBytes != null);
+ assertTrue(imageSizeFile.width == imageSizeBytes.width);
+ assertTrue(imageSizeFile.height == imageSizeBytes.height);
+ }
+
+ public void checkGuessFormat(File imageFile, byte[] imageFileBytes)
+ throws IOException, ImageReadException
+ {
+ // check guessFormat()
+ ImageFormat imageFormatFile = Sanselan.guessFormat(imageFile);
+ assertTrue(imageFormatFile != null);
+ assertTrue(imageFormatFile != ImageFormat.IMAGE_FORMAT_UNKNOWN);
+ // Debug.debug("imageFormatFile", imageFormatFile);
+
+ ImageFormat imageFormatBytes = Sanselan.guessFormat(imageFileBytes);
+ assertTrue(imageFormatBytes != null);
+ assertTrue(imageFormatBytes != ImageFormat.IMAGE_FORMAT_UNKNOWN);
+ // Debug.debug("imageFormatBytes", imageFormatBytes);
+
+ assertTrue(imageFormatBytes == imageFormatFile);
+ }
+
+ public void checkGetICCProfileBytes(File imageFile, byte[] imageFileBytes)
+ throws IOException, ImageReadException
+ {
+ // check guessFormat()
+ byte iccBytesFile[] = Sanselan.getICCProfileBytes(imageFile);
+
+ byte iccBytesBytes[] = Sanselan.getICCProfileBytes(imageFileBytes);
+
+ assertTrue((iccBytesFile != null) == (iccBytesBytes != null));
+
+ if (iccBytesFile == null)
+ return;
+
+ compareByteArrays(iccBytesFile, iccBytesBytes);
+ }
+
+ public void checkGetImageInfo(File imageFile, byte[] imageFileBytes)
+ throws IOException, ImageReadException, IllegalAccessException,
+ IllegalArgumentException, InvocationTargetException
+ {
+ // check guessFormat()
+ ImageInfo imageInfoFile = Sanselan.getImageInfo(imageFile);
+
+ ImageInfo imageInfoBytes = Sanselan.getImageInfo(imageFileBytes);
+
+ assertTrue(imageInfoFile != null);
+ assertTrue(imageInfoBytes != null);
+
+ Method methods[] = ImageInfo.class.getMethods();
+ for (int i = 0; i < methods.length; i++)
+ {
+ Method method = methods[i];
+ method.getModifiers();
+ if (!Modifier.isPublic(method.getModifiers()))
+ continue;
+ if (!method.getName().startsWith("get"))
+ continue;
+ if (method.getName().equals("getClass"))
+ continue;
+ if (method.getGenericParameterTypes().length > 0)
+ continue;
+
+ Object valueFile = method.invoke(imageInfoFile, null);
+ Object valueBytes = method.invoke(imageInfoBytes, null);
+
+ assertTrue(valueFile.equals(valueBytes));
+ }
+
+ // only have to test values from imageInfoFile; we already know values match.
+ assertTrue(imageInfoFile.getBitsPerPixel() > 0);
+
+ assertTrue(imageInfoFile.getFormat() != null);
+ assertTrue(imageInfoFile.getFormat() != ImageFormat.IMAGE_FORMAT_UNKNOWN);
+
+ assertTrue(imageInfoFile.getFormatName() != null);
+
+ assertTrue(imageInfoFile.getWidth() > 0);
+ assertTrue(imageInfoFile.getHeight() > 0);
+
+ assertTrue(imageInfoFile.getMimeType() != null);
+
+ assertTrue(imageInfoFile.getNumberOfImages() > 0);
+
+ }
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceImageTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan.common.byteSources;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.sanselan.SanselanTest;
+
+public abstract class ByteSourceTest extends SanselanTest
+{
+ public ByteSourceTest(String name)
+ {
+ super(name);
+ }
+
+
+
+ protected File createTempFile(byte src[]) throws IOException
+ {
+ // make temp file directory
+ File tmp = new File(".", "tmp");
+ tmp.mkdirs();
+
+ // create temp file.
+ File file = File.createTempFile("raw_", ".bin", tmp);
+ file.deleteOnExit();
+
+ // write test bytes to file.
+ OutputStream os = new FileOutputStream(file);
+ os = new BufferedOutputStream(os);
+ os.write(src);
+ os.close();
+
+ // test that all bytes written to file.
+ assertTrue(src.length == file.length());
+
+ return file;
+ }
+
+ protected byte[][] getTestByteArrays()
+ {
+ byte emptyArray[] = (new byte[0]);
+
+ byte single[] = new byte[1];
+ for (int i = 0; i < single.length; i++)
+ single[i] = (byte) i;
+
+ byte simple[] = new byte[256];
+ for (int i = 0; i < simple.length; i++)
+ simple[i] = (byte) i;
+
+ byte zeroes[] = new byte[256];
+ for (int i = 0; i < zeroes.length; i++)
+ zeroes[i] = 0;
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ for (int i = 0; i < 256 * 256; i++)
+ {
+ baos.write(0xff & i);
+ baos.write(0xff & (i >> 8));
+ }
+ byte longArray[] = (baos.toByteArray());
+
+ return new byte[][]{
+ emptyArray, single, simple, zeroes, longArray,
+ };
+ }
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/common/byteSources/ByteSourceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan.formats.png;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+import org.apache.sanselan.ImageFormat;
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.ImageWriteException;
+import org.apache.sanselan.Sanselan;
+import org.apache.sanselan.SanselanTest;
+import org.apache.sanselan.util.IOUtils;
+
+public class PngWriteReadTest extends SanselanTest
+{
+ public PngWriteReadTest(String name)
+ {
+ super(name);
+ }
+
+ private int[][] getSimpleRawData(int width, int height, int value)
+ {
+ int[][] result = new int[height][width];
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ result[y][x] = value;
+ return result;
+ }
+
+ private int[][] getAscendingRawData(int width, int height)
+ {
+ int[][] result = new int[height][width];
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ int alpha = (x + y) % 256;
+ int value = (x + y) % 256;
+ int argb = (0xff & alpha) << 24 | (0xff & value) << 16
+ | (0xff & value) << 8 | (0xff & value) << 0;
+
+ result[y][x] = argb;
+ }
+ return result;
+ }
+
+ private int[][] randomRawData(int width, int height)
+ {
+ Random random = new Random();
+ int[][] result = new int[height][width];
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ int argb = random.nextInt();
+ result[y][x] = argb;
+ }
+ return result;
+ }
+
+ public void test() throws IOException, ImageReadException,
+ ImageWriteException
+ {
+ int[][] smallBlackPixels = getSimpleRawData(256, 256, 0);
+ int[][] singleBlackPixel = getSimpleRawData(1, 1, 0);
+ int[][] smallRedPixels = getSimpleRawData(256, 256, 0xffff0000);
+ int[][] singleRedPixel = getSimpleRawData(1, 1, 0xffff0000);
+ int[][] smallAscendingPixels = getAscendingRawData(256, 256);
+ int[][] smallRandomPixels = randomRawData(256, 256);
+
+ int[][][] testData = {
+ smallBlackPixels, singleBlackPixel, smallRedPixels,
+ singleRedPixel, smallAscendingPixels, smallRandomPixels,
+ };
+
+ for (int i = 0; i < testData.length; i++)
+ {
+ int rawData[][] = testData[i];
+ writeAndReadImageData(rawData);
+ }
+ }
+
+ private BufferedImage imageDataToBufferedImage(int[][] rawData)
+ {
+ int width = rawData[0].length;
+ int height = rawData.length;
+ BufferedImage image = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_ARGB);
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ image.setRGB(x, y, rawData[y][x]);
+ }
+ return image;
+ }
+
+ private int[][] bufferedImageToImageData(BufferedImage image)
+ {
+ int width = image.getWidth();
+ int height = image.getHeight();
+ int[][] result = new int[height][width];
+
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ result[y][x] = image.getRGB(x, y);
+ }
+ return result;
+ }
+
+ private void writeAndReadImageData(int[][] rawData) throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ BufferedImage srcImage = imageDataToBufferedImage(rawData);
+
+ Map writeParams = new HashMap();
+ // writeParams.put(SanselanConstants.PARAM_KEY_FORMAT, ImageFormat.IMAGE_FORMAT_PNG);
+ // writeParams.put(PngConstants.PARAM_KEY_PNG_FORCE_TRUE_COLOR,
+ // Boolean.TRUE);
+
+ byte bytes[] = Sanselan.writeImageToBytes(srcImage,
+ ImageFormat.IMAGE_FORMAT_PNG, writeParams);
+
+ // Debug.debug("bytes", bytes);
+
+ File tmpDirectory = new File("tmp");
+ File tempFile = File.createTempFile("temp", ".png", tmpDirectory);
+ IOUtils.writeToFile(bytes, tempFile);
+
+ BufferedImage dstImage = Sanselan.getBufferedImage(bytes);
+
+ assertTrue(null != dstImage);
+ assertTrue(srcImage.getWidth() == dstImage.getWidth());
+ assertTrue(srcImage.getHeight() == dstImage.getHeight());
+
+ int dstData[][] = bufferedImageToImageData(dstImage);
+ compare(rawData, dstData);
+ }
+
+ private void compare(int[][] a, int[][] b)
+ {
+ assertTrue(null != a);
+ assertTrue(null != b);
+ assertTrue(a.length == b.length);
+
+ for (int y = 0; y < a.length; y++)
+ {
+ assertTrue(a[y].length == b[y].length);
+ // make sure row lengths consistent.
+ assertTrue(a[0].length == b[y].length);
+ for (int x = 0; x < a[y].length; x++)
+ {
+ assertTrue(a[y][x] == b[y][x]);
+ }
+ }
+ }
+
+}
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteReadTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java?rev=599241&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java (added)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java Wed Nov 28 17:34:16 2007
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sanselan.test.util;
+
+import java.io.File;
+
+import org.apache.sanselan.util.Debug;
+
+public class FSTraversal
+{
+
+ public static final int MODE_FILES = 1;
+ public static final int MODE_FOLDERS = 2;
+ public static final int MODE_FILES_AND_FOLDERS = 3;
+ public static final int MODE_ALL = 4;
+
+ public interface Visitor
+ {
+ public boolean visit(File file, double progressEstimate);
+ }
+
+ public boolean traverseFiles(File file, Visitor visitor)
+ {
+
+ return traverse(file, MODE_FILES, visitor);
+ }
+
+ public boolean traverseFolders(File file, Visitor visitor)
+ {
+
+ return traverse(file, MODE_FOLDERS, visitor);
+ }
+
+ public boolean traverseAll(File file, Visitor visitor)
+ {
+
+ return traverse(file, MODE_FILES_AND_FOLDERS, visitor);
+ }
+
+ public boolean traverse(File file, int mode, Visitor visitor)
+ {
+ return traverse(file, mode, visitor, 0, 1);
+ }
+
+ private boolean traverse(File file, int mode, Visitor visitor,
+ double estimate, double estimateIncrement)
+ {
+
+ if (file.isFile())
+ {
+ if ((mode == MODE_FILES) || (mode == MODE_FILES_AND_FOLDERS)
+ || (mode == MODE_ALL))
+ {
+ if (!visitor.visit(file, estimate))
+ return false;
+ }
+ }
+ else if (file.isDirectory())
+ {
+ File files[] = file.listFiles();
+ if (files != null)
+ {
+ for (int i = 0; i < files.length; i++)
+ {
+ File child = files[i];
+ if (ON_MAC_OS_X && child.isDirectory())
+ {
+ String name = child.getName();
+ if (name.equalsIgnoreCase("automount")
+ || name.equalsIgnoreCase("private")
+ || name.equalsIgnoreCase("Network")
+ || name.equalsIgnoreCase("Volumes"))
+ {
+ continue;
+ // return true;
+ }
+ }
+
+ if (!traverse(child, mode, visitor, estimate
+ + estimateIncrement * i / files.length,
+ estimateIncrement / files.length))
+ return false;
+ }
+ }
+
+ if ((mode == MODE_FOLDERS) || (mode == MODE_FILES_AND_FOLDERS)
+ || (mode == MODE_ALL))
+ {
+ if (!visitor.visit(file, estimate))
+ return false;
+ }
+ }
+ else
+ {
+ if (mode == MODE_ALL)
+ {
+ if (!visitor.visit(file, estimate))
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public boolean traverseFiles(Visitor visitor)
+ {
+
+ return traverse(MODE_FILES, visitor);
+ }
+
+ public boolean traverseFolders(Visitor visitor)
+ {
+
+ return traverse(MODE_FOLDERS, visitor);
+ }
+
+ public boolean traverseAll(Visitor visitor)
+ {
+
+ return traverse(MODE_FILES_AND_FOLDERS, visitor);
+ }
+
+ public boolean traverse(int mode, Visitor visitor)
+ {
+ return traverse(mode, visitor, 0, 1);
+ }
+
+ private static boolean ON_MAC_OS_X;
+
+ static
+ {
+ try
+ {
+ ON_MAC_OS_X = (System.getProperty("mrj.version") != null);
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+
+ ON_MAC_OS_X = false;
+ }
+ }
+
+ private boolean traverse(int mode, Visitor visitor, double estimate,
+ double estimateIncrement)
+ {
+ File roots[] = File.listRoots();
+
+ if (ON_MAC_OS_X)
+ {
+ File Volumes = new File("/Volumes/");
+ roots = Volumes.listFiles();
+ }
+ else
+ roots = File.listRoots();
+
+ if (roots == null)
+ return false;
+
+ for (int i = 0; i < roots.length; i++)
+ {
+ File root = roots[i];
+
+ if (root == null || !root.exists())
+ continue;
+
+ if (!traverse(roots[i], mode, visitor, estimate
+ + estimateIncrement * i / roots.length, estimateIncrement
+ / roots.length))
+ return false;
+ }
+
+ return true;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/test/util/FSTraversal.java
------------------------------------------------------------------------------
svn:eol-style = native