ADDED: TranslationXmlStreamReader + tests for XML based translation files

This commit is contained in:
Kc
2018-09-30 23:57:27 +02:00
parent e9d8b3e6e3
commit 6bc0eeac4d
8 changed files with 477 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
package test;
import globalization.TranslationEntry;
import globalization.TranslationXmlStreamReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import org.junit.Assert;
import static org.junit.Assert.*;
import static test.utils.AssertExtensions.*;
import org.junit.Test;
import test.utils.EqualityComparer;
public class GlobalizationTests {
private class TranslationEntryEqualityComparer implements EqualityComparer<TranslationEntry> {
@Override
public boolean equals(TranslationEntry first, TranslationEntry second) {
if(first == null && second == null)
return true;
if(first == null && second != null || first != null && second == null)
return false;
if(!first.locale().equals(second.locale()))
return false;
if(!first.path().equals(second.path()))
return false;
if(!first.template().equals(second.template()))
return false;
return true;
}
@Override
public int hashCode(TranslationEntry item) {
int hash = 17;
final int multiplier = 59;
hash = hash * multiplier + item.locale().hashCode();
hash = hash * multiplier + item.path().hashCode();
hash = hash * multiplier + item.template().hashCode();
return hash;
}
}
@Test
public void TestTranslationXmlReader() throws XMLStreamException {
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<localization>\n" +
" <context locale=\"en-GB\">\n" +
" <context path=\"path1\">\n" +
" <context path=\"path2/path3\">\n" +
" <translation key=\"key1\" template=\"1\" />\n" +
" <translation key=\"key2\" template=\"2\" />\n" +
" </context>\n" +
" </context>\n" +
" </context>\n" +
"</localization>\n";
List<TranslationEntry> expected = new ArrayList<TranslationEntry>();
expected.add(new TranslationEntry(Locale.forLanguageTag("en-GB"), "/path1/path2/path3/key1", "1"));
expected.add(new TranslationEntry(Locale.forLanguageTag("en-GB"), "/path1/path2/path3/key2", "2"));
InputStream is = new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")));
TranslationXmlStreamReader reader = new TranslationXmlStreamReader();
Iterable<TranslationEntry> actual = reader.ReadFrom(is);
assertSetEquals(expected, actual, new TranslationEntryEqualityComparer());
}
}

View File

@@ -0,0 +1,21 @@
package test.utils;
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
public class AssertExtensions {
public static <T> void assertSetEquals(Iterable<T> expected, Iterable<T> actual, EqualityComparer<T> comparer) {
Set<EquatableWrapper<T>> expectedSet = new HashSet<EquatableWrapper<T>>();
for(T item: expected)
expectedSet.add(new EquatableWrapper<T>(item, comparer));
Set<EquatableWrapper<T>> actualSet = new HashSet<EquatableWrapper<T>>();
for(T item: actual)
actualSet.add(new EquatableWrapper<T>(item, comparer));
Assert.assertEquals(expectedSet, actualSet);
}
}

View File

@@ -0,0 +1,6 @@
package test.utils;
public interface EqualityComparer<T> {
boolean equals(T first, T second);
int hashCode(T item);
}

View File

@@ -0,0 +1,34 @@
package test.utils;
class EquatableWrapper<T> {
private final T item;
private final EqualityComparer<T> comparer;
public EquatableWrapper(T item, EqualityComparer<T> comparer) {
this.item = item;
this.comparer = comparer;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if (!(this.getClass().isInstance(obj)))
return false;
EquatableWrapper<T> otherWrapper = (EquatableWrapper<T>)obj;
if (otherWrapper.item == this.item)
return true;
return this.comparer.equals(this.item, otherWrapper.item);
}
@Override
public int hashCode() {
return this.comparer.hashCode(this.item);
}
@Override
public String toString() {
return this.item.toString();
}
}