Skip to content

Commit

Permalink
Merge pull request #35 from nbauma109/bind-types-to-types-visitor-test
Browse files Browse the repository at this point in the history
added BindTypesToTypesVisitorTest
  • Loading branch information
nbauma109 authored May 20, 2023
2 parents e735132 + 1b6a4f8 commit aae2fbe
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void visit(InnerObjectType type) {
public void visit(GenericType type) {
TypeArgument ta = bindings.get(type.getName());

if ((ta == null) || (ta == WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT)) {
if (ta == null || ta == WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT) {
result = TYPE_OBJECT.createType(type.getDimension());
} else {
typeArgumentToTypeVisitor.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

@SuppressWarnings("all")
public class TypeMakerTest extends TestCase {
protected TypeMaker typeMaker = new TypeMaker(new ClassPathLoader());
protected TypeMaker typeMaker = new TypeMaker();

protected ObjectType otAbstractUntypedIteratorDecorator = makeObjectType(AbstractUntypedIteratorDecorator.class);
protected ObjectType otArrayList = makeObjectType(ArrayList.class);
Expand Down Expand Up @@ -720,20 +720,23 @@ public void testSearchSuperParameterizedType() throws Exception {
ObjectType treeMap = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/TreeMap");
ObjectType abstMap = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/AbstractMap");
ObjectType set = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/Set");
ObjectType genericSet = set.createType(new GenericType("V"));
ObjectType unmodifiableEntrySet = typeMaker.makeFromInternalTypeName("org/apache/commons/collections4/map/UnmodifiableEntrySet");
ObjectType mapEntry = typeMaker.makeFromDescriptorOrInternalTypeName("java/util/Map$Entry");
ObjectType valueView = typeMaker.makeFromInternalTypeName("org/apache/commons/collections4/bidimap/TreeBidiMap$ValueView");
TypeArguments typeArguments = new TypeArguments(Arrays.asList(new GenericType("K"), new GenericType("V")));
mapEntry = mapEntry.createType(typeArguments);
unmodifiableEntrySet = unmodifiableEntrySet.createType(typeArguments);
set = set.createType(mapEntry);
ObjectType mapEntrySet = set.createType(mapEntry);
assertEquals(abstMap, typeMaker.searchSuperParameterizedType(abstMap, hashMap));
assertEquals(abstMap, typeMaker.searchSuperParameterizedType(abstMap, treeMap));
assertEquals(treeMap, typeMaker.searchSuperParameterizedType(ObjectType.TYPE_UNDEFINED_OBJECT, treeMap));
assertEquals(treeMap, typeMaker.searchSuperParameterizedType(ObjectType.TYPE_OBJECT, treeMap));
assertEquals(ObjectType.TYPE_CLASS, typeMaker.searchSuperParameterizedType(ObjectType.TYPE_CLASS, ObjectType.TYPE_CLASS));
assertNull(typeMaker.searchSuperParameterizedType((ObjectType) ObjectType.TYPE_CLASS.createType(1), ObjectType.TYPE_CLASS));
assertNull(typeMaker.searchSuperParameterizedType(ObjectType.TYPE_CLASS, (ObjectType) ObjectType.TYPE_CLASS.createType(1)));
assertEquals(set, typeMaker.searchSuperParameterizedType(set, unmodifiableEntrySet));
assertEquals(mapEntrySet, typeMaker.searchSuperParameterizedType(set, unmodifiableEntrySet));
assertEquals(set, typeMaker.searchSuperParameterizedType(genericSet, valueView));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.jd.core.v1.service.converter.classfiletojavasyntax.visitor;

import org.jd.core.v1.model.javasyntax.type.BaseType;
import org.jd.core.v1.model.javasyntax.type.GenericType;
import org.jd.core.v1.model.javasyntax.type.InnerObjectType;
import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.jd.core.v1.model.javasyntax.type.TypeArgument;
import org.jd.core.v1.model.javasyntax.type.WildcardTypeArgument;
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class BindTypesToTypesVisitorTest {
private BindTypesToTypesVisitor visitor;
private Map<String, TypeArgument> bindings;
private TypeMaker typeMaker;

@Before
public void setup() {
visitor = new BindTypesToTypesVisitor();
bindings = new HashMap<>();
typeMaker = new TypeMaker();
visitor.setBindings(bindings);
}

@Test
public void visitGenericTypeWhenTypeArgumentIsNull() {
// Given
GenericType genericType = new GenericType("Test", 0);
bindings.put("Test", null);

// When
visitor.init();
genericType.accept(visitor);

// Then
BaseType result = visitor.getType();
assertEquals(ObjectType.TYPE_OBJECT.createType(0), result);
}

@Test
public void visitGenericTypeWhenTypeArgumentIsWildcard() {
// Given
GenericType genericType = new GenericType("Test", 0);
bindings.put("Test", WildcardTypeArgument.WILDCARD_TYPE_ARGUMENT);

// When
visitor.init();
genericType.accept(visitor);

// Then
BaseType result = visitor.getType();
assertEquals(ObjectType.TYPE_OBJECT.createType(0), result);
}

@Test
public void visitGenericTypeWhenTypeArgumentIsNotWildcardNorNull() {
// Given
GenericType genericType = new GenericType("Test", 0);
ObjectType typeArgument = ObjectType.TYPE_OBJECT;
bindings.put("Test", typeArgument);

// When
visitor.init();
genericType.accept(visitor);

// Then
BaseType result = visitor.getType();

// Assuming that result is equivalent to what
// typeArgumentToTypeVisitor.getType() returns, with increased dimension
assertEquals(typeArgument.createType(0 + genericType.getDimension()), result);
}

@Test
public void visitInnerObjectTypeWhenResultIsNotEqualOuterTypeAndTypeArgumentsIsNotWildcardNorNull() {
// Given
ObjectType outerType = typeMaker.makeFromInternalTypeName("OuterType<T>");
InnerObjectType innerObjectType = (InnerObjectType) typeMaker.makeFromInternalTypeName("OuterType<T>$InnerType<U>");

outerType.accept(visitor);

// When
visitor.init();
innerObjectType.accept(visitor);

// Then
BaseType result = visitor.getType();

// Assuming that when typeArguments is not WildcardTypeArgument or null and
// result is not equal to outerType,
// the result is a new InnerObjectType with the same typeArguments
assertEquals(innerObjectType, result);
}
}

0 comments on commit aae2fbe

Please sign in to comment.