Skip to content

Commit

Permalink
PLANNER-533 Reproducing tests for getKieSessionModel(null) problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Feb 19, 2016
1 parent 44a20ce commit d42debd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 35 deletions.
Expand Up @@ -17,17 +17,13 @@
package org.optaplanner.core.api.solver;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;

import com.google.common.io.Resources;
import org.apache.commons.io.IOUtils;
import org.drools.compiler.CommonTestMethodBase;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.ReleaseId;
Expand All @@ -39,33 +35,13 @@
public class KieContainerSolverFactoryTest extends CommonTestMethodBase {

private static KieServices kieServices;
private static ReleaseId releaseId;

@BeforeClass
public static void deployTestdataKjar() throws IOException {
kieServices = KieServices.Factory.get();
releaseId = kieServices.newReleaseId("org.optaplanner", "optaplanner-testdata-kjar", "1.0.0");

Resource valueClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataValue.java",
"testdata/kjar/ClassloadedTestdataValue.java");
Resource entityClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataEntity.java",
"testdata/kjar/ClassloadedTestdataEntity.java");
Resource solutionClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataSolution.java",
"testdata/kjar/ClassloadedTestdataSolution.java");
Resource scoreRules = buildResource("org/optaplanner/core/api/solver/kieContainerTestdataScoreRules.drl",
"testdata/kjar/scoreRules.drl");
Resource solverConfig = buildResource("org/optaplanner/core/api/solver/kieContainerTestdataSolverConfig.xml",
"testdata/kjar/kieContainerTestdataSolverConfig.solver");
Resource scanAnnotatedSolverConfig = buildResource("org/optaplanner/core/api/solver/scanAnnotatedKieContainerTestdataSolverConfig.xml",
"testdata/kjar/scanAnnotatedKieContainerTestdataSolverConfig.solver");
String kmodule = readResourceToString("org/optaplanner/core/api/solver/kieContainerKmodule.xml");
createAndDeployJar(kieServices, kmodule, releaseId,
valueClass, entityClass, solutionClass,
scoreRules,
solverConfig, scanAnnotatedSolverConfig);
}

private static Resource buildResource(String resourceString, String targetPath) throws IOException {
protected static Resource buildResource(String resourceString, String targetPath) throws IOException {
String content = readResourceToString(resourceString);
Resource resource = kieServices.getResources().newReaderResource(new StringReader(content), "UTF-8");
resource.setTargetPath(targetPath);
Expand All @@ -75,45 +51,94 @@ private static Resource buildResource(String resourceString, String targetPath)
return resource;
}

private static String readResourceToString(String resourceString) throws IOException {
protected static String readResourceToString(String resourceString) throws IOException {
URL url = Resources.getResource(resourceString);
return Resources.toString(url, Charset.forName("UTF-8"));
}

protected ReleaseId deployTestdataKjar(String artifactid, String kmodulePath, String solverConfigPath)
throws IOException {
ReleaseId releaseId = kieServices.newReleaseId("org.optaplanner.core.test", artifactid, "1.0.0");

String kmodule = readResourceToString(kmodulePath);
Resource valueClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataValue.java",
"testdata/kjar/ClassloadedTestdataValue.java");
Resource entityClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataEntity.java",
"testdata/kjar/ClassloadedTestdataEntity.java");
Resource solutionClass = buildResource("org/optaplanner/core/impl/testdata/domain/classloaded/ClassloadedTestdataSolution.java",
"testdata/kjar/ClassloadedTestdataSolution.java");
Resource scoreRules = buildResource("org/optaplanner/core/api/solver/kieContainerTestdataScoreRules.drl",
"testdata/kjar/scoreRules.drl");
Resource solverConfig = buildResource(solverConfigPath,
"testdata/kjar/solverConfig.solver");

createAndDeployJar(kieServices, kmodule, releaseId,
valueClass, entityClass, solutionClass,
scoreRules, solverConfig);
return releaseId;
}

// ************************************************************************
// Test methods
// ************************************************************************

@Test
public void buildSolverWithReleaseId() {
public void buildSolverWithReleaseId() throws IOException {
ReleaseId releaseId = deployTestdataKjar(
"buildSolverWithReleaseId",
"org/optaplanner/core/api/solver/kieContainerNamedKsessionKmodule.xml",
"org/optaplanner/core/api/solver/kieContainerNamedKsessionTestdataSolverConfig.xml");
SolverFactory<TestdataSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(
releaseId, "testdata/kjar/kieContainerTestdataSolverConfig.solver");
releaseId, "testdata/kjar/solverConfig.solver");
Solver<TestdataSolution> solver = solverFactory.buildSolver();
assertNotNull(solver);
}

@Test
public void buildSolverWithKieContainer() {
public void buildSolverWithKieContainer() throws IOException {
ReleaseId releaseId = deployTestdataKjar(
"buildSolverWithKieContainer",
"org/optaplanner/core/api/solver/kieContainerNamedKsessionKmodule.xml",
"org/optaplanner/core/api/solver/kieContainerNamedKsessionTestdataSolverConfig.xml");
KieContainer kieContainer = kieServices.newKieContainer(releaseId);
SolverFactory<TestdataSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(
kieContainer, "testdata/kjar/kieContainerTestdataSolverConfig.solver");
kieContainer, "testdata/kjar/solverConfig.solver");
Solver<TestdataSolution> solver = solverFactory.buildSolver();
assertNotNull(solver);
}

@Test
public void buildScanAnnotatedSolverWithReleaseId() {
public void buildScanAnnotatedClassesSolver() throws IOException {
ReleaseId releaseId = deployTestdataKjar(
"buildScanAnnotatedClassesSolver",
"org/optaplanner/core/api/solver/kieContainerNamedKsessionKmodule.xml",
"org/optaplanner/core/api/solver/scanAnnotatedKieContainerTestdataSolverConfig.xml");
SolverFactory<TestdataSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(
releaseId, "testdata/kjar/scanAnnotatedKieContainerTestdataSolverConfig.solver");
releaseId, "testdata/kjar/solverConfig.solver");
Solver<TestdataSolution> solver = solverFactory.buildSolver();
assertNotNull(solver);
}

@Test
public void buildScanAnnotatedSolverWithKieContainer() {
KieContainer kieContainer = kieServices.newKieContainer(releaseId);
public void buildSolverWithDefaultKsessionKmodule() throws IOException {
ReleaseId releaseId = deployTestdataKjar(
"buildSolverWithDefaultKsessionKmodule",
"org/optaplanner/core/api/solver/kieContainerDefaultKsessionKmodule.xml",
"org/optaplanner/core/api/solver/kieContainerDefaultKsessionTestdataSolverConfig.xml");
SolverFactory<TestdataSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(
releaseId, "testdata/kjar/solverConfig.solver");
Solver<TestdataSolution> solver = solverFactory.buildSolver();
assertNotNull(solver);
}

@Test
public void buildSolverWithEmptyKmodule() throws IOException {
ReleaseId releaseId = deployTestdataKjar(
"buildSolverWithEmptyKmodule",
"org/optaplanner/core/api/solver/kieContainerEmptyKmodule.xml",
"org/optaplanner/core/api/solver/kieContainerDefaultKsessionTestdataSolverConfig.xml");
SolverFactory<TestdataSolution> solverFactory = SolverFactory.createFromKieContainerXmlResource(
kieContainer, "testdata/kjar/scanAnnotatedKieContainerTestdataSolverConfig.solver");
releaseId, "testdata/kjar/solverConfig.solver");
Solver<TestdataSolution> solver = solverFactory.buildSolver();
assertNotNull(solver);
}
Expand Down
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
<!-- This file is copy pasted to the META-INF director in the KieContainer tests to avoid false positive tests -->
<kbase packages="testdata.kjar">
<ksession name="testdataKsession" default="true"/>
</kbase>
</kmodule>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is copy pasted to another package in the KieContainer test to avoid false positive tests -->
<solver>
<solutionClass>testdata.kjar.ClassloadedTestdataSolution</solutionClass>
<entityClass>testdata.kjar.ClassloadedTestdataEntity</entityClass>

<!-- Score configuration -->
<scoreDirectorFactory>
<scoreDefinitionType>SIMPLE</scoreDefinitionType>
</scoreDirectorFactory>
</solver>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
<!-- This file is copy pasted to the META-INF director in the KieContainer tests to avoid false positive tests -->
</kmodule>

0 comments on commit d42debd

Please sign in to comment.