Skip to content

Commit

Permalink
Merge pull request #25 from TheMrMilchmann/master
Browse files Browse the repository at this point in the history
Add support for TestNG
  • Loading branch information
zyxist committed Mar 22, 2018
2 parents fab4300 + 7311774 commit 028e726
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/com/zyxist/chainsaw/ChainsawPlugin.java
Expand Up @@ -50,6 +50,7 @@ public class ChainsawPlugin implements Plugin<Project> {
private static final TestEngine[] TEST_ENGINES = {
new JUnit4(),
new JUnit5(),
new TestNG(),
new NoTestEngine()
};

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/zyxist/chainsaw/JavaModule.java
Expand Up @@ -26,6 +26,7 @@ public class JavaModule {
private String moduleName;
private boolean allowModuleNamingViolations = false;
private List<String> addTestModules = new ArrayList<>();
private List<String> exportTestPackages = new ArrayList<>();
private JavaModuleHacks hacks = new JavaModuleHacks();

public String getName() {
Expand All @@ -52,6 +53,14 @@ public void setExtraTestModules(List<String> testModules) {
this.addTestModules = testModules;
}

public List<String> getExportedTestPackages() {
return exportTestPackages;
}

public void setExportedTestPackages(List<String> testPackages) {
this.exportTestPackages = testPackages;
}

public void hacks(Action<? super JavaModuleHacks> action) {
action.execute(hacks);
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/zyxist/chainsaw/tests/TestNG.java
@@ -0,0 +1,43 @@
/*
* Copyright 2017 the original author or authors.
*
* 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 com.zyxist.chainsaw.tests;

import com.zyxist.chainsaw.ChainsawPlugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

import java.util.Arrays;
import java.util.List;

public class TestNG implements TestEngine {
private static final Logger LOGGER = Logging.getLogger(ChainsawPlugin.class);

private static final String TESTNG_GROUP = "org.testng";
private static final String TESTNG_ARTIFACT = "testng";

private static final String TESTNG_MODULE_NAME = "testng";

@Override
public boolean accepts(Project project) {
return TestEngine.checkTestDependencies(project, dep -> dep.getGroup().equals(TESTNG_GROUP) && dep.getName().equals(TESTNG_ARTIFACT));
}

@Override
public List<String> getTestEngineModules() {
return Arrays.asList(TESTNG_MODULE_NAME);
}
}
Expand Up @@ -19,6 +19,7 @@
import com.zyxist.chainsaw.TaskConfigurator;
import com.zyxist.chainsaw.algorithms.ModulePatcher;
import com.zyxist.chainsaw.jigsaw.JigsawCLI;
import com.zyxist.chainsaw.jigsaw.cli.ExportItem;
import com.zyxist.chainsaw.jigsaw.cli.PatchItem;
import com.zyxist.chainsaw.jigsaw.cli.ReadItem;
import org.gradle.api.Action;
Expand Down Expand Up @@ -62,6 +63,10 @@ public Optional<Action<Task>> doFirst(Project project, final Test testTask) {
.read(new ReadItem(moduleConfig.getName())
.toAll(testEngine.getTestEngineModules())
.toAll(moduleConfig.getExtraTestModules()));
moduleConfig.getExportedTestPackages().forEach(exportTestPackage -> cli.exportList()
.export(new ExportItem(moduleConfig.getName(), exportTestPackage)
.toAll(testEngine.getTestEngineModules())));

moduleConfig.getHacks().applyHacks(cli);
patcher
.patchFrom(project, PATCH_CONFIGURATION_NAME)
Expand Down
Expand Up @@ -20,6 +20,7 @@ public class Dependencies {
public static final String JUNIT5_PLUGIN_DEPENDENCY = "org.junit.platform:junit-platform-gradle-plugin:1.0.0";
public static final String JUNIT5_API_DEPENDENCY = "org.junit.jupiter:junit-jupiter-api:5.0.0";
public static final String JUNIT5_ENGINE_DEPENDENCY = "org.junit.jupiter:junit-jupiter-engine:5.0.0";
public static final String TESTNG_DEPENDENCY = "org.testng:testng:6.14.2";
public static final String MOCKITO_DEPENDENCY = "org.mockito:mockito-core:2.11.0";
public static final String GUAVA_DEPENDENCY = "com.google.guava:guava:23.2-jre";
public static final String GUICE_DEPENDENCY = "com.google.inject:guice:4.2.0";
Expand Down
Expand Up @@ -49,6 +49,7 @@ public class JigsawProjectBuilder {
private final List<String> exportedPackages = new ArrayList<>();

private final List<String> extraTestModules = new ArrayList<>();
private final List<String> exportedTestPackages = new ArrayList<>();
private final List<String> openConfig = new ArrayList<>();
private final List<String> patchConfig = new ArrayList<>();

Expand Down Expand Up @@ -150,6 +151,11 @@ public JigsawProjectBuilder extraTestModule(String moduleName) {
return this;
}

public JigsawProjectBuilder exportedTestPackage(String pkg) {
this.exportedTestPackages.add(pkg);
return this;
}

public JigsawProjectBuilder openedModule(String opening, String openedPackage, String destination) {
this.openConfig.add("opens('"+opening+"', '"+openedPackage+"', '"+destination+"')");
return this;
Expand Down Expand Up @@ -314,8 +320,11 @@ private void generateApplicationConfig(StringBuilder build) {
}

private void generateTestConfig(StringBuilder build) {
if (!testJvmArgs.isEmpty()) {
if (!testJvmArgs.isEmpty() || !testCompileDependencies.isEmpty()) {
build.append("test {\n");
if (testCompileDependencies.contains(Dependencies.TESTNG_DEPENDENCY)) {
build.append(" useTestNG()");
}
for (String arg: testJvmArgs) {
build.append(" jvmArgs '" + arg + "'\n");
}
Expand All @@ -333,6 +342,9 @@ private void generateJavaModuleConfig(StringBuilder build) {
if (!extraTestModules.isEmpty()) {
build.append("javaModule.extraTestModules = ['" + String.join("', '", extraTestModules) + "']\n");
}
if (!exportedTestPackages.isEmpty()) {
build.append("javaModule.exportedTestPackages = ['" + String.join("', '", exportedTestPackages) + "']\n");
}
if (!patchConfig.isEmpty() || ! openConfig.isEmpty()) {
build.append("javaModule.hacks {\n");
for (String open : openConfig) {
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright 2017 the original author or authors.
*
* 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 com.zyxist.chainsaw.builder.factory

import com.zyxist.chainsaw.builder.JavaCodeFactory
import com.zyxist.chainsaw.builder.JigsawProjectBuilder

class TestNGExportedTestPackageTestFactory implements JavaCodeFactory {
boolean mocks;

static def exportedTestPackageTest() {
def factory = new TestNGExportedTestPackageTestFactory()
factory.mocks = false
return factory
}

static def exportedTestPackageTestWithMocks() {
def factory = new TestNGExportedTestPackageTestFactory()
factory.mocks = true
return factory
}

@Override
String getFilename(JigsawProjectBuilder builder) {
return builder.getPackageName().replace(".", "/") + "/test/AClassTest.java"
}

@Override
String generateCode(JigsawProjectBuilder builder) {
def mockitoImport = ""
def mockitoUsage = ""
if (mocks) {
mockitoImport = "import static org.mockito.Mockito.mock;"
mockitoUsage = "Object obj = mock(Object.class);"
}
return """
package ${builder.getPackageName()}.test;
import ${builder.getPackageName()}.AClass;
${mockitoImport}
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
public class AClassTest {
@Test
public void isAnInstanceOfAClass() {
${mockitoUsage}
assertTrue(new AImplementation() instanceof AClass);
}
static class AImplementation extends AClass {
@Override
public void aMethod(String aString) {
// Do nothing
}
}
}
"""
}
}
@@ -0,0 +1,72 @@
/*
* Copyright 2017 the original author or authors.
*
* 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 com.zyxist.chainsaw.builder.factory

import com.zyxist.chainsaw.builder.JavaCodeFactory
import com.zyxist.chainsaw.builder.JigsawProjectBuilder

class TestNGSampleTestFactory implements JavaCodeFactory {
boolean mocks;

static def testngTest() {
def factory = new TestNGSampleTestFactory()
factory.mocks = false
return factory
}

static def testngTestWithMocks() {
def factory = new TestNGSampleTestFactory()
factory.mocks = true
return factory
}

@Override
String getFilename(JigsawProjectBuilder builder) {
return builder.getPackageName().replace(".", "/") + "/AClassTest.java"
}

@Override
String generateCode(JigsawProjectBuilder builder) {
def mockitoImport = ""
def mockitoUsage = ""
if (mocks) {
mockitoImport = "import static org.mockito.Mockito.mock;"
mockitoUsage = "Object obj = mock(Object.class);"
}
return """
package ${builder.getPackageName()};
${mockitoImport}
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
public class AClassTest {
@Test
public void isAnInstanceOfAClass() {
${mockitoUsage}
assertTrue(new AImplementation() instanceof AClass);
}
static class AImplementation extends AClass {
@Override
public void aMethod(String aString) {
// Do nothing
}
}
}
"""
}
}

0 comments on commit 028e726

Please sign in to comment.