Skip to content

Commit 786eea8

Browse files
committed
test Show the json problem
Expresses: #103
1 parent c40e8ad commit 786eea8

3 files changed

Lines changed: 118 additions & 15 deletions

File tree

gp-res-filter/src/test/java/com/ibm/g11n/pipeline/resfilter/impl/JsonResourceTest.java

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.ibm.g11n.pipeline.resfilter.impl;
1717

18-
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertArrayEquals;
1919
import static org.junit.Assert.assertTrue;
2020

2121
import java.io.File;
@@ -44,6 +44,7 @@
4444
*/
4545
public 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
}

gp-res-filter/src/test/java/com/ibm/g11n/pipeline/resfilter/impl/ResourceTestUtil.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616

1717
package com.ibm.g11n.pipeline.resfilter.impl;
1818

19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.fail;
2021

2122
import java.io.BufferedReader;
2223
import java.io.File;
2324
import java.io.FileNotFoundException;
2425
import java.io.FileReader;
2526
import java.io.IOException;
27+
import java.io.Reader;
2628
import java.nio.file.Files;
2729
import java.nio.file.Paths;
2830

31+
import com.google.gson.JsonElement;
32+
import com.google.gson.JsonParser;
33+
2934
/**
3035
* @author farhan, JCEmmons
3136
*
@@ -76,11 +81,13 @@ public static boolean compareFiles(File expected, File actual, int n) throws Fil
7681

7782
return true;
7883
}
84+
7985
/**
8086
* Returns true if the two files match exactly up to the number of lines
81-
* specified in n
87+
* specified in n
8288
*/
83-
public static boolean compareFilesUpTo(File expected, File actual, int n) throws FileNotFoundException, IOException {
89+
public static boolean compareFilesUpTo(File expected, File actual, int n)
90+
throws FileNotFoundException, IOException {
8491
try (BufferedReader expectedRdr = new BufferedReader(new FileReader(expected));
8592
BufferedReader actualRdr = new BufferedReader(new FileReader(actual))) {
8693

@@ -124,4 +131,26 @@ public static String fileToString(File file) throws FileNotFoundException, IOExc
124131
byte[] encoded = Files.readAllBytes(Paths.get(file.getPath()));
125132
return new String(encoded, "UTF-8");
126133
}
134+
135+
/**
136+
* @param expectedWriteFile
137+
* @param tempFile
138+
* @return
139+
* @throws IOException
140+
* @throws FileNotFoundException
141+
*/
142+
public static void compareFilesJson(File expectedWriteFile, File tempFile)
143+
throws FileNotFoundException, IOException {
144+
JsonElement expected = parseJson(expectedWriteFile);
145+
JsonElement actual = parseJson(tempFile);
146+
assertEquals("JSON mismatch: " + tempFile.getName() + " did not match " + expectedWriteFile.getName(), expected,
147+
actual);
148+
}
149+
150+
public static JsonElement parseJson(final File f) throws FileNotFoundException, IOException {
151+
try (final Reader reader = new FileReader(f)) {
152+
JsonElement parse = new JsonParser().parse(reader);
153+
return parse;
154+
}
155+
}
127156
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"hello": {
3+
"1": "Firstly",
4+
"two": "Secondly"
5+
}
6+
}

0 commit comments

Comments
 (0)