Skip to content

Commit

Permalink
Update JUnit to 4.12 and other changes
Browse files Browse the repository at this point in the history
Also do some code modernization, use <> notation where possible,
get rid of some untyped generics, and update tests to use JUnit 4
annotations. Also add some more methods for comparing collections
and maps.
  • Loading branch information
Joel committed Sep 2, 2016
1 parent 3638212 commit 221f164
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 259 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<version>4.12</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class AbstractMultiChildNode<V> extends AbstractNode<V> {

private final AbstractNode<V>[] children;

protected AbstractMultiChildNode(V value, AbstractNode<V>[] children) {
protected AbstractMultiChildNode(V value, AbstractNode<V>... children) {
super(value);
this.children = children;
}
Expand Down Expand Up @@ -153,34 +153,34 @@ public void getPartialMatches(Set<String> partialMatches, char[] searchArr, int
}
break;
}
// // there are existing children. scan them to see if there are any with
// // matching prefixes
// for (int i = 0; i < children.length; i++) {
// AbstractNode<V> childNode = children[i];
// int commonLength = Utils.getCommonLength(searchArr, searchArrOffset, childNode.getPrefix());
//
// // if there was any match...
// if (commonLength == childNode.getPrefix().length) {
// // if there's a value in this child, then it is a partial match and should be added to the set
// if (childNode.value != null) {
// partialMatches.add(new String(searchArr, 0, searchArrOffset + commonLength));
// }
//
// // explore further down this subtree to see if there are more partial matches
// childNode.getPartialMatches(partialMatches, searchArr, searchArrOffset + commonLength);
//
// // no reason to keep searching past here, and let's just save the extra
// // comparison below
// break;
// }
//
// // if we had a partial match, but not a complete one, then there is no way
// // for the other children of the current node to have a match, so we
// // should exit.
// if (commonLength > 0) {
// break;
// }
// }

// // there are existing children. scan them to see if there are any with
// // matching prefixes
// for (int i = 0; i < children.length; i++) {
// AbstractNode<V> childNode = children[i];
// int commonLength = Utils.getCommonLength(searchArr, searchArrOffset, childNode.getPrefix());
//
// // if there was any match...
// if (commonLength == childNode.getPrefix().length) {
// // if there's a value in this child, then it is a partial match and should be added to the set
// if (childNode.value != null) {
// partialMatches.add(new String(searchArr, 0, searchArrOffset + commonLength));
// }
//
// // explore further down this subtree to see if there are more partial matches
// childNode.getPartialMatches(partialMatches, searchArr, searchArrOffset + commonLength);
//
// // no reason to keep searching past here, and let's just save the extra
// // comparison below
// break;
// }
//
// // if we had a partial match, but not a complete one, then there is no way
// // for the other children of the current node to have a match, so we
// // should exit.
// if (commonLength > 0) {
// break;
// }
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ abstract class AbstractRadixTreeMap<V> implements Map<String, V> {

@Override
public final Set<java.util.Map.Entry<String, V>> entrySet() {
return new EntrySet<V>(this, getRoot());
return new EntrySet<>(this, getRoot());
}

@Override
public final Set<String> keySet() {
return new KeySet<V>(this, getRoot());
return new KeySet<>(this, getRoot());
}

@Override
public final Collection<V> values() {
ArrayList<V> l = new ArrayList<V>(size());
ArrayList<V> l = new ArrayList<>(size());
for (Map.Entry<String, V> entry : entrySet()) {
l.add(entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ final class MultiChildNode<V> extends AbstractMultiChildNode<V> {
private final char[] prefix;

public MultiChildNode(char[] prefix,
V value,
AbstractNode<V>[] optimizedChildren) {
V value,
AbstractNode<V>... optimizedChildren) {
super(value, optimizedChildren);
this.prefix = prefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class SingleLengthMultiChildNode<V> extends AbstractMultiChildNode<V> {
private final char prefix;

protected SingleLengthMultiChildNode(char prefix, V value, AbstractNode<V>[] children) {
protected SingleLengthMultiChildNode(char prefix, V value, AbstractNode<V>... children) {
super(value, children);
this.prefix = prefix;
}
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/com/liveramp/commons/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import static junit.framework.Assert.fail;
import static org.junit.Assert.fail;

public class TestUtils {

public static <T> void assertCollectionEquivalent(Collection<T> expectedCollection,
Collection<T> collection) {
assertCollectionEquivalent("Collections not equivalent.", expectedCollection, collection);
}

public static <T> void assertCollectionEquivalent(String message, Collection<T> expectedCollection,
Collection<T> collection) {
if (!CollectionUtils.isEqualCollection(expectedCollection, collection)) {
System.out.println("Expected:");
printCollection(expectedCollection);
Expand Down Expand Up @@ -54,10 +60,18 @@ public static <T> void assertCollectionEquivalent(Collection<T> expectedCollecti
}
}

fail("Collections not equivalent.");
fail(message);
}
}

public static <K, V> void assertMapsEquivalent(Map<K, V> expectedMap, Map<K, V> actualMap) {
assertMapsEquivalent("Maps not equivalent.", expectedMap, actualMap);
}

public static <K, V> void assertMapsEquivalent(String message, Map<K, V> expectedMap, Map<K, V> actualMap) {
assertCollectionEquivalent(message, expectedMap.entrySet(), actualMap.entrySet());
}

public static <T> String getStringRepresentation(T t) {

// Copy BytesWritable toString
Expand All @@ -84,15 +98,15 @@ public static <T> String getStringRepresentation(T t) {
}


protected static void printCollection(Collection collection) {
public static <T> void printCollection(Collection<T> collection) {
printCollection(null, collection);
}

protected static void printCollection(String header, Collection collection) {
public static <T> void printCollection(String header, Collection<T> collection) {
if (header != null) {
System.out.println(header);
}
for (Object item : collection) {
for (T item : collection) {
System.out.println(item);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/liveramp/commons/TestAccessors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.HashSet;

import com.google.common.collect.Sets;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;

public class TestAccessors {
Expand All @@ -15,7 +15,7 @@ public void testFirstOnNull() throws Exception {

@Test(expected = IllegalArgumentException.class)
public void testFirstOnEmpty() throws Exception {
Accessors.first(new HashSet<Object>());
Accessors.first(new HashSet<>());
}

@Test
Expand All @@ -30,7 +30,7 @@ public void testSecondOnNull() throws Exception {

@Test(expected = IllegalArgumentException.class)
public void testSecondOnEmpty() throws Exception {
Accessors.second(new HashSet<Object>());
Accessors.second(new HashSet<>());
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package com.liveramp.commons.collections;

import junit.framework.TestCase;
import org.junit.Test;

public class TestLruHashMap extends TestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class TestLruHashMap {
@Test
public void testIt() throws Exception {
LruHashMap<String, String> m = new LruHashMap<String, String>(3);
LruHashMap<String, String> m = new LruHashMap<>(3);

m.put("blah", "blah");
assertEquals(1, m.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
import java.util.Iterator;
import java.util.Map;

import junit.framework.TestCase;
import org.junit.Test;

import com.liveramp.commons.util.IntegerMemoryUsageEstimator;
import com.liveramp.commons.util.LongMemoryUsageEstimator;
import com.liveramp.commons.util.StringMemoryUsageEstimator;

public class TestMemoryBoundLruHashMap extends TestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class TestMemoryBoundLruHashMap {
@Test
public void testNumBytesCapacity() {
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<Integer, String>(128, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<>(128, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());

assertEquals(0, map.size());
assertEquals(0, map.getNumManagedBytes());
Expand Down Expand Up @@ -94,7 +95,7 @@ public void testNumBytesCapacity() {

@Test
public void testNumItemsCapacity() {
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<Integer, String>(2, 1024, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<>(2, 1024, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());

assertEquals(0, map.size());
assertEquals(0, map.getNumManagedBytes());
Expand Down Expand Up @@ -144,7 +145,7 @@ public void testNumItemsCapacity() {

@Test
public void testIterator() {
MemoryBoundLruHashMap<Long, Long> map = new MemoryBoundLruHashMap<Long, Long>(10, 1024, new LongMemoryUsageEstimator(), new LongMemoryUsageEstimator());
MemoryBoundLruHashMap<Long, Long> map = new MemoryBoundLruHashMap<>(10, 1024, new LongMemoryUsageEstimator(), new LongMemoryUsageEstimator());
map.putAndEvict(1L, 1L);
map.putAndEvict(2L, 2L);
map.putAndEvict(3L, 3L);
Expand All @@ -161,5 +162,4 @@ public void testIterator() {

assertFalse(itr.hasNext());
}

}
Loading

0 comments on commit 221f164

Please sign in to comment.