miercuri, 7 octombrie 2015

Java: Stack Implementation

public class Stack<T> {
private LinkedList<T> container = new LinkedList<T>();
public void push(T v) { container.addFirst(v); }
public T peek() { return container.getFirst(); }
public T pop() { return container.removeFirst(); }
public boolean empty() { return container.isEmpty(); }
public String toString() { return container.toString(); }
}

vineri, 2 octombrie 2015

Java: JUnit: Run a unit test inside a inner class -- using @RunWith(Enclosed.class)

package com.test;

//public class in the file
@RunWith(Enclosed.class)
public class GetCountryBasedOnIPDelegate {
    ...
}

//another class in the file (package access)
@RunWith(Enclosed.class)
class CSVGeoCodeNameProvider {
    ...
    public static class TestCSVGeoCodeNameProvider extends TestCase {
@Before
@Override
public void setUp() throws Exception {
//do some setups
                        ...
}
                //add some tests
@Test
public void testbuildCache() {
long countryIPRangeListSize = countryIPRangeList.size();
GenericUtils.debug(String.format("No. of items in file %s is %d",
GeoIPUtils.ipv4FilePath, countryIPRangeListSize));
assertNotSame(countryIPRangeListSize, 0);
assertNotNull(countryIPRangeList.get(new Long(18153472)).countryCode);
assertEquals(
countryIPRangeList.get(new Long(18153472)).countryCode,
"JP");
}
}
}