Repository: aries-jpa Updated Branches: refs/heads/master eae9b2b6c -> 788ebc689
ARIES-1783 Added a test The test covers the case of calling a method requiring a transaction from a method without a transaction. Project: http://git-wip-us.apache.org/repos/asf/aries-jpa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-jpa/commit/788ebc68 Tree: http://git-wip-us.apache.org/repos/asf/aries-jpa/tree/788ebc68 Diff: http://git-wip-us.apache.org/repos/asf/aries-jpa/diff/788ebc68 Branch: refs/heads/master Commit: 788ebc689d615ab5498ed78eba21a4cc0354761f Parents: eae9b2b Author: Daniel Estermann <soundcrac...@gmail.com> Authored: Sat Jan 13 03:11:37 2018 +0100 Committer: Daniel Estermann <soundcrac...@gmail.com> Committed: Thu Mar 15 18:16:21 2018 +0100 ---------------------------------------------------------------------- .../jpa-container-blueprint-testbundle/pom.xml | 4 + .../impl/CarServiceWithRequiresNew.java | 85 ++++++++++++++++++++ .../resources/OSGI-INF/blueprint/config.xml | 12 +++ .../blueprint/aries/itest/BlueprintTest.java | 8 ++ 4 files changed, 109 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jpa/blob/788ebc68/itests/jpa-container-blueprint-testbundle/pom.xml ---------------------------------------------------------------------- diff --git a/itests/jpa-container-blueprint-testbundle/pom.xml b/itests/jpa-container-blueprint-testbundle/pom.xml index 45fc4cd..ed9bcf4 100644 --- a/itests/jpa-container-blueprint-testbundle/pom.xml +++ b/itests/jpa-container-blueprint-testbundle/pom.xml @@ -40,6 +40,10 @@ Also testing declarative transactions</description> <artifactId>org.osgi.compendium</artifactId> </dependency> <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> <groupId>javax.transaction</groupId> <artifactId>javax.transaction-api</artifactId> <version>1.2</version> http://git-wip-us.apache.org/repos/asf/aries-jpa/blob/788ebc68/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceWithRequiresNew.java ---------------------------------------------------------------------- diff --git a/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceWithRequiresNew.java b/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceWithRequiresNew.java new file mode 100644 index 0000000..d1b041c --- /dev/null +++ b/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/container/itest/bundle/blueprint/impl/CarServiceWithRequiresNew.java @@ -0,0 +1,85 @@ +/* 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.aries.jpa.container.itest.bundle.blueprint.impl; + +import java.util.Arrays; +import java.util.Collection; + +import javax.transaction.Transactional; +import javax.transaction.Transactional.TxType; + +import org.apache.aries.jpa.container.itest.entities.Car; +import org.apache.aries.jpa.container.itest.entities.CarService; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; + +public class CarServiceWithRequiresNew extends AbstractCarServiceImpl { + BundleContext bundleContext; + + @Override + @Transactional(TxType.REQUIRES_NEW) + public Car getCar(String id) { + return em.find(Car.class, id); + } + + @Override + @Transactional(TxType.REQUIRES_NEW) + public void addCar(Car car) { + em.persist(car); + em.flush(); + } + + @Override + @Transactional(TxType.NEVER) + public Collection<Car> getCars() { + Car c = new Car(); + c.setNumberPlate("TR123"); + ServiceReference<CarService> ref = null; + try { + ref = getService(); + CarService carService = bundleContext.getService(ref); + carService.addCar(c); + return Arrays.asList(this.getCar("TR123")); + } finally { + if (ref != null) { + bundleContext.ungetService(ref); + } + } + } + + private ServiceReference<CarService> getService() { + try { + Collection<ServiceReference<CarService>> refs = bundleContext.getServiceReferences(CarService.class, "(type=rn)"); + return refs.iterator().next(); + } catch (InvalidSyntaxException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + @Override + public void updateCar(Car car) { + } + + @Override + public void deleteCar(String id) { + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + +} http://git-wip-us.apache.org/repos/asf/aries-jpa/blob/788ebc68/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml ---------------------------------------------------------------------- diff --git a/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml b/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml index 94060cb..c4c0d76 100644 --- a/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml +++ b/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml @@ -108,6 +108,18 @@ <tx:transaction method="*" value="Required" /> </bean> + <service ref="carServiceRequiresNew" + interface="org.apache.aries.jpa.container.itest.entities.CarService"> + <service-properties> + <entry key="type" value="rn" /> + </service-properties> + </service> + + <bean id="carServiceRequiresNew" + class="org.apache.aries.jpa.container.itest.bundle.blueprint.impl.CarServiceWithRequiresNew"> + <property name="bundleContext" ref="blueprintBundleContext"/> + </bean> + <bean id="carLifeCycle" class="org.apache.aries.jpa.container.itest.bundle.blueprint.impl.CarLifeCycle" > <property name="coordinator" ref="coordinator"/> <property name="carService" ref="carServiceEm"/> http://git-wip-us.apache.org/repos/asf/aries-jpa/blob/788ebc68/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java ---------------------------------------------------------------------- diff --git a/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java index f37ac34..507ff3e 100644 --- a/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java +++ b/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java @@ -33,6 +33,7 @@ import org.apache.aries.jpa.container.itest.entities.Car; import org.apache.aries.jpa.container.itest.entities.CarService; import org.apache.aries.jpa.itest.AbstractCarJPAITest; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.Option; @@ -125,6 +126,13 @@ public class BlueprintTest extends AbstractCarJPAITest { assertNoCars(carService); } + @Test + @Ignore + public void testCarWithRequiresNewAnnotation() throws Exception { + CarService cs = getCarService("rn"); + cs.getCars(); + } + private CarService getCarService(String type) { return getService(CarService.class, "(type=" + type + ")"); }