I've moved these tests to the jest sandbox - due to the compilation errors (no junit 4).
On Thu, Nov 18, 2010 at 5:06 PM, <[email protected]> wrote: > Author: ppoddar > Date: Thu Nov 18 23:06:23 2010 > New Revision: 1036673 > > URL: http://svn.apache.org/viewvc?rev=1036673&view=rev > Log: > OPENJPA-1859: Add few encoding tests > > Added: > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/ > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > (with props) > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > (with props) > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > (with props) > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > (with props) > > > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > (with props) > > Added: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > URL: > http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java?rev=1036673&view=auto > > ============================================================================== > --- > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > (added) > +++ > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > Thu Nov 18 23:06:23 2010 > @@ -0,0 +1,102 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one > + * or more contributor license agreements. See the NOTICE file > + * distributed with this work for additional information > + * regarding copyright ownership. The ASF licenses this file > + * to you under the Apache License, Version 2.0 (the > + * "License"); you may not use this file except in compliance > + * with the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > + * software distributed under the License is distributed on an > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + * KIND, either express or implied. See the License for the > + * specific language governing permissions and limitations > + * under the License. > + */ > + > +package org.apache.openjpa.persistence.jest; > + > +import java.util.Date; > +import java.util.HashSet; > +import java.util.Set; > + > +import javax.persistence.CascadeType; > +import javax.persistence.Entity; > +import javax.persistence.Id; > +import javax.persistence.ManyToOne; > +import javax.persistence.OneToMany; > +import javax.persistence.OneToOne; > + > +/** > + * A persistent entity with singular and plural association to test JEST > object marshalling. > + * > + * @author Pinaki Poddar > + * > + */ > +...@entity > +public class Actor { > + public static enum Gender {Male, Female}; > + @Id > + private String id; > + private String firstName; > + private String lastName; > + private Gender gender; > + private Date dob; > + @OneToOne > + private Actor partner; > + @OneToMany > + private Set<Movie> movies; > + > + protected Actor() { > + > + } > + > + public Actor(String id, String firstName, String lastName, Gender > gender, Date dob) { > + super(); > + this.id = id; > + this.firstName = firstName; > + this.lastName = lastName; > + this.gender = gender; > + this.dob = dob; > + } > + > + public String getId() { > + return id; > + } > + > + public String getFirstName() { > + return firstName; > + } > + > + public String getLastName() { > + return lastName; > + } > + > + public Gender getGender() { > + return gender; > + } > + > + public Date getDob() { > + return dob; > + } > + public Actor getPartner() { > + return partner; > + } > + > + public void setPartner(Actor partner) { > + this.partner = partner; > + } > + > + public Set<Movie> getMovies() { > + return movies; > + } > + > + public void addMovie(Movie movie) { > + if (movies == null) > + movies = new HashSet<Movie>(); > + movies.add(movie); > + } > +} > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Actor.java > > ------------------------------------------------------------------------------ > svn:mime-type = text/plain > > Added: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > URL: > http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java?rev=1036673&view=auto > > ============================================================================== > --- > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > (added) > +++ > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > Thu Nov 18 23:06:23 2010 > @@ -0,0 +1,130 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one > + * or more contributor license agreements. See the NOTICE file > + * distributed with this work for additional information > + * regarding copyright ownership. The ASF licenses this file > + * to you under the Apache License, Version 2.0 (the > + * "License"); you may not use this file except in compliance > + * with the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > + * software distributed under the License is distributed on an > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + * KIND, either express or implied. See the License for the > + * specific language governing permissions and limitations > + * under the License. > + */ > + > +package org.apache.openjpa.persistence.jest; > + > +import java.util.ArrayList; > +import java.util.Date; > +import java.util.List; > +import java.util.Random; > + > +import javax.persistence.EntityManager; > + > +/** > + * Loads some example Actor-Movie data. > + * > + * @author Pinaki Poddar > + * > + */ > +public class DataLoader { > + @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[] {"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)}, > + }; > + > + 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}, > + }; > + > + public void populate(EntityManager em) throws Exception { > + Long count = em.createQuery("select count(m) from Movie m", > Long.class).getSingleResult(); > + if (count != null && count.longValue() > 0) { > + System.err.println("Found " + count + " Movie records in the > database"); > + return; > + } > + > + > + List<Actor> actors = createActors(); > + List<Movie> movies = createMovies(); > + linkActorAndMovie(actors, movies); > + makePartner(actors); > + em.getTransaction().begin(); > + for (Actor a : actors) { > + em.persist(a); > + } > + for (Movie m : movies) { > + em.persist(m); > + } > + 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); > + } > + 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); > + } > + 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); > + 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); > + } > + } > + } > + > + /** > + * 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(); > +} > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/DataLoader.java > > ------------------------------------------------------------------------------ > svn:mime-type = text/plain > > Added: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > URL: > http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java?rev=1036673&view=auto > > ============================================================================== > --- > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > (added) > +++ > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > Thu Nov 18 23:06:23 2010 > @@ -0,0 +1,149 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one > + * or more contributor license agreements. See the NOTICE file > + * distributed with this work for additional information > + * regarding copyright ownership. The ASF licenses this file > + * to you under the Apache License, Version 2.0 (the > + * "License"); you may not use this file except in compliance > + * with the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > + * software distributed under the License is distributed on an > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + * KIND, either express or implied. See the License for the > + * specific language governing permissions and limitations > + * under the License. > + */ > + > +package org.apache.openjpa.persistence.jest; > + > + > +import java.io.PrintWriter; > +import java.util.ArrayList; > +import java.util.Collection; > +import java.util.List; > + > +import javax.persistence.EntityManager; > +import javax.persistence.EntityManagerFactory; > +import javax.persistence.Persistence; > +import javax.persistence.Query; > +import javax.persistence.metamodel.Metamodel; > +import javax.xml.validation.Schema; > + > +import junit.framework.Assert; > + > +import org.apache.openjpa.kernel.OpenJPAStateManager; > +import org.apache.openjpa.kernel.StoreContext; > +import org.apache.openjpa.persistence.JPAFacadeHelper; > +import org.junit.After; > +import org.junit.AfterClass; > +import org.junit.Before; > +import org.junit.BeforeClass; > +import org.junit.Test; > +import org.w3c.dom.Document; > + > +/** > + * Base class for testing JEST outside a container. > + * > + * @author Pinaki Poddar > + * > + */ > +public class LocalJESTTest { > + private static EntityManagerFactory _emf; > + private static Metamodel _model; > + private EntityManager _em; > + > + > + @BeforeClass > + public static void setUpBeforeClass() throws Exception { > + _emf = Persistence.createEntityManagerFactory("jest"); > + _model = _emf.getMetamodel(); > + > + new DataLoader().populate(_emf.createEntityManager()); > + > + } > + > + @AfterClass > + public static void tearDownAfterClass() throws Exception { > + _emf.close(); > + } > + > + @Before > + public void setUp() throws Exception { > + _em = _emf.createEntityManager(); > + _em.getTransaction().begin(); > + } > + > + @After > + public void tearDown() throws Exception { > + _em.getTransaction().rollback(); > + _em.close(); > + } > + > + @Test > + public void testUnitIsAvaliable() { > + Assert.assertNotNull(_emf); > + } > + @Test > + public void testModelIsAvaliable() { > + Assert.assertNotNull(_model); > + Assert.assertFalse(_model.getManagedTypes().isEmpty()); > + } > + > + @Test > + public void testXMLEncoderForColllection() { > + encodeAndValidateQueryResult("select m from Movie m", false); > + } > + > + @Test > + public void testXMLEncoderForInstance() { > + encodeAndValidateQueryResult("select m from Movie m where m.title > = '" + DataLoader.MOVIE_DATA[1][0] + "'", > + true); > + } > + > + @Test > + 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(); > + 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()); > + } > + } > + > + 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)); > + } > + return sms; > + } > + > +} > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/LocalJESTTest.java > > ------------------------------------------------------------------------------ > svn:mime-type = text/plain > > Added: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > URL: > http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java?rev=1036673&view=auto > > ============================================================================== > --- > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > (added) > +++ > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > Thu Nov 18 23:06:23 2010 > @@ -0,0 +1,77 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one > + * or more contributor license agreements. See the NOTICE file > + * distributed with this work for additional information > + * regarding copyright ownership. The ASF licenses this file > + * to you under the Apache License, Version 2.0 (the > + * "License"); you may not use this file except in compliance > + * with the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > + * software distributed under the License is distributed on an > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + * KIND, either express or implied. See the License for the > + * specific language governing permissions and limitations > + * under the License. > + */ > + > +package org.apache.openjpa.persistence.jest; > + > +import java.util.HashSet; > +import java.util.Set; > + > +import javax.persistence.Entity; > +import javax.persistence.FetchType; > +import javax.persistence.GeneratedValue; > +import javax.persistence.Id; > +import javax.persistence.OneToMany; > + > +/** > + * @author Pinaki Poddar > + * > + */ > + > +...@entity > +public class Movie { > + @Id > + @GeneratedValue > + private long id; > + private String title; > + private int year; > + @OneToMany(fetch=FetchType.EAGER) > + private Set<Actor> actors; > + > + protected Movie() { > + > + } > + public Movie(String title, int year) { > + super(); > + this.title = title; > + this.year = year; > + } > + > + public long getId() { > + return id; > + } > + > + public String getTitle() { > + return title; > + } > + > + public void addActor(Actor a) { > + if (actors == null) > + actors = new HashSet<Actor>(); > + actors.add(a); > + } > + > + public Set<Actor> getActors() { > + return actors; > + } > + > + public int getYear() { > + return year; > + } > + > +} > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/Movie.java > > ------------------------------------------------------------------------------ > svn:mime-type = text/plain > > Added: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > URL: > http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java?rev=1036673&view=auto > > ============================================================================== > --- > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > (added) > +++ > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > Thu Nov 18 23:06:23 2010 > @@ -0,0 +1,38 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one > + * or more contributor license agreements. See the NOTICE file > + * distributed with this work for additional information > + * regarding copyright ownership. The ASF licenses this file > + * to you under the Apache License, Version 2.0 (the > + * "License"); you may not use this file except in compliance > + * with the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, > + * software distributed under the License is distributed on an > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + * KIND, either express or implied. See the License for the > + * specific language governing permissions and limitations > + * under the License. > + */ > + > +package org.apache.openjpa.persistence.jest; > + > +import org.apache.openjpa.kernel.Filters; > + > +import junit.framework.TestCase; > + > +/** > + * @author Pinaki Poddar > + * > + */ > +public class TestRelaxQueryParameterType extends TestCase { > + public static enum Gender {MALE, FEMALE}; > + public void testValidStringToEnum() { > + Filters.convert("MALE", Gender.class); > + } > + public void testInvalidStringToEnum() { > + Filters.convert("M", Gender.class); > + } > +} > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > > ------------------------------------------------------------------------------ > svn:eol-style = native > > Propchange: > openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestRelaxQueryParameterType.java > > ------------------------------------------------------------------------------ > svn:mime-type = text/plain > > >
