diff --git a/src/main/java/com/hashbangbash/trie/App.java b/src/main/java/com/hashbangbash/trie/App.java index c926157..252eadb 100644 --- a/src/main/java/com/hashbangbash/trie/App.java +++ b/src/main/java/com/hashbangbash/trie/App.java @@ -10,6 +10,7 @@ import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedInputStream; import java.io.File; +import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -83,6 +84,33 @@ public class App { return null; } + public static void byteArrayToFile(byte[] output, String filename) { + try { + FileOutputStream fos = new FileOutputStream(filename); + + /* + * To write byte array to a file, use + * void write(byte[] bArray) method of Java FileOutputStream class. + * + * This method writes given byte array to a file. + */ + fos.write(output); + + /* + * Close FileOutputStream using, + * void close() method of Java FileOutputStream class. + * + */ + fos.flush(); + fos.close(); + + } catch(FileNotFoundException ex) { + System.out.println("FileNotFoundException : " + ex); + } catch(IOException ioe) { + System.out.println("IOException : " + ioe); + } + } + public static List listFromFile(String filename) throws IOException, FileNotFoundException { FileInputStream fis; DataInputStream in; @@ -182,6 +210,7 @@ public class App { List contents; dos = (DEROctetString)objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7"); + //byteArrayToFile(dos.getOctets(), "herp.bin"); if ((contents = hydrateFromBytes(dos.getOctets())) == null) { System.out.println("FAIL"); return; diff --git a/src/main/java/com/redhat/trie/PathTree.java b/src/main/java/com/redhat/trie/PathTree.java index 4a4400d..ab0e301 100644 --- a/src/main/java/com/redhat/trie/PathTree.java +++ b/src/main/java/com/redhat/trie/PathTree.java @@ -92,8 +92,6 @@ public class PathTree { /** * Constructor using the list of content sets. - * - * FIXME - This is a stub. */ public PathTree(List contentSets) throws PayloadException { try { @@ -120,8 +118,6 @@ public class PathTree { this.payload = payload; - //inflatePathDict - this.modified = false; } diff --git a/src/test/java/com/redhat/trie/TestPathTree.java b/src/test/java/com/redhat/trie/TestPathTree.java index f00545b..acf21cc 100644 --- a/src/test/java/com/redhat/trie/TestPathTree.java +++ b/src/test/java/com/redhat/trie/TestPathTree.java @@ -3,13 +3,18 @@ package com.redhat.trie; import java.util.List; import java.util.ArrayList; import java.util.Collections; +import java.util.Collection; +import java.util.HashSet; + import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; @@ -46,6 +51,39 @@ public class TestPathTree { assertNotNull(pt); } + @Test + public void testThereAndBackAgainPayload() { + byte[] bytes; + PathTree pt0; + PathTree pt1; + List contents0; + List contents1; + + bytes = loadBytes("test.bin"); + pt0 = new PathTree(bytes); + contents0 = pt0.toList(); + for (String str : contents0) { + System.out.println(str); + } + + assertEquals(contents0.size(), 3); + //printByteArray(bytes); + + try { + pt1 = new PathTree(contents0); + assertNotNull(pt1); + //printByteArray(pt1.getPayload()); + contents1 = pt1.toList(); + assertTrue(cmpStrings(contents0, contents1)); + for (String str : contents1) { + System.out.println(str); + } + } catch (Throwable ex) { + fail(ex.toString()); + } + + } + @Test public void testThereAndBackAgainCS() { PathTree pt0 = new PathTree(); @@ -62,17 +100,21 @@ public class TestPathTree { } bytes = pt0.getPayload(); assertNotNull(bytes); - printByteArray(bytes); + //printByteArray(bytes); pt1 = new PathTree(bytes); contents1 = pt1.toList(); - System.out.println(contents0.size()); - System.out.println(contents1.size()); + assertTrue(cmpStrings(contents0, contents1)); + assertEquals(contents0.size(), contents1.size()); - Collections.sort(contents0); - Collections.sort(contents1); + //System.out.println(contents0.size()); + //System.out.println(contents1.size()); + //Collections.sort(contents0); + //Collections.sort(contents1); + + /* System.out.println("Originals, not in New"); for (String thisS : contents0) { if (! contents1.contains(thisS)) { @@ -86,14 +128,31 @@ public class TestPathTree { System.out.println(thisS); } } + */ - assertEquals(contents0.size(), contents1.size()); } - // Helpers // + private boolean cmpStrings(List thisList, List thatList) { + Collection thisColl = new ArrayList(thisList); + Collection thatColl = new ArrayList(thatList); + + Collection similar = new HashSet( thisColl ); + Collection different = new HashSet(); + different.addAll( thisColl ); + different.addAll( thatColl ); + + similar.retainAll( thatColl ); + different.removeAll( similar ); + + if (different.size() > 0) { + System.out.printf("Different:%s%n", different); + } + return (different.size() == 0); + } + private void printByteArray(byte[] bytes) { int width = 30; int counter = 0; @@ -113,6 +172,24 @@ public class TestPathTree { return getClass().getClassLoader().getResourceAsStream(filename); } + private byte[] loadBytes(String filename) { + InputStream in = resStream(filename); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[16834]; + + try { + while ((nRead = in.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + buffer.flush(); + } catch (IOException ex) { + fail(ex.toString()); + } + + return buffer.toByteArray(); + } + private List loadContents(String filename) { String content; List contentList = new ArrayList(); diff --git a/src/test/resources/test.bin b/src/test/resources/test.bin new file mode 100644 index 0000000..e689725 Binary files /dev/null and b/src/test/resources/test.bin differ