Skip to content

Commit 742ede0

Browse files
committed
Initial import of this Hello World.
1 parent ccd81ae commit 742ede0

File tree

7 files changed

+339
-1
lines changed

7 files changed

+339
-1
lines changed

.gitignore

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
target/
1+
# All Maven output goes here, compiled classes, generated code, etc.
2+
# Ignore only folders named target in root dir and direct child dirs.
3+
/target/
4+
*/target/
5+
6+
# Eclipse generated files.
7+
.settings
8+
.classpath
9+
.project
10+
# IntelliJ generated files.
11+
*.iml
12+
.idea
13+
14+
# Autogenerared OS X clutter.
15+
.DS_Store
16+
._.DS_Store
17+
# Windows explorer is just as bad.
18+
Thumbs.db
19+
20+
# Various text editor swap files, etc.
21+
.*.swp

pom.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>java-maven-junit-helloworld</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- Testing dependencies. -->
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.11</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.github.stefanbirkner</groupId>
28+
<artifactId>system-rules</artifactId>
29+
<version>1.4.0</version>
30+
<scope>test</scope>
31+
<!-- junit:junit-dep is deprecated, and junit:junit replaces it. -->
32+
<exclusions>
33+
<exclusion>
34+
<groupId>junit</groupId>
35+
<artifactId>junit-dep</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<!-- Configures the compiler. -->
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-compiler-plugin</artifactId>
47+
<version>3.1</version>
48+
<configuration>
49+
<source>1.6</source>
50+
<target>1.6</target>
51+
<compilerArgs>
52+
<arg>-Xlint</arg>
53+
</compilerArgs>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<!-- Unit tests are run by surefire. -->
58+
<!-- Classes under src/test/java called *Test are included automatically. -->
59+
<!-- Integration tests are run during the test phase. -->
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-surefire-plugin</artifactId>
62+
<version>2.16</version>
63+
</plugin>
64+
<plugin>
65+
<!-- Integration tests are run by failsafe. -->
66+
<!-- Classes under src/test/java called *IT are included automatically. -->
67+
<!-- Integration tests are run during the verify phase. -->
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-failsafe-plugin</artifactId>
70+
<version>2.16</version>
71+
<executions>
72+
<execution>
73+
<goals>
74+
<goal>integration-test</goal>
75+
<goal>verify</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
83+
<reporting>
84+
<plugins>
85+
<plugin>
86+
<!-- JUnit code coverage. -->
87+
<groupId>org.codehaus.mojo</groupId>
88+
<artifactId>cobertura-maven-plugin</artifactId>
89+
<version>2.6</version>
90+
</plugin>
91+
</plugins>
92+
</reporting>
93+
94+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.hellowithtests;
2+
3+
import java.io.PrintStream;
4+
5+
/**
6+
* Simple class that says "Hello!".
7+
*/
8+
public class Hello {
9+
10+
static final String HELLO = "Hello!";
11+
private short times;
12+
13+
/**
14+
* Construct a new Hello instance.
15+
*
16+
* @param times How many times should this class say "Hello!"? The value should be no larger than 20.
17+
* @throws IllegalArgumentException Thrown when times is larger than 20 or a negative number.
18+
*/
19+
public Hello(int times) {
20+
if (times < 0 || times > 20) {
21+
throw new IllegalArgumentException("Parameter «times» should be a positive integer no larger than 20.");
22+
}
23+
this.times = (short) times;
24+
}
25+
26+
/**
27+
* Say "Hello!".
28+
*
29+
* @param printer PrintStream to write output to.
30+
*/
31+
public void sayHello(PrintStream printer) {
32+
for (short i = 0; i < times; i++) {
33+
printer.println(HELLO);
34+
}
35+
}
36+
37+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.hellowithtests;
2+
3+
/**
4+
* A very basic program that demonstrates the use of JUnit tests. The tests include a sample unit test and an
5+
* integration test.
6+
*/
7+
public class HelloApp {
8+
9+
static int DEFAULT_TIMES = 3;
10+
11+
static int EXIT_STATUS_PARAMETER_NOT_UNDERSTOOD = 2;
12+
static int EXIT_STATUS_HELLO_FAILED = 4;
13+
14+
/**
15+
* The main method of this program.
16+
*
17+
* @param args Arguments passed to this program.
18+
*/
19+
public static void main(String[] args) {
20+
21+
int times = DEFAULT_TIMES;
22+
if (args.length >= 1) {
23+
try {
24+
times = Integer.valueOf(args[0]);
25+
} catch (NumberFormatException e) {
26+
System.err.println("I don't understand the parameter you passed me. Is it a number? " +
27+
"Parameter was: [" + args[0] + "]");
28+
System.exit(EXIT_STATUS_PARAMETER_NOT_UNDERSTOOD);
29+
}
30+
}
31+
32+
Hello hi = null;
33+
try {
34+
hi = new Hello(times);
35+
} catch (IllegalArgumentException e) {
36+
System.err.println("Something went wrong: " + e.getMessage());
37+
System.exit(EXIT_STATUS_HELLO_FAILED);
38+
}
39+
hi.sayHello(System.out);
40+
}
41+
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.hellowithtests;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
6+
7+
/**
8+
* Unit test for HelloApp.
9+
* <p/>
10+
* A unit test aims to test all code and code paths of a specific class.
11+
*/
12+
public class HelloAppTest {
13+
14+
@Rule
15+
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
16+
17+
@Test
18+
public void testMain() {
19+
String[] args = {"1"};
20+
HelloApp.main(args);
21+
}
22+
23+
@Test
24+
public void testHelloError() {
25+
exit.expectSystemExitWithStatus(HelloApp.EXIT_STATUS_HELLO_FAILED);
26+
String[] args = {"21"};
27+
HelloApp.main(args);
28+
}
29+
30+
@Test
31+
public void testWrongArgument() {
32+
exit.expectSystemExitWithStatus(HelloApp.EXIT_STATUS_PARAMETER_NOT_UNDERSTOOD);
33+
String[] args = {"bicycle"};
34+
HelloApp.main(args);
35+
}
36+
37+
@Test
38+
public void testDefaultArgument() {
39+
// Passing no arguments should work.
40+
String[] args = {};
41+
HelloApp.main(args);
42+
}
43+
44+
@Test
45+
public void classInstanceForCodeCoverageTest() {
46+
// Strictly speaking this test doesn't achieve anything, because HelloApp contains only a single static
47+
// method, but for purposes of full code coverage it is included. In general,
48+
// it is easier to aim for full code coverage and be done with it, than to remember why class X is stuck at
49+
// 95% code coverage.
50+
new HelloApp();
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.hellowithtests;
2+
3+
import org.junit.Test;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.OutputStream;
7+
import java.io.PrintStream;
8+
9+
import static org.hamcrest.CoreMatchers.*;
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Unit test for Hello.
14+
* <p/>
15+
* A unit test aims to test all code and code paths of a specific class.
16+
*/
17+
public class HelloTest {
18+
19+
@Test
20+
public void testSayHello() {
21+
OutputStream os = new ByteArrayOutputStream();
22+
PrintStream stream = new PrintStream(os, true);
23+
24+
Hello hi = new Hello(1);
25+
hi.sayHello(stream);
26+
27+
assertThat(os.toString(), is(equalTo(Hello.HELLO + "\n")));
28+
}
29+
30+
@Test
31+
public void testSayHelloAFewTimes() {
32+
OutputStream os = new ByteArrayOutputStream();
33+
PrintStream stream = new PrintStream(os, true);
34+
35+
Hello hi = new Hello(3);
36+
hi.sayHello(stream);
37+
38+
// Does it say "Hello!" three times?
39+
String goal = Hello.HELLO + "\n" + Hello.HELLO + "\n" + Hello.HELLO + "\n";
40+
assertThat(os.toString(), is(equalTo(goal)));
41+
}
42+
43+
@Test(expected = IllegalArgumentException.class)
44+
public void testIllegalArgumentForHello21() {
45+
new Hello(21);
46+
}
47+
48+
@Test(expected = IllegalArgumentException.class)
49+
public void testIllegalArgumentForHelloNegative() {
50+
new Hello(-1);
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.hellowithtests;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
6+
7+
import static org.hamcrest.CoreMatchers.*;
8+
import static org.junit.Assert.*;
9+
10+
/**
11+
* Integration test for the hellowithtests program.
12+
* <p/>
13+
* An integration test verifies the workings of a complete program, a module, or a set of dependant classes.
14+
* <p/>
15+
* This integration test uses system-rules, an extension for JUnit that lets you test System.out and System.exit()
16+
* etc.:
17+
* <p/>
18+
* http://www.stefan-birkner.de/system-rules
19+
*/
20+
public class HelloWithTestsIT {
21+
22+
@Rule
23+
public final StandardOutputStreamLog out = new StandardOutputStreamLog();
24+
25+
@Test
26+
public void doesItSayHelloTest() {
27+
String[] args = {"1"};
28+
HelloApp.main(args);
29+
30+
assertThat(out.getLog(), is(equalTo(Hello.HELLO + "\n")));
31+
}
32+
33+
@Test
34+
public void doesItSayHelloTest3() {
35+
String[] args = {"3"};
36+
HelloApp.main(args);
37+
38+
String thrice = Hello.HELLO + "\n" + Hello.HELLO + "\n" + Hello.HELLO + "\n";
39+
assertThat(out.getLog(), is(equalTo(thrice)));
40+
}
41+
}

0 commit comments

Comments
 (0)