You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2010/02/10 21:35:50 UTC

svn commit: r908660 - in /openjpa/trunk/openjpa-integration/validation: pom.xml src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java

Author: dwoods
Date: Wed Feb 10 20:35:22 2010
New Revision: 908660

URL: http://svn.apache.org/viewvc?rev=908660&view=rev
Log:
OPENJPA-1504 Update TestTraversableResolver so it can be used with Hibernate or Agimatec providers.  Upgrade to agimatec-validation-0.9.6-SNAPSHOT, which now supports constraints.xml so all junits pass again with it.

Modified:
    openjpa/trunk/openjpa-integration/validation/pom.xml
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java

Modified: openjpa/trunk/openjpa-integration/validation/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/pom.xml?rev=908660&r1=908659&r2=908660&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/pom.xml (original)
+++ openjpa/trunk/openjpa-integration/validation/pom.xml Wed Feb 10 20:35:22 2010
@@ -73,7 +73,7 @@
                 <dependency>
                     <groupId>com.agimatec</groupId>
                     <artifactId>agimatec-jsr303</artifactId>
-                    <version>0.9.3</version>
+                    <version>0.9.6-SNAPSHOT</version>
                     <scope>test</scope>
                     <exclusions>
                         <!-- force usage of the geronimo-validation_1.0_spec -->
@@ -91,13 +91,13 @@
                 <dependency>
                     <groupId>com.agimatec</groupId>
                     <artifactId>agimatec-validation</artifactId>
-                    <version>0.9.3</version>
+                    <version>0.9.6-SNAPSHOT</version>
                     <scope>test</scope>
                 </dependency>
                 <dependency>
                     <groupId>commons-beanutils</groupId>
                     <artifactId>commons-beanutils</artifactId>
-                    <version>1.8.0</version>
+                    <version>1.8.2</version>
                     <scope>test</scope>
                 </dependency>
                 <dependency>

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java?rev=908660&r1=908659&r2=908660&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java (original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java Wed Feb 10 20:35:22 2010
@@ -19,6 +19,8 @@
 package org.apache.openjpa.integration.validation;
 
 import java.lang.annotation.ElementType;
+import java.lang.reflect.Method;
+import java.security.AccessController;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -27,13 +29,15 @@
 import javax.validation.TraversableResolver;
 
 import junit.framework.TestCase;
-
+import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.lib.util.J2DoPrivHelper;
 import org.apache.openjpa.persistence.OpenJPAEntityManager;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.apache.openjpa.persistence.validation.TraversableResolverImpl;
-
-import org.hibernate.validator.engine.PathImpl;
+// The following 2 are dynamically loaded by loadPathImpl() from setUp()
+// import org.hibernate.validator.engine.PathImpl;
+// import com.agimatec.validation.jsr303.util.PathImpl;
 
 /**
  * Test the TraversableResolver methods
@@ -50,9 +54,10 @@
  */
 public class TestTraversableResolver extends TestCase {
     private static OpenJPAEntityManagerFactorySPI emf = null;
-    private OpenJPAEntityManager em;
+    private Log log = null;
+    private OpenJPAEntityManager em = null;
     private Book book;
-    
+
     /**
      * Create a book with a title that is too long, and the embedded
      * publisher has a name that is also too long. However, use a
@@ -62,9 +67,7 @@
      */
     @Override
     public void setUp() {
-        createEMF("non-validation-pu", "SchemaAction='drop,add')");
         createBook(1, "long title", 234);
-        emf.close();
     }
     
     private void createEMF(String pu, String schemaAction) {
@@ -76,6 +79,19 @@
             "org/apache/openjpa/integration/validation/persistence.xml",
             map);
         assertNotNull(emf);
+        log = emf.getConfiguration().getLog("Tests");
+    }
+
+    private void closeEMF() {
+        log = null;
+        if (em != null) {
+            em.close();
+            em = null;
+        }
+        if (emf != null) {
+            emf.close();
+            emf = null;
+        }
     }
     
     /**
@@ -102,8 +118,6 @@
             exceptionCaught = true;
         }
         assertTrue(exceptionCaught);
-        em.close();
-        emf.close();
     }
     
     /**
@@ -126,8 +140,7 @@
             exceptionCaught = true;
         }
         assertFalse(exceptionCaught);
-        em.close();
-        emf.close();
+        closeEMF();
     }
     
     /**
@@ -154,8 +167,7 @@
             exceptionCaught = true;
         }
         assertTrue(exceptionCaught);
-        em.close();
-        emf.close();
+        closeEMF();
     }
     
     /**
@@ -168,14 +180,14 @@
         em.getTransaction().begin();
         book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
         assertNotNull(book);
-        PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.pages");
-        Path.Node node = path.getLeafNode();
+        // PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.pages");
+        // Path.Node node = path.getLeafNode();
+        Path.Node node = getLeafNodeFromString("org.apache.openjpa.integration.validation.Book.pages");
         TraversableResolver tr = new TraversableResolverImpl();
         assertTrue(tr.isReachable(book, node, Book.class, null, ElementType.METHOD));
         assertTrue(tr.isCascadable(book, node, Book.class, null, ElementType.METHOD));
         em.getTransaction().commit();
-        em.close();
-        emf.close();
+        closeEMF();
     }
     
     /**
@@ -188,16 +200,17 @@
         em.getTransaction().begin();
         book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
         assertNotNull(book);
-        PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.title");
-        Path.Node node = path.getLeafNode();
+        // PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.title");
+        // Path.Node node = path.getLeafNode();
+        Path.Node node = getLeafNodeFromString("org.apache.openjpa.integration.validation.Book.title");
         TraversableResolver tr = new TraversableResolverImpl();
         assertFalse(tr.isReachable(book, node, Book.class, null, ElementType.FIELD));
         em.getTransaction().commit();
-        em.close();
-        emf.close();
+        closeEMF();
     }
     
     private void createBook(int id, String title, int pages) {
+        createEMF("non-validation-pu", "SchemaAction='drop,add')");
         em = emf.createEntityManager();
         book = new Book(id);
         book.setTitle(title);
@@ -209,6 +222,42 @@
         em.getTransaction().begin();
         em.persist(book);
         em.getTransaction().commit();
-        em.close();
+        closeEMF();
     }
+
+    private Path.Node getLeafNodeFromString(String s) {
+        Class<?> PathImpl = null;
+        Path.Node node = null;
+
+        // dynamically load PathImpl depending on the Bean Validation provider
+        try {
+            PathImpl = Class.forName("org.hibernate.validator.engine.PathImpl",
+                true, AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction()));
+        } catch (ClassNotFoundException e) {
+            log.trace("getLeafNodeFromPath: Did not find org.hibernate.validator.engine.PathImpl");
+        }
+        if (PathImpl == null) {
+            try {
+                PathImpl = Class.forName("com.agimatec.validation.jsr303.util.PathImpl",
+                    true, AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction()));
+            } catch (ClassNotFoundException e) {
+                log.trace("getLeafNodeFromPath: Did not find com.agimatec.validation.jsr303.util.PathImpl");
+            }
+        }
+        assertNotNull(PathImpl);
+        try {
+            Method createPathFromString = PathImpl.getMethod("createPathFromString", String.class);
+            assertNotNull(createPathFromString);
+            Method getLeafNode = PathImpl.getMethod("getLeafNode");
+            assertNotNull(getLeafNode);
+            Object path = createPathFromString.invoke(null, s);
+            node = (Path.Node) getLeafNode.invoke(path, null);
+        } catch (NoSuchMethodException e) {
+        } catch (IllegalAccessException ae) {
+        } catch (java.lang.reflect.InvocationTargetException te) {
+        }
+        return node;
+    }
+
 }
+