diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..58147a4 --- /dev/null +++ b/.travis.yml @@ -0,0 +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= diff --git a/README.md b/README.md index fb351b6..b6f82ad 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,13 @@ 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 +## 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: 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..ced47c2 100644 --- a/src/test/java/de/meggsimum/w3w/What3WordsTest.java +++ b/src/test/java/de/meggsimum/w3w/What3WordsTest.java @@ -1,107 +1,111 @@ 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 { - /** - * Create the test case - * - * @param testName - * name of the test case - */ - public What3WordsTest(String testName) { - super(testName); - } + /** + * 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"; - /** - * @return the suite of tests being tested - */ - public static Test suite() { - return new TestSuite(What3WordsTest.class); - } + @Rule + public ExpectedException expectedException = ExpectedException.none(); - /** - * 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); + /** + * 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 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); + } - /** - * 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 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 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 + */ + @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]); + } - /** - * 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); - } + /** + * 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 + */ + @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); + } }