1515 */
1616package com .ibm .g11n .pipeline .resfilter .impl ;
1717
18- import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .assertArrayEquals ;
1919import static org .junit .Assert .assertTrue ;
2020
2121import java .io .File ;
4444 */
4545public class JsonResourceTest {
4646 private static final File INPUT_FILE = new File ("src/test/resource/resfilter/json/input.json" );
47+ private static final File INPUT_FILE2 = new File ("src/test/resource/resfilter/json/other-input.json" );
4748
4849 private static final File EXPECTED_WRITE_FILE = new File ("src/test/resource/resfilter/json/write-output.json" );
4950
@@ -62,8 +63,10 @@ public class JsonResourceTest {
6263 lst .add (ResourceString .with ("$.countries[1].Asia[1]" , "Japan" ).sequenceNumber (9 ).build ());
6364 lst .add (ResourceString .with ("$.countries[1].Asia[2]" , "India" ).sequenceNumber (10 ).build ());
6465 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 ());
66+ lst .add (ResourceString .with ("$.countries[2].Americas['S. America'][1]" , "Venezuela" ).sequenceNumber (12 )
67+ .build ());
68+ lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][0]" , "United States [USA]" )
69+ .sequenceNumber (13 ).build ());
6770 lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][1]" , "Canada" ).sequenceNumber (14 ).build ());
6871 lst .add (ResourceString .with ("$.countries[2].Americas['N. America'][2]" , "Mexico" ).sequenceNumber (15 ).build ());
6972 lst .add (ResourceString .with ("$.countries[3].Africa[0]" , "Egypt" ).sequenceNumber (16 ).build ());
@@ -77,10 +80,14 @@ public class JsonResourceTest {
7780 lst .add (ResourceString .with ("another.text" , "Another plain old string" ).sequenceNumber (24 ).build ());
7881 lst .add (ResourceString .with ("frog['2']" , "Red-eyed Tree Frog" ).sequenceNumber (25 ).build ());
7982 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 ());
83+ lst .add (ResourceString .with ("$['$.xxx']" , "Looks like JSONPATH, but actually plain old string" )
84+ .sequenceNumber (27 ).build ());
85+ lst .add (ResourceString .with ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string" )
86+ .sequenceNumber (28 ).build ());
87+ lst .add (ResourceString .with ("$abc" , "Starts with JSONPATH root char, but just a string" ).sequenceNumber (29 )
88+ .build ());
89+ lst .add (ResourceString .with ("$['ibm.com']['g11n.pipeline.title']" , "Globalization Pipeline" ).sequenceNumber (30 )
90+ .build ());
8491
8592 Collections .sort (lst , new ResourceStringComparator ());
8693 EXPECTED_INPUT_RES_LIST = lst ;
@@ -117,7 +124,8 @@ public class JsonResourceTest {
117124 bundleBuilder .addResourceString ("frog['2']" , "Red-eyed Tree Frog - XL" , 25 );
118125 bundleBuilder .addResourceString ("owl[3]" , "Great Horned Owl - XL" , 26 );
119126 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 );
127+ bundleBuilder .addResourceString ("$['$.']" , "Looks like JSONPATH prefix, but actually plain old string - XL" ,
128+ 28 );
121129 bundleBuilder .addResourceString ("$abc" , "Starts with JSONPATH root char, but just a string - XL" , 29 );
122130 bundleBuilder .addResourceString ("$['ibm.com']['g11n.pipeline.title']" , "Globalization Pipeline - XL" , 30 );
123131 WRITE_BUNDLE = bundleBuilder .build ();
@@ -133,7 +141,8 @@ public void testParse() throws IOException, ResourceFilterException {
133141 LanguageBundle bundle = res .parse (is , null );
134142 List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
135143 Collections .sort (resStrList , new ResourceStringComparator ());
136- assertEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST , resStrList );
144+ assertArrayEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST .toArray (),
145+ resStrList .toArray ());
137146 }
138147 }
139148
@@ -145,12 +154,71 @@ public void testWrite() throws IOException, ResourceFilterException {
145154 try (OutputStream os = new FileOutputStream (tempFile )) {
146155 res .write (os , WRITE_BUNDLE , null );
147156 os .flush ();
148- assertTrue (ResourceTestUtil .compareFiles (EXPECTED_WRITE_FILE , tempFile ));
157+ ResourceTestUtil .compareFilesJson (EXPECTED_WRITE_FILE , tempFile );
158+ }
159+ }
160+
161+ // @Test
162+ // public void testTestFiles() throws IOException, ResourceFilterException {
163+ // // just test the test files
164+ // ResourceTestUtil.compareFilesJson(INPUT_FILE, EXPECTED_WRITE_FILE);
165+ // }
166+
167+ @ Test
168+ public void testReWrite () throws IOException , ResourceFilterException {
169+ // First parse
170+ assertTrue ("The input test file <" + INPUT_FILE + "> does not exist." , INPUT_FILE .exists ());
171+
172+ try (InputStream is = new FileInputStream (INPUT_FILE )) {
173+ JsonResource res2 = new JsonResource ();
174+ LanguageBundle bundle = res2 .parse (is , null );
175+ List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
176+ Collections .sort (resStrList , new ResourceStringComparator ());
177+ assertArrayEquals ("ResourceStrings did not match." , EXPECTED_INPUT_RES_LIST .toArray (),
178+ resStrList .toArray ());
179+
180+ // Now write
181+ File tempFile = File .createTempFile (this .getClass ().getSimpleName (), "2.json" );
182+ // File tempFile = new File("/tmp/2.json");
183+ tempFile .deleteOnExit ();
184+
185+ try (OutputStream os = new FileOutputStream (tempFile )) {
186+ res .write (os , bundle , null );
187+ os .flush ();
188+ ResourceTestUtil .compareFilesJson (INPUT_FILE , tempFile );
189+ }
190+ }
191+ }
192+
193+ @ Test
194+ public void testReWriteOther () throws IOException , ResourceFilterException {
195+ // First parse
196+ assertTrue ("The input test file <" + INPUT_FILE + "> does not exist." , INPUT_FILE2 .exists ());
197+
198+ try (InputStream is = new FileInputStream (INPUT_FILE2 )) {
199+ JsonResource res2 = new JsonResource ();
200+ LanguageBundle bundle = res2 .parse (is , null );
201+ List <ResourceString > resStrList = new ArrayList <>(bundle .getResourceStrings ());
202+ Collections .sort (resStrList , new ResourceStringComparator ());
203+ // assertEquals("ResourceStrings did not match.",
204+ // EXPECTED_INPUT_RES_LIST, resStrList);
205+
206+ // Now write
207+ File tempFile = File .createTempFile (this .getClass ().getSimpleName (), "3.json" );
208+ // File tempFile = new File("/tmp/3.json");
209+ tempFile .deleteOnExit ();
210+
211+ try (OutputStream os = new FileOutputStream (tempFile )) {
212+ res .write (os , bundle , null );
213+ os .flush ();
214+ System .out .println (ResourceTestUtil .fileToString (tempFile ));
215+ ResourceTestUtil .compareFilesJson (INPUT_FILE2 , tempFile );
216+ }
149217 }
150218 }
151219
152220 // TODO: Not ready yet
153- // @Test
154- // public void testMerge() throws IOException, ResourceFilterException {
155- // }
221+ // @Test
222+ // public void testMerge() throws IOException, ResourceFilterException {
223+ // }
156224}
0 commit comments