openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r1038009 - in /openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest: DataLoader.java LocalJESTTest.java Movie.java
Date Tue, 23 Nov 2010 07:34:32 GMT
Author: ppoddar
Date: Tue Nov 23 07:34:31 2010
New Revision: 1038009

URL: http://svn.apache.org/viewvc?rev=1038009&view=rev
Log:
OPENJPA-1851: modify data laoding to verifiable values

Modified:
    openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java
    openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java
    openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java

Modified: openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java?rev=1038009&r1=1038008&r2=1038009&view=diff
==============================================================================
--- openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java
(original)
+++ openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java
Tue Nov 23 07:34:31 2010
@@ -33,27 +33,42 @@ import javax.persistence.EntityManager;
  *
  */
 public class DataLoader {
+    // Hand-tuned data for Testing
     @SuppressWarnings("deprecation")
     public static Object[][] ACTOR_DATA = {
-        new Object[] {"m01", "Robert", "Redford", Actor.Gender.Male, new Date(1950, 1, 12)},
-        new Object[] {"m02", "Robert", "De Niro", Actor.Gender.Male, new Date(1940, 4, 14)},
-        new Object[] {"m03", "Al",     "Pacino",  Actor.Gender.Male, new Date(1950, 1, 12)},
-        new Object[] {"m04", "Brad",   "Pitt",    Actor.Gender.Male, new Date(1940, 4, 14)},
-        new Object[] {"m05", "Clint",  "Eastwood",Actor.Gender.Male, new Date(1950, 1, 12)},
+        new Object[] {"m1", "Robert", "Redford",  Actor.Gender.Male, new Date(50, 1, 12)},
+        new Object[] {"m2", "Robert", "De Niro",  Actor.Gender.Male, new Date(40, 4, 14)},
+        new Object[] {"m3", "Al",     "Pacino",   Actor.Gender.Male, new Date(50, 1, 12)},
+        new Object[] {"m4", "Jack",   "Nichelson",Actor.Gender.Male, new Date(40, 4, 14)},
+        new Object[] {"m5", "Clint",  "Eastwood", Actor.Gender.Male, new Date(50, 1, 12)},
         
-        new Object[] {"f01", "Meryl",   "Streep",    Actor.Gender.Female, new Date(1940,
4, 14)},
-        new Object[] {"f02", "Anglina", "Jolie",     Actor.Gender.Female, new Date(1950,
1, 12)},
-        new Object[] {"f03", "Goldie",  "Hawn",      Actor.Gender.Female, new Date(1940,
4, 14)},
-        new Object[] {"f04", "Diane",   "Keaton",    Actor.Gender.Female, new Date(1950,
1, 12)},
-        new Object[] {"f05", "Catherine", "Hepburn", Actor.Gender.Female, new Date(1940,
4, 14)},
+        new Object[] {"f1", "Meryl",   "Streep",    Actor.Gender.Female, new Date(40, 4,
14)},
+        new Object[] {"f2", "Fay",     "Dunaway",   Actor.Gender.Female, new Date(50, 1,
12)},
+        new Object[] {"f3", "Jodie",   "Foster",    Actor.Gender.Female, new Date(40, 4,
14)},
+        new Object[] {"f4", "Diane",   "Keaton",    Actor.Gender.Female, new Date(50, 1,
12)},
+        new Object[] {"f5", "Catherine", "Hepburn", Actor.Gender.Female, new Date(40, 4,
14)},
     };
     
     public static Object[][] MOVIE_DATA = {
-        new Object[] {"One flew over the cuckoo's nest", 1980},
-        new Object[] {"Everyone Says I Love You", 1980},
-        new Object[] {"Where Eagles Dare", 1980},
-        new Object[] {"Fight Club", 1980},
-        new Object[] {"Horse Whisperer", 1980},
+        new Object[] {"1", "China Town", 1980},
+        new Object[] {"2", "Taxi Driver", 1980},
+        new Object[] {"3", "Where Eagles Dare", 1980},
+        new Object[] {"4", "Godfather", 1980},
+        new Object[] {"5", "Horse Whisperer", 1980},
+    };
+    
+    public static int[][] MOVIE_ACTORS = {
+        new int[] {3,6},
+        new int[] {1,7},
+        new int[] {4},
+        new int[] {2,3,8},
+        new int[] {0}
+    };
+    
+    public static int[][] PARTNERS = {
+        new int[] {3,6},
+        new int[] {1,7},
+        new int[] {3,8},
     };
 
     public void populate(EntityManager em) throws Exception {
@@ -64,9 +79,9 @@ public class DataLoader {
         }
         
         
-        List<Actor> actors = createActors();
-        List<Movie> movies = createMovies();
-        linkActorAndMovie(actors, movies);
+        Actor[] actors = createActors();
+        Movie[] movies = createMovies();
+        linkActorAndMovie(movies, actors);
         makePartner(actors);
         em.getTransaction().begin();
         for (Actor a : actors) {
@@ -78,53 +93,44 @@ public class DataLoader {
         em.getTransaction().commit();
     }
     
-    List<Actor> createActors() {
-        List<Actor> actors = new ArrayList<Actor>();
-        for (Object[] a : ACTOR_DATA) {
-            Actor actor = new Actor((String)a[0], (String)a[1], (String)a[2], (Actor.Gender)a[3],
(Date)a[4]);
-            actors.add(actor);
+    Actor[] createActors() {
+        Actor[] actors = new Actor[ACTOR_DATA.length];
+        for (int i = 0; i < ACTOR_DATA.length; i++) {
+            Object[] a = ACTOR_DATA[i];
+            actors[i] = new Actor((String)a[0], (String)a[1], (String)a[2], (Actor.Gender)a[3],
(Date)a[4]);
         }
         return actors;
     }
     
-    List<Movie> createMovies() {
-        List<Movie> movies = new ArrayList<Movie>();
-        for (Object[] m : MOVIE_DATA) {
-            Movie movie = new Movie((String)m[0], (Integer)m[1]);
-            movies.add(movie);
+    Movie[] createMovies() {
+        Movie[] movies = new Movie[MOVIE_DATA.length];
+        for (int i = 0; i < MOVIE_DATA.length; i++) {
+            Object[] m = MOVIE_DATA[i];
+            movies[i] = new Movie((String)m[0], (String)m[1], (Integer)m[2]);
         }
         return movies;
     }
     
-    void linkActorAndMovie(List<Actor> actors, List<Movie> movies) {
-        for (Actor a : actors) {
-            int n = rng.nextInt(movies.size());
-            for (int i = 0; i < n; i++) {
-                Movie m = random(movies);
+    void linkActorAndMovie(Movie[] movies, Actor[] actors) {
+        for (int i = 0; i < MOVIE_ACTORS.length; i++) {
+            int[] roles = MOVIE_ACTORS[i];
+            Movie m = movies[i];
+            for (int j = 0; j < roles.length; j++) {
+                Actor a = actors[roles[j]];
                 a.addMovie(m);
                 m.addActor(a);
             }
         }
     }
     
-    void makePartner(List<Actor> actors) {
-        for (Actor p : actors) {
-            if (p.getPartner() != null)
-                continue;
-            Actor f = random(actors);
-            if (f.getPartner() == null && p.getGender() != f.getGender()) {
-               p.setPartner(f);
-               f.setPartner(p);
-            }
+    void makePartner(Actor[] actors) {
+        for (int i = 0; i < PARTNERS.length; i++) {
+            int[] partners = PARTNERS[i];
+            Actor a1 = actors[partners[0]];
+            Actor a2 = actors[partners[1]];
+            a1.setPartner(a2);
+            a2.setPartner(a1);
        }
     }
     
-    /**
-     * Select a random element from the given list. 
-     */
-    private <T> T random(List<T> list) {
-        return list.get(rng.nextInt(list.size()));
-    }
-    
-    private static Random rng = new Random();
 }

Modified: openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java?rev=1038009&r1=1038008&r2=1038009&view=diff
==============================================================================
--- openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java
(original)
+++ openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java
Tue Nov 23 07:34:31 2010
@@ -20,9 +20,14 @@
 package org.apache.openjpa.persistence.jest;
 
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import javax.persistence.EntityManager;
@@ -47,8 +52,9 @@ import org.w3c.dom.Document;
  */
 public class LocalJESTTest extends TestCase {
     private static EntityManagerFactory _emf;
-    private static Metamodel _model;
+    protected static Metamodel _model;
     private EntityManager _em;
+    File parent = new File(".", "openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest");
 
     
     public void setUp() throws Exception {
@@ -76,55 +82,41 @@ public class LocalJESTTest extends TestC
         Assert.assertFalse(_model.getManagedTypes().isEmpty());
     }
 
-    public void testXMLEncoderForColllection() {
-        encodeAndValidateQueryResult("select m from Movie m", false);
-    }
-    
-    public void testXMLEncoderForInstance() {
-        encodeAndValidateQueryResult("select m from Movie m where m.title = '" + DataLoader.MOVIE_DATA[1][0]
+ "'", 
-            true);
-    }
     
-    public void testDomainModelEncoder() {
-        DomainCommand formatter = new DomainCommand();
-        Document doc = formatter.encode(_model);
-        try {
-            new XMLFormatter().write(doc, new PrintWriter(System.err));
-        } catch (Exception e) {
-            e.printStackTrace();
-            Assert.fail();
-        }
-    }
     
-    void encodeAndValidateQueryResult(String jpql, boolean single) {
-        XMLFormatter formatter = new XMLFormatter();
+    /**
+     * Execute the given JPQL query and returns the managed instances.
+     *
+     * @return the formatter output.
+     */
+    protected List<OpenJPAStateManager> getQueryResult(String jpql, boolean single)
{
         Query query = _em.createQuery(jpql);
-        Document doc = null;
-        if (single)
-            doc = formatter.encodeManagedInstance(toStateManager(query.getSingleResult()),
_model);
-        else
-            doc = formatter.encodeManagedInstances(toStateManager(query.getResultList()),
_model);
-        try {
-            formatter.write(doc, new PrintWriter(System.err));
-            formatter.validate(doc);
-        } catch (Exception e) {
-            e.printStackTrace();
-            Assert.fail(e.getMessage());
-        }
+        List<?> result = single ? Collections.singletonList(query.getSingleResult())
: query.getResultList();
+        assertFalse(result.isEmpty());
+        return toStateManager(result);
     }
     
-    OpenJPAStateManager toStateManager(Object obj) {
-        StoreContext broker = ((StoreContext)JPAFacadeHelper.toBroker(_em));
-        return broker.getStateManager(obj);
-    }
     
     List<OpenJPAStateManager> toStateManager(Collection<?> objects) {
         StoreContext broker = ((StoreContext)JPAFacadeHelper.toBroker(_em));
         List<OpenJPAStateManager> sms = new ArrayList<OpenJPAStateManager>();
         for (Object o : objects) {
-            sms.add(broker.getStateManager(o));
+            OpenJPAStateManager sm = broker.getStateManager(o);
+            assertNotNull(sm);
+            sms.add(sm);
         }
         return sms;
     }
     
+    PrintWriter createTestOutput(String name) {
+        try {
+            File file = new File(parent, name);
+            System.err.println(this.getName() + " output : [" + file.getAbsolutePath() +
"]");
+            return new PrintWriter(new FileOutputStream(file, false), true);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+            fail(e.getMessage());
+        }
+        return null;
+    }
 }

Modified: openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java?rev=1038009&r1=1038008&r2=1038009&view=diff
==============================================================================
--- openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java
(original)
+++ openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java
Tue Nov 23 07:34:31 2010
@@ -36,8 +36,7 @@ import javax.persistence.OneToMany;
 @Entity
 public class Movie {
     @Id
-    @GeneratedValue
-    private long id;
+    private String id;
     private String title;
     private int year;
     @OneToMany(fetch=FetchType.EAGER)
@@ -46,13 +45,14 @@ public class Movie {
     protected Movie() {
         
     }
-    public Movie(String title, int year) {
+    public Movie(String id, String title, int year) {
         super();
+        this.id = id;
         this.title = title;
         this.year = year;
     }
     
-    public long getId() {
+    public String getId() {
         return id;
     }
     



Mime
View raw message