/tests/com/google/appengine/datanucleus/query/JPQLQueryTest.java
http://datanucleus-appengine.googlecode.com/ · Java · 3434 lines · 2943 code · 393 blank · 98 comment · 47 complexity · 3aab801a7e71a928c5a79dcec9a57053 MD5 · raw file
Large files are truncated click here to view the full file
- /**********************************************************************
- Copyright (c) 2009 Google Inc.
- Licensed 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 com.google.appengine.datanucleus.query;
- import com.google.appengine.api.datastore.DatastoreFailureException;
- import com.google.appengine.api.datastore.DatastoreTimeoutException;
- import com.google.appengine.api.datastore.Entity;
- import com.google.appengine.api.datastore.Key;
- import com.google.appengine.api.datastore.KeyFactory;
- import com.google.appengine.api.datastore.Query.FilterOperator;
- import com.google.appengine.api.datastore.Query.FilterPredicate;
- import com.google.appengine.api.datastore.Query.SortDirection;
- import com.google.appengine.api.datastore.Query.SortPredicate;
- import com.google.appengine.api.datastore.ShortBlob;
- import com.google.appengine.api.datastore.dev.LocalDatastoreService;
- import com.google.appengine.datanucleus.DatastoreManager;
- import com.google.appengine.datanucleus.DatastoreServiceFactoryInternal;
- import com.google.appengine.datanucleus.DatastoreServiceInterceptor;
- import com.google.appengine.datanucleus.ExceptionThrowingDatastoreDelegate;
- import com.google.appengine.datanucleus.PrimitiveArrays;
- import com.google.appengine.datanucleus.TestUtils;
- import com.google.appengine.datanucleus.Utils;
- import com.google.appengine.datanucleus.WriteBlocker;
- import com.google.appengine.datanucleus.jpa.JPATestCase;
- import com.google.appengine.datanucleus.test.jdo.Flight;
- import com.google.appengine.datanucleus.test.jdo.KitchenSink;
- import com.google.appengine.datanucleus.test.jdo.Person;
- import com.google.appengine.datanucleus.test.jpa.BidirectionalChildListJPA;
- import com.google.appengine.datanucleus.test.jpa.BidirectionalChildLongPkListJPA;
- import com.google.appengine.datanucleus.test.jpa.BidirectionalGrandchildListJPA;
- import com.google.appengine.datanucleus.test.jpa.Book;
- import com.google.appengine.datanucleus.test.jpa.DetachableJPA;
- import com.google.appengine.datanucleus.test.jpa.HasBytesJPA;
- import com.google.appengine.datanucleus.test.jpa.HasDoubleJPA;
- import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkJPA;
- import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkSeparateIdFieldJPA;
- import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkSeparateNameFieldJPA;
- import com.google.appengine.datanucleus.test.jpa.HasEnumJPA;
- import com.google.appengine.datanucleus.test.jpa.HasFloatJPA;
- import com.google.appengine.datanucleus.test.jpa.HasKeyAncestorStringPkJPA;
- import com.google.appengine.datanucleus.test.jpa.HasKeyPkJPA;
- import com.google.appengine.datanucleus.test.jpa.HasLongPkJPA;
- import com.google.appengine.datanucleus.test.jpa.HasMultiValuePropsJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyKeyPkListJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyKeyPkSetJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyListJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyLongPkListJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyLongPkSetJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyUnencodedStringPkListJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToManyUnencodedStringPkSetJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToOneJPA;
- import com.google.appengine.datanucleus.test.jpa.HasOneToOneParentJPA;
- import com.google.appengine.datanucleus.test.jpa.HasStringAncestorStringPkJPA;
- import com.google.appengine.datanucleus.test.jpa.HasUnencodedStringPkJPA;
- import com.google.appengine.datanucleus.test.jpa.InTheHouseJPA;
- import com.google.appengine.datanucleus.test.jpa.NullDataJPA;
- import com.google.appengine.datanucleus.test.jpa.AbstractBaseClassesJPA.Base1;
- import com.google.appengine.datanucleus.test.jpa.UnidirectionalSingeTableChildJPA.UnidirTop;
- import com.google.apphosting.api.ApiProxy;
- import com.google.apphosting.api.DatastorePb;
- import org.datanucleus.api.jpa.JPAQuery;
- import org.datanucleus.exceptions.NucleusFatalUserException;
- import org.datanucleus.exceptions.NucleusUserException;
- import org.datanucleus.query.expression.Expression;
- import org.easymock.EasyMock;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.math.BigDecimal;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import javax.persistence.NoResultException;
- import javax.persistence.NonUniqueResultException;
- import javax.persistence.PersistenceException;
- import javax.persistence.Query;
- import javax.persistence.QueryTimeoutException;
- import javax.persistence.TypedQuery;
- import static com.google.appengine.datanucleus.test.jdo.Flight.newFlightEntity;
- /**
- * @author Max Ross <maxr@google.com>
- */
- public class JPQLQueryTest extends JPATestCase {
- private static final List<SortPredicate> NO_SORTS = Collections.emptyList();
- private static final List<FilterPredicate> NO_FILTERS = Collections.emptyList();
- private static final FilterPredicate TITLE_EQ_2 =
- new FilterPredicate("title", FilterOperator.EQUAL, 2L);
- private static final FilterPredicate TITLE_EQ_2STR =
- new FilterPredicate("title", FilterOperator.EQUAL, "2");
- private static final FilterPredicate ISBN_EQ_4 =
- new FilterPredicate("isbn", FilterOperator.EQUAL, 4L);
- private static final FilterPredicate TITLE_GT_2 =
- new FilterPredicate("title", FilterOperator.GREATER_THAN, 2L);
- private static final FilterPredicate TITLE_GTE_2 =
- new FilterPredicate("title", FilterOperator.GREATER_THAN_OR_EQUAL, 2L);
- private static final FilterPredicate ISBN_LT_4 =
- new FilterPredicate("isbn", FilterOperator.LESS_THAN, 4L);
- private static final FilterPredicate ISBN_LTE_4 =
- new FilterPredicate("isbn", FilterOperator.LESS_THAN_OR_EQUAL, 4L);
- private static final FilterPredicate TITLE_NEQ_NULL_LITERAL =
- new FilterPredicate("title", FilterOperator.NOT_EQUAL, null);
- private static final FilterPredicate TITLE_NEQ_2_LITERAL =
- new FilterPredicate("title", FilterOperator.NOT_EQUAL, 2L);
- private static final SortPredicate TITLE_ASC =
- new SortPredicate("title", SortDirection.ASCENDING);
- private static final SortPredicate ISBN_DESC =
- new SortPredicate("isbn", SortDirection.DESCENDING);
- private static final FilterPredicate TITLE_IN_2_ARGS =
- new FilterPredicate("title", FilterOperator.IN, Arrays.asList("2", 2L));
- private static final FilterPredicate TITLE_IN_3_ARGS =
- new FilterPredicate("title", FilterOperator.IN, Arrays.asList("2", 2L, false));
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- DatastoreServiceInterceptor.install(getStoreManager(), new WriteBlocker());
- }
- @Override
- protected void tearDown() throws Exception {
- try {
- super.tearDown();
- } finally {
- DatastoreServiceInterceptor.uninstall();
- }
- }
- @Override
- protected EntityManagerFactoryName getEntityManagerFactoryName() {
- return EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed;
- }
- public void testUnsupportedFilters_NoResultExpr() {
- String baseQuery = "SELECT FROM " + Book.class.getName() + " b ";
- testUnsupportedFilters(baseQuery);
- }
- public void testUnsupportedFilters_PrimaryResultExpr() {
- String baseQuery = "SELECT b FROM " + Book.class.getName() + " b ";
- testUnsupportedFilters(baseQuery);
- }
- private void testUnsupportedFilters(String baseQuery) {
- Set<Expression.Operator> unsupportedOps =
- new HashSet<Expression.Operator>(DatastoreQuery.UNSUPPORTED_OPERATORS);
- baseQuery += "WHERE ";
- assertQueryUnsupportedByOrm(baseQuery + "NOT title = 'foo'", Expression.OP_NOT, unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "(title + author) = 'foo'", Expression.OP_ADD,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "title + author = 'foo'", Expression.OP_ADD,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "(title - author) = 'foo'", Expression.OP_SUB,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "title - author = 'foo'", Expression.OP_SUB,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "(title / author) = 'foo'", Expression.OP_DIV,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "title / author = 'foo'", Expression.OP_DIV,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "(title * author) = 'foo'", Expression.OP_MUL,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "title * author = 'foo'", Expression.OP_MUL,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "(title % author) = 'foo'", Expression.OP_MOD,
- unsupportedOps);
- assertQueryUnsupportedByOrm(baseQuery + "title % author = 'foo'", Expression.OP_MOD,
- unsupportedOps);
- assertQueryRequiresUnsupportedDatastoreFeature(baseQuery + "title LIKE '%foo'");
- // can't have 'or' on multiple properties
- assertQueryRequiresUnsupportedDatastoreFeature(baseQuery + "title = 'yar' or author = null");
- assertQueryRequiresUnsupportedDatastoreFeature(baseQuery + "isbn = 4 and (title = 'yar' or author = 'yam')");
- assertQueryRequiresUnsupportedDatastoreFeature(baseQuery + "title IN('yar') or author = 'yam'");
- // can only check equality
- assertQueryRequiresUnsupportedDatastoreFeature(baseQuery + "title > 5 or title < 2");
- // multiple inequality filters
- // TODO(maxr) Make this pass against the real datastore.
- // We need to have it return BadRequest instead of NeedIndex for that to happen.
- assertQueryUnsupportedByDatastore(baseQuery + "(title > 2 AND isbn < 4)", IllegalArgumentException.class);
- // inequality filter prop is not the same as the first order by prop
- assertQueryUnsupportedByDatastore(baseQuery + "(title > 2) order by isbn", IllegalArgumentException.class);
- // gets split into multiple inequality props
- assertQueryUnsupportedByDatastore(baseQuery + "title <> 2 AND isbn <> 4", IllegalArgumentException.class);
- assertEquals(
- new HashSet<Expression.Operator>(Arrays.asList(Expression.OP_CONCAT, Expression.OP_COM,
- Expression.OP_NEG, Expression.OP_IS,
- Expression.OP_LIKE,
- Expression.OP_ISNOT)), unsupportedOps);
- }
- public void testEvaluateInMemory() {
- ds.put(null, newFlightEntity("1", "yar", "bam", 1, 2));
- ds.put(null, newFlightEntity("1", "yam", null, 1, 2));
- // This is impossible in the datastore, so run totally in-memory
- String query = "SELECT f FROM " + Flight.class.getName() + " f WHERE origin = 'yar' OR dest IS null";
- Query q = em.createQuery(query);
- q.setHint("datanucleus.query.evaluateInMemory", "true");
- try {
- List<Flight> results = (List<Flight>) q.getResultList();
- assertEquals("Number of results was wrong", 2, results.size());
- } catch (RuntimeException e) {
- fail("Threw exception when evaluating query in-memory, but should have run");
- }
- }
- public void testSupportedFilters_NoResultExpr() {
- String baseQuery = "SELECT FROM " + Book.class.getName() + " b ";
- testSupportedFilters(baseQuery);
- }
- public void testSupportedFilters_PrimaryResultExpr() {
- String baseQuery = "SELECT b FROM " + Book.class.getName() + " b ";
- testSupportedFilters(baseQuery);
- }
- private void testSupportedFilters(String baseQuery) {
- assertQuerySupported(baseQuery, NO_FILTERS, NO_SORTS);
- baseQuery += "WHERE ";
- assertQuerySupported(baseQuery + "title = 2", Utils.newArrayList(TITLE_EQ_2), NO_SORTS);
- assertQuerySupported(baseQuery + "title = \"2\"", Utils.newArrayList(TITLE_EQ_2STR), NO_SORTS);
- assertQuerySupported(baseQuery + "(title = 2)", Utils.newArrayList(TITLE_EQ_2), NO_SORTS);
- assertQuerySupported(baseQuery + "title = 2 AND isbn = 4",
- Utils.newArrayList(TITLE_EQ_2,ISBN_EQ_4),
- NO_SORTS);
- assertQuerySupported(baseQuery + "(title = 2 AND isbn = 4)",
- Utils.newArrayList(TITLE_EQ_2, ISBN_EQ_4),
- NO_SORTS);
- assertQuerySupported(baseQuery + "(title = 2) AND (isbn = 4)", Utils.newArrayList(
- TITLE_EQ_2, ISBN_EQ_4), NO_SORTS);
- assertQuerySupported(baseQuery + "title > 2", Utils.newArrayList(TITLE_GT_2), NO_SORTS);
- assertQuerySupported(baseQuery + "title >= 2", Utils.newArrayList(TITLE_GTE_2), NO_SORTS);
- assertQuerySupported(baseQuery + "isbn < 4", Utils.newArrayList(ISBN_LT_4), NO_SORTS);
- assertQuerySupported(baseQuery + "isbn <= 4", Utils.newArrayList(ISBN_LTE_4), NO_SORTS);
- baseQuery = "SELECT FROM " + Book.class.getName() + " b ";
- assertQuerySupported(baseQuery + "ORDER BY title ASC", NO_FILTERS,
- Utils.newArrayList(TITLE_ASC));
- assertQuerySupported(baseQuery + "ORDER BY isbn DESC", NO_FILTERS,
- Utils.newArrayList(ISBN_DESC));
- assertQuerySupported(baseQuery + "ORDER BY title ASC, isbn DESC", NO_FILTERS,
- Utils.newArrayList(TITLE_ASC, ISBN_DESC));
- assertQuerySupported(baseQuery + "WHERE title = 2 AND isbn = 4 ORDER BY title ASC, isbn DESC",
- Utils.newArrayList(TITLE_EQ_2, ISBN_EQ_4),
- Utils.newArrayList(TITLE_ASC, ISBN_DESC));
- assertQuerySupported(baseQuery + "WHERE title <> null",
- Utils.newArrayList(TITLE_NEQ_NULL_LITERAL), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title <> 2",
- Utils.newArrayList(TITLE_NEQ_2_LITERAL), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title = '2' OR title = 2",
- Utils.newArrayList(TITLE_IN_2_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title = '2' OR title = 2 OR title = false",
- Utils.newArrayList(TITLE_IN_3_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title IN ('2', 2)",
- Utils.newArrayList(TITLE_IN_2_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title IN ('2', 2, false)",
- Utils.newArrayList(TITLE_IN_3_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE (title = '2' OR title = 2) AND isbn = 4",
- Utils.newArrayList(ISBN_EQ_4, TITLE_IN_2_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title IN ('2', 2) AND isbn = 4",
- Utils.newArrayList(ISBN_EQ_4, TITLE_IN_2_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE (title = '2' OR title = 2 OR title = false) AND isbn = 4",
- Utils.newArrayList(ISBN_EQ_4, TITLE_IN_3_ARGS), NO_SORTS);
- assertQuerySupported(baseQuery + "WHERE title IN ('2', 2, false) AND isbn = 4",
- Utils.newArrayList(ISBN_EQ_4, TITLE_IN_3_ARGS), NO_SORTS);
- }
- public void test2Equals2OrderBy() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book"));
- ds.put(Book.newBookEntity("Joe Blow", "54321", "A Book"));
- ds.put(Book.newBookEntity("Jane Blow", "13579", "Baz Book"));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE author = 'Joe Blow'" +
- " ORDER BY title DESC, isbn ASC");
- @SuppressWarnings("unchecked")
- List<Book> result = (List<Book>) q.getResultList();
- assertEquals(4, result.size());
- assertEquals("12345", result.get(0).getIsbn());
- assertEquals("11111", result.get(1).getIsbn());
- assertEquals("67890", result.get(2).getIsbn());
- assertEquals("54321", result.get(3).getIsbn());
- }
- public void testDefaultOrderingIsAsc() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book"));
- ds.put(Book.newBookEntity("Joe Blow", "54321", "A Book"));
- ds.put(Book.newBookEntity("Jane Blow", "13579", "Baz Book"));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE author = 'Joe Blow'" +
- " ORDER BY title");
- @SuppressWarnings("unchecked")
- List<Book> result = (List<Book>) q.getResultList();
- assertEquals(4, result.size());
- assertEquals("54321", result.get(0).getIsbn());
- assertEquals("67890", result.get(1).getIsbn());
- assertEquals("11111", result.get(2).getIsbn());
- assertEquals("12345", result.get(3).getIsbn());
- }
- public void testLimitQuery() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book"));
- ds.put(Book.newBookEntity("Joe Blow", "54321", "A Book"));
- ds.put(Book.newBookEntity("Jane Blow", "13579", "Baz Book"));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE author = 'Joe Blow'" +
- " ORDER BY title DESC, isbn ASC");
- q.setMaxResults(1);
- @SuppressWarnings("unchecked")
- List<Book> result1 = (List<Book>) q.getResultList();
- assertEquals(1, result1.size());
- assertEquals("12345", result1.get(0).getIsbn());
- q.setMaxResults(0);
- @SuppressWarnings("unchecked")
- List<Book> result2 = (List<Book>) q.getResultList();
- assertEquals(0, result2.size());
- try {
- q.setMaxResults(-1);
- fail("expected iae");
- } catch (IllegalArgumentException iae) {
- // good
- }
- }
- public void testOffsetQuery() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book"));
- ds.put(Book.newBookEntity("Joe Blow", "54321", "A Book"));
- ds.put(Book.newBookEntity("Jane Blow", "13579", "Baz Book"));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE author = 'Joe Blow'" +
- " ORDER BY title DESC, isbn ASC");
- q.setFirstResult(0);
- @SuppressWarnings("unchecked")
- List<Book> result1 = (List<Book>) q.getResultList();
- assertEquals(4, result1.size());
- assertEquals("12345", result1.get(0).getIsbn());
- q.setFirstResult(1);
- @SuppressWarnings("unchecked")
- List<Book> result2 = (List<Book>) q.getResultList();
- assertEquals(3, result2.size());
- assertEquals("11111", result2.get(0).getIsbn());
- try {
- q.setFirstResult(-1);
- fail("expected iae");
- } catch (IllegalArgumentException iae) {
- // good
- }
- }
- public void testOffsetLimitQuery() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book"));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book"));
- ds.put(Book.newBookEntity("Joe Blow", "54321", "A Book"));
- ds.put(Book.newBookEntity("Jane Blow", "13579", "Baz Book"));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE author = 'Joe Blow'" +
- " ORDER BY title DESC, isbn ASC");
- q.setFirstResult(0);
- q.setMaxResults(0);
- @SuppressWarnings("unchecked")
- List<Book> result1 = (List<Book>) q.getResultList();
- assertEquals(0, result1.size());
- q.setFirstResult(1);
- q.setMaxResults(0);
- @SuppressWarnings("unchecked")
- List<Book> result2 = (List<Book>) q.getResultList();
- assertEquals(0, result2.size());
- q.setFirstResult(0);
- q.setMaxResults(1);
- @SuppressWarnings("unchecked")
- List<Book> result3 = (List<Book>) q.getResultList();
- assertEquals(1, result3.size());
- q.setFirstResult(0);
- q.setMaxResults(2);
- @SuppressWarnings("unchecked")
- List<Book> result4 = (List<Book>) q.getResultList();
- assertEquals(2, result4.size());
- assertEquals("12345", result4.get(0).getIsbn());
- q.setFirstResult(1);
- q.setMaxResults(1);
- @SuppressWarnings("unchecked")
- List<Book> result5 = (List<Book>) q.getResultList();
- assertEquals(1, result5.size());
- assertEquals("11111", result5.get(0).getIsbn());
- q.setFirstResult(2);
- q.setMaxResults(5);
- @SuppressWarnings("unchecked")
- List<Book> result6 = (List<Book>) q.getResultList();
- assertEquals(2, result6.size());
- assertEquals("67890", result6.get(0).getIsbn());
- }
- public void testSerialization() throws IOException {
- Query q = em.createQuery("select from " + Book.class.getName() + " b ");
- q.getResultList();
- JPQLQuery innerQuery = (JPQLQuery) ((JPAQuery) q).getInternalQuery();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- // the fact that this doesn't blow up is the test
- oos.writeObject(innerQuery);
- }
- public void testBindVariables() {
- assertQuerySupported("select from " + Book.class.getName() + " b where title = :titleParam",
- Utils.newArrayList(TITLE_EQ_2), NO_SORTS, "titleParam", 2L);
- assertQuerySupported("select from " + Book.class.getName() + " b" +
- " where title = :titleParam AND isbn = :isbnParam",
- Utils.newArrayList(TITLE_EQ_2, ISBN_EQ_4), NO_SORTS, "titleParam", 2L, "isbnParam", 4L);
- assertQuerySupported("select from " + Book.class.getName() + " b" +
- " where title = :titleParam AND isbn = :isbnParam order by title asc, isbn desc",
- Utils.newArrayList(TITLE_EQ_2, ISBN_EQ_4),
- Utils.newArrayList(TITLE_ASC, ISBN_DESC), "titleParam", 2L, "isbnParam", 4L);
- }
- public void testKeyQuery() {
- Entity bookEntity = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity);
- javax.persistence.Query q = em.createQuery(
- "select from " + Book.class.getName() + " b where id = :key");
- q.setParameter("key", KeyFactory.keyToString(bookEntity.getKey()));
- @SuppressWarnings("unchecked")
- List<Book> books = (List<Book>) q.getResultList();
- assertEquals(1, books.size());
- assertEquals(bookEntity.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
- // now issue the same query, but instead of providing a String version of
- // the key, provide the Key itself.
- q.setParameter("key", bookEntity.getKey());
- @SuppressWarnings("unchecked")
- List<Book> books2 = (List<Book>) q.getResultList();
- assertEquals(1, books2.size());
- assertEquals(bookEntity.getKey(), KeyFactory.stringToKey(books2.get(0).getId()));
- }
- public void testKeyQuery_KeyPk() {
- Entity entityWithName = new Entity(HasKeyPkJPA.class.getSimpleName(), "blarg");
- Entity entityWithId = new Entity(HasKeyPkJPA.class.getSimpleName());
- ds.put(entityWithName);
- ds.put(entityWithId);
- Query q = em.createQuery(
- "select from " + HasKeyPkJPA.class.getName() + " b where id = :key");
- q.setParameter("key", entityWithName.getKey());
- HasKeyPkJPA result = (HasKeyPkJPA) q.getSingleResult();
- assertEquals(entityWithName.getKey(), result.getId());
- q = em.createQuery("select from " + HasKeyPkJPA.class.getName() + " b where id = :mykey");
- q.setParameter("mykey", entityWithId.getKey());
- result = (HasKeyPkJPA) q.getSingleResult();
- assertEquals(entityWithId.getKey(), result.getId());
- q = em.createQuery("select from " + HasKeyPkJPA.class.getName() + " b where id = :mykeyname");
- q.setParameter("mykeyname", entityWithName.getKey().getName());
- result = (HasKeyPkJPA) q.getSingleResult();
- assertEquals(entityWithName.getKey(), result.getId());
- q = em.createQuery("select from " + HasKeyPkJPA.class.getName() + " b where id = :mykeyid");
- q.setParameter("mykeyid", entityWithId.getKey().getId());
- result = (HasKeyPkJPA) q.getSingleResult();
- assertEquals(entityWithId.getKey(), result.getId());
- }
- public void testKeyQueryWithSorts() {
- Entity bookEntity = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity);
- javax.persistence.Query q = em.createQuery(
- "select from " + Book.class.getName() + " c where id = :key order by isbn ASC");
- q.setParameter("key", KeyFactory.keyToString(bookEntity.getKey()));
- @SuppressWarnings("unchecked")
- List<Book> books = (List<Book>) q.getResultList();
- assertEquals(1, books.size());
- assertEquals(bookEntity.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
- }
- public void testKeyQuery_MultipleFilters() {
- Entity bookEntity = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity);
- javax.persistence.Query q = em.createQuery(
- "select from " + Book.class.getName() + " c where id = :key and isbn = \"67890\"");
- q.setParameter("key", KeyFactory.keyToString(bookEntity.getKey()));
- @SuppressWarnings("unchecked")
- List<Book> books = (List<Book>) q.getResultList();
- assertEquals(1, books.size());
- assertEquals(bookEntity.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
- }
- public void testKeyQuery_NonEqualityFilter() {
- Entity bookEntity1 = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity1);
- Entity bookEntity2 = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity2);
- javax.persistence.Query q = em.createQuery(
- "select from " + Book.class.getName()
- + " b where id > :key");
- q.setParameter("key", KeyFactory.keyToString(bookEntity1.getKey()));
- @SuppressWarnings("unchecked")
- List<Book> books = (List<Book>) q.getResultList();
- assertEquals(1, books.size());
- assertEquals(bookEntity2.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
- }
- public void testKeyQuery_SortByKey() {
- Entity bookEntity1 = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity1);
- Entity bookEntity2 = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity2);
- javax.persistence.Query q = em.createQuery(
- "select from " + Book.class.getName()
- + " b order by id DESC");
- @SuppressWarnings("unchecked")
- List<Book> books = (List<Book>) q.getResultList();
- assertEquals(2, books.size());
- assertEquals(bookEntity2.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
- }
- public void testKeyQuery_FilterAndSortByKeyComponent() {
- // filter by pk-id
- assertQueryUnsupportedByDatastore(
- "select from " + HasEncodedStringPkSeparateIdFieldJPA.class.getName() + " b where id = 4",
- NucleusFatalUserException.class);
- // sort by pk-id
- assertQueryUnsupportedByDatastore(
- "select from " + HasEncodedStringPkSeparateIdFieldJPA.class.getName() + " b order by id",
- NucleusFatalUserException.class);
- // filter by pk-id
- assertQueryUnsupportedByDatastore(
- "select from " + HasEncodedStringPkSeparateNameFieldJPA.class.getName() + " b where name = 4",
- NucleusFatalUserException.class);
- // sort by pk-id
- assertQueryUnsupportedByDatastore(
- "select from " + HasEncodedStringPkSeparateNameFieldJPA.class.getName() + " b order by name",
- NucleusFatalUserException.class);
- }
- public void testAncestorQuery() {
- Entity bookEntity = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity);
- Entity
- hasAncestorEntity =
- new Entity(HasStringAncestorStringPkJPA.class.getSimpleName(), bookEntity.getKey());
- ds.put(hasAncestorEntity);
- javax.persistence.Query q = em.createQuery(
- "select from " + HasStringAncestorStringPkJPA.class.getName()
- + " b where ancestorId = :ancId");
- q.setParameter("ancId", KeyFactory.keyToString(bookEntity.getKey()));
- @SuppressWarnings("unchecked")
- List<HasStringAncestorStringPkJPA>
- haList =
- (List<HasStringAncestorStringPkJPA>) q.getResultList();
- assertEquals(1, haList.size());
- assertEquals(bookEntity.getKey(), KeyFactory.stringToKey(haList.get(0).getAncestorId()));
- assertEquals(
- bookEntity.getKey(), getDatastoreQuery(q).getLatestDatastoreQuery().getAncestor());
- assertEquals(NO_FILTERS, getFilterPredicates(q));
- assertEquals(NO_SORTS, getSortPredicates(q));
- }
- public void testIllegalAncestorQuery() {
- Entity bookEntity = Book.newBookEntity("Joe Blow", "67890", "Bar Book");
- ds.put(bookEntity);
- Entity
- hasAncestorEntity =
- new Entity(HasStringAncestorStringPkJPA.class.getName(), bookEntity.getKey());
- ds.put(hasAncestorEntity);
- javax.persistence.Query q = em.createQuery(
- "select from " + HasStringAncestorStringPkJPA.class.getName() + " b"
- + " where ancestorId > :ancId");
- q.setParameter("ancId", KeyFactory.keyToString(bookEntity.getKey()));
- q.setHint(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
- try {
- q.getResultList();
- fail("expected udfe");
- } catch (PersistenceException pe) {
- if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
- // good
- }
- else {
- throw pe;
- }
- }
- }
- public void testSortByFieldWithCustomColumn() {
- ds.put(Book.newBookEntity("Joe Blow", "67890", "Bar Book", 2003));
- ds.put(Book.newBookEntity("Joe Blow", "11111", "Bar Book", 2002));
- ds.put(Book.newBookEntity("Joe Blow", "12345", "Foo Book", 2001));
- Query q = em.createQuery("SELECT FROM " +
- Book.class.getName() + " b" +
- " WHERE b.author = 'Joe Blow'" +
- " ORDER BY firstPublished ASC");
- @SuppressWarnings("unchecked")
- List<Book> result = (List<Book>) q.getResultList();
- assertEquals(3, result.size());
- assertEquals("12345", result.get(0).getIsbn());
- assertEquals("11111", result.get(1).getIsbn());
- assertEquals("67890", result.get(2).getIsbn());
- }
- private interface BookProvider {
- Book getBook(Key key);
- }
- private class AttachedBookProvider implements BookProvider {
- public Book getBook(Key key) {
- return em.find(Book.class, key);
- }
- }
- private class TransientBookProvider implements BookProvider {
- public Book getBook(Key key) {
- Book b = new Book();
- b.setId(KeyFactory.keyToString(key));
- return b;
- }
- }
- private void testFilterByChildObject(BookProvider bp) {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bookEntity = Book.newBookEntity(parentEntity.getKey(), "Joe Blow", "11111", "Bar Book", 1929);
- ds.put(bookEntity);
- Book book = bp.getBook(bookEntity.getKey());
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book = :b");
- q.setParameter("b", book);
- List<HasOneToOneJPA> result = (List<HasOneToOneJPA>) q.getResultList();
- assertEquals(1, result.size());
- assertEquals(parentEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- }
- public void testFilterByChildObject() {
- testFilterByChildObject(new AttachedBookProvider());
- testFilterByChildObject(new TransientBookProvider());
- }
- private void testFilterByChildObject_AdditionalFilterOnParent(BookProvider bp) {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bookEntity = Book.newBookEntity(parentEntity.getKey(), "Joe Blow", "11111", "Bar Book", 1929);
- ds.put(bookEntity);
- Book book = bp.getBook(bookEntity.getKey());
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where id = :parentId and book = :b");
- q.setParameter("parentId", KeyFactory.keyToString(bookEntity.getKey()));
- q.setParameter("b", book);
- List<HasOneToOneJPA> result = (List<HasOneToOneJPA>) q.getResultList();
- assertTrue(result.isEmpty());
- q.setParameter("parentId", KeyFactory.keyToString(parentEntity.getKey()));
- q.setParameter("b", book);
- result = (List<HasOneToOneJPA>) q.getResultList();
- assertEquals(1, result.size());
- assertEquals(parentEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- }
- public void testFilterByChildObject_AdditionalFilterOnParent() {
- testFilterByChildObject_AdditionalFilterOnParent(new AttachedBookProvider());
- testFilterByChildObject_AdditionalFilterOnParent(new TransientBookProvider());
- }
- private void testFilterByChildObject_UnsupportedOperator(BookProvider bp) {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bookEntity = Book.newBookEntity(parentEntity.getKey(), "Joe Blow", "11111", "Bar Book", 1929);
- ds.put(bookEntity);
- Book book = bp.getBook(bookEntity.getKey());
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book > :b");
- q.setHint(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
- q.setParameter("b", book);
- try {
- q.getResultList();
- fail("expected udfe");
- } catch (PersistenceException pe) {
- if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
- // good
- }
- else {
- throw pe;
- }
- }
- }
- public void testFilterByChildObject_UnsupportedOperator() {
- testFilterByChildObject_UnsupportedOperator(new AttachedBookProvider());
- testFilterByChildObject_UnsupportedOperator(new TransientBookProvider());
- }
- private void testFilterByChildObject_ValueWithoutAncestor(BookProvider bp) {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bookEntity = Book.newBookEntity("Joe Blow", "11111", "Bar Book", 1929);
- ds.put(bookEntity);
- Book book = bp.getBook(bookEntity.getKey());
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book = :b");
- q.setParameter("b", book);
- q.setHint(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
- try {
- q.getResultList();
- fail("expected JPAException");
- } catch (PersistenceException e) {
- // good
- }
- }
- public void testFilterByChildObject_ValueWithoutAncestor() {
- testFilterByChildObject_ValueWithoutAncestor(new AttachedBookProvider());
- testFilterByChildObject_ValueWithoutAncestor(new TransientBookProvider());
- }
- public void testFilterByChildObject_KeyIsWrongType() {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book = :b");
- q.setParameter("b", parentEntity.getKey());
- q.setHint(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
- try {
- q.getResultList();
- fail("expected JPAException");
- } catch (PersistenceException e) {
- // good
- }
- }
- public void testFilterByChildObject_KeyParentIsWrongType() {
- Key parent = KeyFactory.createKey("yar", 44);
- Entity bookEntity = new Entity(Book.class.getSimpleName(), parent);
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book = :b");
- q.setParameter("b", bookEntity.getKey());
- assertTrue(q.getResultList().isEmpty());
- }
- public void testFilterByChildObject_ValueWithoutId() {
- Entity parentEntity = new Entity(HasOneToOneJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bookEntity = Book.newBookEntity("Joe Blow", "11111", "Bar Book", 1929);
- ds.put(bookEntity);
- Book book = new Book();
- Query q = em.createQuery(
- "select from " + HasOneToOneJPA.class.getName() + " c where book = :b");
- q.setParameter("b", book);
- q.setHint(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
- try {
- q.getResultList();
- fail("expected JPAException");
- } catch (PersistenceException e) {
- // good
- }
- }
- public void testFilterByParentObject() {
- Entity parentEntity = new Entity(HasOneToManyListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bidirEntity =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity);
- Entity bidirEntity2 =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity2);
- HasOneToManyListJPA parent =
- em.find(HasOneToManyListJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
- Query q = em.createQuery("SELECT FROM " +
- BidirectionalChildListJPA.class.getName() +
- " c WHERE parent = :p");
- q.setParameter("p", parent);
- @SuppressWarnings("unchecked")
- List<BidirectionalChildListJPA> result = (List<BidirectionalChildListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(bidirEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(bidirEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByParentLongObjectId() {
- Entity parentEntity = new Entity(HasOneToManyLongPkListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bidirEntity =
- new Entity(BidirectionalChildLongPkListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity);
- Entity bidirEntity2 =
- new Entity(BidirectionalChildLongPkListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity2);
- HasOneToManyLongPkListJPA parent =
- em.find(HasOneToManyLongPkListJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
- Query q = em.createQuery("SELECT FROM " +
- BidirectionalChildLongPkListJPA.class.getName() + " c WHERE parent = :p");
- q.setParameter("p", parent.getId());
- @SuppressWarnings("unchecked")
- List<BidirectionalChildLongPkListJPA> result = (List<BidirectionalChildLongPkListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(bidirEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(bidirEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByParentIntObjectId() {
- Entity parentEntity = new Entity(HasOneToManyLongPkListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity bidirEntity =
- new Entity(BidirectionalChildLongPkListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity);
- Entity bidirEntity2 =
- new Entity(BidirectionalChildLongPkListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity2);
- HasOneToManyLongPkListJPA parent =
- em.find(HasOneToManyLongPkListJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
- Query q = em.createQuery("SELECT FROM " +
- BidirectionalChildLongPkListJPA.class.getName() + " c WHERE parent = :p");
- q.setParameter("p", parent.getId().intValue());
- @SuppressWarnings("unchecked")
- List<BidirectionalChildLongPkListJPA> result = (List<BidirectionalChildLongPkListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(bidirEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(bidirEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByParentObjectWhereParentIsAChild() {
- Entity parentEntity = new Entity(HasOneToManyListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity childEntity = new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(childEntity);
- Entity grandChildEntity1 =
- new Entity(BidirectionalGrandchildListJPA.class.getSimpleName(), childEntity.getKey());
- ds.put(grandChildEntity1);
- Entity grandChildEntity2 =
- new Entity(BidirectionalGrandchildListJPA.class.getSimpleName(), childEntity.getKey());
- ds.put(grandChildEntity2);
- BidirectionalChildListJPA child =
- em.find(BidirectionalChildListJPA.class, KeyFactory.keyToString(childEntity.getKey()));
- Query q = em.createQuery(
- "select from " + BidirectionalGrandchildListJPA.class.getName() + " c where parent = :p");
- q.setParameter("p", child);
- @SuppressWarnings("unchecked")
- List<BidirectionalGrandchildListJPA> result = (List<BidirectionalGrandchildListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(grandChildEntity1.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(grandChildEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByParentId() {
- Entity parentEntity = new Entity(HasOneToManyListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity
- bidirEntity =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity);
- Entity
- bidirEntity2 =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity2);
- HasOneToManyListJPA parent =
- em.find(HasOneToManyListJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
- Query q = em.createQuery("SELECT FROM " +
- BidirectionalChildListJPA.class.getName() +
- " c WHERE parent = :p");
- q.setParameter("p", parent.getId());
- @SuppressWarnings("unchecked")
- List<BidirectionalChildListJPA> result = (List<BidirectionalChildListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(bidirEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(bidirEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByParentKey() {
- Entity parentEntity = new Entity(HasOneToManyListJPA.class.getSimpleName());
- ds.put(parentEntity);
- Entity
- bidirEntity =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity);
- Entity
- bidirEntity2 =
- new Entity(BidirectionalChildListJPA.class.getSimpleName(), parentEntity.getKey());
- ds.put(bidirEntity2);
- Query q = em.createQuery("SELECT FROM " +
- BidirectionalChildListJPA.class.getName() +
- " c WHERE parent = :p");
- q.setParameter("p", parentEntity.getKey());
- @SuppressWarnings("unchecked")
- List<BidirectionalChildListJPA> result = (List<BidirectionalChildListJPA>) q.getResultList();
- assertEquals(2, result.size());
- assertEquals(bidirEntity.getKey(), KeyFactory.stringToKey(result.get(0).getId()));
- assertEquals(bidirEntity2.getKey(), KeyFactory.stringToKey(result.get(1).getId()));
- }
- public void testFilterByMultiValueProperty() {
- Entity entity = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
- entity.setProperty("strList", Utils.newArrayList("1", "2", "3"));
- entity.setProperty("keyList",
- Utils.newArrayList(KeyFactory.createKey("be", "bo"),
- KeyFactory.createKey("bo", "be")));
- ds.put(entity);
- Query q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where strList = :p1 AND strList = :p2");
- q.setParameter("p1", "1");
- q.setParameter("p2", "3");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(1, result.size());
- q.setParameter("p1", "1");
- q.setParameter("p2", "4");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result2 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result2.size());
- q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName() + " c where keyList = :p1 AND keyList = :p2");
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be"));
- assertEquals(1, result.size());
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be2"));
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result3 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result3.size());
- }
- public void testNoPutsAfterLoadingMultiValueProperty() throws NoSuchMethodException {
- switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
- testFilterByMultiValueProperty();
- em.close();
- }
- public void testFilterByMultiValueProperty_MemberOf_ArgsWrongOrder() {
- Entity entity = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
- entity.setProperty("strList", Utils.newArrayList("1", "2", "3"));
- entity.setProperty("keyList",
- Utils.newArrayList(KeyFactory.createKey("be", "bo"),
- KeyFactory.createKey("bo", "be")));
- ds.put(entity);
- Query q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where strList MEMBER OF :p1 AND strList MEMBER OF :p2");
- q.setParameter("p1", "1");
- q.setParameter("p2", "3");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(1, result.size());
- q.setParameter("p1", "1");
- q.setParameter("p2", "4");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result2 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result2.size());
- q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where keyList MEMBER OF :p1 AND keyList MEMBER OF :p2");
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be"));
- result = q.getResultList();
- assertEquals(1, result.size());
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be2"));
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result3 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result3.size());
- }
- public void testFilterByMultiValueProperty_MemberOf_ArgsCorrectOrder() {
- Entity entity = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
- entity.setProperty("strList", Utils.newArrayList("1", "2", "3"));
- entity.setProperty("keyList",
- Utils.newArrayList(KeyFactory.createKey("be", "bo"),
- KeyFactory.createKey("bo", "be")));
- ds.put(entity);
- Query q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where :p1 MEMBER OF strList AND :p2 MEMBER OF strList");
- q.setParameter("p1", "1");
- q.setParameter("p2", "3");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(1, result.size());
- q.setParameter("p1", "1");
- q.setParameter("p2", "4");
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result2 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result2.size());
- q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where :p1 MEMBER OF keyList AND :p2 MEMBER OF keyList");
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be"));
- result = q.getResultList();
- assertEquals(1, result.size());
- q.setParameter("p1", KeyFactory.createKey("be", "bo"));
- q.setParameter("p2", KeyFactory.createKey("bo", "be2"));
- @SuppressWarnings("unchecked")
- List<HasMultiValuePropsJPA> result3 = (List<HasMultiValuePropsJPA>) q.getResultList();
- assertEquals(0, result3.size());
- // try one with a literal value
- q = em.createQuery(
- "select from " + HasMultiValuePropsJPA.class.getName()
- + " c where '1' MEMBER OF strList");
- result = q.getResultList();
- assertEquals(1, result.size());
- }
- public void testFilterByEmbeddedField() {
- Entity entity = new Entity(Person.class.getSimpleName());
- e…