Skip to content
Permalink
Browse files
BATCHEE-88 unit test which shows the broken TX error handling
  • Loading branch information
struberg committed May 15, 2016
1 parent 04f73a6 commit 4313c77012a8ed43de3b0b947f9857711b11f65a
Showing 10 changed files with 381 additions and 0 deletions.
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.batchee</groupId>
<artifactId>batchee</artifactId>
<version>0.4-incubating-SNAPSHOT</version>
</parent>

<artifactId>integration-tests</artifactId>
<packaging>pom</packaging>

<modules>
<module>transaction</module>
</modules>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- we do not need to deploy the whole it module -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.batchee</groupId>
<artifactId>integration-tests</artifactId>
<version>0.4-incubating-SNAPSHOT</version>
</parent>

<artifactId>integration-transaction</artifactId>
<packaging>jar</packaging>


<dependencies>
<dependency>
<groupId>org.apache.batchee</groupId>
<artifactId>batchee-jbatch</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jbatch_1.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>

</dependencies>

</project>
@@ -0,0 +1,120 @@
/*
* Copyright 2012 International Business Machines Corp.
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.batchee.its.transaction;

import javax.batch.runtime.context.StepContext;
import javax.transaction.RollbackException;
import javax.transaction.Status;

import java.util.Properties;

import org.apache.batchee.container.exception.TransactionManagementException;
import org.apache.batchee.spi.TransactionManagementService;
import org.apache.batchee.spi.TransactionManagerAdapter;

/**
* A batchee
*/
public class MockTransactionManagerService implements TransactionManagementService {

public static MockTransactionManagerAdapter mockTxAdapter = new MockTransactionManagerAdapter();

@Override
public TransactionManagerAdapter getTransactionManager(StepContext stepContext)
{
return mockTxAdapter;
}

@Override
public void init(Properties batchConfig)
{

}

public static class MockTransactionManagerAdapter implements TransactionManagerAdapter
{
private int txStatus = Status.STATUS_NO_TRANSACTION;

public void setTxStatus(int txStatus)
{
this.txStatus = txStatus;
}

@Override
public void begin()
{
txStatus = Status.STATUS_ACTIVE;
}

@Override
public void commit()
{
switch (txStatus) {
case Status.STATUS_ACTIVE:
txStatus = Status.STATUS_COMMITTED;
break;
case Status.STATUS_ROLLEDBACK:
case Status.STATUS_MARKED_ROLLBACK:
throw new TransactionManagementException(new RollbackException());
default :
throw new TransactionManagementException(new IllegalStateException("no mock-tx on current thread"));
}
}

@Override
public int getStatus()
{
return txStatus;
}

@Override
public void rollback()
{
switch (txStatus) {
case Status.STATUS_ACTIVE:
txStatus = Status.STATUS_ROLLEDBACK;
break;
case Status.STATUS_ROLLEDBACK:
case Status.STATUS_MARKED_ROLLBACK:
break;
default :
throw new TransactionManagementException(new IllegalStateException("no mock-tx on current thread"));
}
}

@Override
public void setRollbackOnly()
{
switch (txStatus) {
case Status.STATUS_ACTIVE:
case Status.STATUS_MARKED_ROLLBACK:
txStatus = Status.STATUS_MARKED_ROLLBACK;
break;
case Status.STATUS_ROLLEDBACK:
break;
default :
throw new TransactionManagementException(new IllegalStateException("no mock-tx on current thread"));
}
}

@Override
public void setTransactionTimeout(int arg0)
{
// no op
}
}
}
@@ -0,0 +1,34 @@
/*
* 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.batchee.its.transaction;

import javax.batch.api.chunk.AbstractItemReader;

/**
* Sample Reader which simply counts from 1..5
*/
public class TxErrorReader extends AbstractItemReader {
private int i = 1;

@Override
public Object readItem() throws Exception {
if (i > 5) {
return null;
}
return Integer.valueOf(i++);
}
}
@@ -0,0 +1,37 @@
/*
* 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.batchee.its.transaction;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.BatchStatus;

import org.apache.batchee.util.Batches;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TxErrorTest {


@Test
public void testRolledBackDuringWork() {
final JobOperator jobOperator = BatchRuntime.getJobOperator();
BatchStatus batchStatus = Batches.waitFor(jobOperator, jobOperator.start("txtest1", null));
Assert.assertEquals(batchStatus, BatchStatus.COMPLETED);
Assert.assertEquals(TxErrorWriter1.written.intValue(), 5);
}
}
@@ -0,0 +1,37 @@
/*
* 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.batchee.its.transaction;

import javax.batch.api.chunk.AbstractItemWriter;
import javax.transaction.Status;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class TxErrorWriter1 extends AbstractItemWriter {
public static AtomicInteger written = new AtomicInteger(0);

@Override
public void writeItems(List items) throws Exception {
Integer i = (Integer) items.get(0);

if (i.intValue() == 3) {
MockTransactionManagerService.mockTxAdapter.setTxStatus(Status.STATUS_ROLLEDBACK);
}

written.incrementAndGet();
}
}
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
See the NOTICE file distributed with this work for additional information
regarding copyright ownership. Licensed under the Apache License,
Version 2.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<job id="txtest1" version="1.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<step id="txtest1-step">
<chunk item-count="1">
<reader ref="org.apache.batchee.its.transaction.TxErrorReader"></reader>
<writer ref="org.apache.batchee.its.transaction.TxErrorWriter1"></writer>
<skippable-exception-classes>
<include class="java.lang.Exception"/> <!-- all exceptions... -->
</skippable-exception-classes>
</chunk>
</step>
</job>
@@ -0,0 +1,16 @@
#
# 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.
TransactionManagementService=org.apache.batchee.its.transaction.MockTransactionManagerService
@@ -45,6 +45,11 @@ public interface TransactionManagerAdapter {
* Roll back the transaction associated with the
* current thread. When this method completes,
* the thread becomes associated with no transaction.
*
* Attention: this must not throw an Exception until there is a setup problem!
* So if the tx is not active anymore and we get an Exception while trying
* to rollback then it MUST get catched away. Otoh if there is a setup
* or system issue the Exception from the rollback needs to get thrown.
*/
public void rollback();

0 comments on commit 4313c77

Please sign in to comment.