openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig L Russell <Craig.Russ...@Sun.COM>
Subject Re: svn commit: r687806 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query: TestMultipleEntityProjection.java domain/Magazine.java domain/Publisher.java
Date Thu, 21 Aug 2008 20:13:36 GMT
Hi Pinaki,

Nice test case. But I'm confused why there is a prolog with  
TestLockGroupsWithHorizontalBaseType.java. May be good to remove this  
bit...

Craig

On Aug 21, 2008, at 10:11 AM, ppoddar@apache.org wrote:

> Author: ppoddar
> Date: Thu Aug 21 10:11:42 2008
> New Revision: 687806
>
> URL: http://svn.apache.org/viewvc?rev=687806&view=rev
> Log:
> Add a test case for projection of multiple entity.
>
> Added:
>    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
>    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Magazine.java
>    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Publisher.java
>
> Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/ 
> apache/openjpa/persistence/jdbc/query/ 
> TestMultipleEntityProjection.java
> URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java?rev=687806&view=auto
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java  
> (added)
> +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java Thu  
> Aug 21 10:11:42 2008
> @@ -0,0 +1,161 @@
> +/*
> + * TestLockGroupsWithHorizontalBaseType.java
> + *
> + * Created on October 4, 2006, 5:03 PM
> + *
> + * To change this template, choose Tools | Template Manager
> + * and open the template in the editor.
> + */
> +
> +/*
> + * 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.jdbc.query;
> +
> +import java.util.List;
> +
> +import javax.persistence.EntityManager;
> +import javax.persistence.Query;
> +
> +import org.apache.openjpa.persistence.jdbc.query.domain.Magazine;
> +import org.apache.openjpa.persistence.jdbc.query.domain.Publisher;
> +import org.apache.openjpa.persistence.test.SingleEMFTestCase;
> +
> +/**
> + * Tests multiple entities in projection are returned with both  
> inner and outer
> + * joins.
> + *
> + * Originally reported in
> + * <A HREF="http://n2.nabble.com/Selecting-multiple-objects---bug--tc732941.html

> ">
> + * OpenJPA mailing list</A>
> + *
> + * @author Pinaki Poddar
> + *
> + */
> +public class TestMultipleEntityProjection extends SingleEMFTestCase {
> +	private static boolean INNER_JOIN = true;
> +	private static String[][] MAGAZINE_PUBLISHER_NAME_PAIRS = {
> +		new String[] {"Times",      "Publisher-0"},
> +		new String[] {"Times Life", "Publisher-1"},
> +		new String[] {"Times Kid",  null},
> +		new String[] {"Newsweek",   "Publisher-3"},
> +		new String[] {"People",     null},
> +		new String[] {"Nature",     "Publisher-5"},
> +		new String[] {"MIT Review", "Publisher-6"},
> +		new String[] {"Wired",      "Publisher-7"},
> +		
> +	};
> +	
> +	public void setUp() {
> +		super.setUp(CLEAR_TABLES, Magazine.class, Publisher.class);
> +		createData();
> +	}
> +
> +	/**
> +	 * Create Magazine and associate Publisher unless its name is null.
> +	 */
> +	void createData() {
> +		EntityManager em = emf.createEntityManager();
> +		em.getTransaction().begin();
> +		for (int i = 0; i < MAGAZINE_PUBLISHER_NAME_PAIRS.length; i++) {
> +			String magName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][0];
> +			String pubName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][1];
> +			Magazine mag = new Magazine();
> +			mag.setName(magName);
> +			if (pubName != null) {
> +				Publisher pub = new Publisher();
> +				pub.setName(pubName);
> +				mag.setPublisher(pub);
> +				em.persist(pub);
> +			}
> +			em.persist(mag);
> +		}
> +		em.getTransaction().commit();
> +	}
> +
> +	public void testMultipleEntitiesInProjectionUsingOuterJoin() {
> +		String jpql = "select m, p " +
> +		              "from Magazine m left outer join m.publisher p " +
> +		              "where ((:useName = false) or (m.name like :name))";
> +		EntityManager em = emf.createEntityManager();
> +		Query query = em.createQuery(jpql);
> +		
> +		String magKey = "Times";
> +		query.setParameter("useName", true);
> +		query.setParameter("name", '%'+magKey+'%');
> +		
> +		List result = query.getResultList();
> +		
> +		int expecetedCount = getExpecetedResultCount(magKey, !INNER_JOIN);
> +		assertFalse(result.isEmpty());
> +		assertEquals(expecetedCount, result.size());
> +		for (Object o : result) {
> +			assertTrue(o instanceof Object[]);
> +			Object[] row = (Object[])o;
> +			assertEquals(2, row.length);
> +			assertTrue(row[0] instanceof Magazine);
> +			assertTrue(row[1] == null || row[1] instanceof Publisher);
> +			assertNotNull(row[0]);
> +			assertEquals(((Magazine)row[0]).getPublisher(), row[1]);
> +		}
> +	}
> +	
> +	public void testMultipleEntitiesInProjectionUsingInnerJoin() {
> +		String jpql = "select m, p " +
> +		              "from Magazine m inner join m.publisher p " +
> +		              "where ((:useName = false) or (m.name like :name))";
> +		EntityManager em = emf.createEntityManager();
> +		Query query = em.createQuery(jpql);
> +		
> +		String magKey = "Times";
> +		query.setParameter("useName", true);
> +		query.setParameter("name", '%'+magKey+'%');
> +		
> +		List result = query.getResultList();
> +		
> +		int expecetedCount = getExpecetedResultCount(magKey, INNER_JOIN);
> +		assertFalse(result.isEmpty());
> +		assertEquals(expecetedCount, result.size());
> +		for (Object o : result) {
> +			assertTrue(o instanceof Object[]);
> +			Object[] row = (Object[])o;
> +			assertEquals(2, row.length);
> +			assertTrue(row[0] instanceof Magazine);
> +			assertTrue(row[1] instanceof Publisher);
> +			assertNotNull(row[0]);
> +			assertNotNull(row[1]);
> +			assertEquals(((Magazine)row[0]).getPublisher(), row[1]);
> +		}
> +	}
> +	
> +	/**
> +	 * Count number of expected result based on inner/outer join  
> condition and
> +	 * the name of the Magazine.
> +	 */
> +	private int getExpecetedResultCount(String key, boolean innerJoin) {
> +		int count = 0;
> +		for (int i = 0; i < MAGAZINE_PUBLISHER_NAME_PAIRS.length; i++) {
> +			String magName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][0];
> +			String pubName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][1];
> +			if (magName.indexOf(key) != -1)
> +				if (!innerJoin || (innerJoin && pubName != null))
> +					count++;
> +		}
> +		return count;
> +	}
> +}
>
> Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/ 
> apache/openjpa/persistence/jdbc/query/domain/Magazine.java
> URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java?rev=687806&view=auto
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Magazine.java (added)
> +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Magazine.java Thu Aug 21  
> 10:11:42 2008
> @@ -0,0 +1,61 @@
> +/*
> + * 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.jdbc.query.domain;
> +
> +import javax.persistence.*;
> +
> +/**
> + * Simple persistent entity as a target of bi-directional one-to-one
> + * association.
> + *
> + * @author Pinaki Poddar
> + *
> + */
> +@Entity
> +public class Magazine {
> +	@Id
> +	@GeneratedValue
> +	private long id;
> +
> +	private String name;
> +	
> +	@OneToOne
> +	private Publisher publisher;
> +
> +	public String getName() {
> +		return name;
> +	}
> +
> +	public void setName(String name) {
> +		this.name = name;
> +	}
> +
> +	public long getId() {
> +		return id;
> +	}
> +
> +	public Publisher getPublisher() {
> +		return publisher;
> +	}
> +
> +	public void setPublisher(Publisher publisher) {
> +		this.publisher = publisher;
> +	}
> +
> +}
>
> Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/ 
> apache/openjpa/persistence/jdbc/query/domain/Publisher.java
> URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java?rev=687806&view=auto
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Publisher.java (added)
> +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/ 
> openjpa/persistence/jdbc/query/domain/Publisher.java Thu Aug 21  
> 10:11:42 2008
> @@ -0,0 +1,50 @@
> +/*
> + * 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.jdbc.query.domain;
> +
> +import javax.persistence.*;
> +
> +/**
> + * Simple persistent entity as a target of uni-directional one-to-one
> + * association.
> + *
> + * @author Pinaki Poddar
> + *
> + */
> +@Entity
> +public class Publisher {
> +	@Id
> +	@GeneratedValue
> +	private long id;
> +
> +	private String name;
> +	
> +	public String getName() {
> +		return name;
> +	}
> +
> +	public void setName(String name) {
> +		this.name = name;
> +	}
> +
> +	public long getId() {
> +		return id;
> +	}
> +
> +}
>
>

Craig L Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Mime
View raw message