Skip to content

Commit

Permalink
typetools: tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
  • Loading branch information
KetothXupack committed Oct 22, 2013
1 parent 4001ffc commit dcfa2ad
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.nohope.Matchers.and;
import static org.nohope.Matchers.not;
import static org.nohope.reflection.ModifierMatcher.*;
import static org.nohope.Matchers.*;

//import static java.lang.reflect.Modifier.PUBLIC;

Expand Down Expand Up @@ -957,7 +975,8 @@ public static Class<?> getClass(final Type type) {
return null;
}

public static Class<?>[] getAllBounds(final Type type) {
// TODO: tree traversal
public static List<Class<?>> getAllBounds(final Type type) {
final List<Class<?>> result = new ArrayList<>();
if (type instanceof TypeVariable) {
final TypeVariable variable = (TypeVariable) type;
Expand All @@ -977,7 +996,7 @@ public static Class<?>[] getAllBounds(final Type type) {
}
}

return result.toArray(new Class<?>[result.size()]);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static boolean compare(@Nonnull final XMLGregorianCalendar c1,
return compareResultMatcher.matches(c1.compare(c2));
}

public static class LazyDataTypeFactorySingleton {
static class LazyDataTypeFactorySingleton {
private static final DatatypeFactory datatypeFactory;
static {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;
import org.nohope.IMatcher;
import org.nohope.test.UtilitiesTestSupport;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.*;
import static org.nohope.Matchers.*;
import static org.nohope.reflection.IntrospectionUtils.*;
import static org.nohope.reflection.ModifierMatcher.*;
import static org.nohope.Matchers.*;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">ketoth xupack</a>
Expand Down Expand Up @@ -106,6 +110,25 @@ public void primitiveNotCompatibleWithNull() {
}
}

@Test
public void className() {
assertEquals(String.class.getCanonicalName(), getCanonicalClassName("123"));
assertNull(getCanonicalClassName(null));
}

@Test
public void wrappers() {
final Set<Class<?>> wrappers = new HashSet<>();
for (final Class<?> primitive : getPrimitives()) {
wrappers.add(autoBox(primitive));
}
assertEquals(wrappers, new HashSet<>(getWrappers()));

for (final Class<?> c : getWrappers()) {
assertFalse(c.isPrimitive());
}
}

@Test
public void autoboxing() {
for (final Class<?> primitive : getPrimitives()) {
Expand Down Expand Up @@ -254,6 +277,33 @@ public void privateMethodInvoke()
assertEquals(3, result);
}

@Test
public void genericBounds() {
final List<Class<?>> bounds = getAllBounds(new TypeReference<Map<String, Integer>>() {}.getType());
assertTrue(bounds.isEmpty());
}

@Test
public void classGetting() {
assertNull(IntrospectionUtils.getClass((Object) null));
assertEquals(String.class, IntrospectionUtils.getClass("xxx"));
final Object clazz = String.class;
assertEquals(clazz, IntrospectionUtils.getClass(clazz));
}

@Test
public void casting() {
final Object o = "123";
assertEquals(o, cast(o, String.class));
assertEquals(o, cast(o, TypeReference.erasure(String.class)));

try {
cast(o, Integer.class);
fail();
} catch (final ClassCastException ignored) {
}
}

@Test
public void arrayTypeShrinking() {
Object[] testArray;
Expand Down Expand Up @@ -420,6 +470,22 @@ public void collisionMethod()
invoke(this, "method5", 1);
}

@Test
public void constructorSearch() {
class A {
private A(int x) {
}
public A(long y) {
}
}
assertEquals(1, searchConstructors(A.class, new IMatcher<Constructor<? extends A>>() {
@Override
public boolean matches(final Constructor<? extends A> obj) {
return !Modifier.isPrivate(obj.getModifiers());
}
}).size());
}

@Test
public void methodSearch() throws NoSuchMethodException {
assertNotNull(searchMethod(getClass(), not(or(PUBLIC, PROTECTED, PRIVATE)), "testCall"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.nohope.typetools;

import org.nohope.test.UtilitiesTestSupport;

/**
* Date: 07.08.12
* Time: 17:26
*/
public class TTimeSingletonTest extends UtilitiesTestSupport {
@Override
protected Class<?> getUtilityClass() {
return TTime.LazyDataTypeFactorySingleton.class;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nohope.typetools.collection;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Test;
import org.nohope.ITranslator;
import org.nohope.test.UtilitiesTestSupport;
Expand Down Expand Up @@ -27,7 +28,7 @@ protected Class<?> getUtilityClass() {

@Test
public void toMap() {
final Map<String,Integer> map = CollectionUtils.toMap(
final Map<String, Integer> map = CollectionUtils.toMap(
new HashMap<String, Integer>(),
Arrays.asList(1, 2, 3),
new ITranslator<Integer, String>() {
Expand All @@ -44,6 +45,25 @@ public String translate(final Integer source) {
}
}

@Test
public void toExtendedMap() {
final Map<Long, Long> map = CollectionUtils.toExtendedMap(
new HashMap<Long, Long>(),
Arrays.asList(1, 2, 3),
new ITranslator<Integer, Map.Entry<Long, Long>>() {
@Override
public Map.Entry<Long, Long> translate(final Integer source) {
return new ImmutablePair<>(source * 10L, source + 1L);
}
});

assertEquals(3, map.size());

for (final Map.Entry<Long, Long> entry : map.entrySet()) {
assertEquals((long) entry.getKey(), 10L * (entry.getValue() - 1L));
}
}

@Test
public void toCollection() {
final Set<String> set = CollectionUtils.toCollection(
Expand Down

0 comments on commit dcfa2ad

Please sign in to comment.