Author: veithen
Date: Sat May 22 12:42:01 2010
New Revision: 947289
URL: http://svn.apache.org/viewvc?rev=947289&view=rev
Log:
Cleaned up the unit test code that needs to compare (byte or character) streams
and made that code reusable.
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
(with props)
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
(with props)
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/pom.xml
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java?rev=947289&r1=947288&r2=947289&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
Sat May 22 12:42:01 2010
@@ -30,6 +30,7 @@ import org.apache.axiom.om.impl.MTOMXMLS
import org.apache.axiom.om.impl.builder.XOPAwareStAXOMBuilder;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
+import org.apache.axiom.testutils.io.IOTestUtils;
import javax.activation.DataHandler;
import javax.xml.stream.XMLStreamReader;
@@ -38,11 +39,9 @@ import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
-import java.util.Arrays;
import java.util.Set;
public class AttachmentsTest extends AbstractTestCase {
@@ -151,11 +150,11 @@ public class AttachmentsTest extends Abs
dataIs = ias.getNextStream();
expectedDataIs = getTestResource(img1FileName);
- compareStreams(dataIs, expectedDataIs);
+ IOTestUtils.compareStreams(dataIs, expectedDataIs);
dataIs = ias.getNextStream();
expectedDataIs = getTestResource(img2FileName);
- compareStreams(dataIs, expectedDataIs);
+ IOTestUtils.compareStreams(dataIs, expectedDataIs);
// Confirm that no more streams are left
assertEquals(null, ias.getNextStream());
@@ -254,13 +253,6 @@ public class AttachmentsTest extends Abs
assertTrue(outBase64ToBase64.indexOf("GBgcGBQgHBwcJCQgKDBQNDAsL") !=
-1);
}
-
- private void compareStreams(InputStream data, InputStream expected) throws
Exception {
- byte[] dataArray = this.getStreamAsByteArray(data, -1);
- byte[] expectedArray = this.getStreamAsByteArray(expected, -1);
- assertTrue(Arrays.equals(dataArray, expectedArray));
- }
-
public void testSWAWriteWithContentIDOrder() throws Exception {
// Read the stream that has soap xml followed by BAttachment then
AAttachment
@@ -343,7 +335,7 @@ public class AttachmentsTest extends Abs
InputStream expectedDataIs = getTestResource(img2FileName);
// Compare data across streams
- compareStreams(dataIs, expectedDataIs);
+ IOTestUtils.compareStreams(dataIs, expectedDataIs);
}
public void testNonExistingMIMEPart() throws Exception {
@@ -450,31 +442,6 @@ public class AttachmentsTest extends Abs
}
}
- /**
- * Returns the contents of the input stream as byte array.
- *
- * @param stream the <code>InputStream</code>
- * @param length the number of bytes to copy, if length < 0, the number is
unlimited
- * @return the stream content as byte array
- */
- private byte[] getStreamAsByteArray(InputStream stream, int length) throws
IOException {
- if (length == 0) return new byte[0];
- boolean checkLength = true;
- if (length < 0) {
- length = Integer.MAX_VALUE;
- checkLength = false;
- }
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
- int nextValue = stream.read();
- if (checkLength) length--;
- while (-1 != nextValue && length >= 0) {
- byteStream.write(nextValue);
- nextValue = stream.read();
- if (checkLength) length--;
- }
- return byteStream.toByteArray();
- }
-
private void testGetSOAPPartContentID(String contentTypeStartParam, String
contentId)
throws Exception {
// It doesn't actually matter what the stream *is* it just needs to
exist
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java?rev=947289&r1=947288&r2=947289&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
Sat May 22 12:42:01 2010
@@ -20,7 +20,6 @@
package org.apache.axiom.om.util;
import java.io.ByteArrayInputStream;
-import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.SequenceInputStream;
@@ -48,16 +47,11 @@ import org.apache.axiom.om.util.AXIOMUti
import org.apache.axiom.om.util.ElementHelper;
import org.apache.axiom.testutils.activation.RandomDataSource;
import org.apache.axiom.testutils.io.CharacterStreamComparator;
+import org.apache.axiom.testutils.io.IOTestUtils;
import org.apache.axiom.util.stax.TextFromElementReader;
import org.apache.commons.io.IOUtils;
public class ElementHelperTest extends TestCase {
- private void compareStreams(Reader s1, Reader s2) throws IOException {
- Writer comparator = new CharacterStreamComparator(s2);
- IOUtils.copy(s1, comparator);
- comparator.close();
- }
-
public void testGetTextAsStreamWithSingleTextNode() throws Exception {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMElement element = factory.createOMElement(new QName("a"));
@@ -82,7 +76,7 @@ public class ElementHelperTest extends T
new WrappedTextNodeOMDataSourceFromDataSource(qname, ds, cs));
Reader in = ElementHelper.getTextAsStream(element, true);
assertFalse(in instanceof StringReader);
- compareStreams(new InputStreamReader(ds.getInputStream(), cs), in);
+ IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(),
cs), in);
}
public void testGetTextAsStreamWithoutCaching() throws Exception {
@@ -102,7 +96,7 @@ public class ElementHelperTest extends T
new SequenceInputStream(v.elements()), "ascii");
OMElement element = new StAXOMBuilder(reader).getDocumentElement();
Reader in = ElementHelper.getTextAsStream(element, false);
- compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"),
in);
+ IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(),
"ascii"), in);
}
public void testWriteTextTo() throws Exception {
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/pom.xml
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/pom.xml?rev=947289&r1=947288&r2=947289&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testutils/pom.xml
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testutils/pom.xml Sat
May 22 12:42:01 2010
@@ -40,6 +40,11 @@
<artifactId>geronimo-activation_1.1_spec</artifactId>
</dependency>
<dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.4</version>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java?rev=947289&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
Sat May 22 12:42:01 2010
@@ -0,0 +1,70 @@
+/*
+ * 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.axiom.testutils.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.Assert;
+
+/**
+ * {...@link OutputStream} implementation that compares the data written to it
with another character
+ * sequence specified by an {...@link InputStream}.
+ */
+public class ByteStreamComparator extends OutputStream {
+ private final InputStream in;
+ private final byte[] compareBuffer = new byte[1024];
+ private int position;
+
+ public ByteStreamComparator(InputStream in) {
+ this.in = in;
+ }
+
+ public void write(byte[] buffer, int off, int len) throws IOException {
+ while (len > 0) {
+ int c = in.read(compareBuffer, 0, Math.min(compareBuffer.length,
len));
+ if (c == -1) {
+ Assert.fail("The two streams have different lengths");
+ }
+ for (int i=0; i<c; i++) {
+ if (buffer[off] != compareBuffer[i]) {
+ Assert.fail("Byte mismatch at position " + position);
+ }
+ off++;
+ len--;
+ position++;
+ }
+ }
+ }
+
+ public void flush() throws IOException {
+ }
+
+ public void close() throws IOException {
+ if (in.read() != -1) {
+ Assert.fail("The two streams have different lengths");
+ }
+ }
+
+ public void write(int b) throws IOException {
+ write(new byte[] { (byte)b });
+ }
+}
Propchange:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java?rev=947289&r1=947288&r2=947289&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
Sat May 22 12:42:01 2010
@@ -28,7 +28,6 @@ import junit.framework.Assert;
/**
* {...@link Writer} implementation that compares the data written to it with
another character
* sequence specified by a {...@link Reader}.
- * This class is meant to be used in JUnit test cases.
*/
public class CharacterStreamComparator extends Writer {
private final Reader in;
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java?rev=947289&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
Sat May 22 12:42:01 2010
@@ -0,0 +1,44 @@
+/*
+ * 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.axiom.testutils.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+
+import org.apache.commons.io.IOUtils;
+
+public final class IOTestUtils {
+ private IOTestUtils() {}
+
+ public static void compareStreams(InputStream s1, InputStream s2) throws
IOException {
+ OutputStream comparator = new ByteStreamComparator(s2);
+ IOUtils.copy(s1, comparator);
+ comparator.close();
+ }
+
+ public static void compareStreams(Reader s1, Reader s2) throws IOException
{
+ Writer comparator = new CharacterStreamComparator(s2);
+ IOUtils.copy(s1, comparator);
+ comparator.close();
+ }
+}
Propchange:
webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
------------------------------------------------------------------------------
svn:eol-style = native