Commit 23d46c82 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.

Initial commit

parents
Due to ongoing efforts to ensure you have the best possible learning experience, we may update the working files for this course from time to time.
We recommend that you download the latest version of these files from:
http://www.infiniteskills.com/02337
\ No newline at end of file
= CJP-Examples: files for _Checking Java Programs_
I've been an advocate of advanced techniques and strong testing for most of my life as a software developer.
This repository was first set up in 2007 (in CVS; moved to GitHub in 2015).
It was originally built with Ant but has mostly been moved to Maven to simplify classpath.
Classpath is also the reason I chose to split this repo into
*multiple projects*, each an Eclipse project and a Maven-buildable project.
Otherwise the classpath (and the Maven pom file) would become overly large.
Not everything builds or runs at the moment: patches welcome; complaints, not so much.
A few parts of this will indeed likely stay on Ant because
one or two of the tools are only available as Ant plugins.
While I do know about the maven-ant-plugin, it's still just easier this way.
There are several main directories:
. _general_ is the catch-all containing most demos for JUnit, PMD, Findbugs and JPF;
. _spock_ contains the Spock examples;
. _junit5_ contains forward-looking statements about the next release of JUnit.
. _pmd_ contains some examples of PMD
. _findbugs_ contains some examples of FindBugs
. _jpf_ - not maintained, contains JPF examples from 2007, using Ant.
== The Sad Tale of JPF
JavaPathFinder (JPF) is a really nice set of tools for examine state modelling of your Java code.
It was created at NASA to verify the software used in the Mars Rover control program
(not the embedded software in the device, but the ground-based controller software).
Unfortunately, it has never been easy to get started with, nor has it made the leap from
Ant to Maven - although a third-party project set up Maven repositories, these have become
lost in the sands of time. There is a jpf-maven-plugin project which you can find and build,
but since the repositories themselves are no longer available, it is probably pointless.
"Look on my works, ye mighty, and despair..."
== Attention Users of the 2007 Book Version
The head commit of the master branch will be totally broken if you try to follow
the directions in the 2007 edition of the book. If you still want to follow a
decade-old book you should only have to do `git checkout a1ad04` after cloning the git repo.
When you install it YOU WILL HAVE TO SET some Eclipse variables
before it will work! This is expected. Emails asking why it doesn't
work out of the box when you haven't read this file are probably going
to be ignored, but you won't know that if you didn't read
this file; that's why it's called a README file, I suppose.
The details are in the 2007 book.
== Contact
Thanks for reading/watching _Checking Java Programs_!
Ian Darwin
http://cjp.darwinsys.com/[Book website]
http://shop.oreilly.com/product/9780596510237.do[Order the 2007 book from O'Reilly]
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cjp-arquillian</name>
<comment>Arquillian - keeps the bugs away</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.jboss.tools.jst.web.kb.kbbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.jboss.tools.cdi.core.cdibuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.jboss.tools.jst.web.kb.kbnature</nature>
<nature>org.jboss.tools.cdi.core.cdinature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=arquillian-jbossas-remote
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?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>
<groupId>com.darwinsys.cjp</groupId>
<artifactId>cjp-arquillian</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<inceptionYear>2015</inceptionYear>
<name>cjp-arquillian</name>
<url>http://cjp.darwinsys.com/</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<javase.version>1.8</javase.version>
<javaee.version>1.7</javaee.version>
<maven.compiler.source>${javase.version}</maven.compiler.source>
<maven.compiler.target>${javase.version}</maven.compiler.target>
<maven.compiler.testSource>${javase.version}</maven.compiler.testSource>
<maven.compiler.testTarget>${javase.version}</maven.compiler.testTarget>
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>arquillian-weld-ee-embedded</id>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-7.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>1.1.9.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile><id>arquillian-glassfish-embedded</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile><id>arquillian-jbossas-remote</id>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-remote</artifactId>
<version>8.1.0.CR1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
package arqdemo;
import javax.inject.Inject;
/**
* A POJO that uses a Helloifier injected by CDI.
*/
public class Hello {
@Inject
Helloifier hello;
public String demo() {
return hello.hello();
}
public String demo(String s) {
return hello.hello(s);
}
}
package arqdemo;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named @RequestScoped
public class Helloifier implements Serializable {
private static final long serialVersionUID = 1L;
public String hello() {
return "Hello, world";
}
public String hello(String s) {
return "Hello, " + s;
}
}
package arqdemo;
import javax.ejb.Singleton;
import javax.inject.Inject;
/**
* A SFSB that uses a Helloifier injected by CDI.
*/
@Singleton
public class WelcomeEjb {
@Inject
Helloifier hello;
public String greet(String name) {
return hello.hello(name);
}
public String greet() {
return hello.hello();
}
}
package arqdemo;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
@RunWith(Arquillian.class)
public class HelloTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Hello.class, Helloifier.class)
// Needed on older Java EE (5 and 6) to trigger CDI startup
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject
Hello hello;
@Test
public void should_create_greeting() {
assertEquals("Hello, world", hello.demo());
assertEquals("Hello, Java Developer", hello.demo("Java Developer"));
}
}
package arqdemo;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import javax.ejb.EJB;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(Arquillian.class)
public class WelcomeEjbTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "helloejb.jar")
.addClasses(WelcomeEjbTest.class, WelcomeEjb.class, Helloifier.class);
}
@EJB
WelcomeEjb hello;
@Test
@Ignore // So Arquillian tests will pass "by default" with the weld profile, minimal dependency.
public void test() throws IOException {
String greeting = hello.greet("Jo");
// Check out the response
assertEquals("Hello, Jo", greeting);
}
}
#!/bin/sh
# For some reason Arquillian requires this jar, even though it's probably
# never used.
# The version number of 'jdk' is crude, but, running this does seem to
# partly solve the dependency problem - removing all but one error.
# Change the argument after -Dfile= to whereever this jar is located in your JDK
mvn install:install-file -Dfile=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/lib/jconsole.jar -DgroupId=sun.jdk -DartifactId=jconsole -Dversion=jdk -Dpackaging=jar
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cjp-coverage</name>
<comment>Measure the coverage of your test cases.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<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.darwinsys.cjp</groupId>
<artifactId>cjp-coverage</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<inceptionYear>2007</inceptionYear>
<name>Checking Java Programs - Cobertura</name>
<description>
Measure the coverage of your test cases.
</description>
<url>http://darwinsys.com/cjp/</url>
<licenses>
<license>
<name>BSD 2-Clause "New" or "Revised" license</name>
<url>http://opensource.org/licenses/BSD-2-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>ian</id>
<name>Ian Darwin</name>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<javase.version>1.8</javase.version>
<maven.compiler.source>${javase.version}</maven.compiler.source>
<maven.compiler.target>${javase.version}</maven.compiler.target>
<maven.compiler.testSource>${javase.version}</maven.compiler.testSource>
<maven.compiler.testTarget>${javase.version}</maven.compiler.testTarget>
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>
</properties>
<dependencies>
<!-- JUNIT -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
package cjp;
/**
* A simple sequence: getNext() returns the next value, etc.
*/
public class Demo {
int i = 0;
public int getNext() {
return i++;
}
public void add(int i) {
this.i = 0;
}
public void reset() {