use of spoon.test.annotation.testclasses.AnnotParamTypes in project spoon by INRIA.
the class AnnotationTest method testReplaceAnnotationValue.
@Test
public void testReplaceAnnotationValue() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
// contract: test replace of single value
CtExpression integerValue = a.getValue("integer");
assertEquals(42, ((CtLiteral<Integer>) integerValue).getValue().intValue());
assertEquals(42, annot.integer());
integerValue.replace(factory.createLiteral(17));
CtExpression newIntegerValue = a.getValue("integer");
assertEquals(17, ((CtLiteral<Integer>) newIntegerValue).getValue().intValue());
assertEquals(17, annot.integer());
// even if second value is null
try {
a.getValue("integer").replace(Arrays.asList(factory.createLiteral(18), null));
fail();
} catch (SpoonException e) {
// OK
}
// contract: replacing of single value by no value
a.getValue("integer").delete();
assertNull(a.getValue("integer"));
try {
annot.integer();
fail();
} catch (NullPointerException e) {
// OK - fails because int cannot be null
}
// contract: replace with null value means remove
a.getValue("string").replace((CtElement) null);
assertNull(a.getValue("string"));
// contract: check that null value can be returned
assertNull(annot.string());
// contract: replace with null value in collection means remove
a.getValue("clazz").replace(Collections.singletonList(null));
assertNull(a.getValue("clazz"));
// contract: check that null value can be returned
assertNull(annot.clazz());
// contract: test replace of item in collection
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
CtNewArray<?> integersNewArray = (CtNewArray) a.getValue("integers");
integersNewArray.getElements().get(0).replace(Arrays.asList(null, factory.createLiteral(101), null, factory.createLiteral(102)));
assertEquals(2, annot.integers().length);
assertEquals(101, annot.integers()[0]);
assertEquals(102, annot.integers()[1]);
}
use of spoon.test.annotation.testclasses.AnnotParamTypes in project spoon by INRIA.
the class AnnotationTest method testAnnotationParameterTypes.
@Test
public void testAnnotationParameterTypes() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("World", annot.strings()[1]);
assertEquals(Integer.class, annot.clazz());
assertEquals(2, annot.classes().length);
assertEquals(Integer.class, annot.classes()[0]);
assertEquals(String.class, annot.classes()[1]);
assertEquals(true, annot.b());
assertEquals('c', annot.c());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
CtMethod<?> m2 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m2")).get(0);
annotations = m2.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("world", annot.strings()[1]);
assertEquals(false, annot.b());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
// tests binary expressions
CtMethod<?> m3 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m3")).get(0);
annotations = m3.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(45, annot.integer());
assertEquals(2, annot.integers().length);
assertEquals(40, annot.integers()[0]);
assertEquals(42 * 3, annot.integers()[1]);
assertEquals("Hello World!concatenated", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Helloconcatenated", annot.strings()[0]);
assertEquals("worldconcatenated", annot.strings()[1]);
assertEquals(true, annot.b());
assertEquals(42 ^ 1, annot.byt());
assertEquals((short) 42 / 2, annot.s());
assertEquals(43, annot.l());
assertEquals(3.14f * 2f, annot.f(), 0f);
assertEquals(3.14159d / 3d, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dddd", annot.ia().value());
}
Aggregations