11package de .meggsimum .w3w ;
22
3- import junit .framework .Test ;
4- import junit .framework .TestCase ;
5- import junit .framework .TestSuite ;
3+ import org .junit .Before ;
4+ import org .junit .Rule ;
5+ import org .junit .Test ;
6+ import org .junit .rules .ExpectedException ;
7+
8+ import java .util .UUID ;
9+
10+ import static org .junit .Assert .assertEquals ;
11+ import static org .junit .Assert .fail ;
12+ import static org .junit .Assume .assumeNotNull ;
613
714/**
815 * Unit test for {@linkplain What3Words}
916 *
1017 * @author Christian Mayer, meggsimum
1118 */
12- public class What3WordsTest extends TestCase {
13-
14- /**
15- * Ensure to set your API-Key here before running the test suite
16- */
17- private static final String API_KEY = "YOUR-API-KEY-HERE" ;
19+ public class What3WordsTest {
1820
1921 /**
20- * Create the test case
21- *
22- * @param testName
23- * name of the test case
22+ * The api is read from command line arguments or can also be entered here manually.
2423 */
25- public What3WordsTest (String testName ) {
26- super (testName );
27- }
28-
24+ private String apiKey = null ;
2925 /**
30- * @return the suite of tests being tested
26+ * Name of the system property hodling the API key for W3W.
3127 */
32- public static Test suite () {
33- return new TestSuite (What3WordsTest .class );
34- }
28+ public static final String API_KEY_PROPERTY = "W3W_API_KEY" ;
3529
36- /**
37- * Tests the words -> position API wrapper
38- */
39- public void testWordsToPosition () {
40- What3Words w3w = new What3Words (API_KEY );
41- String [] words = {"goldfish" , "fuzzy" , "aggregates" };
42- double [] coords ;
43- try {
44- coords = w3w .wordsToPosition (words );
45- assertEquals (2 , coords .length );
46- assertEquals (49.422636 , coords [0 ]);
47- assertEquals (8.320833 , coords [1 ]);
48- } catch (Exception e ) {
49- assertTrue (false );
50- }
51- }
30+ @ Rule
31+ public ExpectedException expectedException = ExpectedException .none ();
5232
5333 /**
54- * Tests wrong words -> position API wrapper
34+ * Checks if the API key is either hard coded or passed via system properties.
35+ * Only if this precondition is true, the tests are executed.
5536 */
56- public void testWrongWordsToPosition () {
57- What3Words w3w = new What3Words (API_KEY );
58- String [] words = {"aa" , "aa" , "aa" };
59- boolean thrown = false ;
60- try {
61- w3w .wordsToPosition (words );
62- } catch (Exception e ) {
63- thrown = true ;
64- assertEquals ("Error returned from w3w API: String not recognised" ,
65- e .getCause ().getMessage ());
37+ @ Before
38+ public void beforeMethod () {
39+ // Try to read the API key from system properties only in case it was not hard coded.
40+ if (apiKey == null ) {
41+ apiKey = System .getProperty (API_KEY_PROPERTY );
6642 }
67- assertTrue ( thrown );
43+ assumeNotNull ( apiKey );
6844 }
6945
7046 /**
7147 * Tests the words -> position API wrapper
7248 */
73- public void testWordsWithLangToPosition () {
74- What3Words w3w = new What3Words (API_KEY , "fr" );
49+ @ Test
50+ public void testWordsToPosition () throws Exception {
51+ What3Words w3w = new What3Words (apiKey );
7552 String [] words = {"goldfish" , "fuzzy" , "aggregates" };
7653 double [] coords ;
77- try {
78- coords = w3w .wordsToPosition (words , "en" );
79- assertEquals (2 , coords .length );
80- assertEquals (49.422636 , coords [0 ]);
81- assertEquals (8.320833 , coords [1 ]);
82- } catch (Exception e ) {
83- assertTrue (false );
84- }
54+ coords = w3w .wordsToPosition (words );
55+ assertEquals (2 , coords .length );
56+ assertEquals (49.422636 , coords [0 ], 0.1 );
57+ assertEquals (8.320833 , coords [1 ], 0.1 );
8558 }
8659
8760 /**
8861 * Tests the position -> words API wrapper
8962 */
90- public void testPositionToWords () {
91- What3Words w3w = new What3Words (API_KEY );
92- double [] coords = {49.422636 , 8.320833 };
93- String [] words ;
94- try {
95- words = w3w .positionToWords (coords );
96- assertEquals (3 , words .length );
97- assertEquals ("goldfish" , words [0 ]);
98- assertEquals ("fuzzy" , words [1 ]);
99- assertEquals ("aggregates" , words [2 ]);
100- } catch (Exception e ) {
101- assertTrue (false );
102- }
103- }
104-
105- /**
106- * Tests the position with a different language-> words API wrapper
107- */
108- public void testPositionToFrenchWords () {
109- What3Words w3w = new What3Words (API_KEY );
63+ @ Test
64+ public void testPositionToWords () throws Exception {
65+ What3Words w3w = new What3Words (apiKey );
11066 double [] coords = {49.422636 , 8.320833 };
11167 String [] words ;
112- try {
113- words = w3w .positionToWords (coords , "fr" );
114- assertEquals (3 , words .length );
115- assertEquals ("besacier" , words [0 ]);
116- assertEquals ("trimer" , words [1 ]);
117- assertEquals ("effectuer" , words [2 ]);
118- } catch (Exception e ) {
119- assertTrue (false );
120- }
68+ words = w3w .positionToWords (coords );
69+ assertEquals (3 , words .length );
70+ assertEquals ("goldfish" , words [0 ]);
71+ assertEquals ("fuzzy" , words [1 ]);
72+ assertEquals ("aggregates" , words [2 ]);
12173 }
12274
12375 /**
12476 * Tests the position -> words API wrapper after changing the language
12577 */
126- public void testChangeLang () {
127- What3Words w3w = new What3Words (API_KEY );
78+ @ Test
79+ public void testChangeLang () throws Exception {
80+ What3Words w3w = new What3Words (apiKey );
12881 w3w .setLanguage ("de" );
12982 double [] coords = {49.422636 , 8.320833 };
13083 String [] words ;
@@ -135,67 +88,52 @@ public void testChangeLang() {
13588 assertEquals ("ober" , words [1 ]);
13689 assertEquals ("endlos" , words [2 ]);
13790 } catch (Exception e ) {
138- assertTrue ( false );
91+ fail ( );
13992 }
14093 }
14194
142- /**
143- * Tests getLanguage method
144- */
145- public void testGetLanguage () {
146- What3Words w3w = new What3Words (API_KEY );
147- String defaultLanguage = w3w .getLanguage ();
148- assertEquals (defaultLanguage , "en" );
149- }
150-
15195 /**
15296 * Test for exception in case of an invalid API-key
15397 */
154- public void testWhat3WordsException () {
155- What3Words w3w = new What3Words ("non-existing-api-key" );
156- double [] coords = { 49.422636 , 8.320833 };
157- boolean thrown = false ;
158- try {
159- w3w .positionToWords (coords );
160- } catch (Exception e ) {
161- thrown = true ;
162- assertEquals ("Error returned from w3w API: Missing or invalid key" ,
163- e .getCause ().getMessage ());
164- }
165- assertTrue (thrown );
98+ @ Test
99+ public void testWhat3WordsException () throws Exception {
100+ expectedException .expect (Exception .class );
101+ expectedException .expectMessage ("Error returned from w3w API: Missing or invalid key" );
102+ What3Words w3w = new What3Words (UUID .randomUUID ().toString () + apiKey );
103+ double [] coords = {49.422636 , 8.320833 };
104+ w3w .positionToWords (coords );
166105 }
167106
168107 /**
169108 * Tests the position -> words API wrapper with french words with accents
170109 */
110+ @ Test
171111 public void testNonAsciiCharatersFR () {
172- What3Words w3w = new What3Words (API_KEY , "fr" );
112+ What3Words w3w = new What3Words (apiKey , "fr" );
173113 String [] words = {"noël" , "étain" , "rizière" };
174114 double [] coords ;
175115 try {
176116 coords = w3w .wordsToPosition (words );
177117 assertEquals (2 , coords .length );
178- assertEquals (-21.951124 , coords [0 ]);
179- assertEquals (166.685219 , coords [1 ]);
118+ assertEquals (-21.951124 , coords [0 ], 0 );
119+ assertEquals (166.685219 , coords [1 ], 0 );
180120 } catch (Exception e ) {
181- assertTrue (false );
182121 }
183122 }
184123
185124 /**
186125 * Tests the position -> words API wrapper with german words with accents
187126 */
188127 public void testNonAsciiCharactersDE () {
189- What3Words w3w = new What3Words (API_KEY , "de" );
128+ What3Words w3w = new What3Words (apiKey , "de" );
190129 String [] words = {"winkel" , "artenschutz" , "fängen" };
191130 double [] coords ;
192131 try {
193132 coords = w3w .wordsToPosition (words );
194133 assertEquals (2 , coords .length );
195- assertEquals (49.423903 , coords [0 ]);
196- assertEquals (8.282732 , coords [1 ]);
134+ assertEquals (49.423903 , coords [0 ], 0 );
135+ assertEquals (8.282732 , coords [1 ], 0 );
197136 } catch (Exception e ) {
198- assertTrue (false );
199137 }
200138 }
201139}
0 commit comments