Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests (most of which fail) for some of the existing utilities.
- Loading branch information
dmcoe
committed
Aug 30, 2012
1 parent
c6e3e9e
commit c38cbd5
Showing
6 changed files
with
878 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
testing/org.cishell.utilities.tests/src/org/cishell/utilities/ArrayListUtilitiesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.cishell.utilities; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
|
||
public class ArrayListUtilitiesTests { | ||
|
||
@Test | ||
/** | ||
* Test the makePreview method | ||
*/ | ||
public void testMakePreview() { | ||
String separator = ", "; | ||
String ellipsis = "..."; | ||
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6); | ||
int requestedPrefixSize = 2; | ||
int requestedSuffixSize = 1; | ||
String result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, ..., 6".equals(result)); | ||
|
||
requestedPrefixSize = 7; | ||
requestedSuffixSize = 7; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, 3, 4, 5, 6".equals(result)); | ||
|
||
requestedPrefixSize = 0; | ||
requestedSuffixSize = 2; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("..., 5, 6".equals(result)); | ||
|
||
requestedPrefixSize = 2; | ||
requestedSuffixSize = 0; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, ...".equals(result)); | ||
|
||
requestedPrefixSize = 6; | ||
requestedSuffixSize = 0; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, 3, 4, 5, 6".equals(result)); | ||
|
||
requestedPrefixSize = 0; | ||
requestedSuffixSize = 6; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, 3, 4, 5, 6".equals(result)); | ||
|
||
requestedPrefixSize = 5; | ||
requestedSuffixSize = 0; | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1, 2, 3, 4, 5, ...".equals(result)); | ||
|
||
requestedPrefixSize = 7; | ||
requestedSuffixSize = 7; | ||
result = ArrayListUtilities.makePreview(Arrays.asList(1), requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("1".equals(result)); | ||
|
||
requestedPrefixSize = 2; | ||
requestedSuffixSize = 2; | ||
result = ArrayListUtilities.makePreview(Arrays.asList(list, list ,list, list, list ,list, list, list ,list, list, list ,list, list), requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
assertTrue("[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], ..., [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]".equals(result)); | ||
|
||
requestedPrefixSize = -1; | ||
requestedSuffixSize = -1; | ||
try { | ||
result = ArrayListUtilities.makePreview(list, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
System.out.println(requestedPrefixSize + "," + requestedSuffixSize + ": " + result); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
// Ok, an error was expected | ||
} | ||
|
||
requestedPrefixSize = 7; | ||
requestedSuffixSize = 7; | ||
try { | ||
result = ArrayListUtilities.makePreview(null, requestedPrefixSize, requestedSuffixSize, separator, ellipsis); | ||
System.out.println("null = " + result); | ||
fail(); | ||
} catch (NullPointerException e) { | ||
// Ok, an error was expected. | ||
} | ||
} | ||
|
||
/** | ||
* Test the {@link ArrayListUtilities#unionCollections(java.util.Collection, java.util.Collection, java.util.Collection)} method. | ||
*/ | ||
@Test | ||
public void testUnionCollections() { | ||
Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(new Integer[]{1, 2, 3, 4, 5})); | ||
Set<Integer> s2 = new HashSet<Integer>(Arrays.asList(new Integer[]{6, 7, 8, 9, 10})); | ||
Set<Integer> skip = new HashSet<Integer>(); | ||
|
||
Collection<Integer> union = ArrayListUtilities.unionCollections(s1, s2, skip); | ||
assertTrue(union.containsAll(s1)); | ||
assertTrue(union.containsAll(s2)); | ||
|
||
skip = new HashSet<Integer>(Arrays.asList(new Integer[]{2, 9})); | ||
union = ArrayListUtilities.unionCollections(s1, s2, skip); | ||
assertFalse(union.containsAll(s1)); | ||
assertFalse(union.containsAll(s2)); | ||
for (Integer i : skip) { | ||
assertTrue(!union.contains(i)); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
testing/org.cishell.utilities.tests/src/org/cishell/utilities/PrefuseUtilitiesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.cishell.utilities; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.cishell.utilities.PrefuseUtilities.UninterpretableObjectException; | ||
import org.junit.Test; | ||
|
||
public class PrefuseUtilitiesTests { | ||
|
||
@Test | ||
/** | ||
* Test Objects that might be from the Prefuse wrapping issue. | ||
*/ | ||
public void testValidWrappedObjects() { | ||
List<Pair<?, ?>> wrappedAndAnswers = new ArrayList<Pair<?, ?>>(); | ||
|
||
// integer | ||
wrappedAndAnswers.add(new Pair(Integer.valueOf(1), Integer.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Integer[] { Integer.valueOf(1) }, | ||
Integer.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Integer[0], null)); | ||
|
||
// long | ||
wrappedAndAnswers.add(new Pair(Long.valueOf(1L), Long.valueOf(1L))); | ||
wrappedAndAnswers.add(new Pair(new Long[] { Long.valueOf(1L) }, Long | ||
.valueOf(1L))); | ||
wrappedAndAnswers.add(new Pair(new Long[0], null)); | ||
|
||
// string | ||
wrappedAndAnswers.add(new Pair(String.valueOf(1), String.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new String[] { String.valueOf(1) }, | ||
String.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new String[0], null)); | ||
|
||
// boolean | ||
wrappedAndAnswers.add(new Pair(Boolean.valueOf(true), Boolean | ||
.valueOf(true))); | ||
wrappedAndAnswers.add(new Pair(new Boolean[] { Boolean.valueOf(true) }, | ||
Boolean.valueOf(true))); | ||
wrappedAndAnswers.add(new Pair(new Boolean[0], null)); | ||
|
||
// double | ||
wrappedAndAnswers.add(new Pair(Double.valueOf(1), Double.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Double[] { Double.valueOf(1) }, | ||
Double.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Double[0], null)); | ||
|
||
// float | ||
wrappedAndAnswers.add(new Pair(Float.valueOf(1), Float.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Float[] { Float.valueOf(1) }, Float | ||
.valueOf(1))); | ||
wrappedAndAnswers.add(new Pair(new Float[0], null)); | ||
|
||
for (Pair<?, ?> wrappedAndAnswer : wrappedAndAnswers) { | ||
Object result = PrefuseUtilities | ||
.removePrefuseArrayWrapper(wrappedAndAnswer | ||
.getFirstObject()); | ||
Object answer = wrappedAndAnswer.getSecondObject(); | ||
if (result != null && wrappedAndAnswer.getSecondObject() != null) { | ||
assertTrue(result + " did not match the expected: " + answer, | ||
result.equals(answer)); | ||
} else { | ||
assertTrue(result == answer); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
/** | ||
* Test objects that are NOT from a prefuse wrapping issue. | ||
*/ | ||
public void testInvalidObjects() { | ||
int[] actualArray = new int[] { 1, 2, 3 }; | ||
assertTrue(actualArray == PrefuseUtilities | ||
.removePrefuseArrayWrapper(actualArray)); | ||
|
||
try { | ||
PrefuseUtilities.removePrefuseArrayWrapper(null); | ||
fail(); | ||
} catch (NullPointerException e) { | ||
// null was invalid input | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
testing/org.cishell.utilities.tests/src/org/cishell/utilities/SetUtilitiesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.cishell.utilities; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
public class SetUtilitiesTests { | ||
@Test | ||
public void testGetKeysOfMapEntrySetWithValue() { | ||
Map<String, Integer> map = new HashMap<String, Integer>(); | ||
map.put("Hello", 1); | ||
map.put("Goodbye", 2); | ||
map.put(null, 2); | ||
|
||
Collection<String> oneResult = SetUtilities.getKeysOfMapEntrySetWithValue(map.entrySet(), 1); | ||
assertTrue(oneResult.size() == 1); | ||
assertTrue(oneResult.contains("Hello")); | ||
assertFalse(oneResult.contains("Goodbye")); | ||
assertFalse(oneResult.contains(null)); | ||
|
||
Collection<String> twoResult = SetUtilities.getKeysOfMapEntrySetWithValue(map.entrySet(), 2); | ||
assertTrue(twoResult.size() == 2); | ||
assertTrue(twoResult.contains("Goodbye")); | ||
assertTrue(twoResult.contains(null)); | ||
assertFalse(twoResult.contains("Hello")); | ||
|
||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
testing/org.cishell.utilities.tests/src/org/cishell/utilities/StringUtilitiesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.cishell.utilities; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.junit.Test; | ||
|
||
public class StringUtilitiesTests { | ||
|
||
@Test | ||
/** | ||
* Test the stripSurroundingQuotes method | ||
*/ | ||
public void testStripSurroundingQuotes() { | ||
Map<String, String> quotedAndAnswer = new HashMap<String, String>(); | ||
quotedAndAnswer.put("\"", "\""); | ||
quotedAndAnswer.put("\"\"", ""); | ||
quotedAndAnswer.put("\"\"\"", "\""); | ||
quotedAndAnswer.put("\"Hello!\"", "Hello!"); | ||
quotedAndAnswer.put("\"\"\"\"", "\"\""); | ||
quotedAndAnswer.put("\"\"Hello!\"\"", "\"Hello!\""); | ||
|
||
for (Entry<String, String> entry : quotedAndAnswer.entrySet()) { | ||
|
||
assertTrue(entry.getValue().equals( | ||
StringUtilities.stripSurroundingQuotes(entry.getKey()))); | ||
|
||
} | ||
|
||
try { | ||
StringUtilities.stripSurroundingQuotes(null); | ||
fail(); | ||
} catch (NullPointerException e) { | ||
// OK, expected exception | ||
} | ||
} | ||
|
||
@Test | ||
/** | ||
* Test the countOccurrencesOfChar method | ||
*/ | ||
public void testCountOccurrencesOfChar() { | ||
Map<Pair<CharSequence, String>, Integer> inputAndOutput = new HashMap<Pair<CharSequence, String>, Integer>(); | ||
inputAndOutput.put(new Pair<CharSequence, String>("", "1"), 0); | ||
inputAndOutput.put(new Pair<CharSequence, String>( | ||
"#128ahf-a-dgfba-d-f!", "@"), 0); | ||
inputAndOutput.put(new Pair<CharSequence, String>( | ||
"#128ahf-a-dgfba-d-f!", "1"), 1); | ||
inputAndOutput.put(new Pair<CharSequence, String>( | ||
"#128ahf-a-dgfba-d-f!", "#"), 1); | ||
inputAndOutput.put(new Pair<CharSequence, String>( | ||
"David is very nice!", "i"), 3); | ||
inputAndOutput.put(new Pair<CharSequence, String>("(((((((._))", ")"), | ||
2); | ||
|
||
for (Entry<Pair<CharSequence, String>, Integer> entry : inputAndOutput | ||
.entrySet()) { | ||
|
||
int answer = StringUtilities.countOccurrencesOfChar(entry.getKey() | ||
.getFirstObject(), entry.getKey().getSecondObject() | ||
.toCharArray()[0]); | ||
assertTrue(answer == entry.getValue()); | ||
} | ||
|
||
try { | ||
StringUtilities.countOccurrencesOfChar(null, '0'); | ||
fail(); | ||
} catch (NullPointerException e) { | ||
// OK, expected exception | ||
} | ||
} | ||
|
||
@Test | ||
/** | ||
* Test the {@link StringUtilities#multiply(String, int)} method. | ||
*/ | ||
public void testMultipy() { | ||
try { | ||
StringUtilities.multiply(null, 0); | ||
fail(); | ||
} catch (NullPointerException e) { | ||
// OK, expected exception | ||
} | ||
|
||
try { | ||
StringUtilities.multiply("target", -5); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
// Ok, expected exception was caught | ||
} | ||
|
||
Map<Pair<String, Integer>, String> inputOutput = new HashMap<Pair<String,Integer>, String>(); | ||
inputOutput.put(new Pair<String, Integer>("####", 5), "####################"); | ||
inputOutput.put(new Pair<String, Integer>("####", 0), ""); | ||
inputOutput.put(new Pair<String, Integer>("abcde", 2), "abcdeabcde"); | ||
inputOutput.put(new Pair<String, Integer>("", 2), ""); | ||
for (Entry<Pair<String, Integer>, String> entry : inputOutput.entrySet()) { | ||
String answer = StringUtilities.multiply(entry.getKey().getFirstObject(), entry.getKey().getSecondObject()); | ||
assertTrue(answer + ": " + entry.getValue(), entry.getValue().equals(answer)); | ||
} | ||
} | ||
|
||
/** | ||
* Test the {@link StringUtilities#filterEmptyStrings(String[])} | ||
* | ||
*/ | ||
@Test | ||
public void testFilterEmptyStrings() { | ||
List<String> emptyStrings = Arrays.asList(new String[] {""}); | ||
List<String> notEmptyStrings = Arrays.asList(new String[] {"1", " 2 ", "3\n", " "}); | ||
List<String> stringsToFilter = new ArrayList<String>(emptyStrings); | ||
stringsToFilter.addAll(notEmptyStrings); | ||
|
||
String[] filtered = StringUtilities.filterEmptyStrings(stringsToFilter.toArray(new String[0])); | ||
assertTrue(Arrays.asList(filtered).containsAll(notEmptyStrings)); | ||
for (String empty : emptyStrings) { | ||
assertFalse(Arrays.asList(filtered) + " contains '" + empty + "'.", Arrays.asList(filtered).contains(empty)); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.