You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2006/03/16 21:08:38 UTC
svn commit: r386449 - in /xmlgraphics/commons/trunk/test: ./ java/ java/org/
java/org/apache/ java/org/apache/xmlgraphics/
java/org/apache/xmlgraphics/image/ java/org/apache/xmlgraphics/image/codec/
java/org/apache/xmlgraphics/image/codec/png/ java/org...
Author: jeremias
Date: Thu Mar 16 12:08:36 2006
New Revision: 386449
URL: http://svn.apache.org/viewcvs?rev=386449&view=rev
Log:
Ported some tests from Batik and converted them to JUnit.
Removed i18n stuff in the test code because it wasn't up-to-date anyway.
Added:
xmlgraphics/commons/trunk/test/
xmlgraphics/commons/trunk/test/java/
xmlgraphics/commons/trunk/test/java/org/
xmlgraphics/commons/trunk/test/java/org/apache/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTest.java
- copied, changed from r386425, xmlgraphics/batik/trunk/test-sources/org/apache/batik/ext/awt/image/codec/png/PNGEncoderTest.java
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/Base64Test.java
- copied, changed from r386436, xmlgraphics/batik/trunk/test-sources/org/apache/batik/util/Base64Test.java
xmlgraphics/commons/trunk/test/resources/
xmlgraphics/commons/trunk/test/resources/org/
xmlgraphics/commons/trunk/test/resources/org/apache/
xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/
xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/util/
xmlgraphics/commons/trunk/test/resources/org/apache/xmlgraphics/util/io/
- copied from r386443, xmlgraphics/batik/trunk/test-references/org/apache/batik/util/
Copied: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTest.java (from r386425, xmlgraphics/batik/trunk/test-sources/org/apache/batik/ext/awt/image/codec/png/PNGEncoderTest.java)
URL: http://svn.apache.org/viewcvs/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTest.java?p2=xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTest.java&p1=xmlgraphics/batik/trunk/test-sources/org/apache/batik/ext/awt/image/codec/png/PNGEncoderTest.java&r1=386425&r2=386449&rev=386449&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/test-sources/org/apache/batik/ext/awt/image/codec/png/PNGEncoderTest.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/codec/png/PNGEncoderTest.java Thu Mar 16 12:08:36 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2004,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,20 @@
* limitations under the License.
*/
-package org.apache.batik.ext.awt.image.codec.png;
+package org.apache.xmlgraphics.image.codec.png;
-import org.apache.batik.test.*;
-import java.awt.*;
-import java.awt.geom.*;
-import java.awt.image.*;
-import java.io.*;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.RenderedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
/**
* This test validates the PNGEncoder operation. It creates a
@@ -30,28 +37,9 @@
* @author <a href="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
* @version $Id$
*/
-public class PNGEncoderTest extends AbstractTest {
- /**
- * Error when image cannot be encoded
- * {0} = trace for the exception which was reported
- */
- public static final String ERROR_CANNOT_ENCODE_IMAGE
- = "PNGEncoderTest.error.cannot.encode.image";
+public class PNGEncoderTest extends TestCase {
- /**
- * Error when image cannot be decoded
- * {0} = trace for the exception which was reported
- */
- public static final String ERROR_CANNOT_DECODE_IMAGE
- = "PNGEncoderTest.error.cannot.decode.image";
-
- /**
- * Decoded image differs from encoded image
- */
- public static final String ERROR_DECODED_DOES_NOT_MATCH_ENCODED
- = "PNGEncoderTest.error.decoded.does.not.match.encoded";
-
- public TestReport runImpl() throws Exception {
+ public void testPNGEncoding() throws Exception {
// Create a BufferedImage to be encoded
BufferedImage image = new BufferedImage(100, 75, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig = image.createGraphics();
@@ -76,39 +64,30 @@
// will be stored.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream os = buildOutputStream(bos);
+ try {
+ // Now, try to encode image
+ PNGEncodeParam params =
+ PNGEncodeParam.getDefaultEncodeParam(image);
+ PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params);
- // Now, try to encode image
- PNGEncodeParam params =
- PNGEncodeParam.getDefaultEncodeParam(image);
- PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params);
-
- try{
pngImageEncoder.encode(image);
+ } finally {
os.close();
- }catch(Exception e){
- return reportException(ERROR_CANNOT_ENCODE_IMAGE, e);
}
// Now, try to decode image
- InputStream is
- = buildInputStream(bos);
+ InputStream is = buildInputStream(bos);
PNGImageDecoder pngImageDecoder
= new PNGImageDecoder(is, new PNGDecodeParam());
RenderedImage decodedRenderedImage = null;
- try{
- decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
- }catch(Exception e){
- return reportException(ERROR_CANNOT_DECODE_IMAGE,
- e);
- }
+ decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0);
BufferedImage decodedImage = null;
- if(decodedRenderedImage instanceof BufferedImage){
+ if (decodedRenderedImage instanceof BufferedImage) {
decodedImage = (BufferedImage)decodedRenderedImage;
- }
- else{
+ } else {
decodedImage = new BufferedImage(decodedRenderedImage.getWidth(),
decodedRenderedImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
@@ -119,11 +98,9 @@
}
// Compare images
- if(checkIdentical(image, decodedImage) != true){
- return reportError(ERROR_DECODED_DOES_NOT_MATCH_ENCODED);
+ if (checkIdentical(image, decodedImage) != true) {
+ fail("Decoded image does not match the original");
}
-
- return reportSuccess();
}
/**
@@ -148,21 +125,20 @@
* Compares the data for the two images
*/
public static boolean checkIdentical(BufferedImage imgA,
- BufferedImage imgB){
+ BufferedImage imgB) {
boolean identical = true;
- if(imgA.getWidth() == imgB.getWidth()
- &&
- imgA.getHeight() == imgB.getHeight()){
+ if (imgA.getWidth() == imgB.getWidth()
+ && imgA.getHeight() == imgB.getHeight()){
int w = imgA.getWidth();
int h = imgA.getHeight();
- for(int i=0; i<h; i++){
- for(int j=0; j<w; j++){
- if(imgA.getRGB(j,i) != imgB.getRGB(j,i)){
+ for (int i = 0; i < h; i++) {
+ for (int j = 0; j < w; j++) {
+ if (imgA.getRGB(j, i) != imgB.getRGB(j, i)) {
identical = false;
break;
}
}
- if( !identical ){
+ if (!identical) {
break;
}
}
Copied: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/Base64Test.java (from r386436, xmlgraphics/batik/trunk/test-sources/org/apache/batik/util/Base64Test.java)
URL: http://svn.apache.org/viewcvs/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/Base64Test.java?p2=xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/Base64Test.java&p1=xmlgraphics/batik/trunk/test-sources/org/apache/batik/util/Base64Test.java&r1=386436&r2=386449&rev=386449&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/test-sources/org/apache/batik/util/Base64Test.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/io/Base64Test.java Thu Mar 16 12:08:36 2006
@@ -1,6 +1,6 @@
/*
- Copyright 2001,2003 The Apache Software Foundation
+ Copyright 2001,2003,2006 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -15,172 +15,51 @@
limitations under the License.
*/
-package org.apache.batik.util;
-
-import org.apache.batik.test.AbstractTest;
-import org.apache.batik.test.DefaultTestReport;
-import org.apache.batik.test.TestReport;
+package org.apache.xmlgraphics.util.io;
+import java.io.File;
import java.io.PipedOutputStream;
import java.io.PipedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
-import java.io.StringWriter;
-import java.io.PrintWriter;
import java.net.URL;
+import junit.framework.TestCase;
+
/**
- * This test validates that the Base65 encoder/decoders work properly.
+ * This test validates that the Base64 encoder/decoders work properly.
*
* @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
* @version $Id$
*/
-public class Base64Test extends AbstractTest {
- /**
- * Error when bad action string given.
- * {0} = Bad action string
- */
- public static final String ERROR_BAD_ACTION_STRING
- = "Base64Test.error.bad.action.string";
-
- /**
- * Error when unable to read/open in URL
- * {0} = URL
- * {1} = exception stack trace.
- */
- public static final String ERROR_CANNOT_READ_IN_URL
- = "Base64Test.error.cannot.read.in.url";
-
- /**
- * Error when unable to read/open ref URL
- * {0} = URL
- * {1} = exception stack trace.
- */
- public static final String ERROR_CANNOT_READ_REF_URL
- = "Base64Test.error.cannot.read.ref.url";
-
- /**
- * Result didn't match reference result.
- * {0} = first byte of mismatch
- */
- public static final String ERROR_WRONG_RESULT
- = "Base64Test.error.wrong.result";
-
- public static final String ENTRY_KEY_ERROR_DESCRIPTION
- = "Base64Test.entry.key.error.description";
-
- protected String action = null;
- protected URL in = null;
- protected URL ref = null;
-
- /**
- * Constructor. ref is ignored if action == ROUND.
- * @param action The action to perform, one of:
- * ROUND : base64 encode then base64 decode.
- * ENCODE : encode in to base 64 and compare result to ref.
- * DECODE : decode in (must be base 64) and compare to ref.
- * @param in The source file to apply 'action' to.
- * @param ref The reference file.
- */
- public Base64Test(String action, URL in, URL ref) {
- this.action = action;
- this.in = in;
- this.ref = ref;
- }
-
- /**
- * Constructor, for round trip testing (only one file required).
- * @param in The source file to round trip.
- */
- public Base64Test(URL in) {
- this.action = "ROUND";
- this.in = in;
- }
-
- /**
- * Returns this Test's name
- */
- public String getName() {
- return action + " -- " + in + " -- " + super.getName();
- }
-
- /**
- * This method will only throw exceptions if some aspect
- * of the test's internal operation fails.
- */
- public TestReport runImpl() throws Exception {
- DefaultTestReport report
- = new DefaultTestReport(this);
-
- InputStream inIS;
+public class Base64Test extends TestCase {
- try {
- inIS = in.openStream();
- } catch(Exception e) {
- StringWriter trace = new StringWriter();
- e.printStackTrace(new PrintWriter(trace));
- report.setErrorCode(ERROR_CANNOT_READ_IN_URL);
- report.setDescription(new TestReport.Entry[] {
- new TestReport.Entry
- (TestMessages.formatMessage
- (ENTRY_KEY_ERROR_DESCRIPTION, null),
- TestMessages.formatMessage
- (ERROR_CANNOT_READ_IN_URL,
- new String[]{in.toString(), trace.toString()}))
- });
- report.setPassed(false);
- return report;
- }
+ private void innerBase64Test(String action, URL in, URL ref) throws Exception {
+ InputStream inIS = in.openStream();
if (action.equals("ROUND"))
- this.ref = in;
+ ref = in;
else if (!action.equals("ENCODE") &&
!action.equals("DECODE")) {
- report.setErrorCode(ERROR_BAD_ACTION_STRING);
- report.setDescription(new TestReport.Entry[] {
- new TestReport.Entry
- (TestMessages.formatMessage
- (ENTRY_KEY_ERROR_DESCRIPTION, null),
- TestMessages.formatMessage(ERROR_BAD_ACTION_STRING,
- new String[]{action}))
- });
- report.setPassed(false);
- return report;
+ fail("Bad action string");
}
- InputStream refIS;
- try {
- refIS = ref.openStream();
- } catch(Exception e) {
- StringWriter trace = new StringWriter();
- e.printStackTrace(new PrintWriter(trace));
- report.setErrorCode(ERROR_CANNOT_READ_REF_URL);
- report.setDescription(new TestReport.Entry[] {
- new TestReport.Entry
- (TestMessages.formatMessage
- (ENTRY_KEY_ERROR_DESCRIPTION, null),
- TestMessages.formatMessage
- (ERROR_CANNOT_READ_REF_URL,
- new String[]{ref.toString(), trace.toString()}))
- });
- report.setPassed(false);
- return report;
- }
+ InputStream refIS = ref.openStream();
if (action.equals("ENCODE") ||
action.equals("ROUND")) {
- // We need to encode the incomming data
- PipedOutputStream pos = new PipedOutputStream();
- OutputStream os = new Base64EncoderStream(pos);
+ // We need to encode the incomming data
+ PipedOutputStream pos = new PipedOutputStream();
+ OutputStream os = new Base64EncodeStream(pos);
- // Copy the input to the Base64 Encoder (in a seperate thread).
- Thread t = new StreamCopier(inIS, os);
+ // Copy the input to the Base64 Encoder (in a seperate thread).
+ Thread t = new StreamCopier(inIS, os);
- // Read that from the piped output stream.
- inIS = new PipedInputStream(pos);
- t.start();
+ // Read that from the piped output stream.
+ inIS = new PipedInputStream(pos);
+ t.start();
}
if (action.equals("DECODE")||
@@ -191,21 +70,43 @@
int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE"));
- if (mismatch == -1) {
- report.setPassed(true);
- return report;
+ if (mismatch != -1) {
+ fail("Wrong result");
}
+ }
+
+ private void innerBase64Test(String action, String in, String ref) throws Exception {
+ final String baseURL = "file:test/resources/org/apache/xmlgraphics/util/io/";
+ innerBase64Test(action, new URL(baseURL + in), new URL(baseURL + ref));
+ }
+
+ private void innerBase64Test(String in) throws Exception {
+ innerBase64Test("ROUND", in, in);
+ }
- report.setErrorCode(ERROR_WRONG_RESULT);
- report.setDescription(new TestReport.Entry[] {
- new TestReport.Entry
- (TestMessages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
- TestMessages.formatMessage(ERROR_WRONG_RESULT,
- new String[]{""+mismatch}))
- });
- report.setPassed(false);
- return report;
+ private void testBase64Group(String name) throws Exception {
+ innerBase64Test("ENCODE", name, name + ".64");
+ innerBase64Test("DECODE", name + ".64", name);
+ innerBase64Test(name);
}
+
+ /**
+ * This method will only throw exceptions if some aspect
+ * of the test's internal operation fails.
+ */
+ public void testBase64() throws Exception {
+ System.out.println(new File(".").getCanonicalPath());
+ testBase64Group("zeroByte");
+ testBase64Group("oneByte");
+ testBase64Group("twoByte");
+ testBase64Group("threeByte");
+ testBase64Group("fourByte");
+ testBase64Group("tenByte");
+ testBase64Group("small");
+ testBase64Group("medium");
+ innerBase64Test("DECODE", "medium.pc.64", "medium");
+ innerBase64Test("large");
+}
/**
* Returns true if the contents of <tt>is1</tt> match the
@@ -213,16 +114,16 @@
*/
public static int compareStreams(InputStream is1, InputStream is2,
boolean skipws) {
- byte [] data1 = new byte[100];
- byte [] data2 = new byte[100];
- int off1=0;
- int off2=0;
- int idx=0;
+ byte[] data1 = new byte[100];
+ byte[] data2 = new byte[100];
+ int off1 = 0;
+ int off2 = 0;
+ int idx = 0;
try {
- while(true) {
- int len1 = is1.read(data1, off1, data1.length-off1);
- int len2 = is2.read(data2, off2, data2.length-off2);
+ while (true) {
+ int len1 = is1.read(data1, off1, data1.length - off1);
+ int len2 = is2.read(data2, off2, data2.length - off2);
if (off1 != 0) {
if (len1 == -1)
@@ -247,11 +148,11 @@
return idx;
// check if the rest of is2 is whitespace...
- for (int i2=0; i2<len2; i2++)
+ for (int i2 = 0; i2 < len2; i2++)
if ((data2[i2] != '\n') &&
(data2[i2] != '\r') &&
(data2[i2] != ' '))
- return idx+i2;
+ return idx + i2;
off1 = off2 = 0;
continue;
}
@@ -262,18 +163,18 @@
return idx;
// Check if rest of is1 is whitespace...
- for (int i1=0; i1<len1; i1++)
+ for (int i1 = 0; i1 < len1; i1++)
if ((data1[i1] != '\n') &&
(data1[i1] != '\r') &&
(data1[i1] != ' '))
- return idx+i1;
+ return idx + i1;
off1 = off2 = 0;
continue;
}
int i1=0;
int i2=0;
- while((i1<len1) && (i2<len2)) {
+ while((i1 < len1) && (i2 < len2)) {
if (skipws) {
if ((data1[i1] == '\n') ||
(data1[i1] == '\r') ||
@@ -296,12 +197,12 @@
}
if (i1 != len1)
- System.arraycopy(data1, i1, data1, 0, len1-i1);
+ System.arraycopy(data1, i1, data1, 0, len1 - i1);
if (i2 != len2)
- System.arraycopy(data2, i2, data2, 0, len2-i2);
- off1 = len1-i1;
- off2 = len2-i2;
- idx+=i2;
+ System.arraycopy(data2, i2, data2, 0, len2 - i2);
+ off1 = len1 - i1;
+ off2 = len2 - i2;
+ idx += i2;
}
} catch(IOException ioe) {
ioe.printStackTrace();
@@ -324,8 +225,8 @@
public void run() {
try {
- byte [] data = new byte[1000];
- while(true) {
+ byte[] data = new byte[1000];
+ while (true) {
int len = src.read(data, 0, data.length);
if (len == -1) break;
---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org