You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2009/06/16 19:36:04 UTC

svn commit: r785313 - in /openjpa/branches/1.3.x/openjpa-persistence: pom.xml src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java

Author: mikedd
Date: Tue Jun 16 17:36:03 2009
New Revision: 785313

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

(cherry picked from commit f81e627b27627374ea9574c6811d51645ba7428a)

Modified:
    openjpa/branches/1.3.x/openjpa-persistence/pom.xml
    openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java

Modified: openjpa/branches/1.3.x/openjpa-persistence/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/pom.xml?rev=785313&r1=785312&r2=785313&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/pom.xml (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/pom.xml Tue Jun 16 17:36:03 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>
@@ -105,32 +79,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/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java?rev=785313&r1=785312&r2=785313&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestPersistenceProductDerivation.java Tue Jun 16 17:36:03 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);
+        }
+    }
 }