Return-Path: X-Original-To: apmail-deltaspike-commits-archive@www.apache.org Delivered-To: apmail-deltaspike-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1958A10E02 for ; Thu, 27 Jun 2013 08:39:10 +0000 (UTC) Received: (qmail 24806 invoked by uid 500); 27 Jun 2013 08:39:09 -0000 Delivered-To: apmail-deltaspike-commits-archive@deltaspike.apache.org Received: (qmail 24720 invoked by uid 500); 27 Jun 2013 08:39:04 -0000 Mailing-List: contact commits-help@deltaspike.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@deltaspike.apache.org Delivered-To: mailing list commits@deltaspike.apache.org Received: (qmail 24638 invoked by uid 99); 27 Jun 2013 08:39:02 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Jun 2013 08:39:02 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0714E383BF; Thu, 27 Jun 2013 08:39:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: thug@apache.org To: commits@deltaspike.apache.org Date: Thu, 27 Jun 2013 08:39:03 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [03/11] DELTASPIKE-60 Data module initial import http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java new file mode 100644 index 0000000..febb490 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java @@ -0,0 +1,135 @@ +/* + * 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.deltaspike.data.impl.audit; + +import static org.apache.deltaspike.data.test.util.TestDeployments.initDeployment; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.deltaspike.data.api.audit.CurrentUser; +import org.apache.deltaspike.data.impl.audit.AuditEntityListener; +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.AuditedEntity; +import org.apache.deltaspike.data.test.domain.Principal; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ArchivePaths; +import org.junit.Test; + +public class AuditEntityListenerTest extends TransactionalTestCase +{ + + @Deployment + public static Archive deployment() + { + return initDeployment() + .addPackage(AuditEntityListener.class.getPackage()) + .addAsWebInfResource("test-orm.xml", ArchivePaths.create("classes/META-INF/orm.xml")) + .addPackage(AuditedEntity.class.getPackage()); + } + + @PersistenceContext + private EntityManager entityManager; + + private final String who = "test999"; + private final Principal principal = new Principal(who); + + @Produces + @CurrentUser + public String who() + { + return who; + } + + @Produces + @CurrentUser + public Principal entity() throws Exception + { + try + { + entityManager.persist(principal); + } + catch (Throwable e) + { + } + return principal; + } + + @Test + public void should_set_creation_date() throws Exception + { + // given + AuditedEntity entity = new AuditedEntity(); + + // when + entityManager.persist(entity); + entityManager.flush(); + + // then + assertNotNull(entity.getCreated()); + assertNotNull(entity.getModified()); + assertEquals(entity.getCreated().getTime(), entity.getModified()); + } + + @Test + public void should_set_modification_date() throws Exception + { + // given + AuditedEntity entity = new AuditedEntity(); + entityManager.persist(entity); + entityManager.flush(); + + // when + entity = entityManager.find(AuditedEntity.class, entity.getId()); + entity.setName("test"); + entityManager.flush(); + + // then + assertNotNull(entity.getGregorianModified()); + assertNotNull(entity.getTimestamp()); + } + + @Test + public void should_set_changing_principal() + { + // given + AuditedEntity entity = new AuditedEntity(); + + // when + entityManager.persist(entity); + entityManager.flush(); + + // then + assertNotNull(entity.getChanger()); + assertEquals(who, entity.getChanger()); + assertNotNull(entity.getPrincipal()); + assertEquals(who, entity.getPrincipal().getName()); + } + + @Override + protected EntityManager getEntityManager() + { + return entityManager; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/TimestampsProviderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/TimestampsProviderTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/TimestampsProviderTest.java new file mode 100644 index 0000000..baf4f0b --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/TimestampsProviderTest.java @@ -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.deltaspike.data.impl.audit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import org.apache.deltaspike.data.api.audit.CreatedOn; +import org.apache.deltaspike.data.impl.audit.AuditPropertyException; +import org.apache.deltaspike.data.impl.audit.TimestampsProvider; +import org.apache.deltaspike.data.test.domain.AuditedEntity; +import org.apache.deltaspike.data.test.domain.Simple; +import org.junit.Test; + +public class TimestampsProviderTest +{ + + @Test + public void should_set_dates_for_creation() + { + // given + AuditedEntity entity = new AuditedEntity(); + + // when + new TimestampsProvider().prePersist(entity); + + // then + assertNotNull(entity.getCreated()); + assertNotNull(entity.getModified()); + assertNull(entity.getGregorianModified()); + assertNull(entity.getTimestamp()); + } + + @Test + public void should_set_dates_for_update() + { + // given + AuditedEntity entity = new AuditedEntity(); + + // when + new TimestampsProvider().preUpdate(entity); + + // then + assertNull(entity.getCreated()); + assertNotNull(entity.getModified()); + assertNotNull(entity.getGregorianModified()); + assertNotNull(entity.getTimestamp()); + } + + @Test + public void should_not_fail_on_non_audited_entity() + { + // given + Simple entity = new Simple(); + + // when + TimestampsProvider provider = new TimestampsProvider(); + provider.prePersist(entity); + provider.preUpdate(entity); + + // then finish the test + } + + @Test(expected = AuditPropertyException.class) + public void should_fail_on_invalid_entity() + { + // given + InvalidEntity entity = new InvalidEntity(); + + // when + new TimestampsProvider().prePersist(entity); + + // then + fail(); + } + + private static class InvalidEntity + { + + @CreatedOn + private String nonTemporal; + + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java new file mode 100644 index 0000000..0f66c00 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java @@ -0,0 +1,141 @@ +/* + * 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.deltaspike.data.impl.builder.part; + +import static org.junit.Assert.assertEquals; + +import org.apache.deltaspike.data.impl.builder.MethodExpressionException; +import org.apache.deltaspike.data.impl.meta.RepositoryComponent; +import org.apache.deltaspike.data.impl.meta.RepositoryEntity; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.service.SimpleFetchRepository; +import org.apache.deltaspike.data.test.service.SimpleRepository; +import org.junit.Test; + +/** + * + * @author thomashug + */ +public class QueryRootTest +{ + + private final RepositoryComponent repo = new RepositoryComponent(SimpleRepository.class, new RepositoryEntity(Simple.class, Long.class)); + private final RepositoryComponent repoFetchBy = new RepositoryComponent(SimpleFetchRepository.class, new RepositoryEntity(Simple.class, Long.class)); + + @Test + public void should_create_simple_query() + { + // given + final String name = "findByName"; + final String expected = + "select e from Simple e " + + "where e.name = ?1"; + + // when + String result = QueryRoot.create(name, repo).getJpqlQuery().trim(); + + // then + assertEquals(expected, result); + } + + @Test + public void should_create_complex_query() + { + // given + final String name = "findByNameAndTemporalBetweenOrEnabledIsNull" + + "AndCamelCaseAndEmbedded_embeddNotEqualOrderByEmbedded_embeddDesc"; + final String expected = + "select e from Simple e " + + "where e.name = ?1 " + + "and e.temporal between ?2 and ?3 " + + "or e.enabled IS NULL " + + "and e.camelCase = ?4 " + + "and e.embedded.embedd <> ?5 " + + "order by e.embedded.embedd desc"; + + // when + String result = QueryRoot.create(name, repo).getJpqlQuery().trim(); + + // then + assertEquals(expected, result); + } + + @Test + public void should_create_query_with_order_by_only() + { + // given + final String name = "findByOrderByIdAsc"; + final String expected = + "select e from Simple e " + + "order by e.id asc"; + + // when + String result = QueryRoot.create(name, repo).getJpqlQuery().trim(); + + // then + assertEquals(expected, result); + } + + @Test(expected = MethodExpressionException.class) + public void should_fail_in_where() + { + // given + final String name = "findByInvalid"; + + // when + QueryRoot.create(name, repo); + } + + @Test(expected = MethodExpressionException.class) + public void should_fail_with_prefix_only() + { + // given + final String name = "findBy"; + + // when + QueryRoot.create(name, repo); + } + + @Test(expected = MethodExpressionException.class) + public void should_fail_in_order_by() + { + // given + final String name = "findByNameOrderByInvalidDesc"; + + // when + QueryRoot.create(name, repo); + } + + @Test + public void should_use_alternative_prefix() + { + // given + final String name = "fetchByName"; + final String expected = + "select e from Simple e " + + "where e.name = ?1"; + + // when + String result = QueryRoot.create(name, repoFetchBy).getJpqlQuery().trim(); + + // then + assertEquals(expected, result); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java new file mode 100644 index 0000000..65ad856 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java @@ -0,0 +1,308 @@ +/* + * 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.deltaspike.data.impl.criteria; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.OneToMany; +import org.apache.deltaspike.data.test.domain.OneToOne; +import org.apache.deltaspike.data.test.domain.Parent; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.service.ParentRepository; +import org.apache.deltaspike.data.test.service.SimpleCriteriaRepository; +import org.apache.deltaspike.data.test.service.Statistics; +import org.apache.deltaspike.data.test.util.TestDeployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.Archive; +import org.junit.Test; + +/** + * + * @author thomashug + */ +public class CriteriaTest extends TransactionalTestCase +{ + + @Deployment + public static Archive deployment() + { + return TestDeployments.initDeployment() + .addClasses(SimpleCriteriaRepository.class, ParentRepository.class, Statistics.class) + .addPackage(Simple.class.getPackage()); + } + + @Inject + private SimpleCriteriaRepository repo; + + @Inject + private ParentRepository parentRepo; + + @Produces + @PersistenceContext + private EntityManager entityManager; + + @Test + public void should_create_criteria_query() + { + // given + final String name = "testCreateCriteriaQuery"; + createSimple(name, 55); + + // when + List result1 = repo.queryByCriteria(name, Boolean.TRUE, 0, 50); + List result2 = repo.queryByCriteria(name, Boolean.TRUE, 50, 100); + List result3 = repo.queryByCriteria(name, Boolean.FALSE, 50, 100); + + // then + assertEquals(0, result1.size()); + assertEquals(1, result2.size()); + assertEquals(0, result3.size()); + } + + @Test + public void should_create_join_criteria_query() + { + // given + final String name = "testCreateJoinCriteriaQuery"; + final String nameOne = name + "-one"; + final String nameMany = name + "-many"; + Parent parent = new Parent(name); + parent.setOne(new OneToOne(nameOne)); + parent.add(new OneToMany(nameMany)); + + entityManager.persist(parent); + entityManager.flush(); + + // when + List result = parentRepo.joinQuery(name, nameOne, nameMany); + + // then + assertEquals(1, result.size()); + assertNotNull(result.get(0)); + + Parent queried = result.get(0); + assertEquals(name, queried.getName()); + assertNotNull(queried.getOne()); + assertEquals(nameOne, queried.getOne().getName()); + assertEquals(1, queried.getMany().size()); + assertEquals(nameMany, queried.getMany().get(0).getName()); + } + + @Test + public void should_create_or_query() + { + // given + final String name = "testCreateOrQuery"; + Parent parent1 = new Parent(name + "1"); + parent1.setValue(25L); + Parent parent2 = new Parent(name + "2"); + parent2.setValue(75L); + Parent parent3 = new Parent(name + "3"); + parent3.setValue(25L); + Parent parent4 = new Parent(name + "1"); + parent4.setValue(75L); + + entityManager.persist(parent1); + entityManager.persist(parent2); + entityManager.persist(parent3); + entityManager.persist(parent4); + entityManager.flush(); + + // when + List result = parentRepo.orQuery(name + "1", name + "2"); + + // then + assertEquals(2, result.size()); + } + + @Test + public void should_create_ordered_query() + { + // given + final String name = "testCreateOrderedQuery"; + Parent parent1 = new Parent(name + "99"); + Parent parent2 = new Parent(name + "12"); + Parent parent3 = new Parent(name + "19"); + Parent parent4 = new Parent(name + "02"); + + entityManager.persist(parent1); + entityManager.persist(parent2); + entityManager.persist(parent3); + entityManager.persist(parent4); + entityManager.flush(); + + // when + List result = parentRepo.orderedQuery(); + + // then + assertEquals(4, result.size()); + assertEquals(name + "02", result.get(0).getName()); + assertEquals(name + "12", result.get(1).getName()); + assertEquals(name + "19", result.get(2).getName()); + assertEquals(name + "99", result.get(3).getName()); + } + + @Test + public void should_create_query_wihtout_nulls() + { + // given + final String name = "testCreateQueryWihtoutNulls"; + Parent parent = new Parent(name); + + entityManager.persist(parent); + entityManager.flush(); + + // when + List result = parentRepo.nullAwareQuery(name, null, null); + + // then + assertEquals(1, result.size()); + assertEquals(name, result.get(0).getName()); + } + + @Test + public void should_create_fetch_query() + { + // given + final String name = "testCreateFetchQuery"; + Parent parent = new Parent(name); + parent.add(new OneToMany(name + "-1")); + parent.add(new OneToMany(name + "-2")); + + entityManager.persist(parent); + entityManager.flush(); + + // when + Parent result = parentRepo.fetchQuery(name); + + // then + assertNotNull(result); + assertEquals(name, result.getName()); + assertNotNull(result.getMany()); + assertEquals(2, result.getMany().size()); + } + + @Test + public void should_create_in_query() + { + // given + final String name = "testCreateInQuery"; + Parent parent1 = new Parent(name + "-1"); + Parent parent2 = new Parent(name + "-2"); + Parent parent3 = new Parent(name + "-3"); + + entityManager.persist(parent1); + entityManager.persist(parent2); + entityManager.persist(parent3); + entityManager.flush(); + + // when + List result = parentRepo.fetchByName(name + "-1", name + "-2", name + "-3"); + + // then + assertNotNull(result); + assertEquals(3, result.size()); + } + + @Test + public void should_create_select_criteria_with_result_type() + { + // given + final String name = "testCreateSelectCriteriaWithResultType"; + createSimple(name, 1); + createSimple(name, 2); + createSimple(name, 3); + createSimple(name, 4); + createSimple(name, 99); + + // when + Statistics result = repo.queryWithSelect(name); + + // then + assertNotNull(result.getAverage()); + assertEquals(Long.valueOf(5l), result.getCount()); + } + + @Test + public void should_create_select_criteria_without_result_type() + { + // given + final String name = "testCreateSelectCriteriaWithoutResultType"; + createSimple(name, 10); + createSimple(name, 99); + + // when + Object[] result = repo.queryWithSelectAggregateReturnArray(name); + + // then + assertEquals(Integer.valueOf(10), result[0]); + assertEquals(Integer.valueOf(99), result[1]); + assertTrue(result[2] instanceof java.sql.Date); + assertTrue(result[3] instanceof java.sql.Time); + assertTrue(result[4] instanceof java.sql.Timestamp); + } + + @Test + public void should_create_select_criteria_with_attributes() + { + // given + final String name = "testCreateSelectCriteriaWithAttributes"; + createSimple(name, 10); + createSimple(name, 99); + + // when + List results = repo.queryWithSelectAttributes(name); + + // then + for (Object[] result : results) + { + assertEquals(name, result[0]); + assertEquals(name.toUpperCase(), result[1]); + assertEquals(name.toLowerCase(), result[2]); + assertEquals(name.substring(1), result[3]); + assertEquals(name.substring(1, 1 + 2), result[4]); + } + } + + @Override + protected EntityManager getEntityManager() + { + return entityManager; + } + + private Simple createSimple(String name, Integer counter) + { + Simple result = new Simple(name); + result.setCounter(counter); + entityManager.persist(result); + entityManager.flush(); + return result; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java new file mode 100644 index 0000000..6f40ac4 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java @@ -0,0 +1,93 @@ +/* + * 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.deltaspike.data.impl.handler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import javax.inject.Inject; + +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.service.SimpleRepositoryWithEntityManager; +import org.apache.deltaspike.data.test.service.SimpleRepositoryWithOverriddenEntityManager; +import org.apache.deltaspike.data.test.service.Simplistic; +import org.apache.deltaspike.data.test.service.SimplisticEntityManagerResolver; +import org.apache.deltaspike.data.test.util.TestDeployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +public class EntityManagerTest +{ + + @Deployment + public static Archive deployment() + { + return TestDeployments.initDeployment() + .addClasses(SimpleRepositoryWithEntityManager.class, + SimpleRepositoryWithOverriddenEntityManager.class, + EntityManagerTestProducer.class, + Simplistic.class, SimplisticEntityManagerResolver.class); + } + + @Inject + private SimpleRepositoryWithEntityManager repoWithAnnotation; + + @Inject + private SimpleRepositoryWithOverriddenEntityManager repoWithInjection; + + @Test + public void should_use_qualified_entity_manager() + { + // when + List result = repoWithAnnotation.findByName("testUseQualifiedEntityManager"); + + // then + assertNotNull(result); + assertEquals(0, result.size()); + } + + @Test + public void should_use_injected_entity_manager() + { + // when + List result = repoWithInjection.findByName("testUseInjectedEntityManager"); + + // then + assertNotNull(result); + assertEquals(0, result.size()); + } + + @Test + public void should_inject_entity_manager() + { + // when + List result = repoWithInjection.findWithEm("testInjectEntityManager"); + + // then + assertNotNull(result); + assertEquals(0, result.size()); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTestProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTestProducer.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTestProducer.java new file mode 100644 index 0000000..87aeda4 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTestProducer.java @@ -0,0 +1,35 @@ +/* + * 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.deltaspike.data.impl.handler; + +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.deltaspike.data.test.service.Simplistic; + +public class EntityManagerTestProducer +{ + + @Produces + @Simplistic + @PersistenceContext + private EntityManager entityManager; + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java new file mode 100644 index 0000000..79c34c0 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java @@ -0,0 +1,359 @@ +/* + * 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.deltaspike.data.impl.handler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; + +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.metamodel.SingularAttribute; + +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.domain.Simple_; +import org.apache.deltaspike.data.test.service.ExtendedRepositoryInterface; +import org.apache.deltaspike.data.test.util.TestDeployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.Archive; +import org.junit.Test; + +public class EntityRepositoryHandlerTest extends TransactionalTestCase +{ + + @Deployment + public static Archive deployment() + { + return TestDeployments.initDeployment() + .addClasses(ExtendedRepositoryInterface.class) + .addPackage(Simple.class.getPackage()); + } + + @Inject + private ExtendedRepositoryInterface repo; + + @Produces + @PersistenceContext + private EntityManager entityManager; + + @Test + public void should_save() throws Exception + { + // given + Simple simple = new Simple("test"); + + // when + simple = repo.save(simple); + + // then + assertNotNull(simple.getId()); + } + + @Test + public void should_merge() throws Exception + { + // given + Simple simple = createSimple("testMerge"); + Long id = simple.getId(); + + // when + final String newName = "testMergeUpdated"; + simple.setName(newName); + simple = repo.save(simple); + + // then + assertEquals(id, simple.getId()); + assertEquals(newName, simple.getName()); + } + + @Test + public void should_save_and_flush() throws Exception + { + // given + Simple simple = new Simple("test"); + + // when + simple = repo.saveAndFlush(simple); + Simple fetch = (Simple) entityManager + .createNativeQuery("select * from simple_table where id = ?", Simple.class) + .setParameter(1, simple.getId()) + .getSingleResult(); + + // then + assertEquals(simple.getId(), fetch.getId()); + } + + @Test + public void should_refresh() throws Exception + { + // given + final String name = "testRefresh"; + Simple simple = createSimple(name); + + // when + simple.setName("override"); + repo.refresh(simple); + + // then + assertEquals(name, simple.getName()); + } + + @Test + public void should_find_by_pk() throws Exception + { + // given + Simple simple = createSimple("testFindByPk"); + + // when + Simple find = repo.findBy(simple.getId()); + + // then + assertEquals(simple.getName(), find.getName()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_find_by_example() throws Exception + { + // given + Simple simple = createSimple("testFindByExample"); + + // when + List find = repo.findBy(simple, Simple_.name); + + // then + assertNotNull(find); + assertFalse(find.isEmpty()); + assertEquals(simple.getName(), find.get(0).getName()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_find_by_example_with_start_and_max() throws Exception + { + // given + Simple simple = createSimple("testFindByExample1", Integer.valueOf(10)); + createSimple("testFindByExample1", Integer.valueOf(10)); + + // when + List find = repo.findBy(simple, 0, 1, Simple_.name, Simple_.counter); + + // then + assertNotNull(find); + assertFalse(find.isEmpty()); + assertEquals(1, find.size()); + assertEquals(simple.getName(), find.get(0).getName()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_find_by_example_with_no_attributes() throws Exception + { + // given + Simple simple = createSimple("testFindByExample"); + SingularAttribute[] attributes = new SingularAttribute[] {}; + + // when + List find = repo.findBy(simple, attributes); + + // then + assertNotNull(find); + assertFalse(find.isEmpty()); + assertEquals(simple.getName(), find.get(0).getName()); + } + + @Test + public void should_find_all() + { + // given + createSimple("testFindAll1"); + createSimple("testFindAll2"); + + // when + List find = repo.findAll(); + + // then + assertEquals(2, find.size()); + } + + @Test + public void should_find_by_all_with_start_and_max() + { + // given + createSimple("testFindAll1"); + createSimple("testFindAll2"); + + // when + List find = repo.findAll(0, 1); + + // then + assertEquals(1, find.size()); + } + + @Test + @SuppressWarnings({ "unchecked" }) + public void should_find_by_like() + { + // given + createSimple("testFindAll1"); + createSimple("testFindAll2"); + Simple example = new Simple("test"); + + // when + List find = repo.findByLike(example, Simple_.name); + + // then + assertEquals(2, find.size()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_find_by_like_with_start_and_max() + { + // given + createSimple("testFindAll1"); + createSimple("testFindAll2"); + Simple example = new Simple("test"); + + // when + List find = repo.findByLike(example, 1, 10, Simple_.name); + + // then + assertEquals(1, find.size()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_find_by_like_non_string() + { + // given + createSimple("testFindAll1", 1); + createSimple("testFindAll2", 2); + Simple example = new Simple("test"); + example.setCounter(1); + + // when + List find = repo.findByLike(example, Simple_.name, Simple_.counter); + + // then + assertEquals(1, find.size()); + } + + @Test + public void should_count_all() + { + // given + createSimple("testCountAll"); + + // when + Long result = repo.count(); + + // then + assertEquals(Long.valueOf(1), result); + } + + @Test + @SuppressWarnings("unchecked") + public void should_count_with_attributes() + { + // given + Simple simple = createSimple("testFindAll1", Integer.valueOf(55)); + createSimple("testFindAll2", Integer.valueOf(55)); + + // when + Long result = repo.count(simple, Simple_.name, Simple_.counter); + + // then + assertEquals(Long.valueOf(1), result); + } + + @Test + @SuppressWarnings("unchecked") + public void should_count_with_no_attributes() + { + // given + Simple simple = createSimple("testFindAll1"); + createSimple("testFindAll2"); + SingularAttribute[] attributes = new SingularAttribute[] {}; + + // when + Long result = repo.count(simple, attributes); + + // then + assertEquals(Long.valueOf(2), result); + } + + @Test + @SuppressWarnings("unchecked") + public void should_count_by_like() + { + // given + createSimple("testFindAll1"); + createSimple("testFindAll2"); + Simple example = new Simple("test"); + + // when + Long count = repo.countLike(example, Simple_.name); + + // then + assertEquals(Long.valueOf(2), count); + } + + @Test + public void should_remove() + { + // given + Simple simple = createSimple("testRemove"); + + // when + repo.remove(simple); + repo.flush(); + Simple lookup = entityManager.find(Simple.class, simple.getId()); + + // then + assertNull(lookup); + } + + @Override + protected EntityManager getEntityManager() + { + return entityManager; + } + + private Simple createSimple(String name) + { + return createSimple(name, null); + } + + private Simple createSimple(String name, Integer counter) + { + Simple result = new Simple(name); + result.setCounter(counter); + entityManager.persist(result); + entityManager.flush(); + return result; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java new file mode 100644 index 0000000..5aa48e3 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java @@ -0,0 +1,294 @@ +/* + * 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.deltaspike.data.impl.handler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.domain.Simple2; +import org.apache.deltaspike.data.test.domain.SimpleBuilder; +import org.apache.deltaspike.data.test.service.Simple2Repository; +import org.apache.deltaspike.data.test.service.SimpleRepository; +import org.apache.deltaspike.data.test.util.TestDeployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.Archive; +import org.junit.Before; +import org.junit.Test; + +public class QueryHandlerTest extends TransactionalTestCase +{ + + @Deployment + public static Archive deployment() + { + return TestDeployments.initDeployment() + .addClasses(SimpleRepository.class, Simple2Repository.class) + .addPackage(Simple.class.getPackage()); + } + + @Inject + private SimpleRepository repo; + + @Inject + private Simple2Repository repo2; + + @Produces + @PersistenceContext + private EntityManager entityManager; + + private SimpleBuilder builder; + + @Test + public void should_delegate_to_implementation() + { + // given + final String name = "testDelegateToImplementation"; + builder.createSimple(name); + + // when + List result = repo.implementedQueryByName(name); + + // then + assertNotNull(result); + assertEquals(1, result.size()); + } + + @Test + public void should_create_named_query_index() + { + // given + final String name = "testCreateNamedQueryIndex"; + builder.createSimple(name); + + // when + List result = repo.findByNamedQueryIndexed(name, Boolean.TRUE); + + // then + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals(name, result.get(0).getName()); + } + + @Test + public void should_create_named_query_named() + { + // given + final String name = "testCreateNamedQueryNamed"; + Simple simple = builder.createSimple(name); + + // when + Simple result = repo.findByNamedQueryNamed(simple.getId(), Boolean.TRUE); + + // then + assertNotNull(result); + assertEquals(name, result.getName()); + } + + @Test + public void should_run_annotated_query() + { + // given + final String name = "testRunAnnotatedQuery"; + builder.createSimple(name); + + // when + Simple result = repo.findByQuery(name); + + // then + assertNotNull(result); + assertEquals(name, result.getName()); + } + + @Test + public void should_create_query_by_method_name() + { + // given + final String name = "testCreateQueryByMethodName"; + builder.createSimple(name); + + // when + Simple result = repo.findByNameAndEnabled(name, Boolean.TRUE); + + // then + assertNotNull(result); + assertEquals(name, result.getName()); + } + + @Test + public void should_restrict_result_size_by_annotation() + { + // given + final String name = "testRestrictResultSizeByAnnotation"; + builder.createSimple(name); + builder.createSimple(name); + + // when + List result = repo.findByNamedQueryIndexed(name, Boolean.TRUE); + + // then + assertNotNull(result); + assertEquals(1, result.size()); + } + + @Test + public void should_restrict_result_size_by_parameters() + { + // given + final String name = "testRestrictResultSizeByParameters"; + builder.createSimple(name); + Simple second = builder.createSimple(name); + + // when + List result = repo.findByNamedQueryRestricted(name, Boolean.TRUE, 1, 1); + + // then + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals(second.getId(), result.get(0).getId()); + } + + @Test + public void should_work_with_2nd_repo() + { + // given + final String name = "testWorkWith2ndRepository"; + Simple2 simple = createSimple2(name); + + // when + Simple2 result = repo2.findByName(name); + + // then + assertNotNull(result); + assertEquals(simple.getId(), result.getId()); + assertEquals(name, result.getName()); + } + + @Test + public void should_return_aggregate() + { + // given + final String name = "testReturnAggregate"; + builder.createSimple(name); + + // when + Long result = repo.findCountByQuery(name); + + // then + assertNotNull(result); + } + + @Test + public void should_find_with_native_query() + { + // given + final String name = "testFindWithNativeQuery"; + builder.createSimple(name); + builder.createSimple(name); + + // when + List result = repo.findWithNative(name); + + // then + assertNotNull(result); + assertEquals(2, result.size()); + } + + @Test + public void should_order_result_by_method_order_by() + { + // given + final String name = "testFindWithNativeQuery"; + builder.createSimple(name, Integer.valueOf(33)); + builder.createSimple(name, Integer.valueOf(66)); + builder.createSimple(name, Integer.valueOf(66)); + builder.createSimple(name, Integer.valueOf(22)); + builder.createSimple(name, Integer.valueOf(55)); + + // when + List result = repo.findByOrderByCounterAscIdDesc(); + + // then + assertNotNull(result); + assertFalse(result.isEmpty()); + long lastId = Long.MAX_VALUE; + int lastCounter = Integer.MIN_VALUE; + for (Simple simple : result) + { + long currentId = simple.getId().longValue(); + int currentCounter = simple.getCounter().intValue(); + if (currentCounter == lastCounter) + { + assertTrue(currentId < lastId); + } + else + { + assertTrue(currentCounter > lastCounter); + } + lastId = currentId; + lastCounter = currentCounter; + } + } + + @Test + public void should_execute_update() + { + // given + final String name = "testFindWithNativeQuery"; + final String newName = "testFindWithNativeQueryUpdated" + System.currentTimeMillis(); + Simple s = builder.createSimple(name); + + // when + int count = repo.updateNameForId(newName, s.getId()); + + // then + assertEquals(1, count); + } + + @Before + public void setup() + { + builder = new SimpleBuilder(entityManager); + } + + @Override + protected EntityManager getEntityManager() + { + return entityManager; + } + + private Simple2 createSimple2(String name) + { + Simple2 result = new Simple2(name); + entityManager.persist(result); + entityManager.flush(); + return result; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/AnnotationMetadataExtractorTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/AnnotationMetadataExtractorTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/AnnotationMetadataExtractorTest.java new file mode 100644 index 0000000..99aa24f --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/AnnotationMetadataExtractorTest.java @@ -0,0 +1,84 @@ +/* + * 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.deltaspike.data.impl.meta.extractor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.deltaspike.data.api.Repository; +import org.apache.deltaspike.data.impl.meta.RepositoryEntity; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.service.RepositoryInterface; +import org.junit.Test; + +public class AnnotationMetadataExtractorTest +{ + + @Test + public void should_extract_entity_class_from_repo_annotation() + { + // given + AnnotationMetadataExtractor extractor = new AnnotationMetadataExtractor(); + + // when + RepositoryEntity result = extractor.extract(RepositoryInterface.class); + + // then + assertNotNull(result); + assertEquals(Simple.class, result.getEntityClass()); + assertEquals(Long.class, result.getPrimaryClass()); + } + + @Test + public void should_throw_excption_when_annotation_with_entity_class_not_present() + { + // given + AnnotationMetadataExtractor extractor = new AnnotationMetadataExtractor(); + + // when + RepositoryEntity result = extractor.extract(NoEntityPresentRepository.class); + + // then + assertNull(result); + } + + @Test + public void should_throw_exception_when_annotation_with_non_entity_class() + { + // given + AnnotationMetadataExtractor extractor = new AnnotationMetadataExtractor(); + + // when + RepositoryEntity result = extractor.extract(NonEntityRepository.class); + + // then + assertNull(result); + } + + @Repository + private static class NoEntityPresentRepository + { + } + + @Repository(forEntity = Object.class) + private static class NonEntityRepository + { + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractorTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractorTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractorTest.java new file mode 100644 index 0000000..af2fcda --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractorTest.java @@ -0,0 +1,64 @@ +/* + * 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.deltaspike.data.impl.meta.extractor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.deltaspike.data.impl.meta.RepositoryEntity; +import org.apache.deltaspike.data.impl.meta.extractor.MetadataExtractor; +import org.apache.deltaspike.data.impl.meta.extractor.TypeMetadataExtractor; +import org.apache.deltaspike.data.test.domain.Simple; +import org.apache.deltaspike.data.test.service.RepositoryInterface; +import org.apache.deltaspike.data.test.service.SimpleRepository; +import org.junit.Test; + +public class TypeMetadataExtractorTest +{ + + @Test + public void should_extract_from_class() + { + // given + MetadataExtractor extractor = new TypeMetadataExtractor(); + + // when + RepositoryEntity result = extractor.extract(SimpleRepository.class); + + // then + assertNotNull(result); + assertEquals(Simple.class, result.getEntityClass()); + assertEquals(Long.class, result.getPrimaryClass()); + } + + @Test + public void should_not_extract_from_annotation() + { + // given + MetadataExtractor extractor = new TypeMetadataExtractor(); + + // when + RepositoryEntity result = extractor.extract(RepositoryInterface.class); + + // then + assertNull(result); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java new file mode 100644 index 0000000..9116ba8 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java @@ -0,0 +1,99 @@ +/* + * 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.deltaspike.data.impl.meta.unit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.deltaspike.data.impl.meta.unit.DescriptorHierarchyBuilder; +import org.apache.deltaspike.data.impl.meta.unit.EntityDescriptor; +import org.apache.deltaspike.data.impl.meta.unit.MappedSuperclassDescriptor; +import org.junit.Before; +import org.junit.Test; + +public class DescriptorHierarchyBuilderTest +{ + + private final List entities = new LinkedList(); + private final List superClasses = new LinkedList(); + + @Before + public void before() + { + entities.add(new EntityDescriptor("test", null, EntityLevel3.class.getName(), null, null)); + entities.add(new EntityDescriptor("test", null, EntityLevel5.class.getName(), null, null)); + + superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel1.class.getName(), null, "id")); + superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel4.class.getName(), null, null)); + superClasses.add(new MappedSuperclassDescriptor("test", null, MappedUnrelated.class.getName(), null, null)); + superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel2.class.getName(), null, null)); + } + + @Test + public void should_build_hierarchy() + { + // given + DescriptorHierarchyBuilder builder = DescriptorHierarchyBuilder.newInstance(entities, superClasses); + + // when + builder.buildHierarchy(); + + // then + assertEquals(entities.size(), 2); + assertEquals(EntityLevel3.class, entities.get(0).getEntityClass()); + assertEquals(EntityLevel5.class, entities.get(1).getEntityClass()); + assertEquals(MappedLevel2.class, entities.get(0).getParent().getEntityClass()); + assertEquals(MappedLevel4.class, entities.get(1).getParent().getEntityClass()); + + assertEquals(superClasses.size(), 4); + assertNull(superClasses.get(0).getParent()); + assertEquals(EntityLevel3.class, superClasses.get(1).getParent().getEntityClass()); + assertNull(superClasses.get(2).getParent()); + assertEquals(MappedLevel1.class, superClasses.get(3).getParent().getEntityClass()); + } + + private static class MappedLevel1 + { + @SuppressWarnings("unused") + private Long id; + } + + private static class MappedLevel2 extends MappedLevel1 + { + } + + private static class EntityLevel3 extends MappedLevel2 + { + } + + private static class MappedLevel4 extends EntityLevel3 + { + } + + private static class EntityLevel5 extends MappedLevel4 + { + } + + private static class MappedUnrelated + { + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/OrmXmlBasedRepositoryTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/OrmXmlBasedRepositoryTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/OrmXmlBasedRepositoryTest.java new file mode 100644 index 0000000..3da8343 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/OrmXmlBasedRepositoryTest.java @@ -0,0 +1,94 @@ +/* + * 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.deltaspike.data.impl.meta.unit; + +import static org.junit.Assert.assertEquals; + +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.mapped.MappedOne; +import org.apache.deltaspike.data.test.service.MappedOneRepository; +import org.apache.deltaspike.data.test.util.TestDeployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ArchivePaths; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; + +public class OrmXmlBasedRepositoryTest extends TransactionalTestCase +{ + + @Deployment + public static Archive deployment() + { + return TestDeployments + .initDeployment("(.*mapped.*)|(.*test.*)") + .addClasses(MappedOneRepository.class) + .addAsLibraries( + ShrinkWrap.create(JavaArchive.class, "domain.jar") + .addPackage(MappedOne.class.getPackage()) + .addAsResource("test-custom-orm.xml", ArchivePaths.create("META-INF/custom-orm.xml")) + ) + .addAsWebInfResource("test-mapped-persistence.xml", + ArchivePaths.create("classes/META-INF/persistence.xml")) + .addAsWebInfResource("test-default-orm.xml", ArchivePaths.create("classes/META-INF/orm.xml")); + } + + @Produces + @PersistenceContext + private EntityManager entityManager; + + @Inject + private MappedOneRepository mappedOneRepository; + + @Test + public void should_find_by() + { + // given + MappedOne one = createMappedOne("shouldFindBy"); + + // when + MappedOne byPk = mappedOneRepository.findBy(one.getId()); + MappedOne byName = mappedOneRepository.findByName("shouldFindBy"); + + // then + assertEquals(one.getId(), byPk.getId()); + assertEquals(one.getId(), byName.getId()); + } + + @Override + protected EntityManager getEntityManager() + { + return entityManager; + } + + private MappedOne createMappedOne(String name) + { + MappedOne result = new MappedOne(name); + entityManager.persist(result); + entityManager.flush(); + return result; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java new file mode 100644 index 0000000..40da703 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java @@ -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.deltaspike.data.impl.meta.unit; + +import static org.apache.deltaspike.data.test.util.TestDeployments.TEST_FILTER; +import static org.apache.deltaspike.data.test.util.TestDeployments.addDependencies; +import static org.apache.deltaspike.data.test.util.TestDeployments.createApiArchive; +import static org.apache.deltaspike.data.test.util.TestDeployments.createImplPackages; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.apache.deltaspike.data.impl.RepositoryDefinitionException; +import org.apache.deltaspike.data.impl.RepositoryExtension; +import org.apache.deltaspike.data.impl.meta.RepositoryEntity; +import org.apache.deltaspike.data.test.TransactionalTestCase; +import org.apache.deltaspike.data.test.domain.Parent; +import org.apache.deltaspike.data.test.domain.TeeId; +import org.apache.deltaspike.data.test.domain.mapped.MappedOne; +import org.apache.deltaspike.data.test.domain.mapped.MappedThree; +import org.apache.deltaspike.data.test.domain.mapped.MappedTwo; +import org.apache.deltaspike.data.test.service.MappedOneRepository; +import org.apache.deltaspike.data.test.util.Logging; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ArchivePaths; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +public class PersistenceUnitsTest +{ + + @Deployment + public static Archive deployment() + { + Logging.reconfigure(); + return addDependencies(ShrinkWrap + .create(WebArchive.class, "test.war") + .addAsLibrary(createApiArchive()) + .addPackages(true, TEST_FILTER, createImplPackages()) + .addPackages(true, Parent.class.getPackage()) + .addClasses(RepositoryExtension.class, RepositoryDefinitionException.class, + TransactionalTestCase.class, MappedOneRepository.class) + .addAsWebInfResource("test-mapped-persistence.xml", + ArchivePaths.create("classes/META-INF/persistence.xml")) + .addAsWebInfResource("test-default-orm.xml", ArchivePaths.create("classes/META-INF/orm.xml")) + .addAsWebInfResource("test-custom-orm.xml", ArchivePaths.create("classes/META-INF/custom-orm.xml")) + .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml")) + .addAsWebInfResource("META-INF/services/javax.enterprise.inject.spi.Extension", + ArchivePaths.create("classes/META-INF/services/javax.enterprise.inject.spi.Extension"))); + } + + @Test + public void should_recognize_entity_data() + { + // given + + // when + boolean positive1 = PersistenceUnits.instance().isEntity(MappedOne.class); + boolean positive2 = PersistenceUnits.instance().isEntity(MappedTwo.class); + boolean positive3 = PersistenceUnits.instance().isEntity(MappedThree.class); + boolean negative = PersistenceUnits.instance().isEntity(Long.class); + + // then + assertTrue(positive1); + assertTrue(positive2); + assertTrue(positive3); + assertFalse(negative); + } + + @Test + public void should_recognize_ids() + { + // given + + // when + String idField1 = PersistenceUnits.instance().primaryKeyField(MappedOne.class); + String idField2 = PersistenceUnits.instance().primaryKeyField(MappedThree.class); + + // then + assertEquals("id", idField1); + assertEquals("id", idField2); + } + + @Test + public void should_recognize_name() + { + // given + + // when + String name = PersistenceUnits.instance().entityName(MappedOne.class); + + // then + assertEquals("Mapped_One", name); + } + + @Test + public void should_recognize_id_class() + { + // given + + // when + Class idClass = PersistenceUnits.instance().primaryKeyIdClass(MappedTwo.class); + + // then + assertEquals(TeeId.class, idClass); + } + + @Test + public void should_prepare_repo_entity() + { + // given + + // when + RepositoryEntity entity1 = PersistenceUnits.instance().lookupMetadata(MappedOne.class); + RepositoryEntity entity2 = PersistenceUnits.instance().lookupMetadata(MappedTwo.class); + RepositoryEntity entity3 = PersistenceUnits.instance().lookupMetadata(MappedThree.class); + + // then + assertNotNull(entity1); + assertNotNull(entity2); + assertEquals(Long.class, entity1.getPrimaryClass()); + assertEquals(TeeId.class, entity2.getPrimaryClass()); + assertEquals(Long.class, entity3.getPrimaryClass()); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifierTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifierTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifierTest.java new file mode 100644 index 0000000..e190022 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifierTest.java @@ -0,0 +1,62 @@ +/* + * 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.deltaspike.data.impl.meta.verifier; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.deltaspike.data.impl.meta.verifier.EntityVerifier; +import org.apache.deltaspike.data.test.domain.Simple; +import org.junit.Test; + +public class EntityVerifierTest +{ + + @Test + public void should_accept_entity_class() + { + // given + EntityVerifier entityVerifier = new EntityVerifier(); + + // when + boolean isValid = entityVerifier.verify(Simple.class); + + // then + assertTrue(isValid); + } + + @Test + public void should_not_accept_class_without_entity_annotation() + { + // given + EntityVerifier entityVerifier = new EntityVerifier(); + + // when + boolean isValid = entityVerifier.verify(EntityWithoutId.class); + + // then + assertFalse(isValid); + + } + + private static class EntityWithoutId + { + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/ClassToIntrospect.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/ClassToIntrospect.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/ClassToIntrospect.java new file mode 100644 index 0000000..284e935 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/ClassToIntrospect.java @@ -0,0 +1,100 @@ +/* + * 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.deltaspike.data.impl.property; + +import java.net.URL; + +public class ClassToIntrospect +{ + private String name; + + private String p; + + private URL URL; + + public long primitiveProperty = 0; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getP() + { + return p; + } + + public void setP(String p) + { + this.p = p; + } + + public String getTitle() + { + return "Hero"; + } + + public String get() + { + return null; + } + + public boolean is() + { + return false; + } + + public void getFooBar() + { + } + + public void setSalary(Double base, Double bonus) + { + } + + public URL getURL() + { + return URL; + } + + public void setURL(URL URL) + { + this.URL = URL; + } + + public Boolean isValid() + { + return false; + } + + public boolean isValidPrimitiveBoolean() + { + return false; + } + + public long getPrimitiveProperty() + { + return 0l; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromFieldTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromFieldTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromFieldTest.java new file mode 100644 index 0000000..e58fa4f --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromFieldTest.java @@ -0,0 +1,44 @@ +/* + * 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.deltaspike.data.impl.property; + +import java.lang.reflect.Field; + +import org.apache.deltaspike.data.impl.property.FieldProperty; +import org.apache.deltaspike.data.impl.property.Properties; +import org.junit.Test; + +/** + * Verify that only valid properties are permitted, as per the JavaBean specification. + * + * @author Vivian Steller + */ +public class PropertyFromFieldTest +{ + + @Test + public void testAccessingPrimitiveTypedFieldProperty() throws Exception + { + final Field field = ClassToIntrospect.class.getField("primitiveProperty"); + + FieldProperty propertyUT = Properties.createProperty(field); + propertyUT.getValue(new ClassToIntrospect()); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromMethodTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromMethodTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromMethodTest.java new file mode 100644 index 0000000..dbe6d34 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/PropertyFromMethodTest.java @@ -0,0 +1,144 @@ +/* + * 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.deltaspike.data.impl.property; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; +import java.net.URL; + +import org.apache.deltaspike.data.impl.property.MethodProperty; +import org.apache.deltaspike.data.impl.property.Properties; +import org.apache.deltaspike.data.impl.property.Property; +import org.junit.Test; + +/** + * Verify that only valid properties are permitted, as per the JavaBean specification. + * + * @author Dan Allen + * @see http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html + */ +public class PropertyFromMethodTest +{ + + @Test + public void testValidPropertyGetterMethod() throws Exception + { + Method getter = ClassToIntrospect.class.getMethod("getName"); + Property p = Properties.createProperty(getter); + assertNotNull(p); + assertEquals("name", p.getName()); + assertEquals(getter, p.getMember()); + } + + @Test + public void testValidPropertySetterMethod() throws Exception + { + Property p = Properties.createProperty(ClassToIntrospect.class.getMethod("setName", String.class)); + assertNotNull(p); + assertEquals("name", p.getName()); + } + + @Test + public void testReadOnlyProperty() throws Exception + { + Property p = Properties.createProperty(ClassToIntrospect.class.getMethod("getTitle")); + assertNotNull(p); + assertEquals("title", p.getName()); + assertTrue(p.isReadOnly()); + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyPropertyGetterMethod() throws Exception + { + Properties.createProperty(ClassToIntrospect.class.getMethod("get")); + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyBooleanPropertyGetterMethod() throws Exception + { + Properties.createProperty(ClassToIntrospect.class.getMethod("is")); + } + + @Test(expected = IllegalArgumentException.class) + public void testNonPrimitiveBooleanPropertyIsMethod() throws Exception + { + Properties.createProperty(ClassToIntrospect.class.getMethod("isValid")); + } + + @Test + public void testSingleCharPropertyGetterMethod() throws Exception + { + Method getter = ClassToIntrospect.class.getMethod("getP"); + Property p = Properties.createProperty(getter); + assertNotNull(p); + assertEquals("p", p.getName()); + assertEquals(getter, p.getMember()); + } + + @Test + public void testSingleCharPropertySetterMethod() throws Exception + { + Property p = Properties.createProperty(ClassToIntrospect.class.getMethod("setP", String.class)); + assertNotNull(p); + assertEquals("p", p.getName()); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetterMethodWithVoidReturnType() throws Exception + { + Properties.createProperty(ClassToIntrospect.class.getMethod("getFooBar")); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetterMethodWithMultipleParameters() throws Exception + { + Properties.createProperty(ClassToIntrospect.class.getMethod("setSalary", Double.class, Double.class)); + } + + @Test + public void testAcronymProperty() throws Exception + { + Method getter = ClassToIntrospect.class.getMethod("getURL"); + Property p = Properties.createProperty(getter); + assertNotNull(p); + assertEquals("URL", p.getName()); + assertEquals(getter, p.getMember()); + } + + // SOLDER-298 + @Test + public void testPrimitiveBooleanProperty() throws Exception + { + Property p = Properties.createProperty(ClassToIntrospect.class.getMethod("isValidPrimitiveBoolean")); + + assertNotNull(p); + } + + @Test + public void testAccessingPrimitiveTypedMethodProperty() throws Exception + { + final Method method = ClassToIntrospect.class.getMethod("getPrimitiveProperty"); + + MethodProperty propertyUT = Properties.createProperty(method); + propertyUT.getValue(new ClassToIntrospect()); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ae1a7147/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/query/PropertyQueryTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/query/PropertyQueryTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/query/PropertyQueryTest.java new file mode 100644 index 0000000..d37b1d1 --- /dev/null +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/property/query/PropertyQueryTest.java @@ -0,0 +1,108 @@ +/* + * 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.deltaspike.data.impl.property.query; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.apache.deltaspike.data.impl.property.Property; +import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria; +import org.apache.deltaspike.data.impl.property.query.PropertyQueries; +import org.apache.deltaspike.data.impl.property.query.PropertyQuery; +import org.apache.deltaspike.data.impl.property.query.TypedPropertyCriteria; +import org.junit.Test; + +/** + * Validate the property query mechanism. + * + * @author Dan Allen + */ +public class PropertyQueryTest +{ + /** + * Querying for a single result with a criteria that matches multiple properties should throw an exception. + * + * @see PropertyQuery#getSingleResult() + */ + @Test(expected = RuntimeException.class) + public void testNonUniqueSingleResultThrowsException() + { + PropertyQuery q = PropertyQueries. createQuery(Person.class); + q.addCriteria(new TypedPropertyCriteria(String.class)); + q.getSingleResult(); + } + + /** + * Querying for a single result with a criteria that does not match any properties should throw an exception. + * + * @see PropertyQuery#getSingleResult() + */ + @Test(expected = RuntimeException.class) + public void testEmptySingleResultThrowsException() + { + PropertyQuery q = PropertyQueries. createQuery(Person.class); + q.addCriteria(new TypedPropertyCriteria(Integer.class)); + q.getSingleResult(); + } + + /** + * Querying for a single result with a criterai that matches exactly one property should return the property. + * + * @see PropertyQuery#getSingleResult() + */ + @Test + public void testSingleResult() + { + PropertyQuery q = PropertyQueries. createQuery(Person.class); + q.addCriteria(new NamedPropertyCriteria("name")); + Property p = q.getSingleResult(); + assertNotNull(p); + Person o = new Person(); + o.setName("Trap"); + assertEquals("Trap", p.getValue(o)); + } + + public static class Person + { + + private String name; + private String title; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getTitle() + { + return title; + } + + public void setTitle(String title) + { + this.title = title; + } + } + +}