From e53d48b0e43d36bb2c1bf615be6d320f46728643 Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 10:59:28 +0200 Subject: [PATCH 1/6] Skip tests in case API key is not entered. - Possibility to enter API key via system property - Upgraded JUnit to 4.12 --- README.md | 5 +- pom.xml | 2 +- .../java/de/meggsimum/w3w/What3WordsTest.java | 184 +++++++++--------- 3 files changed, 95 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index fb351b6..fdb119f 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,8 @@ try { JDK and Maven need to be installed on your system. * Clone this repository - * Enter your w3w API-key in the [What3WordsTest class](https://github.com/meggsimum/w3w-java-wrapper/blob/master/src/test/java/de/meggsimum/w3w/What3WordsTest.java#L17) - * Run ``mvn package`` or - * Run ``mvn package -DskipTests`` (in case you do not want to enter your API-key) + * Run ``mvn package -DW3W_API_KEY=YOUR-API-KEY`` or + * Run ``mvn package`` in case you do not want to enter your API key for test. Tests requiring API key will be skipped. * Under ``target/`` you will find the file ``w3w-java-wrapper--jar-with-dependencies.jar`` which can be embedded into your Java application ## Contributions diff --git a/pom.xml b/pom.xml index a26f133..a45ef49 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ junit junit - 3.8.1 + 4.12 test diff --git a/src/test/java/de/meggsimum/w3w/What3WordsTest.java b/src/test/java/de/meggsimum/w3w/What3WordsTest.java index c22beae..d023389 100644 --- a/src/test/java/de/meggsimum/w3w/What3WordsTest.java +++ b/src/test/java/de/meggsimum/w3w/What3WordsTest.java @@ -1,107 +1,107 @@ package de.meggsimum.w3w; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeNotNull; /** * Unit test for {@linkplain What3Words} - * + * * @author Christian Mayer, meggsimum */ -public class What3WordsTest extends TestCase { - - /** - * Ensure to set your API-Key here before running the test suite - */ - private static final String API_KEY = "YOUR-API-KEY-HERE"; +public class What3WordsTest { + + /** + * The api is read from command line arguments or can also be entered here manually. + */ + private String apiKey = null; + /** + * Name of the system property hodling the API key for W3W. + */ + public static final String API_KEY_PROPERTY = "W3W_API_KEY"; - /** - * Create the test case - * - * @param testName - * name of the test case - */ - public What3WordsTest(String testName) { - super(testName); - } + @Rule + public ExpectedException expectedException = ExpectedException.none(); - /** - * @return the suite of tests being tested - */ - public static Test suite() { - return new TestSuite(What3WordsTest.class); - } + /** + * Checks if the API key is either hard coded or passed via system properties. + * Only if this precondition is true, the tests are executed. + */ + @Before + public void beforeMethod() { + // Try to read the API key from system properties only in case it was not hard coded. + if (apiKey == null) { + apiKey = System.getProperty(API_KEY_PROPERTY); + } + assumeNotNull(apiKey); + } - /** - * Tests the words -> position API wrapper - */ - public void testWordsToPosition() { - What3Words w3w = new What3Words(API_KEY); - String[] words = {"goldfish", "fuzzy", "aggregates"}; - double[] coords; - try { - coords = w3w.wordsToPosition(words); - assertEquals(2, coords.length); - assertEquals(49.422636, coords[0]); - assertEquals(8.320833, coords[1]); - } catch (Exception e) { - assertTrue(false); - } - } + /** + * Tests the words -> position API wrapper + */ + @Test + public void testWordsToPosition() throws Exception { + What3Words w3w = new What3Words(apiKey); + String[] words = {"goldfish", "fuzzy", "aggregates"}; + double[] coords; + coords = w3w.wordsToPosition(words); + assertEquals(2, coords.length); + assertEquals(49.422636, coords[0], 0.1); + assertEquals(8.320833, coords[1], 0.1); + } - /** - * Tests the position -> words API wrapper - */ - public void testPositionToWords() { - What3Words w3w = new What3Words(API_KEY); - double[] coords = {49.422636, 8.320833}; - String[] words; - try { - words = w3w.positionToWords(coords); - assertEquals(3, words.length); - assertEquals("goldfish", words[0]); - assertEquals("fuzzy", words[1]); - assertEquals("aggregates", words[2]); - } catch (Exception e) { - assertTrue(false); - } - } + /** + * Tests the position -> words API wrapper + */ + @Test + public void testPositionToWords() throws Exception { + What3Words w3w = new What3Words(apiKey); + double[] coords = {49.422636, 8.320833}; + String[] words; + words = w3w.positionToWords(coords); + assertEquals(3, words.length); + assertEquals("goldfish", words[0]); + assertEquals("fuzzy", words[1]); + assertEquals("aggregates", words[2]); + } - /** - * Tests the position -> words API wrapper after changing the language - */ - public void testChangeLang() { - What3Words w3w = new What3Words(API_KEY); - w3w.setLanguage("de"); - double[] coords = {49.422636, 8.320833}; - String[] words; - try { - words = w3w.positionToWords(coords); - assertEquals(3, words.length); - assertEquals("kleid", words[0]); - assertEquals("ober", words[1]); - assertEquals("endlos", words[2]); - } catch (Exception e) { - assertTrue(false); - } - } + /** + * Tests the position -> words API wrapper after changing the language + */ + @Test + public void testChangeLang() throws Exception { + What3Words w3w = new What3Words(apiKey); + w3w.setLanguage("de"); + double[] coords = {49.422636, 8.320833}; + String[] words; + try { + words = w3w.positionToWords(coords); + assertEquals(3, words.length); + assertEquals("kleid", words[0]); + assertEquals("ober", words[1]); + assertEquals("endlos", words[2]); + } catch (Exception e) { + fail(); + } + } - /** - * Test for exception in case of an invalid API-key - */ - public void testWhat3WordsException() { - What3Words w3w = new What3Words("non-existing-api-key"); - double[] coords = { 49.422636, 8.320833 }; - boolean thrown = false; - try { - w3w.positionToWords(coords); - } catch (Exception e) { - thrown = true; - assertEquals("Error returned from w3w API: Missing or invalid key", - e.getCause().getMessage()); - } - assertTrue(thrown); - } + /** + * Test for exception in case of an invalid API-key + */ + @Test + public void testWhat3WordsException() throws Exception { + expectedException.expect(Exception.class); + expectedException.expectMessage("Error returned from w3w API: Missing or invalid key"); + What3Words w3w = new What3Words(UUID.randomUUID().toString() + apiKey); + double[] coords = {49.422636, 8.320833}; + w3w.positionToWords(coords); + } } From 20d046b8dfdec615482ded1ed0ab5085d37904fc Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 11:21:31 +0200 Subject: [PATCH 2/6] Added travis configuration file --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..90ac9dd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +env: + global: + secure: yE2R3BHbbY58yCbmLY6umJ1MecQtJW4PJnYEFssXPPoheB1hWf7T8xlXRYB5KSXAg9QwYmWtrrgRibc5VajfwNfJ/2lzyGq/vP5HHcN1HfkVYlEv0hwJHdhxtE+l8rYshfHx1qd4+W8CDcbHfBW/QgTnI9wFu2+O0pn3PQ9DxT0pISGcP4pnmZesMHczZA36q7IEpxbv7nyCLm3CqSNiAKUv9I0UWbSvtn4MyBtuEH2OQqx4UwLXtUjuOmcNUJ6HaxBngtptatAsdwkVIKo2niQ9riEWWseCQNYFoH1t4eMuvJwwhq3UkDjFuqR+nVWQm1ZJ8CrzhqD4Ywa/LIjAspi0VHkaxbVWGrGksP6yDXCu76bPVVoYldGxFvZ3KKiOt3li+oZweDsXFQpdNFGKsuqkG79ATyGDeguQSgHWClU2OSzeP0H6jFI3/NfXfD5IgNhNXDkHsnLBRajko4AUodU3zrL1QsDVwKR728jmWSfSH8+j0qMzjpYJoGiv34bcLEQCL18OCt1SzanSi1utQPPxkY3k5TnRImcJ6wvGRm1Kd1nFnoPPGJLV6ePZsd26VHfb7zPXEtd4l7qRPgnrcd4kFqMXmeyOurknlPi4ph9zZVZrG0LLXgh5evu/6OopnuBIJYY9NSqbvOaW8YrQllNTyLr9tQHrn7lCpOsFkOs= From 63679028aec94271152989ffc2bb6d9a2d41758e Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 11:27:01 +0200 Subject: [PATCH 3/6] Added Java language config to .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 90ac9dd..394a0b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +language: java env: global: secure: yE2R3BHbbY58yCbmLY6umJ1MecQtJW4PJnYEFssXPPoheB1hWf7T8xlXRYB5KSXAg9QwYmWtrrgRibc5VajfwNfJ/2lzyGq/vP5HHcN1HfkVYlEv0hwJHdhxtE+l8rYshfHx1qd4+W8CDcbHfBW/QgTnI9wFu2+O0pn3PQ9DxT0pISGcP4pnmZesMHczZA36q7IEpxbv7nyCLm3CqSNiAKUv9I0UWbSvtn4MyBtuEH2OQqx4UwLXtUjuOmcNUJ6HaxBngtptatAsdwkVIKo2niQ9riEWWseCQNYFoH1t4eMuvJwwhq3UkDjFuqR+nVWQm1ZJ8CrzhqD4Ywa/LIjAspi0VHkaxbVWGrGksP6yDXCu76bPVVoYldGxFvZ3KKiOt3li+oZweDsXFQpdNFGKsuqkG79ATyGDeguQSgHWClU2OSzeP0H6jFI3/NfXfD5IgNhNXDkHsnLBRajko4AUodU3zrL1QsDVwKR728jmWSfSH8+j0qMzjpYJoGiv34bcLEQCL18OCt1SzanSi1utQPPxkY3k5TnRImcJ6wvGRm1Kd1nFnoPPGJLV6ePZsd26VHfb7zPXEtd4l7qRPgnrcd4kFqMXmeyOurknlPi4ph9zZVZrG0LLXgh5evu/6OopnuBIJYY9NSqbvOaW8YrQllNTyLr9tQHrn7lCpOsFkOs= From 5f6583195bc0683370fb2b6ffe51f75cdb906c20 Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 11:32:05 +0200 Subject: [PATCH 4/6] Read API key also from environment variables --- src/test/java/de/meggsimum/w3w/What3WordsTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/meggsimum/w3w/What3WordsTest.java b/src/test/java/de/meggsimum/w3w/What3WordsTest.java index d023389..ced47c2 100644 --- a/src/test/java/de/meggsimum/w3w/What3WordsTest.java +++ b/src/test/java/de/meggsimum/w3w/What3WordsTest.java @@ -36,10 +36,14 @@ public class What3WordsTest { */ @Before public void beforeMethod() { - // Try to read the API key from system properties only in case it was not hard coded. + // Try to read the API key from properties only in case it was not hard coded. if (apiKey == null) { apiKey = System.getProperty(API_KEY_PROPERTY); } + // Fall back to environment variable in case API key was not provided as property + if(apiKey == null) { + apiKey = System.getenv(API_KEY_PROPERTY); + } assumeNotNull(apiKey); } From 9379157919dd0c6e1d448650e5ea0ac31468d490 Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 11:54:02 +0200 Subject: [PATCH 5/6] Comment about secret key. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 394a0b6..58147a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: java env: global: + # This is valid only for jstastny repository. Needs to be changed in the upstream project secure: yE2R3BHbbY58yCbmLY6umJ1MecQtJW4PJnYEFssXPPoheB1hWf7T8xlXRYB5KSXAg9QwYmWtrrgRibc5VajfwNfJ/2lzyGq/vP5HHcN1HfkVYlEv0hwJHdhxtE+l8rYshfHx1qd4+W8CDcbHfBW/QgTnI9wFu2+O0pn3PQ9DxT0pISGcP4pnmZesMHczZA36q7IEpxbv7nyCLm3CqSNiAKUv9I0UWbSvtn4MyBtuEH2OQqx4UwLXtUjuOmcNUJ6HaxBngtptatAsdwkVIKo2niQ9riEWWseCQNYFoH1t4eMuvJwwhq3UkDjFuqR+nVWQm1ZJ8CrzhqD4Ywa/LIjAspi0VHkaxbVWGrGksP6yDXCu76bPVVoYldGxFvZ3KKiOt3li+oZweDsXFQpdNFGKsuqkG79ATyGDeguQSgHWClU2OSzeP0H6jFI3/NfXfD5IgNhNXDkHsnLBRajko4AUodU3zrL1QsDVwKR728jmWSfSH8+j0qMzjpYJoGiv34bcLEQCL18OCt1SzanSi1utQPPxkY3k5TnRImcJ6wvGRm1Kd1nFnoPPGJLV6ePZsd26VHfb7zPXEtd4l7qRPgnrcd4kFqMXmeyOurknlPi4ph9zZVZrG0LLXgh5evu/6OopnuBIJYY9NSqbvOaW8YrQllNTyLr9tQHrn7lCpOsFkOs= From 2a935ccec32324b006bb77bbde3566d1661c36a9 Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Sun, 18 Oct 2015 11:56:37 +0200 Subject: [PATCH 6/6] Added build status icon to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index fdb119f..b6f82ad 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ JDK and Maven need to be installed on your system. * Run ``mvn package`` in case you do not want to enter your API key for test. Tests requiring API key will be skipped. * Under ``target/`` you will find the file ``w3w-java-wrapper--jar-with-dependencies.jar`` which can be embedded into your Java application +## Build status +[![Build Status](https://travis-ci.org/jstastny/w3w-java-wrapper.svg)](https://travis-ci.org/jstastny/w3w-java-wrapper) + ## Contributions Any contribution is warmly welcome: