Some initial testing
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/.classpath b/JPA-RS Incubator/tests/JPA-RS Tests/.classpath index cdaadb8..9db7c81 100644 --- a/JPA-RS Incubator/tests/JPA-RS Tests/.classpath +++ b/JPA-RS Incubator/tests/JPA-RS Tests/.classpath
@@ -1,13 +1,15 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry excluding="**/.svn/**" kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> - <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> - <classpathentry combineaccessrules="false" kind="src" path="/JPA-RS"/> <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.antlr"/> <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.asm"/> <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.core"/> <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.moxy"/> <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.oracle"/> + <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.persistence.jpa"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> + <classpathentry combineaccessrules="false" kind="src" path="/JPA-RS"/> + <classpathentry combineaccessrules="false" kind="src" path="/javax.persistence 2.0.0"/> <classpathentry kind="output" path="classes"/> </classpath>
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/bootstrap/TestBootstrap.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/bootstrap/TestBootstrap.java new file mode 100644 index 0000000..a34c74f --- /dev/null +++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/bootstrap/TestBootstrap.java
@@ -0,0 +1,56 @@ +package jpars.test.bootstrap; + +import static org.junit.Assert.*; + +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import jpars.test.util.ExamplePropertiesLoader; + +import org.eclipse.persistence.jaxb.JAXBContext; +import org.eclipse.persistence.jpa.JpaHelper; +import org.eclipse.persistence.jpa.rs.PersistenceContext; +import org.eclipse.persistence.jpa.rs.PersistenceFactory; +import org.eclipse.persistence.sessions.DatabaseSession; +import org.eclipse.persistence.sessions.Session; +import org.eclipse.persistence.sessions.server.ServerSession; +import org.junit.Test; + +/** + * Test bootstrapping of a dynamically created persistence context + * @author tware + * + */ +public class TestBootstrap { + + @Test + public void testBootstrap() { + Map<String, Object> properties = new HashMap<String, Object>(); + ExamplePropertiesLoader.loadProperties(properties); + PersistenceFactory factory = null; + try{ + factory = new PersistenceFactory(); + factory.bootstrapPersistenceContext(new URL("file:///C:/EclipseLinkView2/incubator/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml"), properties); + } catch (Exception e){ + fail(e.toString()); + } + + PersistenceContext context = factory.getPersistenceContext("auction"); + + assertTrue(context.getEmf() != null); + assertTrue(context.getJAXBContext() != null); + + Session session = (ServerSession)JpaHelper.getServerSession(context.getEmf()); + assertTrue("JPA Session did not contain Auction.", session.getProject().getAliasDescriptors().containsKey("Auction")); + assertTrue("JPA Session did not contain Bid.", session.getProject().getAliasDescriptors().containsKey("Bid")); + assertTrue("JPA Session did not contain User.", session.getProject().getAliasDescriptors().containsKey("User")); + + session = (DatabaseSession)((JAXBContext)context.getJAXBContext()).getXMLContext().getSession(0); + assertTrue("JAXB Session did not contain Auction.", session.getProject().getAliasDescriptors().containsKey("Auction")); + assertTrue("JAXB Session did not contain Bid.", session.getProject().getAliasDescriptors().containsKey("Bid")); + assertTrue("JAXB Session did not contain User.", session.getProject().getAliasDescriptors().containsKey("User")); + + } + +}
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java new file mode 100644 index 0000000..3ef9328 --- /dev/null +++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java
@@ -0,0 +1,93 @@ +package jpars.test.crud; + +import static org.junit.Assert.*; + +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; + +import jpars.test.util.ExamplePropertiesLoader; + +import org.eclipse.persistence.dynamic.DynamicEntity; +import org.eclipse.persistence.jpa.rs.PersistenceContext; +import org.eclipse.persistence.jpa.rs.PersistenceFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test basic CRUD operations on a dynamically created PersistenceContext + * @author tware + * + */ +public class CRUDTests { + + private static PersistenceContext persistenceContext; + private static PersistenceFactory factory; + + @BeforeClass + public static void setup(){ + Map<String, Object> properties = new HashMap<String, Object>(); + ExamplePropertiesLoader.loadProperties(properties); + factory = null; + try{ + factory = new PersistenceFactory(); + factory.bootstrapPersistenceContext(new URL("file:///C:/EclipseLinkView2/incubator/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml"), properties); + } catch (Exception e){ + fail(e.toString()); + } + + persistenceContext = factory.getPersistenceContext("auction"); + } + + @AfterClass + public static void tearDown(){ + EntityManager em = persistenceContext.getEmf().createEntityManager(); + em.getTransaction().begin(); + em.createQuery("delete from Bid b").executeUpdate(); + em.createQuery("delete from Auction a").executeUpdate(); + em.createQuery("delete from User u").executeUpdate(); + em.getTransaction().commit(); + factory.closePersistenceContext("auction"); + } + + @Test + public void testCreateAndDelete() { + DynamicEntity entity = persistenceContext.newEntity("User"); + entity.set("name", "Jim"); + persistenceContext.create(null, entity); + entity = persistenceContext.find("User", entity.get("id")); + + assertNotNull("Entity was note persisted", entity); + assertTrue("Entity Name was incorrect", entity.get("name").equals("Jim")); + + persistenceContext.delete(null, "User", entity.get("id")); + + entity = persistenceContext.find("User", entity.get("id")); + + assertNull("Entity was note deleted", entity); + } + + + @Test + public void testQuery(){ + DynamicEntity entity = persistenceContext.newEntity("User"); + entity.set("name", "Jill"); + persistenceContext.create(null, entity); + + entity = persistenceContext.newEntity("User"); + entity.set("name", "Arthur"); + persistenceContext.create(null, entity); + + entity = persistenceContext.newEntity("User"); + entity.set("name", "Judy"); + persistenceContext.create(null, entity); + + List<DynamicEntity> users = (List<DynamicEntity>)persistenceContext.query("User.all", null); + assertTrue(users.size() == 3); + } + +}
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/ExamplePropertiesLoader.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/ExamplePropertiesLoader.java new file mode 100644 index 0000000..84e017c --- /dev/null +++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/ExamplePropertiesLoader.java
@@ -0,0 +1,75 @@ +/****************************************************************************** + * ORACLE CONFIDENTIAL + * Copyright (c) 2011 Oracle. All rights reserved. + * + * Contributors: + * - + ******************************************************************************/ +package jpars.test.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; + +/** + * Helper class that will load persistence unit overrides from a properties file + * in both the current running folder and the current user's home folder. The + * goal is to enable developers and users of the example to customize its + * behaviour without having to modify the source of the example. + */ +public class ExamplePropertiesLoader { + + public static final String DEFAULT_FILENAME = "jpars.properties"; + + /** + * + * @param properties + */ + public static void loadProperties(Map<String, Object> properties) { + loadProperties(properties, DEFAULT_FILENAME); + } + + /** + * + * @param properties + */ + public static void loadProperties(Map<String, Object> properties, String filename) { + loadProperties(properties, new File(filename)); + + String home = System.getProperty("user.home"); + loadProperties(properties, new File(home + System.getProperty("file.separator") + filename)); + + for (Object key : System.getProperties().keySet()) { + String keyName = (String) key; + + if (keyName.startsWith("javax.persistence") || keyName.startsWith("eclipselink")) { + String value = System.getProperty(keyName); + properties.put(keyName, value); + } + } + } + + /** + * + * @param properties + * @param filePath + */ + public static void loadProperties(Map<String, Object> properties, File file) { + try { + if (file.exists()) { + Properties exampleProps = new Properties(); + InputStream in = new FileInputStream(file); + exampleProps.load(in); + in.close(); + + for (Map.Entry<Object, Object> entry : exampleProps.entrySet()) { + properties.put((String) entry.getKey(), entry.getValue()); + } + } + } catch (Exception e) { + // ignore + } + } +}
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-orm.xml b/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-orm.xml new file mode 100644 index 0000000..a957ac6 --- /dev/null +++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-orm.xml
@@ -0,0 +1,87 @@ +<?xml version="1.0" encoding="UTF-8"?> +<entity-mappings version="2.1" + xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + <package>jpars.app.auction.model</package> + + <named-query name="Auction.all"> + <query>SELECT a FROM Auction a</query> + </named-query> + + <named-query name="Auction.open"> + <query>SELECT a FROM Auction a where a.sold = false</query> + </named-query> + + <named-query name="Auction.photo"> + <query>SELECT a.image FROM Auction a where a.id = :auctionId</query> + </named-query> + + <named-query name="Bid.All"> + <query>SELECT b FROM Bid b ORDER BY b.time</query> + </named-query> + + <named-query name="Bid.forAuctionId"> + <query>SELECT b FROM Bid b WHERE b.auction.id =:auctionId ORDER BY b.time</query> + </named-query> + + <named-query name="Bid.maxForAuctionId"> + <query>SELECT MAX(b.bid) FROM Bid b WHERE b.auction.id =:auctionId</query> + </named-query> + + <named-query name="Bid.forUserId"> + <query>SELECT b FROM Bid b WHERE b.user.id =:userId ORDER BY b.time</query> + </named-query> + + <named-query name="User.all"> + <query>SELECT u from User u</query> + </named-query> + + <entity class="User" access="VIRTUAL"> + <table name="AUCTION_USER" /> + <attributes> + <id name="id" attribute-type="Integer"> + <column name="ID" /> + <generated-value/> + </id> + <basic name="name" attribute-type="String" /> + </attributes> + </entity> + + <entity class="Auction" access="VIRTUAL"> + <table name="AUCTION_AUCTION" /> + <attributes> + <id name="id" attribute-type="Integer"> + <column name="ID" /> + <generated-value/> + </id> + <basic name="name" attribute-type="String" /> + <basic name="image" attribute-type="String"/> + <basic name="description" attribute-type="String"> + <column column-definition="CLOB"/> + </basic> + <basic name="startPrice" attribute-type="Double"/> + <basic name="endPrice" attribute-type="Double"/> + <basic name="sold" attribute-type="boolean"/> + </attributes> + </entity> + + <entity class="Bid" access="VIRTUAL"> + <table name="AUCTION_BID" /> + <attributes> + <id name="id" attribute-type="Integer"> + <column name="ID" /> + <generated-value/> + </id> + <basic name="bid" attribute-type="Double" /> + <basic name="time" attribute-type="Long"/> + <one-to-one name="user" fetch="EAGER" target-entity="User"> + <join-column name="USER_ID" /> + </one-to-one> + <one-to-one name="auction" fetch="EAGER" target-entity="Auction"> + <join-column name="AUCTION_ID" /> + </one-to-one> + </attributes> + </entity> + +</entity-mappings> \ No newline at end of file
diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml b/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml new file mode 100644 index 0000000..0a1298d --- /dev/null +++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml
@@ -0,0 +1,16 @@ +<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0"> + <persistence-unit name="auction" transaction-type="RESOURCE_LOCAL"> + + <provider> + org.eclipse.persistence.jpa.PersistenceProvider + </provider> + + <class>jpars.app.auction.model.User</class> + <class>jpars.app.auction.model.Auction</class> + <class>jpars.app.auction.model.Bid</class> + <properties> + <property name="eclipselink.metadata-source" value="XML"/> + <property name="eclipselink.metadata-source.xml.url" value="file:///C:/EclipseLinkView2/incubator/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-orm.xml"/> + </properties> + </persistence-unit> +</persistence> \ No newline at end of file