changeset 1444:22933dfe94ac

BASE64 tests adapted to slightly different output (windows line endings) on windows * tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64DecoderTest.java: enhanced for windows new lines * tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64EncoderTest.java: same
author Jiri Vanek <jvanek@redhat.com>
date Mon, 22 May 2017 18:57:58 +0200
parents e0dc22121163
children 483cc3f4db73
files ChangeLog tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64DecoderTest.java tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64EncoderTest.java
diffstat 3 files changed, 69 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon May 22 18:27:51 2017 +0200
+++ b/ChangeLog	Mon May 22 18:57:58 2017 +0200
@@ -1,3 +1,10 @@
+2017-05-22  Jiri Vanek <jvanek@redhat.com>
+            Tomáš Votava <tomcacolca@gmail.com>
+
+	* tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64DecoderTest.java: enhanced for windows new lines
+	* tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64EncoderTest.java: same
+
+
 2017-05-19  Jiri Vanek <jvanek@redhat.com>
             Alex Kashchenko <akashche@redhat.com>
 
--- a/tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64DecoderTest.java	Mon May 22 18:27:51 2017 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64DecoderTest.java	Mon May 22 18:57:58 2017 +0200
@@ -38,28 +38,24 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import org.junit.Assert;
 import org.junit.Test;
 
-/** Test various corner cases of the parser */
+/**
+ * Test various corner cases of the parser
+ */
 public class BASE64DecoderTest {
 
     private static final String sunClassE = "sun.misc.BASE64Encoder";
-      
+
     @Test
     public void testEmbededBase64Decoder() throws Exception {
-        final byte[] data = BASE64EncoderTest.encoded;
+        final byte[] data = getData();
         ByteArrayOutputStream out2 = new ByteArrayOutputStream();
         BASE64Decoder e2 = new BASE64Decoder();
         e2.decodeBuffer(new ByteArrayInputStream(data), out2);
         byte[] decoded = out2.toByteArray();
         Assert.assertEquals(BASE64EncoderTest.sSrc, new String(decoded, "utf-8"));
-
-
-
     }
 
     @Test
@@ -67,7 +63,7 @@
      * This test will fail, in case taht sun.misc.BASE64Encoder will be removed from builders java
      */
     public void testEmbededBase64DecoderAgainstSunOne() throws Exception {
-        final byte[] data = BASE64EncoderTest.encoded;
+        final byte[] data = getData();
         ByteArrayOutputStream out2 = new ByteArrayOutputStream();
         BASE64Decoder e2 = new BASE64Decoder();
         e2.decodeBuffer(new ByteArrayInputStream(data), out2);
@@ -76,13 +72,12 @@
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         BASE64EncoderTest.getAndInvokeMethod(encoder, "encodeBuffer", encoded2, out);
         Assert.assertArrayEquals(data, out.toByteArray());
-        Assert.assertArrayEquals(BASE64EncoderTest.encoded, out.toByteArray());
-
+        Assert.assertArrayEquals(getData(), out.toByteArray());
     }
-    
-     @Test
+
+    @Test
     public void testEmbededBase64DecoderAgainstEmbededEncoder() throws Exception {
-        final byte[] data = BASE64EncoderTest.encoded;
+        final byte[] data = getData();
         ByteArrayOutputStream out2 = new ByteArrayOutputStream();
         BASE64Decoder e2 = new BASE64Decoder();
         e2.decodeBuffer(new ByteArrayInputStream(data), out2);
@@ -91,9 +86,17 @@
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         encoder.encodeBuffer(encoded2, out);
         Assert.assertArrayEquals(data, out.toByteArray());
-        Assert.assertArrayEquals(BASE64EncoderTest.encoded, out.toByteArray());
+        Assert.assertArrayEquals(getData(), out.toByteArray());
 
     }
 
-  
+
+    private byte[] getData() {
+        byte[] r = new byte[BASE64EncoderTest.encoded.size()];
+        for (int i = 0; i < BASE64EncoderTest.encoded.size(); i++) {
+            r[i]=BASE64EncoderTest.encoded.get(i);
+        }
+        return r;
+    }
+
 }
--- a/tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64EncoderTest.java	Mon May 22 18:27:51 2017 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/replacements/BASE64EncoderTest.java	Mon May 22 18:57:58 2017 +0200
@@ -40,31 +40,55 @@
 import java.io.OutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Arrays;
-import net.sourceforge.jnlp.ServerAccess;
+import java.util.List;
+import net.sourceforge.jnlp.runtime.JNLPRuntime;
 import org.junit.Assert;
 import org.junit.Test;
 
-/** Test various corner cases of the parser */
+/**
+ * Test various corner cases of the parser
+ */
 public class BASE64EncoderTest {
 
     static final String sSrc = "abcdefgHIJKLMNOPQrstuvwxyz1234567890\r\n"
             + "-=+_))(**&&&^^%%$$##@@!!~{}][\":'/\\.,><\n"
             + "+ěšěčřžýáíé=ů/úěřťšďňéíáč";
-    static final byte[] encoded = {89, 87, 74, 106, 90, 71, 86, 109, 90,
+    static final Byte[] part1 = {89, 87, 74, 106, 90, 71, 86, 109, 90,
         48, 104, 74, 83, 107, 116, 77, 84, 85, 53, 80, 85, 70, 70, 121, 99, 51,
         82, 49, 100, 110, 100, 52, 101, 88, 111, 120, 77, 106, 77, 48, 78, 84,
         89, 51, 79, 68, 107, 119, 68, 81, 111, 116, 80, 83, 116, 102, 75, 83,
         107, 111, 75, 105, 111, 109, 74, 105, 90, 101, 88, 105, 85, 108, 74, 67,
-        81, 106, 10, 73, 48, 66, 65, 73, 83, 70, 43, 101, 51, 49, 100, 87, 121,
+        81, 106};
+    static final Byte[] part2 = {73, 48, 66, 65, 73, 83, 70, 43, 101, 51, 49, 100, 87, 121,
         73, 54, 74, 121, 57, 99, 76, 105, 119, 43, 80, 65, 111, 114, 120, 74,
         118, 70, 111, 99, 83, 98, 120, 73, 51, 70, 109, 99, 87, 43, 119, 55, 51,
         68, 111, 99, 79, 116, 119, 54, 107, 57, 120, 97, 56, 118, 119, 55, 114,
-        69, 109, 56, 87, 90, 120, 97, 88, 70, 111, 99, 83, 80, 10, 120, 89, 106,
-        68, 113, 99, 79, 116, 119, 54, 72, 69, 106, 81, 61, 61, 10};
-    
+        69, 109, 56, 87, 90, 120, 97, 88, 70, 111, 99, 83, 80};
+    static final Byte[] part3 = {120, 89, 106, 68, 113, 99, 79, 116, 119, 54, 72, 69, 106, 81, 61, 61};
+
+    private static List<Byte> getBASE64LineEnding() {
+            if (JNLPRuntime.isWindows()) {
+                return Arrays.asList(new Byte[]{13, 10});
+            } else {
+                return Arrays.asList(new Byte[]{10});
+            }
+        }
+
+    static final List<Byte> encoded = new ArrayList<Byte>();
+
     private static final String sunClassD = "sun.misc.BASE64Decoder";
 
+    static {
+        encoded.addAll(Arrays.asList(part1));
+        encoded.addAll(getBASE64LineEnding());
+        encoded.addAll(Arrays.asList(part2));
+        encoded.addAll(getBASE64LineEnding());
+        encoded.addAll(Arrays.asList(part3));
+        encoded.addAll(getBASE64LineEnding());
+    }
+
     @Test
     public void testEmbededBase64Encoder() throws Exception {
         final byte[] data = sSrc.getBytes("utf-8");
@@ -77,11 +101,8 @@
         BASE64Encoder e2 = new BASE64Encoder();
         e2.encodeBuffer(data, out2);
         byte[] encoded2 = out2.toByteArray();
-        Assert.assertArrayEquals(encoded, encoded2);
+        Assert.assertEquals(encoded, byteArrayToByteList(encoded2));
 //      ServerAccess.logErrorReprint(Arrays.toString(encoded2));
-
-
-
     }
 
     @Test
@@ -100,8 +121,8 @@
         Assert.assertArrayEquals(data, decoded);
         Assert.assertEquals(sSrc, new String(decoded, "utf-8"));
     }
-    
-      @Test
+
+    @Test
     public void testEmbededBase64EncoderAgainstEbededDecoder() throws Exception {
         final byte[] data = sSrc.getBytes("utf-8");
         ByteArrayOutputStream out2 = new ByteArrayOutputStream();
@@ -134,4 +155,12 @@
         return m.invoke(instance, params);
 
     }
+
+    private static List<Byte> byteArrayToByteList(byte[] encoded2) {
+         List<Byte> r = new ArrayList<>(encoded2.length);
+        for (byte b : encoded2) {
+            r.add(b);
+        }
+        return r;
+    }
 }