1515 */
1616package com .ibm .g11n .pipeline .resfilter .impl ;
1717
18+ import static org .junit .Assert .assertArrayEquals ;
1819import static org .junit .Assert .assertEquals ;
1920import static org .junit .Assert .assertTrue ;
2021
2122import java .io .File ;
2223import java .io .FileInputStream ;
24+ import java .io .FileNotFoundException ;
2325import java .io .FileOutputStream ;
2426import java .io .IOException ;
2527import java .io .InputStream ;
2931import java .util .Collections ;
3032import java .util .LinkedList ;
3133import java .util .List ;
34+ import java .util .Map .Entry ;
3235
3336import org .junit .Test ;
3437
38+ import com .google .gson .JsonArray ;
39+ import com .google .gson .JsonElement ;
40+ import com .google .gson .JsonObject ;
3541import com .ibm .g11n .pipeline .resfilter .LanguageBundle ;
3642import com .ibm .g11n .pipeline .resfilter .LanguageBundleBuilder ;
3743import com .ibm .g11n .pipeline .resfilter .ResourceFilterException ;
3844import com .ibm .g11n .pipeline .resfilter .ResourceString ;
3945import com .ibm .g11n .pipeline .resfilter .ResourceString .ResourceStringComparator ;
46+ import com .ibm .g11n .pipeline .resfilter .impl .JsonResource .KeyPiece ;
4047
4148/**
4249 * @author farhan
4350 *
4451 */
4552public class JsonResourceTest {
4653 private static final File INPUT_FILE = new File ("src/test/resource/resfilter/json/input.json" );
54+ private static final File INPUT_FILE2 = new File ("src/test/resource/resfilter/json/other-input.json" );
55+ private static final File SPLITKEYS = new File ("src/test/resource/resfilter/json/testSplitKeys.json" );
4756
4857 private static final File EXPECTED_WRITE_FILE = new File ("src/test/resource/resfilter/json/write-output.json" );
4958
@@ -62,8 +71,10 @@ public class JsonResourceTest {
6271 lst .add (ResourceString .with ("$.countries[1].Asia[1]" , "Japan" ).sequenceNumber (9 ).build ());
6372 lst .add (ResourceString .with ("$.countries[1].Asia[2]" , "India" ).sequenceNumber (10 ).build ());
6473 lst .add (ResourceString .with ("$.countries[2].Americas['S. America'][0]" , "Brazil" ).sequenceNumber (11 ).build ());
65- lst .add (ResourceString .with ("$.countries[2].Americas['S. America'][1]" , "Venezuela" ).sequenceNumber (12 ).build ());
66- lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][0]" , "United States [USA]" ).sequenceNumber (13 ).build ());
74+ lst .add (ResourceString .with ("$.countries[2].Americas['S. America'][1]" , "Venezuela" ).sequenceNumber (12 )
75+ .build ());
76+ lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][0]" , "United States [USA]" )
77+ .sequenceNumber (13 ).build ());
6778 lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][1]" , "Canada" ).sequenceNumber (14 ).build ());
6879 lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][2]" , "Mexico" ).sequenceNumber (15 ).build ());
6980 lst .add (ResourceString .with ("$.countries[3].Africa[0]" , "Egypt" ).sequenceNumber (16 ).build ());
@@ -77,10 +88,14 @@ public class JsonResourceTest {
7788 lst .add (ResourceString .with ("another.text" , "Another plain old string" ).sequenceNumber (24 ).build ());
7889 lst .add (ResourceString .with ("frog['2']" , "Red-eyed Tree Frog" ).sequenceNumber (25 ).build ());
7990 lst .add (ResourceString .with ("owl[3]" , "Great Horned Owl" ).sequenceNumber (26 ).build ());
80- lst .add (ResourceString .with ("$['$.xxx']" , "Looks like JSONPATH, but actually plain old string" ).sequenceNumber (27 ).build ());
81- lst .add (ResourceString .with ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string" ).sequenceNumber (28 ).build ());
82- lst .add (ResourceString .with ("$abc" , "Starts with JSONPATH root char, but just a string" ).sequenceNumber (29 ).build ());
83- lst .add (ResourceString .with ("$['ibm.com']['g11n.pipeline.title']" , "Globalization Pipeline" ).sequenceNumber (30 ).build ());
91+ lst .add (ResourceString .with ("$['$.xxx']" , "Looks like JSONPATH, but actually plain old string" )
92+ .sequenceNumber (27 ).build ());
93+ lst .add (ResourceString .with ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string" )
94+ .sequenceNumber (28 ).build ());
95+ lst .add (ResourceString .with ("$abc" , "Starts with JSONPATH root char, but just a string" ).sequenceNumber (29 )
96+ .build ());
97+ lst .add (ResourceString .with ("$['ibm.com']['g11n.pipeline.title']" , "Globalization Pipeline" ).sequenceNumber (30 )
98+ .build ());
8499
85100 Collections .sort (lst , new ResourceStringComparator ());
86101 EXPECTED_INPUT_RES_LIST = lst ;
@@ -117,7 +132,8 @@ public class JsonResourceTest {
117132 bundleBuilder .addResourceString ("frog['2']" , "Red-eyed Tree Frog - XL" , 25 );
118133 bundleBuilder .addResourceString ("owl[3]" , "Great Horned Owl - XL" , 26 );
119134 bundleBuilder .addResourceString ("$['$.xxx']" , "Looks like JSONPATH, but actually plain old string - XL" , 27 );
120- bundleBuilder .addResourceString ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string - XL" , 28 );
135+ bundleBuilder .addResourceString ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string - XL" ,
136+ 28 );
121137 bundleBuilder .addResourceString ("$abc" , "Starts with JSONPATH root char, but just a string - XL" , 29 );
122138 bundleBuilder .addResourceString ("$['ibm.com']['g11n.pipeline.title']" , "Globalization Pipeline - XL" , 30 );
123139 WRITE_BUNDLE = bundleBuilder .build ();
@@ -133,7 +149,8 @@ public void testParse() throws IOException, ResourceFilterException {
133149 LanguageBundle bundle = res .parse (is , null );
134150 List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
135151 Collections .sort (resStrList , new ResourceStringComparator ());
136- assertEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST , resStrList );
152+ assertArrayEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST .toArray (),
153+ resStrList .toArray ());
137154 }
138155 }
139156
@@ -145,12 +162,108 @@ public void testWrite() throws IOException, ResourceFilterException {
145162 try (OutputStream os = new FileOutputStream (tempFile )) {
146163 res .write (os , WRITE_BUNDLE , null );
147164 os .flush ();
148- assertTrue (ResourceTestUtil .compareFiles (EXPECTED_WRITE_FILE , tempFile ));
165+ ResourceTestUtil .compareFilesJson (EXPECTED_WRITE_FILE , tempFile );
166+ }
167+ }
168+
169+ // @Test
170+ // public void testTestFiles() throws IOException, ResourceFilterException {
171+ // // just test the test files
172+ // ResourceTestUtil.compareFilesJson(INPUT_FILE, EXPECTED_WRITE_FILE);
173+ // }
174+
175+ @ Test
176+ public void testReWrite () throws IOException , ResourceFilterException {
177+ // First parse
178+ assertTrue ("The input test file <" + INPUT_FILE + "> does not exist." , INPUT_FILE .exists ());
179+
180+ try (InputStream is = new FileInputStream (INPUT_FILE )) {
181+ JsonResource res2 = new JsonResource ();
182+ LanguageBundle bundle = res2 .parse (is , null );
183+ List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
184+ Collections .sort (resStrList , new ResourceStringComparator ());
185+ assertArrayEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST .toArray (),
186+ resStrList .toArray ());
187+
188+ // Now write
189+ File tempFile = File .createTempFile (this .getClass ().getSimpleName (), "2.json" );
190+ // File tempFile = new File("/tmp/2.json");
191+ tempFile .deleteOnExit ();
192+
193+ try (OutputStream os = new FileOutputStream (tempFile )) {
194+ res .write (os , bundle , null );
195+ os .flush ();
196+ ResourceTestUtil .compareFilesJson (INPUT_FILE , tempFile );
197+ }
198+ }
199+ }
200+
201+ @ Test
202+ public void testReWriteOther () throws IOException , ResourceFilterException {
203+ // First parse
204+ assertTrue ("The input test file <" + INPUT_FILE + "> does not exist." , INPUT_FILE2 .exists ());
205+
206+ try (InputStream is = new FileInputStream (INPUT_FILE2 )) {
207+ JsonResource res2 = new JsonResource ();
208+ LanguageBundle bundle = res2 .parse (is , null );
209+ List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
210+ Collections .sort (resStrList , new ResourceStringComparator ());
211+ // assertEquals("ResourceStrings did not match.",
212+ // EXPECTED_INPUT_RES_LIST, resStrList);
213+
214+ // Now write
215+ File tempFile = File .createTempFile (this .getClass ().getSimpleName (), "3.json" );
216+ // File tempFile = new File("/tmp/3.json");
217+ tempFile .deleteOnExit ();
218+
219+ try (OutputStream os = new FileOutputStream (tempFile )) {
220+ res .write (os , bundle , null );
221+ os .flush ();
222+ System .out .println (ResourceTestUtil .fileToString (tempFile ));
223+ ResourceTestUtil .compareFilesJson (INPUT_FILE2 , tempFile );
224+ }
225+ }
226+ }
227+
228+ /**
229+ * @throws IOException
230+ * @throws FileNotFoundException
231+ * @See {@link JsonResource#splitKeyPieces(String)}
232+ */
233+ @ Test
234+ public void testSplitKeyPieces () throws FileNotFoundException , IOException {
235+ /*
236+ * Initial data generated with: Gson g = new GsonBuilder().create();
237+ * TreeMap<String, List<KeyPiece>> tm = new TreeMap<String,
238+ * List<KeyPiece>>();
239+ *
240+ * for (final ResourceString s : EXPECTED_INPUT_RES_LIST) {
241+ * tm.put(s.getKey(), JsonResource.splitKeyPieces(s.getKey())); }
242+ * System.out.println(g.toJson(tm));
243+ */
244+
245+ JsonObject SPLITKEY_DATA = ResourceTestUtil .parseJson (SPLITKEYS ).getAsJsonObject ();
246+ for (final Entry <String , JsonElement > e : SPLITKEY_DATA .entrySet ()) {
247+ final String key = e .getKey ();
248+ final JsonArray expectList = e .getValue ().getAsJsonArray ();
249+
250+ List <KeyPiece > actualList = JsonResource .splitKeyPieces (key );
251+
252+ String prefix = "‘" + key + "’: " ;
253+ assertEquals (prefix + "key count" , expectList .size (), actualList .size ());
254+ for (int n = 0 ; n < expectList .size (); n ++) {
255+ JsonObject expectObject = expectList .get (n ).getAsJsonObject ();
256+ KeyPiece actualObject = actualList .get (n );
257+ String subPrefix = prefix + " key " + n ;
258+ assertEquals (subPrefix + " value " , expectObject .get ("keyValue" ).getAsString (), actualObject .keyValue );
259+ assertEquals (subPrefix + " type " , expectObject .get ("keyType" ).getAsString (),
260+ actualObject .keyType .name ());
261+ }
149262 }
150263 }
151264
152265 // TODO: Not ready yet
153- // @Test
154- // public void testMerge() throws IOException, ResourceFilterException {
155- // }
266+ // @Test
267+ // public void testMerge() throws IOException, ResourceFilterException {
268+ // }
156269}
0 commit comments