This chapter continues from where Test coverage report using jacoco left us.

Using TestNG instead of JUnit

testng in addition to junit

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdefdef/src/main/java/com/example/wsdefdef/IwantTutorialWorkspaceProvider.java"
package com.example.wsdefdef;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleContext;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleProvider;
public class IwantTutorialWorkspaceProvider implements WorkspaceModuleProvider {
@Override
public JavaSrcModule workspaceModule(WorkspaceModuleContext ctx) {
return JavaSrcModule.with().name("iwant-tutorial-wsdef")
.locationUnderWsRoot("as-iwant-tutorial-developer/i-have/wsdef")
.mainJava("src/main/java").mainDeps(ctx.iwantApiModules())
.mainDeps(ctx.wsdefdefModule())
.mainDeps(ctx.iwantPlugin().jacoco().withDependencies()).end();
.mainDeps(ctx.iwantPlugin().jacoco().withDependencies())
.mainDeps(ctx.iwantPlugin().testng().withDependencies()).end();
}
@Override
public String workspaceFactoryClassname() {
return "com.example.wsdef.IwanttutorialWorkspaceFactory";
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact jcommander-1.48.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact testng-6.9.4.jar)
(0/1 D! org.fluentjava.iwant.api.javamodules.JavaClasses iwant-plugin-testng)
(0/1 D~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D~ org.fluentjava.iwant.api.core.Concatenated eclipse-settings.bin-refs)
(example-hello)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(example-helloutil)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdefdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)

target.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspace.java"
package com.example.wsdef;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.fluentjava.iwant.api.core.Concatenated;
import org.fluentjava.iwant.api.core.Concatenated.ConcatenatedBuilder;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.javamodules.JavaBinModule;
import org.fluentjava.iwant.api.javamodules.JavaModule;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule.IwantSrcModuleSpex;
import org.fluentjava.iwant.api.model.Path;
import org.fluentjava.iwant.api.model.SideEffect;
import org.fluentjava.iwant.api.model.Target;
import org.fluentjava.iwant.api.wsdef.SideEffectDefinitionContext;
import org.fluentjava.iwant.api.wsdef.TargetDefinitionContext;
import org.fluentjava.iwant.api.wsdef.WishDefinitionContext;
import org.fluentjava.iwant.api.wsdef.Workspace;
import org.fluentjava.iwant.core.download.TestedIwantDependencies;
import org.fluentjava.iwant.core.javamodules.JavaModules;
import org.fluentjava.iwant.eclipsesettings.EclipseSettings;
import org.fluentjava.iwant.plugin.jacoco.JacocoDistribution;
import org.fluentjava.iwant.plugin.jacoco.JacocoTargetsOfJavaModules;
import org.fluentjava.iwant.plugin.testng.TestngRunner;
public class IwanttutorialWorkspace implements Workspace {
static class ExampleModules extends JavaModules {
private WishDefinitionContext wishDefCtx;
class ExampleModules extends JavaModules {
@Override
protected IwantSrcModuleSpex commonSettings(IwantSrcModuleSpex m) {
return super.commonSettings(m).testDeps(junit);
}
final JavaBinModule asmAll = binModule("org/ow2/asm", "asm-all",
"5.0.1");
final JavaBinModule hamcrestCore = binModule("org/hamcrest",
"hamcrest-core", "1.3");
final JavaBinModule junit = binModule("junit", "junit", "4.11",
hamcrestCore);
final JavaBinModule jcommander = binModule(
TestedIwantDependencies.jcommander());
final JavaBinModule testng = binModule(TestedIwantDependencies.testng(),
jcommander);
final JavaSrcModule helloUtil = srcModule("example-helloutil")
.noMainResources().end();
final JavaSrcModule hello = srcModule("example-hello").noMainResources()
.mainDeps(helloUtil).end();
final JavaSrcModule testngUser = srcModule("example-testnguser")
.noMainResources().noTestResources()
.testDeps(wishDefCtx.iwantPlugin().testng().withDependencies())
.testRunner(TestngRunner.INSTANCE).end();
}
private final ExampleModules modules = new ExampleModules();
private ExampleModules modules(WishDefinitionContext ctx) {
this.wishDefCtx = ctx;
return new ExampleModules();
}
@Override
public List<? extends Target> targets(TargetDefinitionContext ctx) {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
jacocoReport(), classpathStringOfAll());
jacocoReport(ctx), classpathStringOfAll(ctx));
}
private Target jacocoReport() {
private Target jacocoReport(TargetDefinitionContext ctx) {
List<JavaModule> rtMods = new ArrayList<>();
rtMods.addAll(ctx.iwantPlugin().testng().withDependencies());
List<Path> rt = new ArrayList<>();
rt.add(TestedIwantDependencies.antJar());
rt.add(TestedIwantDependencies.antLauncherJar());
for (JavaModule mod : rtMods) {
rt.add(mod.mainArtifact());
}
return JacocoTargetsOfJavaModules.with()
.jacocoWithDeps(jacoco(), modules.asmAll.mainArtifact())
.antJars(TestedIwantDependencies.antJar(),
TestedIwantDependencies.antLauncherJar())
.modules(modules.allSrcModules()).end()
.jacocoWithDeps(jacoco(), modules(ctx).asmAll.mainArtifact())
.antJars(rt.toArray(new Path[0]))
.modules(modules(ctx).allSrcModules()).end()
.jacocoReport("jacoco-report");
}
private static JacocoDistribution jacoco() {
return JacocoDistribution.newestTestedVersion();
}
private Target classpathStringOfAll() {
private Target classpathStringOfAll(WishDefinitionContext ctx) {
ConcatenatedBuilder cp = Concatenated.named("all-as-cp");
cp.string(".");
for (Path jar : JavaModules
.mainArtifactJarsOf(modules.allSrcModules())) {
.mainArtifactJarsOf(modules(ctx).allSrcModules())) {
cp.string(File.pathSeparator).nativePathTo(jar);
}
return cp.end();
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.modules(modules.allSrcModules()).end());
.modules(modules(ctx).allSrcModules()).end());
}
}
~/iwant-tutorial $ mkdir -p example-testnguser/src/test/java/com/example/testnguser
~/iwant-tutorial $ $EDITOR "example-testnguser/src/test/java/com/example/testnguser/TestngExampleTest.java"
package com.example.testnguser;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
public class TestngExampleTest {
@Test
public void aTestngTest() {
assertEquals("Hello TestNG", TestngExample.message());
}
}
~/iwant-tutorial $ mkdir -p example-testnguser/src/main/java/com/example/testnguser
~/iwant-tutorial $ $EDITOR "example-testnguser/src/main/java/com/example/testnguser/TestngExample.java"
package com.example.testnguser;
public class TestngExample {
public static String message() {
throw new UnsupportedOperationException("TODO test and implement");
}
}

red phase

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/jacoco-report/as-path
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D~ org.fluentjava.iwant.plugin.jacoco.JacocoInstrumentation example-hello-main-classes.jacoco-instr)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-hello-main-classes.jacoco-instr.xml

example-hello-main-classes.jacoco-instr:
[jacoco:instrument] Instrumented 1 classes to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-hello-main-classes.jacoco-instr

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D~ org.fluentjava.iwant.plugin.jacoco.JacocoInstrumentation example-helloutil-main-classes.jacoco-instr)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-helloutil-main-classes.jacoco-instr.xml

example-helloutil-main-classes.jacoco-instr:
[jacoco:instrument] Instrumented 1 classes to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-helloutil-main-classes.jacoco-instr

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D~ org.fluentjava.iwant.plugin.jacoco.JacocoCoverage example-hello.jacococoverage)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-hello.jacococoverage.xml

example-hello.jacococoverage:
[java] JUnit version 4.11
[java] .
[java] Time: 0,103
[java]
[java] OK (1 test)
[java]

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D~ org.fluentjava.iwant.plugin.jacoco.JacocoCoverage example-helloutil.jacococoverage)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-helloutil.jacococoverage.xml

example-helloutil.jacococoverage:
[java] JUnit version 4.11
[java] .
[java] Time: 0,114
[java]
[java] OK (1 test)
[java]

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D! org.fluentjava.iwant.api.javamodules.JavaClasses example-testnguser-main-classes)
(0/1 D! org.fluentjava.iwant.api.javamodules.JavaClasses example-testnguser-test-classes)
(0/1 D! org.fluentjava.iwant.plugin.jacoco.JacocoInstrumentation example-testnguser-main-classes.jacoco-instr)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-testnguser-main-classes.jacoco-instr.xml

example-testnguser-main-classes.jacoco-instr:
[jacoco:instrument] Instrumented 1 classes to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-testnguser-main-classes.jacoco-instr

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D! org.fluentjava.iwant.api.core.ClassNameList example-testnguser-test-class-names)
(0/1 D! org.fluentjava.iwant.plugin.jacoco.JacocoCoverage example-testnguser.jacococoverage)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-testnguser.jacococoverage.xml

example-testnguser.jacococoverage:
[java] [TestNG] Running:
[java] Command line suite
[java]
[java]
[java] ===============================================
[java] Command line suite
[java] Total tests run: 1, Failures: 1, Skips: 0
[java] ===============================================
[java]

BUILD FAILED
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-testnguser.jacococoverage.xml:6: Java returned: 1

Total time: 0 seconds
(FAILED org.fluentjava.iwant.plugin.jacoco.JacocoCoverage example-testnguser.jacococoverage)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.fluentjava.iwant.entry.Iwant.runJavaMain(Iwant.java:591)
at org.fluentjava.iwant.entry.Iwant.evaluate(Iwant.java:265)
at org.fluentjava.iwant.entry.Iwant.main(Iwant.java:231)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.fluentjava.iwant.entry.Iwant.runJavaMain(Iwant.java:591)
at org.fluentjava.iwant.entry2.Iwant2.evaluate(Iwant2.java:77)
at org.fluentjava.iwant.entry2.Iwant2.main(Iwant2.java:50)
... 7 more
Caused by: java.lang.IllegalStateException: org.fluentjava.iwant.entry.Iwant$ExitCalledException
at org.fluentjava.iwant.planner.Planner.join(Planner.java:180)
at org.fluentjava.iwant.entry3.WishEvaluator.freshCachedContent(WishEvaluator.java:143)
at org.fluentjava.iwant.entry3.WishEvaluator.asPath(WishEvaluator.java:165)
at org.fluentjava.iwant.entry3.WishEvaluator.iwant(WishEvaluator.java:104)
at org.fluentjava.iwant.entry3.Iwant3.doEvaluate(Iwant3.java:172)
at org.fluentjava.iwant.entry3.Iwant3.evaluate(Iwant3.java:87)
at org.fluentjava.iwant.entry3.Iwant3.main(Iwant3.java:63)
... 14 more
Caused by: org.fluentjava.iwant.entry.Iwant$ExitCalledException
at org.fluentjava.iwant.entry.Iwant$ExitCatcher.checkExit(Iwant.java:627)
at java.lang.Runtime.exit(Runtime.java:107)
at java.lang.System.exit(System.java:971)
at org.apache.tools.ant.Main.exit(Main.java:264)
at org.apache.tools.ant.Main.startAnt(Main.java:254)
at org.apache.tools.ant.Main.start(Main.java:199)
at org.apache.tools.ant.Main.main(Main.java:287)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.fluentjava.iwant.entry.Iwant.runJavaMain(Iwant.java:591)
at org.fluentjava.iwant.api.antrunner.AntRunner.runAnt(AntRunner.java:22)
at org.fluentjava.iwant.plugin.jacoco.JacocoCoverage.path(JacocoCoverage.java:175)
at org.fluentjava.iwant.entry3.TargetRefreshTask.refresh(TargetRefreshTask.java:53)
at org.fluentjava.iwant.planner.Planner$Worker.run(Planner.java:135)
at java.lang.Thread.run(Thread.java:748)

green phase

~/iwant-tutorial $ mkdir -p example-testnguser/src/main/java/com/example/testnguser
~/iwant-tutorial $ $EDITOR "example-testnguser/src/main/java/com/example/testnguser/TestngExample.java"
package com.example.testnguser;
public class TestngExample {
public static String message() {
throw new UnsupportedOperationException("TODO test and implement");
return "Hello TestNG";
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/jacoco-report/as-path
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses example-testnguser-main-classes)
(0/1 org.fluentjava.iwant.api.javamodules.JavaClasses example-testnguser-test-classes)
(0/1 org.fluentjava.iwant.plugin.jacoco.JacocoInstrumentation example-testnguser-main-classes.jacoco-instr)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-testnguser-main-classes.jacoco-instr.xml

example-testnguser-main-classes.jacoco-instr:
[jacoco:instrument] Instrumented 1 classes to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-testnguser-main-classes.jacoco-instr

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 org.fluentjava.iwant.api.core.ClassNameList example-testnguser-test-class-names)
(0/1 D! org.fluentjava.iwant.plugin.jacoco.JacocoCoverage example-testnguser.jacococoverage)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/example-testnguser.jacococoverage.xml

example-testnguser.jacococoverage:
[java] [TestNG] Running:
[java] Command line suite
[java]
[java]
[java] ===============================================
[java] Command line suite
[java] Total tests run: 1, Failures: 0, Skips: 0
[java] ===============================================
[java]

BUILD SUCCESSFUL
Total time: 0 seconds
(0/1 D~ org.fluentjava.iwant.plugin.jacoco.JacocoReport jacoco-report)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/jacoco-report.xml

jacoco-report:
[echo] Generating /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/jacoco-report
[jacoco:report] Loading execution data file /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-hello.jacococoverage
[jacoco:report] Loading execution data file /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-helloutil.jacococoverage
[jacoco:report] Loading execution data file /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-testnguser.jacococoverage
[jacoco:report] Writing bundle 'jacoco-report' with 3 classes

BUILD SUCCESSFUL
Total time: 0 seconds
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/jacoco-report
~/iwant-tutorial $ cat as-iwant-tutorial-developer/.i-cached/target/jacoco-report/report.csv
GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
jacoco-report,com.example.hello,HelloMain,10,4,0,0,3,1,2,1,2,1
jacoco-report,com.example.helloutil,HelloUtil,3,11,0,0,1,1,1,1,1,1
jacoco-report,com.example.testnguser,TestngExample,3,2,0,0,1,1,1,1,1,1
Output asserted