use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testClassCastExceptionOnForEach.
@Test
public void testClassCastExceptionOnForEach() throws Exception {
// contract: bound query, without any mapping
// This test could fail with a version of JDK <= 8.0.40.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
int count = 0;
}
{
Context context = new Context();
// contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
});
assertTrue(context.count > 0);
}
{
Context context = new Context();
// contract: if the for each implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
throw new ClassCastException("TEST");
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the for each implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach(new CtConsumer<CtType>() {
@Override
public void accept(CtType t) {
context.count++;
throw new ClassCastException("TEST");
}
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select(new Filter<CtType>() {
@Override
public boolean matches(CtType element) {
context.count++;
throw new ClassCastException("TEST");
}
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select((CtType element) -> {
context.count++;
throw new ClassCastException("TEST");
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtFunction<CtType, Object>() {
@Override
public Object apply(CtType input) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtConsumableFunction<CtType>() {
@Override
public void apply(CtType input, CtConsumer<Object> outputConsumer) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input, CtConsumer<Object> outputConsumer) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
}
use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testQueryBuilderWithFilterChain.
@Test
public void testQueryBuilderWithFilterChain() throws Exception {
// contract: query methods can be lazy evaluated in a foreach
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
CtMethod<?> method;
int count = 0;
}
Context context = new Context();
// chaining queries
CtQuery q = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<CtMethod<?>>(CtMethod.class)).map((CtMethod<?> method) -> {
context.method = method;
return method;
}).map(new OverriddenMethodQuery());
// actual evaluation
q.forEach((CtMethod<?> method) -> {
assertTrue(context.method.getReference().isOverriding(method.getReference()));
assertTrue(context.method.isOverriding(method));
context.count++;
});
// sanity check
assertTrue(context.count > 0);
}
use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testInvocationFilterWithExecutableInLibrary.
@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
// contract: When we have an invocation of an executable declared in a library,
// we can filter it and get the executable of the invocation.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
assertEquals(1, invocations.size());
final CtInvocation<?> expectedInv = invocations.get(0);
assertNotNull(expectedInv);
final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
assertNotNull(expectedExecutable);
assertEquals("size", expectedExecutable.getSimpleName());
assertNull(expectedExecutable.getDeclaration());
CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
assertEquals("size", exec.getSimpleName());
assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
assertNotNull(declaration);
assertEquals("size", declaration.getSimpleName());
}
use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testFilterQueryStep.
@Test
public void testFilterQueryStep() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
// Contract: the filter(Filter) can be used to detect if input of query step should pass to next query step.
List<CtElement> realList = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
return true;
}).select(new TypeFilter<>(CtClass.class)).list();
List<CtElement> expectedList = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<>(CtClass.class)).list();
assertArrayEquals(expectedList.toArray(), realList.toArray());
assertTrue(expectedList.size() > 0);
}
use of spoon.Launcher in project spoon by INRIA.
the class FilterTest method testFunctionQueryStep.
@Test
public void testFunctionQueryStep() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
int count = 0;
}
Context context = new Context();
CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
return true;
}).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
((CtQueryImpl) query).logging(true);
query.forEach((CtInterface<?> c) -> {
assertEquals("ITostada", c.getSimpleName());
context.count++;
});
assertTrue(context.count > 0);
}
Aggregations