Skip to content

Travis ci #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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=
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version>-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:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
186 changes: 95 additions & 91 deletions src/test/java/de/meggsimum/w3w/What3WordsTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}