Add unit tests for AnnoationScanner
git-svn-id: https://svn.apache.org/repos/asf/aries/trunk/jpa@1718643 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/AnnotationScanner.java b/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/AnnotationScanner.java
index b285cff..4ec46ae 100644
--- a/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/AnnotationScanner.java
+++ b/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/AnnotationScanner.java
@@ -59,10 +59,14 @@
if (!name.startsWith("set")) {
return null;
}
- return name. substring(3, 4).toLowerCase() + name.substring(4);
+ return lowerCamelCase(name.substring(3));
}
throw new IllegalArgumentException();
}
+
+ private static String lowerCamelCase(String name) {
+ return name.substring(0, 1).toLowerCase() + name.substring(1);
+ }
public static Class<?> getType(AccessibleObject member) {
if (member instanceof Field) {
diff --git a/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/AnnotationScannerTest.java b/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/AnnotationScannerTest.java
new file mode 100644
index 0000000..d317cee
--- /dev/null
+++ b/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/AnnotationScannerTest.java
@@ -0,0 +1,91 @@
+
+/*
+ * 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.aries.jpa.blueprint.impl;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class AnnotationScannerTest {
+
+ private Method setEmMethod;
+ private Field emField;
+ private Method setEmfMethod;
+ private Field emfField;
+
+ public AnnotationScannerTest() throws NoSuchMethodException, SecurityException, NoSuchFieldException {
+ setEmMethod = TestClass.class.getMethod("setEm", new Class<?>[] {EntityManager.class});
+ emField = TestClass.class.getDeclaredField("em");
+ setEmfMethod = TestClass.class.getMethod("setEmf", new Class<?>[] {EntityManagerFactory.class});
+ emfField = TestClass.class.getDeclaredField("emf");
+ }
+
+ @Test
+ public void getNameTest() throws NoSuchMethodException, SecurityException, NoSuchFieldException {
+ Assert.assertEquals("em", AnnotationScanner.getName(setEmMethod));
+ Assert.assertEquals("em", AnnotationScanner.getName(emField));
+ Assert.assertEquals("emf", AnnotationScanner.getName(setEmfMethod));
+ Assert.assertEquals("emf", AnnotationScanner.getName(emfField));
+
+ }
+
+ @Test
+ public void getTypeTest() {
+ Assert.assertEquals(EntityManager.class, AnnotationScanner.getType(setEmMethod));
+ Assert.assertEquals(EntityManager.class, AnnotationScanner.getType(emField));
+ Assert.assertEquals(EntityManagerFactory.class, AnnotationScanner.getType(setEmfMethod));
+ Assert.assertEquals(EntityManagerFactory.class, AnnotationScanner.getType(emfField));
+
+ }
+
+ @Test
+ public void getPCAnnotatedMembersTest() {
+ AnnotationScanner scanner = new AnnotationScanner();
+ List<AccessibleObject> members = scanner.getJpaAnnotatedMembers(TestClass.class, PersistenceContext.class);
+ Assert.assertEquals(1, members.size());
+ AccessibleObject member = members.get(0);
+ Assert.assertEquals(Field.class, member.getClass());
+ Field field = (Field)member;
+ Assert.assertEquals("em", field.getName());
+ }
+
+ @Test
+ public void getPUAnnotatedMembersTest() {
+ AnnotationScanner scanner = new AnnotationScanner();
+ List<AccessibleObject> members = scanner.getJpaAnnotatedMembers(TestClass.class, PersistenceUnit.class);
+ Assert.assertEquals(1, members.size());
+ AccessibleObject member = members.get(0);
+ Assert.assertEquals(Method.class, member.getClass());
+ Method method = (Method)member;
+ Assert.assertEquals("setEmf", method.getName());
+ }
+
+
+}
diff --git a/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/TestClass.java b/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/TestClass.java
new file mode 100644
index 0000000..dfa77ec
--- /dev/null
+++ b/jpa-blueprint/src/test/java/org/apache/aries/jpa/blueprint/impl/TestClass.java
@@ -0,0 +1,41 @@
+/*
+ * 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.aries.jpa.blueprint.impl;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+
+public class TestClass {
+
+ @PersistenceContext(unitName="test")
+ EntityManager em;
+
+ public void setEm(EntityManager em) {
+ this.em = em;
+ }
+
+ EntityManagerFactory emf;
+
+ @PersistenceUnit(unitName="test2")
+ public void setEmf(EntityManagerFactory emf) {
+ this.emf = emf;
+ }
+}