From 6ba9282176827b06660a4ed70f07f655c792b54e Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 14:58:53 +0100
Subject: [PATCH 01/19] try and get travis-ci.com working
---
source/.travis.yml | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 source/.travis.yml
diff --git a/source/.travis.yml b/source/.travis.yml
new file mode 100644
index 0000000..07a3228
--- /dev/null
+++ b/source/.travis.yml
@@ -0,0 +1,2 @@
+language: java
+mvn test
\ No newline at end of file
From 039592cf29a024257ab62162c16016f96685ee4f Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:01:40 +0100
Subject: [PATCH 02/19] moved travis.yml to root
---
source/.travis.yml => .travis.yml | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename source/.travis.yml => .travis.yml (100%)
diff --git a/source/.travis.yml b/.travis.yml
similarity index 100%
rename from source/.travis.yml
rename to .travis.yml
From 6b04e157ac82af3dacf8bba48c256b5b00a9962d Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:14:07 +0100
Subject: [PATCH 03/19] mvn test was not supposed to be in the travis file
---
.travis.yml | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 07a3228..f5c99a7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,2 +1 @@
-language: java
-mvn test
\ No newline at end of file
+language: java
\ No newline at end of file
From a0d2aea617eb24748dc53df3fb6b17960a257a9e Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:19:40 +0100
Subject: [PATCH 04/19] change the build step since everything is in a source
directory
---
.travis.yml | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index f5c99a7..fd0ca5e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1 +1,2 @@
-language: java
\ No newline at end of file
+language: java
+script: mvn test -f ./source/pom.xml
\ No newline at end of file
From 9bcaf69ae062d8d28faae71904dce1be5f99c9e3 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:36:21 +0100
Subject: [PATCH 05/19] added a pom for travis
file based tests may not have full permission on travis and hardcoded
drive dependencies
---
.travis.yml | 2 +-
source/travis-ci-pom.xml | 54 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 source/travis-ci-pom.xml
diff --git a/.travis.yml b/.travis.yml
index fd0ca5e..4af1e4c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,2 +1,2 @@
language: java
-script: mvn test -f ./source/pom.xml
\ No newline at end of file
+script: mvn test -f ./source/travis-ci-pom.xml
\ No newline at end of file
diff --git a/source/travis-ci-pom.xml b/source/travis-ci-pom.xml
new file mode 100644
index 0000000..a04a0f7
--- /dev/null
+++ b/source/travis-ci-pom.xml
@@ -0,0 +1,54 @@
+
+
+
+ 4.0.0
+
+ javaForTesters
+ javaForTesters
+ 1.0-SNAPSHOT
+ jar
+
+
+ UTF-8
+
+
+
+
+
+ junit
+ junit
+ 4.11
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.18.1
+
+
+
+ **/FileTest.java
+ **/PropertiesTest.java
+ **/PropertyExercisesTest.java
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.1
+
+ 1.7
+ 1.7
+
+
+
+
+
\ No newline at end of file
From dee6cd646cb8df4dd23e86a7bdba923dc1cf37f7 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:57:52 +0100
Subject: [PATCH 06/19] set the start of week
set the start of week to allow week of month calculation to work on
different configs
---
.../chap017_datestimes/exercises/DateTimeExercisesTest.java | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
index bc91848..553c2e3 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
@@ -76,6 +76,10 @@ public void useOtherCalendarConstants(){
public void experimentWithCalendarConstants(){
Calendar cal = Calendar.getInstance();
cal.set(2013, Calendar.DECEMBER, 15, 23,39, 54);
+ // week of month depends on first day of week
+ // some places use SUNDAY as first day
+ // set to MONDAY for our calculation
+ cal.setFirstDayOfWeek(Calendar.MONDAY);
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(1));
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(Calendar.SUNDAY));
From b841d0a057e754c72900e3cd403c2f492b202bc0 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 15:59:48 +0100
Subject: [PATCH 07/19] added file exercises as exclusion for travis
---
source/travis-ci-pom.xml | 1 +
1 file changed, 1 insertion(+)
diff --git a/source/travis-ci-pom.xml b/source/travis-ci-pom.xml
index a04a0f7..98de154 100644
--- a/source/travis-ci-pom.xml
+++ b/source/travis-ci-pom.xml
@@ -35,6 +35,7 @@
**/FileTest.java
+ **/FileExercisesTest.java
**/PropertiesTest.java
**/PropertyExercisesTest.java
From 99a41a789c81ad7aae25d45a84900d9cae9d8463 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 16:13:35 +0100
Subject: [PATCH 08/19] set minimal days in week as well
---
.../chap017_datestimes/exercises/DateTimeExercisesTest.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
index 553c2e3..029d52e 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
@@ -79,7 +79,9 @@ public void experimentWithCalendarConstants(){
// week of month depends on first day of week
// some places use SUNDAY as first day
// set to MONDAY for our calculation
+ // and control Minimdal Days in First Week
cal.setFirstDayOfWeek(Calendar.MONDAY);
+ cal.setMinimalDaysInFirstWeek(6);
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(1));
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(Calendar.SUNDAY));
From 784ff1195001b19d7cb8381534771e99f050a1c4 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 16:47:48 +0100
Subject: [PATCH 09/19] try different jdks
---
.travis.yml | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 4af1e4c..00d47d1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,2 +1,5 @@
language: java
-script: mvn test -f ./source/travis-ci-pom.xml
\ No newline at end of file
+script: mvn test -f ./source/travis-ci-pom.xml
+jdk:
+ - oraclejdk8
+ - oraclejdk7
\ No newline at end of file
From 901c78a33b176639f4618db50e57e1776619bce0 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 16:49:41 +0100
Subject: [PATCH 10/19] add openjdk7 too
---
.travis.yml | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 00d47d1..181de92 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,4 +2,5 @@ language: java
script: mvn test -f ./source/travis-ci-pom.xml
jdk:
- oraclejdk8
- - oraclejdk7
\ No newline at end of file
+ - oraclejdk7
+ - openjdk7
\ No newline at end of file
From 5e9ceb60b68fbe1e59abb859e3db1bf92b413d88 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Fri, 17 Jul 2015 17:24:19 +0100
Subject: [PATCH 11/19] updated readme to point to travis-ci.org
---
README.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 2a580ec..6b67901 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,9 @@ You can purchase the book on leanpub: https://leanpub.com/javaForTesters
* Twitter: @eviltester
### Note:
-This code is an extract from the actual source used to create the book. The actual source has embedded macros to allow the book to be automatically generated. While the extraction process has been tested, it may still contain errors. If you find differences from the book, or discover errors in this code, please let the author know. All tests have been run prior to the source release.
+This code is an extract from the actual source used to create the book. The actual source has embedded macros to allow the book to be automatically generated. While the extraction process has been tested, it may still contain errors. If you find differences from the book, or discover errors in this code, please let the author know. All @Test code has been run prior to the source release.
+
+You can see a cloud build of the code provided by [travis-ci.orgtravis-ci.org/eviltester/javaForTestersCode](https://travis-ci.org/eviltester/javaForTestersCode) Only file system level tests are excluded from the cloud build.
License
-------
From 4bfc15f30249adfe4fe8e803a41ef41a6387a346 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 21:07:27 +0100
Subject: [PATCH 12/19] amended after proof of print book
changed the File examples more than any other
---
source/reportingpom.xml | 2 +-
.../examples/classes/ASysOutJunitTest.java | 12 ++
.../examples/IntegerExamplesTest.java | 8 +-
.../examples/TestAppEnvironmentTest.java | 4 +-
.../examples/StatementsTest.java | 4 +-
.../exercises/JunitExercisesTest.java | 9 ++
.../examples/StringComparisonsTest.java | 18 ++-
.../examples/SimpleDateFormatTest.java | 1 +
.../exercises/DateTimeExercisesTest.java | 16 +-
.../examples/PropertiesTest.java | 5 +-
.../exercises/PropertyExercisesTest.java | 8 +-
.../chap019files/examples/FileTest.java | 147 ++++++++++++++----
.../exercises/FileExercisesTest.java | 35 ++++-
13 files changed, 206 insertions(+), 63 deletions(-)
create mode 100644 source/src/test/java/com/javafortesters/chap001basicsofjava/examples/classes/ASysOutJunitTest.java
diff --git a/source/reportingpom.xml b/source/reportingpom.xml
index ce11c2f..4c4ca97 100644
--- a/source/reportingpom.xml
+++ b/source/reportingpom.xml
@@ -15,7 +15,7 @@
http://maven.apache.org/surefire/maven-surefire-plugin/usage.html
http://maven.apache.org/surefire/maven-surefire-report-plugin/report-mojo.html
- mvn clean test -Dmaven.source=reportingpom.xml
+ mvn clean surefire-report:report -Dmaven.source=reportingpom.xml
-->
diff --git a/source/src/test/java/com/javafortesters/chap001basicsofjava/examples/classes/ASysOutJunitTest.java b/source/src/test/java/com/javafortesters/chap001basicsofjava/examples/classes/ASysOutJunitTest.java
new file mode 100644
index 0000000..2b1f564
--- /dev/null
+++ b/source/src/test/java/com/javafortesters/chap001basicsofjava/examples/classes/ASysOutJunitTest.java
@@ -0,0 +1,12 @@
+package com.javafortesters.chap001basicsofjava.examples.classes;
+
+import org.junit.Test;
+
+public class ASysOutJunitTest {
+
+ @Test
+ public void canOutputHelloWorldToConsole(){
+ AClassWithAMethod myClass = new AClassWithAMethod();
+ myClass.aMethodOnAClass();
+ }
+}
diff --git a/source/src/test/java/com/javafortesters/chap004testswithotherclasses/examples/IntegerExamplesTest.java b/source/src/test/java/com/javafortesters/chap004testswithotherclasses/examples/IntegerExamplesTest.java
index c1d0932..dd452a1 100644
--- a/source/src/test/java/com/javafortesters/chap004testswithotherclasses/examples/IntegerExamplesTest.java
+++ b/source/src/test/java/com/javafortesters/chap004testswithotherclasses/examples/IntegerExamplesTest.java
@@ -1,26 +1,20 @@
package com.javafortesters.chap004testswithotherclasses.examples;
import org.junit.Test;
-
import static org.junit.Assert.assertEquals;
public class IntegerExamplesTest {
@Test
public void integerExploration(){
-
Integer four = new Integer(4);
assertEquals("intValue returns int 4",
4, four.intValue());
-
Integer five = new Integer("5");
assertEquals("intValue returns int 5",
5, five.intValue());
-
Integer six = 6;
assertEquals("autoboxing assignment for 6",
6, six.intValue());
}
-
-
-}
\ No newline at end of file
+}
diff --git a/source/src/test/java/com/javafortesters/chap005testwithourownclasses/domainobject/examples/TestAppEnvironmentTest.java b/source/src/test/java/com/javafortesters/chap005testwithourownclasses/domainobject/examples/TestAppEnvironmentTest.java
index 2c0a821..6a8f64a 100644
--- a/source/src/test/java/com/javafortesters/chap005testwithourownclasses/domainobject/examples/TestAppEnvironmentTest.java
+++ b/source/src/test/java/com/javafortesters/chap005testwithourownclasses/domainobject/examples/TestAppEnvironmentTest.java
@@ -8,9 +8,11 @@ public class TestAppEnvironmentTest {
@Test
public void canGetUrlStatically(){
+
assertEquals("Returns Hard Coded URL",
"http://192.123.0.3:67",
- TestAppEnv.getUrl());
+ TestAppEnv.getUrl()
+ );
}
@Test
diff --git a/source/src/test/java/com/javafortesters/chap007basicsofjavarevisited/examples/StatementsTest.java b/source/src/test/java/com/javafortesters/chap007basicsofjavarevisited/examples/StatementsTest.java
index 5df6fc1..26a9df0 100644
--- a/source/src/test/java/com/javafortesters/chap007basicsofjavarevisited/examples/StatementsTest.java
+++ b/source/src/test/java/com/javafortesters/chap007basicsofjavarevisited/examples/StatementsTest.java
@@ -12,8 +12,8 @@ public void statements(){
assertEquals(4, 2+2);
assertEquals("2+2 always = 4",
- 4,
- 2+2);
+ 4,
+ 2+2);
}
}
\ No newline at end of file
diff --git a/source/src/test/java/com/javafortesters/chap014junit/exercises/JunitExercisesTest.java b/source/src/test/java/com/javafortesters/chap014junit/exercises/JunitExercisesTest.java
index b95ddce..49c4875 100644
--- a/source/src/test/java/com/javafortesters/chap014junit/exercises/JunitExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap014junit/exercises/JunitExercisesTest.java
@@ -46,8 +46,17 @@ public void junitHasAssertions(){
public void assertThatWithHamcrestMatchers(){
assertThat(3 + 3, is(6));
+
+ /* failing assert used to generate message in book
+ assertThat(3 + 3, is(7));
+ */
+
assertThat("3 + 3 = 6", 3 + 3, is(6));
+ /* failing assert used to generate message in book
+ assertThat("3 + 3 = 6", 3 + 3, is(7));
+ */
+
assertThat("false is false", false, equalTo(false));
assertThat(false, is(false));
diff --git a/source/src/test/java/com/javafortesters/chap015stringsrevisited/examples/StringComparisonsTest.java b/source/src/test/java/com/javafortesters/chap015stringsrevisited/examples/StringComparisonsTest.java
index 117342d..ede29bb 100644
--- a/source/src/test/java/com/javafortesters/chap015stringsrevisited/examples/StringComparisonsTest.java
+++ b/source/src/test/java/com/javafortesters/chap015stringsrevisited/examples/StringComparisonsTest.java
@@ -33,7 +33,7 @@ public void canCheckContainsOnString(){
assertThat(hello.contains("He"), is(true));
assertThat(hello.contains("Hello"), is(true));
- assertThat(hello.contains("he"), is(false));
+ assertThat(hello.contains("LL"), is(false));
assertThat(hello.contains("z"), is(false));
assertThat(hello.contains("world"), is(false));
@@ -107,6 +107,22 @@ public void checkRegionMatches(){
hello.regionMatches(true, 6, "fez", 0, 2),
is(true));
+ // case insensitive search
+ assertThat(
+ hello.regionMatches(true, 0, "he", 0, 2),
+ is(true));
+
+ // case sensitive search
+ assertThat(
+ hello.regionMatches(false, 0, "hE", 0, 2),
+ is(false));
+ assertThat(
+ hello.regionMatches(0, "hE", 0, 2),
+ is(false));
+ assertThat(
+ hello.regionMatches(0, "He", 0, 2),
+ is(true));
+
assertFalse(
hello.regionMatches(true, 3, "lady", 0, 2));
assertFalse(
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/examples/SimpleDateFormatTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/examples/SimpleDateFormatTest.java
index edaba7b..f155c3f 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/examples/SimpleDateFormatTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/examples/SimpleDateFormatTest.java
@@ -113,6 +113,7 @@ public void unusualDateFormatPatterns() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y M d HH:mm:ss.SSS");
Date date = sdf.parse("2013 12 15 23:39:54.123");
+
String[][] formatElement = {
{"w", "Week in the year"},
{"www", "Week in the year"},
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
index 029d52e..d3c856f 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
@@ -75,19 +75,21 @@ public void useOtherCalendarConstants(){
@Test
public void experimentWithCalendarConstants(){
Calendar cal = Calendar.getInstance();
- cal.set(2013, Calendar.DECEMBER, 15, 23,39, 54);
+ cal.set(2013, Calendar.DECEMBER, 15, 23, 39, 54);
+
+
+ assertThat(cal.get(Calendar.DAY_OF_WEEK), is(1));
+ assertThat(cal.get(Calendar.DAY_OF_WEEK), is(Calendar.SUNDAY));
+ assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
+ assertThat(cal.get(Calendar.DAY_OF_YEAR), is(349));
+
// week of month depends on first day of week
// some places use SUNDAY as first day
// set to MONDAY for our calculation
- // and control Minimdal Days in First Week
+ // and control Minimal Days in First Week
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(6);
-
- assertThat(cal.get(Calendar.DAY_OF_WEEK), is(1));
- assertThat(cal.get(Calendar.DAY_OF_WEEK), is(Calendar.SUNDAY));
assertThat(cal.get(Calendar.WEEK_OF_MONTH), is(2));
- assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
- assertThat(cal.get(Calendar.DAY_OF_YEAR), is(349));
}
@Test
diff --git a/source/src/test/java/com/javafortesters/chap018properties/examples/PropertiesTest.java b/source/src/test/java/com/javafortesters/chap018properties/examples/PropertiesTest.java
index 733b8e4..85b5f7c 100644
--- a/source/src/test/java/com/javafortesters/chap018properties/examples/PropertiesTest.java
+++ b/source/src/test/java/com/javafortesters/chap018properties/examples/PropertiesTest.java
@@ -84,8 +84,9 @@ public void simpleSaveLoadPropertiesFile() throws IOException {
String tempDirectory = System.getProperty("java.io.tmpdir");
String tempResourceFilePath =
- tempDirectory +
- "tempFileForPropertiesStoreTest.properties";
+ new File(tempDirectory,
+ "tempFileForPropertiesStoreTest.properties")
+ .getAbsolutePath();
Properties saved = new Properties();
saved.setProperty("prop1", "Hello");
diff --git a/source/src/test/java/com/javafortesters/chap018properties/exercises/PropertyExercisesTest.java b/source/src/test/java/com/javafortesters/chap018properties/exercises/PropertyExercisesTest.java
index c53de31..57eb000 100644
--- a/source/src/test/java/com/javafortesters/chap018properties/exercises/PropertyExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap018properties/exercises/PropertyExercisesTest.java
@@ -52,10 +52,10 @@ public void canAccessSystemProperties(){
public void canSaveAndLoadAPropertiesFile() throws IOException {
String tempDirectory = System.getProperty("java.io.tmpdir");
- String tempResourceFilePath = tempDirectory +
- System.currentTimeMillis() +
- System.nanoTime() +
- ".properties";
+ String tempResourceFilePath = new File(tempDirectory,
+ System.currentTimeMillis() +
+ System.nanoTime() +
+ ".properties").getAbsolutePath();
Properties saved = new Properties();
diff --git a/source/src/test/java/com/javafortesters/chap019files/examples/FileTest.java b/source/src/test/java/com/javafortesters/chap019files/examples/FileTest.java
index 98d5435..33623a0 100644
--- a/source/src/test/java/com/javafortesters/chap019files/examples/FileTest.java
+++ b/source/src/test/java/com/javafortesters/chap019files/examples/FileTest.java
@@ -1,18 +1,32 @@
package com.javafortesters.chap019files.examples;
-import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.StringEndsWith.endsWith;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
public class FileTest {
+ // when running cross platform there are differences to be aware of
+ // the system properties may not have a / at the end
+ // and building file paths by hand can be problematic requiring
+ // lots of care
+ // so we use the Paths.get instead
+ // http://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html
+ // Then use the returned Path object toFile to create the file object
+ // Both Path and Paths have been available since Java 1.7
+ // https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html
+
@Test
public void aNewFileDoesNotCreateAFile() throws IOException {
@@ -20,7 +34,29 @@ public void aNewFileDoesNotCreateAFile() throws IOException {
assertThat(aTempFile.exists(), is(false));
}
+ @Test
+ public void pathsGetExampleForCrossPlatform() throws IOException {
+
+ String tempDir = System.getProperty("java.io.tmpdir");
+
+ // you would probably just use File for this one
+ File theFile = Paths.get(tempDir, "file.txt").toFile();
+ assertThat(theFile.getAbsolutePath(),
+ endsWith(File.separator + "file.txt"));
+
+ theFile = Paths.get(tempDir, "temp1", "file.txt").toFile();
+ assertThat(theFile.getAbsolutePath(),
+ endsWith(String.format("%s%s%1$s%s",
+ File.separator, "temp1", "file.txt")));
+ theFile = Paths.get(tempDir, "temp1","temp2", "temp3", "file.txt").toFile();
+ assertThat(theFile.getAbsolutePath(),
+ endsWith(String.format("%s%s%1$s%s%1$s%s%1$s%s",
+ File.separator, "temp1", "temp2", "temp3", "file.txt")));
+ }
+
+
+/*
@Test
public void createAFileAndDeleteIt() throws IOException {
File aTempFile = new File("d:/tempJavaForTesters.txt");
@@ -32,11 +68,36 @@ public void createAFileAndDeleteIt() throws IOException {
aTempFile.delete();
assertThat(aTempFile.exists(), is(false));
}
+*/
+
+ // since the above example is commented out
+ // this checks that the basic code works
+ // in a platform independent way using Paths
+ @Test
+ public void createAFileAndDeleteItCodeCheck() throws IOException {
+
+ /* for documentation of simple path
+ File aTempFile = Paths.get("d:", "tempJavaForTesters.txt").toFile();
+ */
+
+ String tempDir = System.getProperty("java.io.tmpdir");
+ File aTempFile = Paths.get(tempDir, "tempJavaForTesters.txt").toFile();
+
+ assertThat(aTempFile.exists(), is(false));
+
+ aTempFile.createNewFile();
+ assertThat(aTempFile.exists(), is(true));
+
+ aTempFile.delete();
+ assertThat(aTempFile.exists(), is(false));
+ }
+
@Test
public void createAFileAndDeleteItAlternativeConstructor() throws IOException {
- File aTempFile = new File("d:", "tempJavaForTesters.txt");
+ String tempDir = System.getProperty("java.io.tmpdir");
+ File aTempFile = new File(tempDir, "tempJavaForTesters.txt");
assertThat(aTempFile.exists(), is(false));
aTempFile.createNewFile();
@@ -46,6 +107,26 @@ public void createAFileAndDeleteItAlternativeConstructor() throws IOException {
assertThat(aTempFile.exists(), is(false));
}
+ @Test
+ public void createLongerPathExample(){
+
+ String tempDirectory = System.getProperty("java.io.tmpdir");
+ File aFile = new File(tempDirectory);
+ aFile = new File(aFile, "1");
+ aFile = new File(aFile, "2");
+ aFile = new File(aFile, "3");
+ aFile = new File(aFile, "4");
+
+ // make it an easy cross platform comparison
+ String filePathWithDots = aFile.getAbsolutePath().replace(File.separator, ".");
+ assertTrue(filePathWithDots.endsWith(".1.2.3.4"));
+
+ Path aPath = Paths.get(tempDirectory, "1", "2", "3", "4");
+ assertEquals(aFile.getAbsolutePath(),
+ aPath.toFile().getAbsolutePath());
+ }
+
+
@Test
public void canonicalVsAbsolute() throws IOException {
@@ -73,9 +154,10 @@ public void canonicalVsAbsolute() throws IOException {
aFile.mkdirs();
System.out.println(oneDirectory.getAbsolutePath());
+ System.out.println(oneDirectory.getCanonicalPath());
System.out.println(aFile.getAbsolutePath());
- assertThat(oneDirectory.getAbsolutePath(), is(oneDirectory.getCanonicalPath()));
+ assertTrue(oneDirectory.getCanonicalPath().endsWith(oneDirectory.getAbsolutePath()));
File relative = new File(tempDirectory, currentMillis);
relative = new File(relative, "1");
@@ -88,7 +170,7 @@ public void canonicalVsAbsolute() throws IOException {
System.out.println(relative.getAbsolutePath());
- assertThat(oneDirectory.getAbsolutePath(), is(relative.getCanonicalPath()));
+ assertTrue(relative.getCanonicalPath().endsWith(oneDirectory.getAbsolutePath()));
relative = new File(tempDirectory, currentMillis);
relative = new File(relative, "1");
@@ -99,7 +181,7 @@ public void canonicalVsAbsolute() throws IOException {
System.out.println(relative.getAbsolutePath());
- assertThat(oneDirectory.getAbsolutePath(), is(relative.getCanonicalPath()));
+ assertTrue(relative.getCanonicalPath().endsWith(oneDirectory.getAbsolutePath()));
}
@@ -107,11 +189,11 @@ public void canonicalVsAbsolute() throws IOException {
public void mkdirsCreatesIntermediateDirs(){
String tempDirectory = System.getProperty("java.io.tmpdir");
- String newDirectoryStructure = tempDirectory +
- System.currentTimeMillis() +
- File.separator +
- System.currentTimeMillis();
- File aDirectory = new File(newDirectoryStructure);
+
+ File aDirectory = Paths.get(tempDirectory,
+ Long.toString(System.currentTimeMillis()),
+ Long.toString(System.currentTimeMillis()))
+ .toFile();
System.out.println(aDirectory.getAbsolutePath());
@@ -125,32 +207,35 @@ public void mkdirsCreatesDirectories(){
String tempDirectory = System.getProperty("java.io.tmpdir");
- String newDirectoryStructure = tempDirectory +
- System.currentTimeMillis();
- //File.separator +
- //System.currentTimeMillis();
+ File aFilePath = Paths.get(tempDirectory,
+ Long.toString(System.currentTimeMillis()),
+ "test.tmp")
+ .toFile();
- System.out.println(newDirectoryStructure);
+ System.out.println(aFilePath.getAbsolutePath());
// mkdir won't create this because it is temp/millis/test.tmp
- File aDirectory = new File(newDirectoryStructure, "test.tmp");
- assertThat(aDirectory.mkdir(), is(false));
- assertThat(aDirectory.exists(), is(false));
- // will create because it is just the temp/millis
- aDirectory = new File(newDirectoryStructure);
- assertThat(aDirectory.mkdir(), is(true));
+ assertThat(aFilePath.mkdir(), is(false));
+ assertThat(aFilePath.exists(), is(false));
+
+ // mkdir will create because it is just the temp/millis
+ File aDirectoryPath = Paths.get(tempDirectory,
+ Long.toString(System.currentTimeMillis()))
+ .toFile();
+
+ assertThat(aDirectoryPath.mkdir(), is(true));
}
@Test
public void fileAndPathSeparator(){
- Assert.assertTrue("Unrecognised OS file separator",
- File.separator.equals("\\") ||
- File.separator.equals("/") );
- Assert.assertTrue("Unrecognised OS path separator",
- File.pathSeparator.equals(";") ||
- File.pathSeparator.equals(":") );
+ assertTrue("Unrecognised OS file separator",
+ File.separator.equals("\\") ||
+ File.separator.equals("/"));
+ assertTrue("Unrecognised OS path separator",
+ File.pathSeparator.equals(";") ||
+ File.pathSeparator.equals(":"));
}
@Test
@@ -167,8 +252,8 @@ public void createATempFileAndDeleteOnExit(){
assertThat( aTempFile.getName().startsWith("prefix"), is(true));
assertThat( aTempFile.getName().endsWith("suffix"), is(true));
- assertThat( aTempFile.getParent() + File.separator,
- is(System.getProperty("java.io.tmpdir")));
+ assertTrue(System.getProperty("java.io.tmpdir").
+ startsWith(aTempFile.getParent()));
assertThat(aTempFile.getAbsolutePath().endsWith("suffix"),
is(true));
@@ -177,8 +262,8 @@ public void createATempFileAndDeleteOnExit(){
assertThat(aTempFile.getCanonicalPath().endsWith("suffix"),
is(true));
- assertThat(aTempFile.getCanonicalPath().startsWith(
- System.getProperty("java.io.tmpdir")), is(true));
+ assertThat(aTempFile.getCanonicalPath().contains(
+ System.getProperty("java.io.tmpdir")), is(true));
assertThat(aTempFile.exists(), is(true));
diff --git a/source/src/test/java/com/javafortesters/chap019files/exercises/FileExercisesTest.java b/source/src/test/java/com/javafortesters/chap019files/exercises/FileExercisesTest.java
index b53dcaf..f333f5b 100644
--- a/source/src/test/java/com/javafortesters/chap019files/exercises/FileExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap019files/exercises/FileExercisesTest.java
@@ -14,7 +14,8 @@ public class FileExercisesTest {
@Test
public void createTempFileVaryTheParameters() throws IOException {
- // on windows these files are in %TEMP%
+ // on Windows these files are in %TEMP%
+ // on Mac these files are in $TMPDIR
File temp1 = File.createTempFile("temp1", null);
File temp2 = File.createTempFile("temp2OutFile", ".out");
@@ -62,17 +63,37 @@ public void writeATestToCheckCanonicalConversion() throws IOException {
File absolute2 = new File("C:/1/2/../../1");
File canonical = new File("C:/1");
- assertThat(canonical.getAbsolutePath(),
- is(canonical.getCanonicalPath()));
- assertThat(canonical.getAbsolutePath(),
- is(absolute1.getCanonicalPath()));
- assertThat(canonical.getAbsolutePath(),
- is(absolute2.getCanonicalPath()));
+ assertThat(trimOsStuff(
+ canonical.getAbsolutePath()),
+ is(trimOsStuff(
+ canonical.getCanonicalPath())));
+ assertThat(trimOsStuff(
+ canonical.getAbsolutePath()),
+ is(trimOsStuff(
+ absolute1.getCanonicalPath())));
+ assertThat(trimOsStuff(
+ canonical.getAbsolutePath()),
+ is(trimOsStuff(
+ absolute2.getCanonicalPath())));
assertThat(absolute1.getAbsolutePath().contains(".."), is(true));
assertThat(absolute2.getAbsolutePath().contains(".."), is(true));
}
+ // The above code runs fine on a Mac and Windows without needing
+ // trimOsStuff
+ // but on Linux, it seems to add a ./ in the middle of the absolute path
+ // e.g. /home/travis/build/eviltester/javaForTestersCode/./source/C:/1
+ // So I add the following method to trim out the operating system addition to
+ // the path e.g. anything before the C:
+ private String trimOsStuff(String absolutePath) {
+ int posOfDrive = absolutePath.indexOf("C:");
+ String pathWithoutOsPrefixes = absolutePath.substring(posOfDrive);
+ System.out.println(String.format(
+ "trimOsStuff: %s became %s",absolutePath, pathWithoutOsPrefixes));
+ return pathWithoutOsPrefixes;
+ }
+
@Test
public void checkThatTheTempDirectoryIsADirectory(){
From d25eda2472599bb407d3bb7f890369d55567dae1 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 21:14:06 +0100
Subject: [PATCH 13/19] amended week of year
---
.../chap017_datestimes/exercises/DateTimeExercisesTest.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
index d3c856f..53c2fd0 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
@@ -80,7 +80,6 @@ public void experimentWithCalendarConstants(){
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(1));
assertThat(cal.get(Calendar.DAY_OF_WEEK), is(Calendar.SUNDAY));
- assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
assertThat(cal.get(Calendar.DAY_OF_YEAR), is(349));
// week of month depends on first day of week
@@ -90,6 +89,10 @@ public void experimentWithCalendarConstants(){
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(6);
assertThat(cal.get(Calendar.WEEK_OF_MONTH), is(2));
+
+ // Week of the year, similarly requires the
+ // config to control first day
+ assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
}
@Test
From 899695ea76dd230e7641b401ff8ef5d7b8d29fec Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 21:19:14 +0100
Subject: [PATCH 14/19] removed file excludes to check if tests are cross
platform enough to work in the cloud build now
---
source/travis-ci-pom.xml | 2 ++
1 file changed, 2 insertions(+)
diff --git a/source/travis-ci-pom.xml b/source/travis-ci-pom.xml
index 98de154..6f1c09c 100644
--- a/source/travis-ci-pom.xml
+++ b/source/travis-ci-pom.xml
@@ -34,10 +34,12 @@
+
From 6817ff4c1623062206688d70e931e55318092cc9 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 21:24:14 +0100
Subject: [PATCH 15/19] try travis on the standard pom.xml
---
.travis.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 181de92..dfa1b27 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,5 @@
language: java
-script: mvn test -f ./source/travis-ci-pom.xml
+script: mvn test -f ./source/pom.xml
jdk:
- oraclejdk8
- oraclejdk7
From 35609f15bea4286418244d5cd31157eaad697ac3 Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 21:30:48 +0100
Subject: [PATCH 16/19] removing travis pom
travis now works from the normal pom.xml
---
.../exercises/DateTimeExercisesTest.java | 8 +--
source/travis-ci-pom.xml | 57 -------------------
2 files changed, 4 insertions(+), 61 deletions(-)
delete mode 100644 source/travis-ci-pom.xml
diff --git a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
index 53c2fd0..0bdb95f 100644
--- a/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap017_datestimes/exercises/DateTimeExercisesTest.java
@@ -89,10 +89,10 @@ public void experimentWithCalendarConstants(){
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(6);
assertThat(cal.get(Calendar.WEEK_OF_MONTH), is(2));
-
- // Week of the year, similarly requires the
- // config to control first day
- assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
+
+ // Week of the year, similarly requires the
+ // config to control first day
+ assertThat(cal.get(Calendar.WEEK_OF_YEAR), is(50));
}
@Test
diff --git a/source/travis-ci-pom.xml b/source/travis-ci-pom.xml
deleted file mode 100644
index 6f1c09c..0000000
--- a/source/travis-ci-pom.xml
+++ /dev/null
@@ -1,57 +0,0 @@
-
-
-
- 4.0.0
-
- javaForTesters
- javaForTesters
- 1.0-SNAPSHOT
- jar
-
-
- UTF-8
-
-
-
-
-
- junit
- junit
- 4.11
-
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- 2.18.1
-
-
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- 3.1
-
- 1.7
- 1.7
-
-
-
-
-
\ No newline at end of file
From 1c0a2611d83641c0260f4d633ee5e19b63520dca Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 25 Aug 2015 22:17:43 +0100
Subject: [PATCH 17/19] added travis build image to readme
---
README.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 6b67901..e627674 100644
--- a/README.md
+++ b/README.md
@@ -22,7 +22,9 @@ You can purchase the book on leanpub: https://leanpub.com/javaForTesters
### Note:
This code is an extract from the actual source used to create the book. The actual source has embedded macros to allow the book to be automatically generated. While the extraction process has been tested, it may still contain errors. If you find differences from the book, or discover errors in this code, please let the author know. All @Test code has been run prior to the source release.
-You can see a cloud build of the code provided by [travis-ci.orgtravis-ci.org/eviltester/javaForTestersCode](https://travis-ci.org/eviltester/javaForTestersCode) Only file system level tests are excluded from the cloud build.
+You can see a cloud build of the code provided by [travis-ci.orgtravis-ci.org/eviltester/javaForTestersCode](https://travis-ci.org/eviltester/javaForTestersCode).
+
+
License
-------
From 530548e5d90e050b2f776f66745115f069fc682a Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Wed, 26 Aug 2015 16:42:27 +0100
Subject: [PATCH 18/19] moved file from chapter14 code into chapter 01
---
.../chap014junit/examples/ASysOutJunitTest.java | 13 -------------
1 file changed, 13 deletions(-)
delete mode 100644 source/src/test/java/com/javafortesters/chap014junit/examples/ASysOutJunitTest.java
diff --git a/source/src/test/java/com/javafortesters/chap014junit/examples/ASysOutJunitTest.java b/source/src/test/java/com/javafortesters/chap014junit/examples/ASysOutJunitTest.java
deleted file mode 100644
index 6d13835..0000000
--- a/source/src/test/java/com/javafortesters/chap014junit/examples/ASysOutJunitTest.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.javafortesters.chap014junit.examples;
-
-import com.javafortesters.chap001basicsofjava.examples.classes.AClassWithAMethod;
-import org.junit.Test;
-
-public class ASysOutJunitTest {
-
- @Test
- public void canOutputHelloWorldToConsole(){
- AClassWithAMethod myClass = new AClassWithAMethod();
- myClass.aMethodOnAClass();
- }
-}
From 753397934797304c364f495cf0712f79277928de Mon Sep 17 00:00:00 2001
From: eviltester <3K1aDZQQgLtvBXuI>
Date: Tue, 26 Jan 2016 09:52:11 +0000
Subject: [PATCH 19/19] fixed assertion error messages
a copy and paste error in the assertion error messages picked up by user
h34b2
---
.../exercises/IntegerExercisesTest.java | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/src/test/java/com/javafortesters/chap004testswithotherclasses/exercises/IntegerExercisesTest.java b/source/src/test/java/com/javafortesters/chap004testswithotherclasses/exercises/IntegerExercisesTest.java
index 80a954c..d43e721 100644
--- a/source/src/test/java/com/javafortesters/chap004testswithotherclasses/exercises/IntegerExercisesTest.java
+++ b/source/src/test/java/com/javafortesters/chap004testswithotherclasses/exercises/IntegerExercisesTest.java
@@ -20,11 +20,11 @@ public void canConfirmIntMinAndMaxLimits(){
public void canConvertIntToHex(){
assertEquals("hex 11 is b", "b",
Integer.toHexString(11));
- assertEquals("hex 10 is b", "a",
+ assertEquals("hex 10 is a", "a",
Integer.toHexString(10));
- assertEquals("hex 3 is b", "3",
+ assertEquals("hex 3 is 3", "3",
Integer.toHexString(3));
- assertEquals("hex 21 is b", "15",
+ assertEquals("hex 21 is 15", "15",
Integer.toHexString(21));
}
}
\ No newline at end of file