fix yml mapper and provide unit tests
This commit is contained in:
parent
a20d4be2cf
commit
b16330f7fd
@ -7,6 +7,7 @@ import de.marhali.easyi18n.util.StringUtil;
|
|||||||
import org.apache.commons.lang.StringEscapeUtils;
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
|
|
||||||
|
import thito.nodeflow.config.ListSection;
|
||||||
import thito.nodeflow.config.MapSection;
|
import thito.nodeflow.config.MapSection;
|
||||||
import thito.nodeflow.config.Section;
|
import thito.nodeflow.config.Section;
|
||||||
|
|
||||||
@ -20,22 +21,23 @@ public class YamlMapper {
|
|||||||
|
|
||||||
public static void read(String locale, Section section, TranslationNode node) {
|
public static void read(String locale, Section section, TranslationNode node) {
|
||||||
for(String key : section.getKeys()) {
|
for(String key : section.getKeys()) {
|
||||||
|
Object value = section.getInScope(key).get();
|
||||||
|
|
||||||
TranslationNode childNode = node.getOrCreateChildren(key);
|
TranslationNode childNode = node.getOrCreateChildren(key);
|
||||||
|
|
||||||
if(section.getMap(key).isPresent()) {
|
if(value instanceof MapSection) {
|
||||||
// Nested element - run recursively
|
// Nested element - run recursively
|
||||||
read(locale, section.getMap(key).get(), childNode);
|
System.out.println("run recurse");
|
||||||
|
read(locale, (MapSection) value, childNode);
|
||||||
} else {
|
} else {
|
||||||
Translation translation = childNode.getValue();
|
Translation translation = childNode.getValue();
|
||||||
|
|
||||||
if(section.getList(key).isPresent() || section.getString(key).isPresent()) {
|
String content = value instanceof ListSection
|
||||||
String content = section.isList(key) && section.getList(key).isPresent()
|
? YamlArrayMapper.read((ListSection) value)
|
||||||
? YamlArrayMapper.read(section.getList(key).get())
|
: StringUtil.escapeControls(String.valueOf(value), true);
|
||||||
: StringUtil.escapeControls(section.getString(key).get(), true);
|
|
||||||
|
|
||||||
translation.put(locale, content);
|
translation.put(locale, content);
|
||||||
childNode.setValue(translation);
|
childNode.setValue(translation);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +52,7 @@ public class YamlMapper {
|
|||||||
MapSection childSection = new MapSection();
|
MapSection childSection = new MapSection();
|
||||||
write(locale, childSection, childNode);
|
write(locale, childSection, childNode);
|
||||||
if(childSection.size() > 0) {
|
if(childSection.size() > 0) {
|
||||||
section.set(key, childSection);
|
section.setInScope(key, childSection);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Translation translation = childNode.getValue();
|
Translation translation = childNode.getValue();
|
||||||
@ -58,11 +60,11 @@ public class YamlMapper {
|
|||||||
|
|
||||||
if(content != null) {
|
if(content != null) {
|
||||||
if(YamlArrayMapper.isArray(content)) {
|
if(YamlArrayMapper.isArray(content)) {
|
||||||
section.set(key, YamlArrayMapper.write(content));
|
section.setInScope(key, YamlArrayMapper.write(content));
|
||||||
} else if(NumberUtils.isNumber(content)) {
|
} else if(NumberUtils.isNumber(content)) {
|
||||||
section.set(key, NumberUtils.createNumber(content));
|
section.setInScope(key, NumberUtils.createNumber(content));
|
||||||
} else {
|
} else {
|
||||||
section.set(key, StringEscapeUtils.unescapeJava(content));
|
section.setInScope(key, StringEscapeUtils.unescapeJava(content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
158
src/test/java/de/marhali/easyi18n/mapper/YamlMapperTest.java
Normal file
158
src/test/java/de/marhali/easyi18n/mapper/YamlMapperTest.java
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
package de.marhali.easyi18n.mapper;
|
||||||
|
|
||||||
|
import de.marhali.easyi18n.io.yaml.YamlArrayMapper;
|
||||||
|
import de.marhali.easyi18n.io.yaml.YamlMapper;
|
||||||
|
import de.marhali.easyi18n.model.TranslationData;
|
||||||
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
import thito.nodeflow.config.MapSection;
|
||||||
|
import thito.nodeflow.config.Section;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link de.marhali.easyi18n.io.yaml.YamlMapper}
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public class YamlMapperTest extends AbstractMapperTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testNonSorting() {
|
||||||
|
Section input = new MapSection();
|
||||||
|
input.set("zulu", "test");
|
||||||
|
input.set("alpha", "test");
|
||||||
|
input.set("bravo", "test");
|
||||||
|
|
||||||
|
TranslationData data = new TranslationData(false, true);
|
||||||
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Set<String> expect = new LinkedHashSet<>(Arrays.asList("zulu", "alpha", "bravo"));
|
||||||
|
Assert.assertEquals(expect, output.getKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testSorting() {
|
||||||
|
Section input = new MapSection();
|
||||||
|
input.set("zulu", "test");
|
||||||
|
input.set("alpha", "test");
|
||||||
|
input.set("bravo", "test");
|
||||||
|
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Set<String> expect = new LinkedHashSet<>(Arrays.asList("alpha", "bravo", "zulu"));
|
||||||
|
Assert.assertEquals(expect, output.getKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testArrays() {
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
data.setTranslation("simple", create(arraySimple));
|
||||||
|
data.setTranslation("escaped", create(arrayEscaped));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertTrue(output.isList("simple"));
|
||||||
|
Assert.assertEquals(arraySimple, YamlArrayMapper.read(output.getList("simple").get()));
|
||||||
|
Assert.assertTrue(output.isList("escaped"));
|
||||||
|
Assert.assertEquals(arrayEscaped, StringEscapeUtils.unescapeJava(YamlArrayMapper.read(output.getList("escaped").get())));
|
||||||
|
|
||||||
|
TranslationData input = new TranslationData(true, true);
|
||||||
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertTrue(YamlArrayMapper.isArray(input.getTranslation("simple").get("en")));
|
||||||
|
Assert.assertTrue(YamlArrayMapper.isArray(input.getTranslation("escaped").get("en")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testSpecialCharacters() {
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
data.setTranslation("chars", create(specialCharacters));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals(specialCharacters, output.getString("chars").get());
|
||||||
|
|
||||||
|
TranslationData input = new TranslationData(true, true);
|
||||||
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals(specialCharacters, StringEscapeUtils.unescapeJava(input.getTranslation("chars").get("en")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testNestedKeys() {
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
data.setTranslation("nested.key.section", create("test"));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals("test", output.getString("nested.key.section").get());
|
||||||
|
|
||||||
|
TranslationData input = new TranslationData(true, true);
|
||||||
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals("test", input.getTranslation("nested.key.section").get("en"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testNonNestedKeys() {
|
||||||
|
TranslationData data = new TranslationData(true, false);
|
||||||
|
data.setTranslation("long.key.with.many.sections", create("test"));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertTrue(output.getKeys().contains("long.key.with.many.sections"));
|
||||||
|
|
||||||
|
TranslationData input = new TranslationData(true, false);
|
||||||
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals("test", input.getTranslation("long.key.with.many.sections").get("en"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testLeadingSpace() {
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
data.setTranslation("space", create(leadingSpace));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals(leadingSpace, output.getString("space").get());
|
||||||
|
|
||||||
|
TranslationData input = new TranslationData(true, true);
|
||||||
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals(leadingSpace, input.getTranslation("space").get("en"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testNumbers() {
|
||||||
|
TranslationData data = new TranslationData(true, true);
|
||||||
|
data.setTranslation("numbered", create("15000"));
|
||||||
|
|
||||||
|
Section output = new MapSection();
|
||||||
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals(15000, output.getInteger("numbered").get().intValue());
|
||||||
|
|
||||||
|
Section input = new MapSection();
|
||||||
|
input.set("numbered", 143.23);
|
||||||
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
|
Assert.assertEquals("143.23", data.getTranslation("numbered").get("en"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user