You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by li...@apache.org on 2012/01/18 00:19:58 UTC

[2/14] git commit: [DELTASPIKE-46] Adding tests for AnnotatedTypeBuilder

[DELTASPIKE-46] Adding tests for AnnotatedTypeBuilder

Also adding some AnnotationLiterals for use in the test and docs.

Fixing IP issues

Submitted on behalf of a third-part: Red Hat, Inc. under the terms of
the ALv2


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/3b81aa0f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/3b81aa0f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/3b81aa0f

Branch: refs/heads/master
Commit: 3b81aa0fda1bd846933aba33e253a2c71e5206b5
Parents: 3aeda25
Author: Jason Porter <li...@apache.org>
Authored: Thu Jan 12 23:53:52 2012 -0700
Committer: Jason Porter <li...@apache.org>
Committed: Tue Jan 17 16:14:33 2012 -0700

----------------------------------------------------------------------
 .../core/api/literal/AlternativeLiteral.java       |   31 +++++
 .../core/api/literal/ApplicationScopedLiteral.java |   31 +++++
 .../deltaspike/core/api/literal/ModelLiteral.java  |   31 +++++
 .../deltaspike/core/api/literal/TypedLiteral.java  |   46 +++++++
 .../api/metadata/AnnotatedTypeBuilderTest.java     |  106 +++++++++++++++
 .../apache/deltaspike/test/api/metadata/Cat.java   |   67 +++++++++
 .../api/metadata/NamedAnnotationRedefiner.java     |   51 +++++++
 7 files changed, 363 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/AlternativeLiteral.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/AlternativeLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/AlternativeLiteral.java
new file mode 100644
index 0000000..3e29100
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/AlternativeLiteral.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.api.literal;
+
+import javax.enterprise.inject.Alternative;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * Literal for {@link javax.enterprise.inject.Alternative}.
+ */
+public class AlternativeLiteral extends AnnotationLiteral<Alternative> implements Alternative
+{
+    private static final long serialVersionUID = -4865048799125718216L;
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ApplicationScopedLiteral.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ApplicationScopedLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ApplicationScopedLiteral.java
new file mode 100644
index 0000000..cd6e99d
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ApplicationScopedLiteral.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.api.literal;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * Literal for {@link ApplicationScoped}
+ */
+public class ApplicationScopedLiteral extends AnnotationLiteral<ApplicationScoped> implements ApplicationScoped
+{
+    private static final long serialVersionUID = 6582580975876369665L;
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ModelLiteral.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ModelLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ModelLiteral.java
new file mode 100644
index 0000000..b9eb07e
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ModelLiteral.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.api.literal;
+
+import javax.enterprise.inject.Model;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * Literal for {@link javax.enterprise.inject.Model} annotation.
+ */
+public class ModelLiteral extends AnnotationLiteral<Model> implements Model
+{
+    private static final long serialVersionUID = -1828119201454843678L;
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/TypedLiteral.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/TypedLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/TypedLiteral.java
new file mode 100644
index 0000000..990354e
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/TypedLiteral.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.core.api.literal;
+
+import javax.enterprise.inject.Typed;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ *
+ */
+public class TypedLiteral extends AnnotationLiteral<Typed> implements Typed
+{
+    final private Class<?>[] value;
+
+    public TypedLiteral()
+    {
+        value = new Class<?>[0];
+    }
+
+    public TypedLiteral(Class<?>[] value)
+    {
+        this.value = value;
+    }
+
+    @Override public Class<?>[] value()
+    {
+        return value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/AnnotatedTypeBuilderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/AnnotatedTypeBuilderTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/AnnotatedTypeBuilderTest.java
new file mode 100644
index 0000000..5bf2949
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/AnnotatedTypeBuilderTest.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.test.api.metadata;
+
+import org.apache.deltaspike.core.api.literal.AnyLiteral;
+import org.apache.deltaspike.core.api.literal.TypedLiteral;
+import org.apache.deltaspike.core.api.metadata.builder.AnnotatedTypeBuilder;
+import org.junit.Test;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.Typed;
+import javax.enterprise.inject.spi.AnnotatedConstructor;
+import javax.enterprise.inject.spi.AnnotatedParameter;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.inject.Named;
+import java.util.List;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class AnnotatedTypeBuilderTest
+{
+
+    @Test
+    public void testTypeLevelAnnotationRedefinition()
+    {
+        AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
+        builder.readFromType(Cat.class);
+        builder.redefine(Named.class, new NamedAnnotationRedefiner());
+
+        final AnnotatedType<Cat> cat = builder.create();
+        assertEquals(3, cat.getAnnotations().size());
+        assertTrue(cat.isAnnotationPresent(Named.class));
+        assertTrue(cat.isAnnotationPresent(Alternative.class));
+        assertTrue(cat.isAnnotationPresent(ApplicationScoped.class));
+        assertEquals("tomcat", cat.getAnnotation(Named.class).value());
+
+        builder = new AnnotatedTypeBuilder<Cat>();
+        builder.readFromType(cat);
+        builder.removeFromAll(Named.class);
+
+        final AnnotatedType<Cat> noNameCat = builder.create();
+        assertFalse(noNameCat.isAnnotationPresent(Named.class));
+        assertEquals(2, noNameCat.getAnnotations().size());
+    }
+
+    @Test
+    public void testAdditionOfAnnotation()
+    {
+        final AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
+        builder.readFromType(Cat.class, true);
+        builder.addToClass(new TypedLiteral());
+
+        final AnnotatedType<Cat> catAnnotatedType = builder.create();
+        assertThat(catAnnotatedType.isAnnotationPresent(Typed.class), is(true));
+    }
+
+    @Test
+    public void modifyAnnotationsOnConstructorParameter() throws NoSuchMethodException
+    {
+        final AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
+        builder.readFromType(Cat.class, true);
+        builder.removeFromConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, Default.class);
+        builder.addToConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, new AnyLiteral());
+
+        final AnnotatedType<Cat> catAnnotatedType = builder.create();
+        Set<AnnotatedConstructor<Cat>> catCtors = catAnnotatedType.getConstructors();
+
+        assertThat(catCtors.size(), is(2));
+
+        for (AnnotatedConstructor<Cat> ctor : catCtors)
+        {
+            if (ctor.getParameters().size() == 2)
+            {
+                List<AnnotatedParameter<Cat>> ctorParams = ctor.getParameters();
+
+                assertThat(ctorParams.get(1).getAnnotations().size(), is(1));
+                assertThat((AnyLiteral) ctorParams.get(1).getAnnotations().toArray()[0], is(new AnyLiteral()));
+            }
+        }
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/Cat.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/Cat.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/Cat.java
new file mode 100644
index 0000000..4a21cff
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/Cat.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.test.api.metadata;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+@Named("cat")
+public class Cat
+{
+    private String color;
+    private String gender;
+
+    public Cat()
+    {
+    }
+
+    @Inject
+    public Cat(String color, String gender)
+    {
+        this.color = color;
+        this.gender = gender;
+    }
+
+    @PostConstruct
+    public void setup()
+    {
+    }
+
+    public String getColor()
+    {
+        return color;
+    }
+
+    public void setColor(String color)
+    {
+        this.color = color;
+    }
+
+    public String getGender()
+    {
+        return gender;
+    }
+
+    public void setGender(String gender)
+    {
+        this.gender = gender;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/3b81aa0f/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/NamedAnnotationRedefiner.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/NamedAnnotationRedefiner.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/NamedAnnotationRedefiner.java
new file mode 100644
index 0000000..ddf9003
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/metadata/NamedAnnotationRedefiner.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.test.api.metadata;
+
+import org.apache.deltaspike.core.api.literal.AlternativeLiteral;
+import org.apache.deltaspike.core.api.literal.ApplicationScopedLiteral;
+import org.apache.deltaspike.core.api.metadata.AnnotationRedefiner;
+import org.apache.deltaspike.core.api.metadata.RedefinitionContext;
+import org.apache.deltaspike.core.api.metadata.builder.AnnotationBuilder;
+import org.apache.webbeans.annotation.NamedLiteral;
+
+import javax.inject.Named;
+
+/**
+ *
+ */
+public class NamedAnnotationRedefiner implements AnnotationRedefiner<Named>
+{
+    @Override
+    public void redefine(RedefinitionContext<Named> ctx)
+    {
+        Named named = ctx.getAnnotatedElement().getAnnotation(Named.class);
+        if ("cat".equals(named.value()))
+        {
+            AnnotationBuilder builder = ctx.getAnnotationBuilder();
+            // add two annotations
+            builder.add(new AlternativeLiteral());
+            builder.add(new ApplicationScopedLiteral());
+            // change the value of @Named
+            builder.remove(Named.class);
+            builder.add(new NamedLiteral("tomcat"));
+        }
+    }
+}