Author: jrbauer
Date: Wed May 13 15:08:49 2009
New Revision: 774393

URL: http://svn.apache.org/viewvc?rev=774393&view=rev
Log:
OPENJPA-932 Committing test updates contributed by Rick Curtis

Modified:
    openjpa/trunk/openjpa-persistence/pom.xml
    
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java

Modified: openjpa/trunk/openjpa-persistence/pom.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/pom.xml?rev=774393&r1=774392&r2=774393&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/pom.xml (original)
+++ openjpa/trunk/openjpa-persistence/pom.xml Wed May 13 15:08:49 2009
@@ -54,32 +54,6 @@
                    <id>jdk5-compiler</id>
             <build>
               <plugins>
-                                       <plugin>
-                                               
<groupId>org.apache.maven.plugins</groupId>
-                                               
<artifactId>maven-surefire-plugin</artifactId>
-                                               <configuration>
-                                                       
<additionalClasspathElements>
-                                                               
<additionalClasspathElement>${basedir}/target/test-classes/second-persistence.jar</additionalClasspathElement>
-                                                       
</additionalClasspathElements>
-                                               </configuration>
-                                       </plugin>
-                                       <plugin>
-                                               
<artifactId>maven-antrun-plugin</artifactId>
-                                               <executions>
-                                                       <execution>
-                                                               
<phase>test-compile</phase>
-                                                               <configuration>
-                                                                       <tasks>
-                                                                               
<jar destfile="${basedir}/target/test-classes/second-persistence.jar"
-                                                                               
        basedir="${basedir}/src/test/resources/second-persistence" />
-                                                                       </tasks>
-                                                               </configuration>
-                                                               <goals>
-                                                                       
<goal>run</goal>
-                                                               </goals>
-                                                       </execution>
-                                               </executions>
-                                       </plugin>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
@@ -106,32 +80,6 @@
             <build>
               <plugins>
                                        <plugin>
-                                               
<groupId>org.apache.maven.plugins</groupId>
-                                               
<artifactId>maven-surefire-plugin</artifactId>
-                                               <configuration>
-                                                       
<additionalClasspathElements>
-                                                               
<additionalClasspathElement>${basedir}/target/test-classes/second-persistence.jar</additionalClasspathElement>
-                                                       
</additionalClasspathElements>
-                                               </configuration>
-                                       </plugin>
-                                       <plugin>
-                                               
<artifactId>maven-antrun-plugin</artifactId>
-                                               <executions>
-                                                       <execution>
-                                                               
<phase>test-compile</phase>
-                                                               <configuration>
-                                                                       <tasks>
-                                                                               
<jar destfile="${basedir}/target/test-classes/second-persistence.jar"
-                                                                               
        basedir="${basedir}/src/test/resources/second-persistence" />
-                                                                       </tasks>
-                                                               </configuration>
-                                                               <goals>
-                                                                       
<goal>run</goal>
-                                                               </goals>
-                                                       </execution>
-                                               </executions>
-                                       </plugin>
-                       <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-antrun-plugin</artifactId>
                            <executions>

Modified: 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java?rev=774393&r1=774392&r2=774393&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
 (original)
+++ 
openjpa/trunk/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
 Wed May 13 15:08:49 2009
@@ -18,19 +18,77 @@
  */
 package org.apache.openjpa.persistence;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.Arrays;
 import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 
 import junit.framework.TestCase;
 
 public class TestPersistenceProductDerivation extends TestCase {
-
+    private File sourceFile;
+    private File targetFile;
+    
+    ClassLoader originalLoader = null;
+    ClassLoader tempLoader = null;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        String currentDir = System.getProperty("user.dir");
+        
+        // openjpa-persistence/src/test/resources/second-persistence/
+        //   META-INF/persistence.xml
+        sourceFile = new File(currentDir + File.separator + 
"src"+File.separator 
+            + "test" + File.separator + "resources"  + File.separator 
+                + "second-persistence" + File.separator + "META-INF" 
+                + File.separator + "persistence.xml");
+        
+        // openjpa-persistence/target/test-classes/
+        //   TestPersistenceProductDerivation_generated_(time_stamp).jar       
 
+        targetFile = new File(currentDir + File.separator + "target" + 
+            File.separator + "test-classes" + File.separator + 
+            "TestPersistenceProductDerivation_generated_" +
+            System.currentTimeMillis() + ".jar");
+        
+        targetFile.deleteOnExit();        
+        buildJar(sourceFile,targetFile);
+        
+        // Hold a reference to the current classloader so we can cleanup
+        // when we're done.
+        originalLoader = Thread.currentThread().getContextClassLoader();
+        tempLoader = new TempUrlLoader(new URL[]{targetFile.toURI().toURL()}
+            ,originalLoader);        
+        Thread.currentThread().setContextClassLoader(tempLoader);
+            
+    }
+    
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        // Restore the original classloader.
+        Thread.currentThread().setContextClassLoader(originalLoader);
+        
+        // For whatever reason, this file won't ever delete. I searched around
+        // and found numerous documented problems with deleting files. Perhaps
+        // sometime in the future this problem will be fixed. For now it 
doesn't
+        // really matter since we generate a new file every time.       
+        if(targetFile.delete()==false){
+            System.out.println("The file " + targetFile + " wasn't deleted.");
+        }
+    }
     /**
-     * Added for OPENJPA-932. Verifies a ppd properly loads pu's from multiple 
archives.
+     * Added for OPENJPA-932. Verifies a ppd properly loads pu's from multiple 
+     * archives.
      * 
      * @throws Exception
      */
-    public void testGetAnchorsInResource()throws Exception{
+    public void testGetAnchorsInResource()throws Exception {
         
         List<String> expectedPUs = Arrays.asList(
             new String[]{"pu_1","pu_2","pu_3"});
@@ -40,4 +98,31 @@
         
         assertEquals(expectedPUs, actual);        
     }
+    
+    private void buildJar(File sourceFile, File targetFile) throws Exception {
+        
+        JarOutputStream out = new JarOutputStream(
+            new BufferedOutputStream(new FileOutputStream(targetFile)));
+        
+        BufferedInputStream in = 
+            new BufferedInputStream(new FileInputStream(sourceFile));
+
+        out.putNextEntry(new JarEntry("META-INF/"));
+        out.putNextEntry(new JarEntry("META-INF/persistence.xml"));
+        //write the xml to the jar
+        byte[] buf = new byte[1024];
+        int i;
+        while ((i = in.read(buf)) != -1) {
+          out.write(buf, 0, i);
+        }
+        
+        out.close();
+        in.close();        
+    }
+
+    class TempUrlLoader extends URLClassLoader {
+        public TempUrlLoader(URL[] urls, ClassLoader parent) {
+            super(urls,parent);
+        }
+    }
 }


Reply via email to