Author: mthl
Date: Sat May 25 21:24:02 2019
New Revision: 1860019
URL: http://svn.apache.org/viewvc?rev=1860019&view=rev
Log:
Improved: Turn ‘UtilIOTests’ into a unit test class
(OFBIZ-11067)
Previously this test was disabled, however since it now seems to work
now we enable it.
Added:
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
(with props)
Removed:
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/test/UtilIOTests.java
Modified:
ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml
Added:
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java?rev=1860019&view=auto
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
(added)
+++
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
Sat May 25 21:24:02 2019
@@ -0,0 +1,110 @@
+/*
+ * 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.ofbiz.base.util;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class UtilIOTests {
+ private static final byte[] trademarkBytes = new byte[] {
+ (byte) 0xE2, (byte) 0x84, (byte) 0xA2
+ };
+
+ @Test
+ public void testReadString() throws Exception {
+ readStringTest_0("unix line ending", "\n", new byte[] { 0x0A });
+ readStringTest_0("mac line ending", "\r", new byte[] { 0x0D });
+ readStringTest_0("windows line ending", "\r\n", new byte[] { 0x0D,
0x0A });
+ }
+
+ private static byte[] join(byte[]... parts) {
+ int count = 0;
+ for (byte[] part: parts) {
+ count += part.length;
+ }
+ byte[] result = new byte[count];
+ int i = 0;
+ for (byte[] part: parts) {
+ System.arraycopy(part, 0, result, i, part.length);
+ i += part.length;
+ }
+ return result;
+ }
+
+ private static void readStringTest_0(String label, String lineSeparator,
byte[] extra) throws IOException {
+ String originalLineSeparator = System.getProperty("line.separator");
+ try {
+ System.getProperties().put("line.separator", lineSeparator);
+ readStringTest_1(label + ":mark", "\u2122", join(trademarkBytes));
+ readStringTest_1(label + ":mark NL", "\u2122\n",
join(trademarkBytes, extra));
+ readStringTest_1(label + ":NL mark", "\n\u2122", join(extra,
trademarkBytes));
+ } finally {
+ System.getProperties().put("line.separator",
originalLineSeparator);
+ }
+ }
+
+ private static void readStringTest_1(String label, String wanted, byte[]
toRead) throws IOException {
+ assertEquals("readString bytes default:" + label, wanted,
UtilIO.readString(toRead));
+ assertEquals("readString bytes UTF-8:" + label, wanted,
UtilIO.readString(toRead, "UTF-8"));
+ assertEquals("readString bytes UTF8:" + label, wanted,
UtilIO.readString(toRead, UtilIO.getUtf8()));
+ assertEquals("readString bytes offset/length default:" + label,
wanted, UtilIO.readString(toRead, 0, toRead.length));
+ assertEquals("readString bytes offset/length UTF-8:" + label, wanted,
UtilIO.readString(toRead, 0, toRead.length, "UTF-8"));
+ assertEquals("readString bytes offset/length UTF8:" + label, wanted,
UtilIO.readString(toRead, 0, toRead.length, UtilIO.getUtf8()));
+ assertEquals("readString stream default:" + label, wanted,
UtilIO.readString(new ByteArrayInputStream(toRead)));
+ assertEquals("readString stream UTF-8:" + label, wanted,
UtilIO.readString(new ByteArrayInputStream(toRead), "UTF-8"));
+ assertEquals("readString stream UTF8:" + label, wanted,
UtilIO.readString(new ByteArrayInputStream(toRead), UtilIO.getUtf8()));
+ }
+
+ @Test
+ public void testWriteString() throws Exception {
+ writeStringTest_0("unix line ending", "\n", new byte[] { 0x0A });
+ writeStringTest_0("mac line ending", "\r", new byte[] { 0x0D });
+ writeStringTest_0("windows line ending", "\r\n", new byte[] { 0x0D,
0x0A });
+ }
+
+ private static void writeStringTest_0(String label, String lineSeparator,
byte[] extra) throws IOException {
+ String originalLineSeparator = System.getProperty("line.separator");
+ try {
+ System.getProperties().put("line.separator", lineSeparator);
+ writeStringTest_1(label + ":mark", join(trademarkBytes), "\u2122");
+ writeStringTest_1(label + ":mark NL", join(trademarkBytes, extra),
"\u2122\n");
+ writeStringTest_1(label + ":NL mark", join(extra, trademarkBytes),
"\n\u2122");
+ } finally {
+ System.getProperties().put("line.separator",
originalLineSeparator);
+ }
+ }
+
+ private static void writeStringTest_1(String label, byte[] wanted, String
toWrite) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ UtilIO.writeString(baos, toWrite);
+ assertArrayEquals("writeString default:" + label, wanted,
baos.toByteArray());
+ baos = new ByteArrayOutputStream();
+ UtilIO.writeString(baos, "UTF-8", toWrite);
+ assertArrayEquals("writeString UTF-8:" + label, wanted,
baos.toByteArray());
+ baos = new ByteArrayOutputStream();
+ UtilIO.writeString(baos, UtilIO.getUtf8(), toWrite);
+ assertArrayEquals("writeString UTF8:" + label, wanted,
baos.toByteArray());
+ }
+}
Propchange:
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
------------------------------------------------------------------------------
svn:keywords = Date Rev Author URL Id
Propchange:
ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilIOTests.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml?rev=1860019&r1=1860018&r2=1860019&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml Sat May 25
21:24:02 2019
@@ -29,7 +29,6 @@
<junit-test-suite
class-name="org.apache.ofbiz.base.util.collections.test.FlexibleMapAccessorTests"/>
<junit-test-suite
class-name="org.apache.ofbiz.base.conversion.test.TestBooleanConverters"/>
<junit-test-suite
class-name="org.apache.ofbiz.base.conversion.test.TestJSONConverters"/>
- <!--junit-test-suite
class-name="org.apache.ofbiz.base.util.test.UtilIOTests"/-->
<junit-test-suite
class-name="org.apache.ofbiz.base.test.BaseUnitTests"/>
<groovy-test-suite name="simple"
location="component://base/groovyScripts/test/SimpleTests.groovy"/>
</test-group>