Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -14,18 +14,19 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.typesafe.akka</groupId>
|
||||
<artifactId>akka-stream_2.11</artifactId>
|
||||
<artifactId>akka-stream_${scala.version}</artifactId>
|
||||
<version>${akkastreams.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.typesafe.akka</groupId>
|
||||
<artifactId>akka-stream-testkit_2.11</artifactId>
|
||||
<artifactId>akka-stream-testkit_${scala.version}</artifactId>
|
||||
<version>${akkastreams.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<akkastreams.version>2.5.2</akkastreams.version>
|
||||
<scala.version>2.11</scala.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+42
-42
@@ -1,42 +1,42 @@
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
public class TreeNode {
|
||||
|
||||
private int value;
|
||||
private TreeNode rightChild;
|
||||
private TreeNode leftChild;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public TreeNode getRightChild() {
|
||||
return rightChild;
|
||||
}
|
||||
|
||||
public void setRightChild(TreeNode rightChild) {
|
||||
this.rightChild = rightChild;
|
||||
}
|
||||
|
||||
public TreeNode getLeftChild() {
|
||||
return leftChild;
|
||||
}
|
||||
|
||||
public void setLeftChild(TreeNode leftChild) {
|
||||
this.leftChild = leftChild;
|
||||
}
|
||||
|
||||
public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) {
|
||||
this.value = value;
|
||||
this.rightChild = rightChild;
|
||||
this.leftChild = leftChild;
|
||||
}
|
||||
|
||||
public TreeNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
public class TreeNode {
|
||||
|
||||
private int value;
|
||||
private TreeNode rightChild;
|
||||
private TreeNode leftChild;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public TreeNode getRightChild() {
|
||||
return rightChild;
|
||||
}
|
||||
|
||||
public void setRightChild(TreeNode rightChild) {
|
||||
this.rightChild = rightChild;
|
||||
}
|
||||
|
||||
public TreeNode getLeftChild() {
|
||||
return leftChild;
|
||||
}
|
||||
|
||||
public void setLeftChild(TreeNode leftChild) {
|
||||
this.leftChild = leftChild;
|
||||
}
|
||||
|
||||
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
|
||||
this.value = value;
|
||||
this.rightChild = rightChild;
|
||||
this.leftChild = leftChild;
|
||||
}
|
||||
|
||||
public TreeNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
+53
-68
@@ -1,68 +1,53 @@
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class TreeReverser {
|
||||
|
||||
public TreeNode createBinaryTree() {
|
||||
|
||||
TreeNode leaf1 = new TreeNode(3);
|
||||
TreeNode leaf2 = new TreeNode(1);
|
||||
TreeNode leaf3 = new TreeNode(9);
|
||||
TreeNode leaf4 = new TreeNode(6);
|
||||
|
||||
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
|
||||
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
|
||||
|
||||
TreeNode root = new TreeNode(4, nodeRight, nodeLeft);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public void reverseRecursive(TreeNode treeNode) {
|
||||
if (treeNode == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode temp = treeNode.getLeftChild();
|
||||
treeNode.setLeftChild(treeNode.getRightChild());
|
||||
treeNode.setRightChild(temp);
|
||||
|
||||
reverseRecursive(treeNode.getLeftChild());
|
||||
reverseRecursive(treeNode.getRightChild());
|
||||
}
|
||||
|
||||
public void reverseIterative(TreeNode treeNode) {
|
||||
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
|
||||
|
||||
if (treeNode != null) {
|
||||
queue.add(treeNode);
|
||||
}
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
|
||||
TreeNode node = queue.poll();
|
||||
if (node.getLeftChild() != null)
|
||||
queue.add(node.getLeftChild());
|
||||
if (node.getRightChild() != null)
|
||||
queue.add(node.getRightChild());
|
||||
|
||||
TreeNode temp = node.getLeftChild();
|
||||
node.setLeftChild(node.getRightChild());
|
||||
node.setRightChild(temp);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(TreeNode root) {
|
||||
if (root == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
|
||||
|
||||
buffer.append(toString(root.getLeftChild()));
|
||||
buffer.append(toString(root.getRightChild()));
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class TreeReverser {
|
||||
|
||||
public void reverseRecursive(TreeNode treeNode) {
|
||||
if (treeNode == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TreeNode temp = treeNode.getLeftChild();
|
||||
treeNode.setLeftChild(treeNode.getRightChild());
|
||||
treeNode.setRightChild(temp);
|
||||
|
||||
reverseRecursive(treeNode.getLeftChild());
|
||||
reverseRecursive(treeNode.getRightChild());
|
||||
}
|
||||
|
||||
public void reverseIterative(TreeNode treeNode) {
|
||||
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
|
||||
|
||||
if (treeNode != null) {
|
||||
queue.add(treeNode);
|
||||
}
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
|
||||
TreeNode node = queue.poll();
|
||||
if (node.getLeftChild() != null)
|
||||
queue.add(node.getLeftChild());
|
||||
if (node.getRightChild() != null)
|
||||
queue.add(node.getRightChild());
|
||||
|
||||
TreeNode temp = node.getLeftChild();
|
||||
node.setLeftChild(node.getRightChild());
|
||||
node.setRightChild(temp);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(TreeNode root) {
|
||||
if (root == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
|
||||
|
||||
buffer.append(toString(root.getLeftChild()));
|
||||
buffer.append(toString(root.getRightChild()));
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
+47
-33
@@ -1,33 +1,47 @@
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TreeReverserUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTreeWhenReversingRecursivelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = reverser.createBinaryTree();
|
||||
|
||||
reverser.reverseRecursive(treeNode);
|
||||
|
||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||
.trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTreeWhenReversingIterativelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = reverser.createBinaryTree();
|
||||
|
||||
reverser.reverseIterative(treeNode);
|
||||
|
||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||
.trim());
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.algorithms.reversingtree;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TreeReverserUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTreeWhenReversingRecursivelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = createBinaryTree();
|
||||
|
||||
reverser.reverseRecursive(treeNode);
|
||||
|
||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||
.trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTreeWhenReversingIterativelyThenReversed() {
|
||||
TreeReverser reverser = new TreeReverser();
|
||||
|
||||
TreeNode treeNode = createBinaryTree();
|
||||
|
||||
reverser.reverseIterative(treeNode);
|
||||
|
||||
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
|
||||
.trim());
|
||||
}
|
||||
|
||||
private TreeNode createBinaryTree() {
|
||||
|
||||
TreeNode leaf1 = new TreeNode(1);
|
||||
TreeNode leaf2 = new TreeNode(3);
|
||||
TreeNode leaf3 = new TreeNode(6);
|
||||
TreeNode leaf4 = new TreeNode(9);
|
||||
|
||||
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
|
||||
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
|
||||
|
||||
TreeNode root = new TreeNode(4, nodeLeft, nodeRight);
|
||||
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
<version>0.0.1</version>
|
||||
<name>apache-meecrowave</name>
|
||||
<description>A sample REST API application with Meecrowave</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
@RunWith(MonoMeecrowave.Runner.class)
|
||||
public class ArticleEndpointsTest {
|
||||
public class ArticleEndpointsUnitTest {
|
||||
|
||||
@ConfigurationInject
|
||||
private Meecrowave.Builder config;
|
||||
@@ -14,6 +14,14 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -31,6 +39,7 @@
|
||||
<properties>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<guava.version>27.1-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class EmptyStringToEmptyOptionalUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenFilteringOnOptional_thenEmptyOptionalIsReturned() {
|
||||
String str = "";
|
||||
Optional<String> opt = Optional.ofNullable(str).filter(s -> !s.isEmpty());
|
||||
Assert.assertFalse(opt.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenFilteringOnOptionalInJava11_thenEmptyOptionalIsReturned() {
|
||||
String str = "";
|
||||
Optional<String> opt = Optional.ofNullable(str).filter(Predicate.not(String::isEmpty));
|
||||
Assert.assertFalse(opt.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenPassingResultOfEmptyToNullToOfNullable_thenEmptyOptionalIsReturned() {
|
||||
String str = "";
|
||||
Optional<String> opt = Optional.ofNullable(Strings.emptyToNull(str));
|
||||
Assert.assertFalse(opt.isPresent());
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.switchExpression;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SwitchUnitTest {
|
||||
|
||||
@Test
|
||||
public void switchJava12(){
|
||||
|
||||
var month = Month.AUG;
|
||||
|
||||
var value = switch(month){
|
||||
case JAN,JUN, JUL -> 3;
|
||||
case FEB,SEP, OCT, NOV, DEC -> 1;
|
||||
case MAR,MAY, APR, AUG -> 2;
|
||||
};
|
||||
|
||||
Assert.assertEquals(value, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void switchLocalVariable(){
|
||||
var month = Month.AUG;
|
||||
int i = switch (month){
|
||||
case JAN,JUN, JUL -> 3;
|
||||
case FEB,SEP, OCT, NOV, DEC -> 1;
|
||||
case MAR,MAY, APR, AUG -> {
|
||||
int j = month.toString().length() * 4;
|
||||
break j;
|
||||
}
|
||||
};
|
||||
Assert.assertEquals(12, i);
|
||||
}
|
||||
|
||||
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
target/
|
||||
.idea/
|
||||
bin/
|
||||
*.iml
|
||||
@@ -0,0 +1,5 @@
|
||||
=========
|
||||
|
||||
## Core Java Lang OOP 2 Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
@@ -0,0 +1,27 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-lang-oop-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-oop-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClass {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClass() {
|
||||
//uncomment this to see the supertype compiler error:
|
||||
//this(myField1);
|
||||
}
|
||||
|
||||
public MyClass(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution1 {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution1() {
|
||||
myField2 = myField1;
|
||||
}
|
||||
|
||||
public MyClassSolution1(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution2 {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution2() {
|
||||
setupMyFields(myField1);
|
||||
}
|
||||
|
||||
public MyClassSolution2(int i) {
|
||||
setupMyFields(i);
|
||||
}
|
||||
|
||||
private void setupMyFields(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution3 {
|
||||
|
||||
private static final int SOME_CONSTANT = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution3() {
|
||||
this(SOME_CONSTANT);
|
||||
}
|
||||
|
||||
public MyClassSolution3(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyException extends RuntimeException {
|
||||
private int errorCode = 0;
|
||||
|
||||
public MyException(String message) {
|
||||
//uncomment this to see the supertype compiler error:
|
||||
//super(message + getErrorCode());
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>libraries2</artifactId>
|
||||
<name>libraries2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-public-repository-group</id>
|
||||
<name>JBoss Public Repository Group</name>
|
||||
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jbpm</groupId>
|
||||
<artifactId>jbpm-test</artifactId>
|
||||
<version>${jbpm.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jbpm;
|
||||
|
||||
import org.kie.api.runtime.manager.Context;
|
||||
import org.kie.internal.runtime.manager.context.EmptyContext;
|
||||
|
||||
import com.baeldung.jbpm.engine.WorkflowEngine;
|
||||
import com.baeldung.jbpm.engine.WorkflowEngineImpl;
|
||||
|
||||
public class WorkflowProcessMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
WorkflowEngine workflowEngine = new WorkflowEngineImpl();
|
||||
String processId = "com.baeldung.bpmn.helloworld";
|
||||
String kbaseId = "kbase";
|
||||
String persistenceUnit = "org.jbpm.persistence.jpa";
|
||||
Context<String> initialContext = EmptyContext.get();
|
||||
workflowEngine.runjBPMEngineForProcess(processId, initialContext, kbaseId, persistenceUnit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.jbpm.engine;
|
||||
|
||||
import org.kie.api.runtime.manager.Context;
|
||||
import org.kie.api.runtime.process.ProcessInstance;
|
||||
|
||||
public interface WorkflowEngine {
|
||||
|
||||
public ProcessInstance runjBPMEngineForProcess(String processId, Context<String> initialContext, String kbaseId, String persistenceUnit);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.jbpm.engine;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.jbpm.test.JBPMHelper;
|
||||
import org.kie.api.KieBase;
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.kie.api.runtime.manager.Context;
|
||||
import org.kie.api.runtime.manager.RuntimeEngine;
|
||||
import org.kie.api.runtime.manager.RuntimeEnvironment;
|
||||
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;
|
||||
import org.kie.api.runtime.manager.RuntimeManager;
|
||||
import org.kie.api.runtime.manager.RuntimeManagerFactory;
|
||||
import org.kie.api.runtime.process.ProcessInstance;
|
||||
|
||||
public class WorkflowEngineImpl implements WorkflowEngine {
|
||||
|
||||
@Override
|
||||
public ProcessInstance runjBPMEngineForProcess(String processId, Context<String> initialContext, String kbaseId, String persistenceUnit) {
|
||||
RuntimeManager manager = null;
|
||||
RuntimeEngine engine = null;
|
||||
ProcessInstance pInstance = null;
|
||||
try {
|
||||
KieBase kbase = getKieBase(kbaseId);
|
||||
manager = createJBPMRuntimeManager(kbase, persistenceUnit);
|
||||
engine = manager.getRuntimeEngine(initialContext);
|
||||
pInstance = executeProcessInstance(processId, manager, initialContext, engine);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (manager != null && engine != null)
|
||||
manager.disposeRuntimeEngine(engine);
|
||||
System.exit(0);
|
||||
}
|
||||
return pInstance;
|
||||
}
|
||||
|
||||
private ProcessInstance executeProcessInstance(String processId, RuntimeManager manager, Context<String> initialContext, RuntimeEngine engine) {
|
||||
KieSession ksession = engine.getKieSession();
|
||||
ProcessInstance pInstance = ksession.startProcess(processId);
|
||||
return pInstance;
|
||||
}
|
||||
|
||||
private KieBase getKieBase(String kbaseId) {
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
KieBase kbase = kContainer.getKieBase(kbaseId);
|
||||
return kbase;
|
||||
}
|
||||
|
||||
private RuntimeManager createJBPMRuntimeManager(KieBase kbase, String persistenceUnit) {
|
||||
JBPMHelper.startH2Server();
|
||||
JBPMHelper.setupDataSource();
|
||||
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||
RuntimeEnvironmentBuilder runtimeEnvironmentBuilder = RuntimeEnvironmentBuilder.Factory.get()
|
||||
.newDefaultBuilder();
|
||||
RuntimeEnvironment runtimeEnvironment = runtimeEnvironmentBuilder.entityManagerFactory(emf)
|
||||
.knowledgeBase(kbase)
|
||||
.get();
|
||||
return RuntimeManagerFactory.Factory.get()
|
||||
.newSingletonRuntimeManager(runtimeEnvironment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
|
||||
<kbase name="kbase" packages="com.baeldung.process" />
|
||||
</kmodule>
|
||||
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<definitions id="Definition"
|
||||
targetNamespace="http://www.jboss.org/drools"
|
||||
typeLanguage="http://www.java.com/javaTypes"
|
||||
expressionLanguage="http://www.mvel.org/2.0"
|
||||
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"
|
||||
xmlns:g="http://www.jboss.org/drools/flow/gpd"
|
||||
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
|
||||
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
|
||||
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
|
||||
xmlns:tns="http://www.jboss.org/drools">
|
||||
|
||||
<process processType="Private" isExecutable="true" id="com.baeldung.bpmn.helloworld" name="HelloWorld Process" tns:packageName="com.baeldung.bpmn.process" >
|
||||
|
||||
<!-- nodes -->
|
||||
<scriptTask id="_jbpm-unique-1" name="HelloWorld" scriptFormat="http://www.java.com/java" >
|
||||
<script>System.out.println("Hello World");</script>
|
||||
</scriptTask>
|
||||
<endEvent id="_jbpm-unique-2" name="End" >
|
||||
<terminateEventDefinition />
|
||||
</endEvent>
|
||||
<startEvent id="_jbpm-unique-0" name="Start" isInterrupting="false"/>
|
||||
|
||||
<!-- connections -->
|
||||
<sequenceFlow id="_jbpm-unique-0-_jbpm-unique-1" sourceRef="_jbpm-unique-0" targetRef="_jbpm-unique-1" />
|
||||
<sequenceFlow id="_jbpm-unique-1-_jbpm-unique-2" sourceRef="_jbpm-unique-1" targetRef="_jbpm-unique-2" />
|
||||
|
||||
</process>
|
||||
|
||||
<bpmndi:BPMNDiagram>
|
||||
<bpmndi:BPMNPlane bpmnElement="com.baeldung.bpmn.helloworld" >
|
||||
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-1" >
|
||||
<dc:Bounds x="119" y="44" width="111" height="48" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-2" >
|
||||
<dc:Bounds x="272" y="43" width="48" height="48" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape bpmnElement="_jbpm-unique-0" >
|
||||
<dc:Bounds x="28" y="44" width="48" height="48" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNEdge bpmnElement="_jbpm-unique-0-_jbpm-unique-1" >
|
||||
<di:waypoint x="52" y="68" />
|
||||
<di:waypoint x="174" y="68" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge bpmnElement="_jbpm-unique-1-_jbpm-unique-2" >
|
||||
<di:waypoint x="174" y="68" />
|
||||
<di:waypoint x="296" y="67" />
|
||||
</bpmndi:BPMNEdge>
|
||||
</bpmndi:BPMNPlane>
|
||||
</bpmndi:BPMNDiagram>
|
||||
|
||||
</definitions>
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.jbpm;
|
||||
|
||||
import org.jbpm.test.JbpmJUnitBaseTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.kie.api.runtime.manager.RuntimeEngine;
|
||||
import org.kie.api.runtime.manager.RuntimeManager;
|
||||
import org.kie.api.runtime.process.ProcessInstance;
|
||||
import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext;
|
||||
|
||||
public class WorkflowEngineIntegrationTest extends JbpmJUnitBaseTestCase {
|
||||
|
||||
private String[] triggeredNodesArray = { "Start", "HelloWorld", "End" };
|
||||
private RuntimeManager manager = null;
|
||||
private RuntimeEngine runtimeEngine = null;
|
||||
private KieSession ksession = null;
|
||||
private ProcessInstance processInstance = null;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
manager = createRuntimeManager(Strategy.SINGLETON, "manager", "com/baeldung/process/helloworld.bpmn");
|
||||
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
|
||||
ksession = runtimeEngine.getKieSession();
|
||||
processInstance = ksession.startProcess("com.baeldung.bpmn.helloworld");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
manager.disposeRuntimeEngine(runtimeEngine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProcessInstance_whenExecutionCompleted_thenVerifyNodesExecutionOrder() {
|
||||
assertNodeTriggered(processInstance.getId(), triggeredNodesArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProcessInstance_whenExecutionCompleted_thenVerifyKnowledgeSessionId() {
|
||||
int ksessionID = ksession.getId();
|
||||
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
|
||||
ksession = runtimeEngine.getKieSession();
|
||||
assertEquals(ksessionID, ksession.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProcessInstance_whenExecutionCompleted_thenVerifyProcessInstanceStatus() {
|
||||
assertProcessInstanceCompleted(processInstance.getId(), ksession);
|
||||
assertTrue("ProcessInstance completed with status 2", processInstance.getState() == 2);
|
||||
}
|
||||
}
|
||||
@@ -37,12 +37,29 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.docx4j</groupId>
|
||||
<artifactId>docx4j</artifactId>
|
||||
<version>${docx4j.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<logback.contrib.version>0.1.5</logback.contrib.version>
|
||||
<jackson.version>2.9.7</jackson.version>
|
||||
<docx4j.version>3.3.5</docx4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>noexception</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>noexception</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.machinezoo.noexception</groupId>
|
||||
<artifactId>noexception</artifactId>
|
||||
<version>${noexception.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<noexception.version>1.1.0</noexception.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.noexception;
|
||||
|
||||
import com.machinezoo.noexception.ExceptionHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CustomExceptionHandler extends ExceptionHandler {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
|
||||
|
||||
@Override
|
||||
public boolean handle(Throwable throwable) {
|
||||
|
||||
if (throwable.getClass()
|
||||
.isAssignableFrom(RuntimeException.class)
|
||||
|| throwable.getClass()
|
||||
.isAssignableFrom(Error.class)) {
|
||||
return false;
|
||||
} else {
|
||||
logger.error("Caught Exception ", throwable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.noexception;
|
||||
|
||||
import com.machinezoo.noexception.Exceptions;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NoExceptionUnitTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void whenStdExceptionHandling_thenCatchAndLog() {
|
||||
try {
|
||||
System.out.println("Result is " + Integer.parseInt("foobar"));
|
||||
} catch (Throwable exception) {
|
||||
logger.error("Caught exception:", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDefaultNoException_thenCatchAndLog() {
|
||||
|
||||
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
|
||||
System.out.println("Result is " + Exceptions.log(logger).get(() -> +Integer.parseInt("foobar")).orElse(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithClassNameAndMessage() {
|
||||
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
|
||||
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
|
||||
}
|
||||
|
||||
@Test(expected = Error.class)
|
||||
public void givenCustomHandler_whenError_thenRethrowError() {
|
||||
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
|
||||
customExceptionHandler.run(() -> throwError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomHandler_whenException_thenCatchAndLog() {
|
||||
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
|
||||
customExceptionHandler.run(() -> throwException());
|
||||
}
|
||||
|
||||
private static void throwError() {
|
||||
throw new Error("This is very bad.");
|
||||
}
|
||||
|
||||
private static void throwException() {
|
||||
String testString = "foo";
|
||||
testString.charAt(5);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user