Skip to content

Commit 52b3eff

Browse files
committed
修复 resx 生成时误删逗号并添加回归测试
1 parent 7717aca commit 52b3eff

3 files changed

Lines changed: 127 additions & 4 deletions

File tree

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
<artifactId>log4j-core</artifactId>
4343
<version>2.25.4</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.13.2</version>
49+
<scope>test</scope>
50+
</dependency>
4551
</dependencies>
4652
<build>
4753
<plugins>

‎src/main/java/com/yanglb/codegen/core/translator/impl/MsgCSTranslatorImpl.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ private void translateResx(WritableModel writableModel) throws CodeGenException
135135
}
136136
}
137137

138-
int idx = sb.lastIndexOf(",");
139-
if (idx != -1) {
140-
sb.deleteCharAt(idx);
141-
}
142138
sb.append("</root>\n");
143139
}
144140

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.yanglb.codegen.core.translator.impl;
2+
3+
import com.yanglb.codegen.model.ParameterModel;
4+
import com.yanglb.codegen.model.TableModel;
5+
import com.yanglb.codegen.model.WritableModel;
6+
import org.apache.commons.cli.DefaultParser;
7+
import org.apache.commons.cli.Options;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.Parameterized;
11+
import org.w3c.dom.Document;
12+
import org.w3c.dom.Element;
13+
import org.w3c.dom.NodeList;
14+
import org.xml.sax.InputSource;
15+
16+
import javax.xml.parsers.DocumentBuilderFactory;
17+
import java.io.StringReader;
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
@RunWith(Parameterized.class)
28+
public class MsgCSTranslatorImplTest {
29+
@Parameterized.Parameters(name = "language={0}")
30+
public static Collection<Object[]> languages() {
31+
return Arrays.asList(new Object[][]{{"default"}, {"en"}, {"ko"}});
32+
}
33+
34+
private final String language;
35+
36+
public MsgCSTranslatorImplTest(String language) {
37+
this.language = language;
38+
}
39+
40+
@Test
41+
public void preservesAllCommasInLastResourceValue() throws Exception {
42+
Document document = translate(new String[][]{
43+
{"Probe.First", "First, comma"},
44+
{"Probe.Last", "Last, multiple, commas,"}
45+
});
46+
47+
assertEquals("First, comma", value(document, "data", "Probe.First"));
48+
assertEquals("Last, multiple, commas,", value(document, "data", "Probe.Last"));
49+
}
50+
51+
@Test
52+
public void preservesCommaWhenLastResourceHasNoComma() throws Exception {
53+
Document document = translate(new String[][]{
54+
{"Probe.First", "First, comma"},
55+
{"Probe.Last", "No comma"}
56+
});
57+
58+
assertEquals("First, comma", value(document, "data", "Probe.First"));
59+
assertEquals("No comma", value(document, "data", "Probe.Last"));
60+
}
61+
62+
@Test
63+
public void preservesCommasInResourceKeys() throws Exception {
64+
Document document = translate(new String[][]{{"Probe,Key", "No comma"}});
65+
66+
assertEquals("No comma", value(document, "data", "Probe,Key"));
67+
}
68+
69+
@Test
70+
public void preservesHeadersWhenResourceValuesContainNoCommas() throws Exception {
71+
Document document = translate(new String[][]{{"Probe", "No comma"}});
72+
73+
assertEquals("System.Resources.ResXResourceWriter, System.Windows.Forms, "
74+
+ "Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
75+
value(document, "resheader", "writer"));
76+
assertEquals("No comma", value(document, "data", "Probe"));
77+
}
78+
79+
@Test
80+
public void preservesCommasAlongsideXmlSpecialCharacters() throws Exception {
81+
String original = "<tag>, & \"quoted\", 한글, 中文";
82+
Document document = translate(new String[][]{{"Probe", original}});
83+
84+
assertEquals(original, value(document, "data", "Probe"));
85+
}
86+
87+
private Document translate(String[][] resources) throws Exception {
88+
TableModel table = new TableModel();
89+
table.setSheetName("CommaProbe");
90+
for (String[] resource : resources) {
91+
Map<String, String> row = new HashMap<>();
92+
row.put("id", resource[0]);
93+
row.put(language, resource[1]);
94+
table.insert(row);
95+
}
96+
97+
ParameterModel parameters = new ParameterModel();
98+
parameters.setFile("CommaProbe.xlsx");
99+
parameters.setOptions(new DefaultParser().parse(new Options(), new String[0]));
100+
HashMap<String, String> settings = new HashMap<>();
101+
settings.put("MsgLang", language);
102+
103+
List<WritableModel> outputs = new MsgCSTranslatorImpl().translate(
104+
settings, parameters, Collections.singletonList(table));
105+
assertEquals(1, outputs.size());
106+
assertEquals("resx", outputs.get(0).getExtension());
107+
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
108+
new InputSource(new StringReader(outputs.get(0).getData().toString())));
109+
}
110+
111+
private String value(Document document, String tag, String name) {
112+
NodeList elements = document.getElementsByTagName(tag);
113+
for (int index = 0; index < elements.getLength(); index++) {
114+
Element element = (Element) elements.item(index);
115+
if (name.equals(element.getAttribute("name"))) {
116+
return element.getElementsByTagName("value").item(0).getTextContent();
117+
}
118+
}
119+
throw new AssertionError("Missing " + tag + " named " + name);
120+
}
121+
}

0 commit comments

Comments
 (0)