Skip to content

Commit

Permalink
test replace in Map
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jun 20, 2017
1 parent e9510c1 commit 2a2c700
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.OutputType;
import spoon.SpoonException;
import spoon.processing.AbstractAnnotationProcessor;
import spoon.processing.ProcessingManager;
import spoon.reflect.annotations.PropertyGetter;
Expand Down Expand Up @@ -66,6 +67,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

Expand All @@ -77,6 +79,7 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

Expand Down Expand Up @@ -1028,4 +1031,59 @@ public void testCreateAnnotation() throws Exception {
assertTrue(type.isAnnotationType());
assertSame(type, type.getReference().getDeclaration());
}

@Test
public void testReplaceAnnotationValue() throws Exception {
CtType<?> type = this.factory.Type().get("spoon.test.annotation.testclasses.Main");

CtMethod<?> m1 = type.getElements(new NameFilter<CtMethod<?>>("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());

//contract: replacing of single value of map by multiple values must fail
//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
}
a.getValue("string").delete();
assertNull(a.getValue("string"));
assertNull(annot.string());

//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]);
}


}

0 comments on commit 2a2c700

Please sign in to comment.