Skip to content
Permalink
Browse files
ARIES-1783 Added a test
The test covers the case of calling a method requiring a transaction
from a method without a transaction.
  • Loading branch information
Smasherr committed Mar 15, 2018
1 parent eae9b2b commit 788ebc689d615ab5498ed78eba21a4cc0354761f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
@@ -39,6 +39,10 @@ Also testing declarative transactions</description>
<groupId>org.osgi</groupId>
<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>
@@ -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;
}

}
@@ -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"/>
@@ -33,6 +33,7 @@
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 void testCoordinationLifecycle() throws InterruptedException, ExecutionEx
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 + ")");
}

0 comments on commit 788ebc6

Please sign in to comment.