more unit tests for payload -> content sets and back

pinned the bug down to the PathTree.condenseSubTreeNodes()
This commit is contained in:
Vincent Batts 2012-11-02 15:59:19 -04:00
parent 37a33c5545
commit d12655d4db
4 changed files with 113 additions and 11 deletions

View file

@ -10,6 +10,7 @@ import java.io.InputStreamReader;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
@ -83,6 +84,33 @@ public class App {
return null; 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<String> listFromFile(String filename) throws IOException, FileNotFoundException { public static List<String> listFromFile(String filename) throws IOException, FileNotFoundException {
FileInputStream fis; FileInputStream fis;
DataInputStream in; DataInputStream in;
@ -182,6 +210,7 @@ public class App {
List<String> contents; List<String> contents;
dos = (DEROctetString)objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7"); dos = (DEROctetString)objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7");
//byteArrayToFile(dos.getOctets(), "herp.bin");
if ((contents = hydrateFromBytes(dos.getOctets())) == null) { if ((contents = hydrateFromBytes(dos.getOctets())) == null) {
System.out.println("FAIL"); System.out.println("FAIL");
return; return;

View file

@ -92,8 +92,6 @@ public class PathTree {
/** /**
* Constructor using the list of content sets. * Constructor using the list of content sets.
*
* FIXME - This is a stub.
*/ */
public PathTree(List<String> contentSets) throws PayloadException { public PathTree(List<String> contentSets) throws PayloadException {
try { try {
@ -120,8 +118,6 @@ public class PathTree {
this.payload = payload; this.payload = payload;
//inflatePathDict
this.modified = false; this.modified = false;
} }

View file

@ -3,13 +3,18 @@ package com.redhat.trie;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Collection;
import java.util.HashSet;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Test; import org.junit.Test;
@ -46,6 +51,39 @@ public class TestPathTree {
assertNotNull(pt); assertNotNull(pt);
} }
@Test
public void testThereAndBackAgainPayload() {
byte[] bytes;
PathTree pt0;
PathTree pt1;
List<String> contents0;
List<String> 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 @Test
public void testThereAndBackAgainCS() { public void testThereAndBackAgainCS() {
PathTree pt0 = new PathTree(); PathTree pt0 = new PathTree();
@ -62,17 +100,21 @@ public class TestPathTree {
} }
bytes = pt0.getPayload(); bytes = pt0.getPayload();
assertNotNull(bytes); assertNotNull(bytes);
printByteArray(bytes); //printByteArray(bytes);
pt1 = new PathTree(bytes); pt1 = new PathTree(bytes);
contents1 = pt1.toList(); contents1 = pt1.toList();
System.out.println(contents0.size()); assertTrue(cmpStrings(contents0, contents1));
System.out.println(contents1.size()); assertEquals(contents0.size(), contents1.size());
Collections.sort(contents0); //System.out.println(contents0.size());
Collections.sort(contents1); //System.out.println(contents1.size());
//Collections.sort(contents0);
//Collections.sort(contents1);
/*
System.out.println("Originals, not in New"); System.out.println("Originals, not in New");
for (String thisS : contents0) { for (String thisS : contents0) {
if (! contents1.contains(thisS)) { if (! contents1.contains(thisS)) {
@ -86,14 +128,31 @@ public class TestPathTree {
System.out.println(thisS); System.out.println(thisS);
} }
} }
*/
assertEquals(contents0.size(), contents1.size());
} }
// Helpers // Helpers
// //
private boolean cmpStrings(List<String> thisList, List<String> thatList) {
Collection<String> thisColl = new ArrayList(thisList);
Collection<String> thatColl = new ArrayList(thatList);
Collection<String> similar = new HashSet<String>( thisColl );
Collection<String> different = new HashSet<String>();
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) { private void printByteArray(byte[] bytes) {
int width = 30; int width = 30;
int counter = 0; int counter = 0;
@ -113,6 +172,24 @@ public class TestPathTree {
return getClass().getClassLoader().getResourceAsStream(filename); 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<String> loadContents(String filename) { private List<String> loadContents(String filename) {
String content; String content;
List<String> contentList = new ArrayList<String>(); List<String> contentList = new ArrayList<String>();

BIN
src/test/resources/test.bin Normal file

Binary file not shown.