Skip to content

Commit

Permalink
Merge pull request #19 from liweinan/upgrade_jberet_core_to_3
Browse files Browse the repository at this point in the history
Upgrade JBeret to 3 + Upgrade JUnit to 5
  • Loading branch information
liweinan authored Sep 12, 2024
2 parents a51fecb + 9b33ff6 commit 0fbe27f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 91 deletions.
12 changes: 10 additions & 2 deletions jberet-rest-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@

package org.jberet.rest.entity;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;


import jakarta.batch.operations.BatchRuntimeException;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;

import com.google.common.base.Throwables;

/**
* Represents a batch exception, {@code BatchRuntimeException}, which includes
* exception type (class), message, and stack trace text.
Expand All @@ -44,7 +48,7 @@ public final class BatchExceptionEntity implements Serializable {
public BatchExceptionEntity(final BatchRuntimeException ex) {
type = ex.getClass();
message = ex.getMessage();
stackTrace = Throwables.getStackTraceAsString(ex);
stackTrace = toString(ex);
}

public Class<? extends BatchRuntimeException> getType() {
Expand All @@ -58,4 +62,32 @@ public String getMessage() {
public String getStackTrace() {
return stackTrace;
}

private static String toString(Throwable origin) {
try (StringWriter writer = new StringWriter()) {
origin.printStackTrace(new PrintWriter(writer));
return writer.toString();
} catch (Throwable e) {
return origin.toString();
}
}

private static Throwable getRootCause(Throwable origin) {
final List<Throwable> list = getThrowableList(origin);
return list.isEmpty() ? null : list.get(list.size() - 1);
}

private static List<Throwable> getThrowableList(Throwable throwable) {
final List<Throwable> list = new ArrayList<>();
while (throwable != null) {
if (!list.contains(throwable)) {
list.add(throwable);
throwable = throwable.getCause();
} else {
// loop detected
throw new IllegalArgumentException("Loop chain detected: ", throwable);
}
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import org.jberet.job.model.Split;
import org.jberet.job.model.Step;
import org.jberet.job.model.Transition;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.wildfly.common.Assert.assertFalse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

/**
* Tests to verify JSON job definition content is properly converted into
Expand All @@ -43,53 +43,62 @@
* @since 1.3.0.Final
*/
public final class JsonJobMapperTest {
@Test(expected = IllegalStateException.class)
@Test
public void missingJobId() throws Exception {
String json = "{\n" +
" \"job\": {\n" +
" \"step\": {\n" +
" \"id\": \"simple.step1\",\n" +
" \"chunk\": {\n" +
" \"reader\": { \"ref\": \"arrayItemReader\" },\n" +
" \"writer\": { \"ref\": \"mockItemWriter\" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
assertThrows(IllegalStateException.class, () -> {
String json = "{\n" +
" \"job\": {\n" +
" \"step\": {\n" +
" \"id\": \"simple.step1\",\n" +
" \"chunk\": {\n" +
" \"reader\": { \"ref\": \"arrayItemReader\" },\n" +
" \"writer\": { \"ref\": \"mockItemWriter\" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
});

}

@Test(expected = IllegalStateException.class)
@Test
public void missingStepId() throws Exception {
String json = "{\n" +
" \"job\": {\n" +
" \"id\": \"simple\",\n" +
" \"step\": {\n" +
" \"chunk\": {\n" +
" \"reader\": { \"-ref\": \"arrayItemReader\" },\n" +
" \"writer\": { \"-ref\": \"mockItemWriter\" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
assertThrows(IllegalStateException.class, () -> {
String json = "{\n" +
" \"job\": {\n" +
" \"id\": \"simple\",\n" +
" \"step\": {\n" +
" \"chunk\": {\n" +
" \"reader\": { \"-ref\": \"arrayItemReader\" },\n" +
" \"writer\": { \"-ref\": \"mockItemWriter\" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
});

}

@Test(expected = IllegalStateException.class)
@Test
public void missingListenerRef() throws Exception {
String json = "{\n" +
" \"job\": {\n" +
" \"id\": \"job1\",\n" +
" \"listeners\": {\n" +
" \"listener\": { \"xxx\": \"xxx\" }\n" +
" },\n" +
" \"step\": {\n" +
" \"id\": \"step1\",\n" +
" \"batchlet\": { \"ref\": \"batchlet1\" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
assertThrows(IllegalStateException.class, () -> {
String json = "{\n" +
" \"job\": {\n" +
" \"id\": \"job1\",\n" +
" \"listeners\": {\n" +
" \"listener\": { \"xxx\": \"xxx\" }\n" +
" },\n" +
" \"step\": {\n" +
" \"id\": \"step1\",\n" +
" \"batchlet\": { \"ref\": \"batchlet1\" }\n" +
" }\n" +
" }\n" +
"}";
final Job job = JsonJobMapper.toJob(json);
});

}

/**
Expand All @@ -102,45 +111,45 @@ public void missingListenerRef() throws Exception {
public void simpleChunkStep() throws Exception {
String json =
"{\n" +
" \"job\": {\n" +
" \"id\": \"simple\",\n" +
" \"step\": {\n" +
" \"id\": \"simple.step1\",\n" +
" \"chunk\": {\n" +
" \"reader\": {\n" +
" \"ref\": \"arrayItemReader\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"RN\",\n" +
" \"value\": \"RV\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"processor\": {\n" +
" \"ref\": \"processor1\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"PN\",\n" +
" \"value\": \"PV\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"writer\": {\n" +
" \"ref\": \"mockItemWriter\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"WN\",\n" +
" \"value\": \"WV\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
" \"job\": {\n" +
" \"id\": \"simple\",\n" +
" \"step\": {\n" +
" \"id\": \"simple.step1\",\n" +
" \"chunk\": {\n" +
" \"reader\": {\n" +
" \"ref\": \"arrayItemReader\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"RN\",\n" +
" \"value\": \"RV\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"processor\": {\n" +
" \"ref\": \"processor1\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"PN\",\n" +
" \"value\": \"PV\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"writer\": {\n" +
" \"ref\": \"mockItemWriter\",\n" +
" \"properties\": {\n" +
" \"property\": \n" +
" {\n" +
" \"name\": \"WN\",\n" +
" \"value\": \"WV\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Job job = JsonJobMapper.toJob(json);
assertEquals("simple", job.getId());
assertEquals(true, job.getRestartableBoolean());
Expand Down Expand Up @@ -387,6 +396,7 @@ public void partitionPlanCollectorAnalyzerReducer() throws Exception {

/**
* Verifies partition mapper
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -535,7 +545,7 @@ public void stepWithTransitionElements() throws Exception {
/**
* Verifies a flow that contains 2 steps and transition elements:
* end, fail, stop and next, and each of the appear once.
*
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -1435,7 +1445,7 @@ private static void verifyTransitionElements(final List<Transition> transitionEl
assertEquals("stop1", transition.getOn());
assertEquals("x", ((Transition.Stop) transition).getExitStatus());
assertEquals("step1", ((Transition.Stop) transition).getRestart());
} else if(transition instanceof Transition.Next){
} else if (transition instanceof Transition.Next) {
nextCount++;
assertEquals("next1", transition.getOn());
assertEquals("step1", ((Transition.Next) transition).getTo());
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<parent>
<artifactId>jberet-parent</artifactId>
<groupId>org.jberet</groupId>
<version>2.1.3.Final</version>
<version>3.0.0.Final</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -28,6 +28,7 @@
<properties>
<version.org.jberet>2.1.3.Final</version.org.jberet>
<insecure.repositories>WARN</insecure.repositories>
<junit-jupiter.version>5.10.3</junit-jupiter.version>
</properties>

<modules>
Expand Down

0 comments on commit 0fbe27f

Please sign in to comment.