Skip to content

Commit

Permalink
test Substitution#insertType
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jun 4, 2017
1 parent af36686 commit ac0799d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.ModelConsistencyChecker;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.support.compiler.FileSystemFile;
import spoon.support.compiler.FileSystemFolder;
import spoon.support.template.Parameters;
import spoon.support.template.SubstitutionVisitor;
import spoon.template.Substitution;
import spoon.template.TemplateMatcher;
import spoon.template.TemplateParameter;
import spoon.test.template.testclasses.ArrayAccessTemplate;
Expand All @@ -41,10 +46,14 @@
import spoon.test.template.testclasses.inheritance.SuperTemplate;
import spoon.test.template.testclasses.logger.Logger;
import spoon.test.template.testclasses.logger.LoggerTemplateProcessor;
import spoon.test.template.testclasses.types.AClassModel;
import spoon.test.template.testclasses.types.AnEnumModel;
import spoon.test.template.testclasses.types.AnIfaceModel;

import java.io.File;
import java.io.Serializable;
import java.rmi.Remote;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -54,6 +63,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -555,4 +566,55 @@ public void testStatementTemplateRootSubstitution() throws Exception {
CtStatement result = new SubstituteRootTemplate(param).apply(resultKlass);
assertEquals("java.lang.String s = \"Spoon is cool!\"", ((CtBlock)result).getStatement(0).toString());
}

@Test
public void testInsertType() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput" });
launcher.addTemplateResource(new FileSystemFolder("./src/test/java/spoon/test/template/testclasses/types"));

launcher.buildModel();
Factory factory = launcher.getFactory();

CtPackage targetPackage = factory.Package().create(factory.getModel().getRootPackage(), "generated");
factory.getModel().getRootPackage().addPackage(targetPackage);


Map<String, Object> parameters = new HashMap<>();
//replace someMethod with genMethod
parameters.put("someMethod", "genMethod");

//contract: we can generate interface
final CtType<?> aIfaceModel = launcher.getFactory().Interface().get(AnIfaceModel.class);
CtType<?> genIface = Substitution.insertType(targetPackage, "GenIface", aIfaceModel, parameters);
assertNotNull(genIface);
assertSame(genIface, factory.Type().get("generated.GenIface"));
CtMethod<?> generatedIfaceMethod = genIface.getMethod("genMethod");
assertNotNull(generatedIfaceMethod);
assertNull(genIface.getMethod("someMethod"));

//add new substitution request - replace AnIfaceModel by GenIface
parameters.put("AnIfaceModel", genIface.getReference());
//contract: we can generate class
final CtType<?> aClassModel = launcher.getFactory().Class().get(AClassModel.class);
CtType<?> genClass = Substitution.insertType(targetPackage, "GenClass", aClassModel, parameters);
assertNotNull(genClass);
assertSame(genClass, factory.Type().get("generated.GenClass"));
CtMethod<?> generatedClassMethod = genClass.getMethod("genMethod");
assertNotNull(generatedClassMethod);
assertNull(genClass.getMethod("someMethod"));
assertTrue(generatedIfaceMethod!=generatedClassMethod);
assertTrue(generatedClassMethod.isOverriding(generatedIfaceMethod));

//contract: we can generate enum
parameters.put("case1", "GOOD");
parameters.put("case2", "BETTER");
final CtType<?> aEnumModel = launcher.getFactory().Type().get(AnEnumModel.class);
CtEnum<?> genEnum = (CtEnum<?>) Substitution.insertType(targetPackage, "GenEnum", aEnumModel, parameters);
assertNotNull(genEnum);
assertSame(genEnum, factory.Type().get("generated.GenEnum"));
assertEquals(2, genEnum.getEnumValues().size());
assertEquals("GOOD", genEnum.getEnumValues().get(0).getSimpleName());
assertEquals("BETTER", genEnum.getEnumValues().get(1).getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spoon.test.template.testclasses.types;

import java.util.AbstractList;

public class AClassModel<E> extends AbstractList<E> implements AnIfaceModel {

public AClassModel() {
}

@Override
public E get(int index) {
throw new IndexOutOfBoundsException();
}

@Override
public int size() {
return 0;
}

@Override
public void someMethod() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package spoon.test.template.testclasses.types;

public enum AnEnumModel {
case1, case2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spoon.test.template.testclasses.types;

import java.io.Serializable;

public interface AnIfaceModel extends Serializable {
void someMethod();
}

0 comments on commit ac0799d

Please sign in to comment.