From 2a2c700d2353d1aefc701eebaeb7d2aedc2ead70 Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Tue, 20 Jun 2017 18:03:26 +0200
Subject: [PATCH] test replace in Map
---
.../spoon/test/annotation/AnnotationTest.java | 58 +++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/src/test/java/spoon/test/annotation/AnnotationTest.java b/src/test/java/spoon/test/annotation/AnnotationTest.java
index 70b6344f66d..c21c5699257 100644
--- a/src/test/java/spoon/test/annotation/AnnotationTest.java
+++ b/src/test/java/spoon/test/annotation/AnnotationTest.java
@@ -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;
@@ -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;
@@ -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;
@@ -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>("m1")).get(0);
+
+ List> 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) integerValue).getValue().intValue());
+ assertEquals(42, annot.integer());
+ integerValue.replace(factory.createLiteral(17));
+ CtExpression newIntegerValue = a.getValue("integer");
+ assertEquals(17, ((CtLiteral) 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]);
+ }
+
+
}