OPENJPA-2444 fix default orm.xml location for ReverseMappingTool

I also fixed the badly formatted and partly broken TestUseSchemaElement.



git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1535648 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Struberg 2013-10-25 07:13:13 +00:00
parent 70f1188d19
commit 4551bbc696
2 changed files with 105 additions and 109 deletions

View File

@ -20,15 +20,11 @@ package org.apache.openjpa.persistence.jdbc.meta;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import junit.textui.TestRunner;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.meta.ReverseMappingTool;
import org.apache.openjpa.lib.util.Files;
@ -38,112 +34,112 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests the added useSchemaElement functionality of the
* ReverseMappingTool and CodeGenerator classes.
*
*
* @author Austin Dorenkamp (ajdorenk)
*/
public class TestUseSchemaElement extends /*TestCase*/ SingleEMFTestCase {
public void setUp() throws Exception {
super.setUp();
File f = new File("./orm.xml");
// Make sure to clean up orm.xml from a prior run
if (f.exists()) {
assertTrue(f.delete());
}
setSupportedDatabases(org.apache.openjpa.jdbc.sql.DerbyDictionary.class);
}
@Override
public String getPersistenceUnitName(){
return "rev-mapping-pu";
}
public void testGettersAndSetters() {
JDBCConfiguration conf = (JDBCConfiguration) ((OpenJPAEntityManagerFactory) emf).getConfiguration();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createNativeQuery("CREATE TABLE USCHEMA.USCHANTBL (ID INTEGER PRIMARY KEY)");
try {
q.executeUpdate();
em.getTransaction().commit();
} catch (Throwable t) {
em.getTransaction().rollback();
System.out.println(t.toString());
}
try {
ReverseMappingTool.Flags flags = new ReverseMappingTool.Flags();
flags.metaDataLevel = "package";
flags.generateAnnotations = true;
flags.accessType = "property";
flags.nullableAsObject = true;
flags.useSchemaName = false;
flags.useSchemaElement = false;
flags.packageName = "";
flags.directory = Files.getFile("./target", null);
ReverseMappingTool.run(conf, new String[0], flags, null);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
/* Now that the tool has been run, we will test it by reading the generated files */
// This tests the removal of the schema annotation in the Uschantbl.java file
File uschantbl = new File("./target/Uschantbl.java");
Scanner inFile = new Scanner("");
String currentLine;
try {
inFile = new Scanner(uschantbl);
} catch (FileNotFoundException e) {
fail("Uschantbl.java not generated in ./target by ReverseMappingTool");
}
while(inFile.hasNextLine())
{
currentLine = inFile.nextLine();
if((currentLine.length()) > 0 && (currentLine.charAt(0) != '@'))
{
continue;
}
if(currentLine.contains("Table(schema="))
{
fail("Uschantbl.java still contains schema name");
}
}
inFile.close();
// Delete file to clean up workspace
assertTrue(uschantbl.delete());
// This tests the removal of the schema name from the orm.xml file
File orm = new File("./orm.xml");
try {
inFile = new Scanner(orm);
} catch (FileNotFoundException e) {
fail("Orm.xml not generated in root directory by ReverseMappingTool");
}
while(inFile.hasNextLine())
{
if(inFile.nextLine().contains("<table schema="))
{
fail("Orm.xml still contains schema name");
}
}
inFile.close();
// Delete file to clean up workspace. Also, test will break with
// org.apache.openjpa.util.UserException if orm.xml exists prior to running
// assertTrue(orm.delete());
}
public static void main(String[] args) {
TestRunner.run(TestUseSchemaElement.class);
}
public void setUp() throws Exception {
super.setUp();
File f = new File("./orm.xml");
// Make sure to clean up orm.xml from a prior run
if (f.exists()) {
assertTrue(f.delete());
}
setSupportedDatabases(org.apache.openjpa.jdbc.sql.DerbyDictionary.class);
}
@Override
public String getPersistenceUnitName(){
return "rev-mapping-pu";
}
public void testGettersAndSetters() throws Exception {
JDBCConfiguration conf = (JDBCConfiguration) ((OpenJPAEntityManagerFactory) emf).getConfiguration();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createNativeQuery("CREATE TABLE USCHEMA.USCHANTBL (ID INTEGER PRIMARY KEY)");
try {
q.executeUpdate();
em.getTransaction().commit();
} catch (Throwable t) {
em.getTransaction().rollback();
System.out.println(t.toString());
}
ReverseMappingTool.Flags flags = new ReverseMappingTool.Flags();
flags.metaDataLevel = "package";
flags.generateAnnotations = true;
flags.accessType = "property";
flags.nullableAsObject = true;
flags.useSchemaName = false;
flags.useSchemaElement = false;
flags.packageName = "";
flags.directory = Files.getFile("./target", null);
ReverseMappingTool.run(conf, new String[0], flags, null);
/* Now that the tool has been run, we will test it by reading the generated files */
// This tests the removal of the schema annotation in the Uschantbl.java file
File uschantbl = new File("./target/Uschantbl.java");
Scanner inFile = null;
String currentLine;
try {
inFile = new Scanner(uschantbl);
while(inFile.hasNextLine())
{
currentLine = inFile.nextLine();
if((currentLine.length()) > 0 && (currentLine.charAt(0) != '@'))
{
continue;
}
if(currentLine.contains("Table(schema="))
{
fail("Uschantbl.java still contains schema name");
}
}
// Delete file to clean up workspace
assertTrue(uschantbl.delete());
} catch (FileNotFoundException e) {
fail("Uschantbl.java not generated in ./target by ReverseMappingTool");
}
finally {
if (inFile != null) {
inFile.close();
}
}
// This tests the removal of the schema name from the orm.xml file
File orm = new File("target/orm.xml");
try {
inFile = new Scanner(orm);
while(inFile.hasNextLine())
{
if(inFile.nextLine().contains("<table schema="))
{
fail("Orm.xml still contains schema name");
}
}
} catch (FileNotFoundException e) {
fail("Orm.xml not generated in root directory by ReverseMappingTool");
}
finally {
if (inFile != null) {
inFile.close();
}
}
// Delete file to clean up workspace. Also, test will break with
// org.apache.openjpa.util.UserException if orm.xml exists prior to running
//assertTrue(orm.delete());
}
}

View File

@ -521,7 +521,7 @@ public class PersistenceMetaDataFactory
J2DoPrivHelper.existsAction(file))).booleanValue())
return file;
}
return new File("orm.xml");
return new File(dir, "orm.xml");
}
public void setConfiguration(Configuration conf) {