This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
PropReaderTests.java
160 lines (133 loc) · 6.05 KB
/
PropReaderTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.zanata.adapter.properties;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TranslationsResource;
public class PropReaderTests
{
private static final Logger log = LoggerFactory.getLogger(PropReaderTests.class);
private static final String TEST_OUTPUT_DIR_STRING = "target/test-output";
private static final File TEST_OUTPUT_DIR = new File(TEST_OUTPUT_DIR_STRING);
PropReader propReader;
static final String ISO_8859_1 = "ISO-8859-1";
@BeforeMethod
public void resetReader()
{
propReader = new PropReader();
}
@Test
public void roundtripSrcPropsToDocXmlToProps() throws Exception
{
String docName = "test.properties";
Resource srcDoc = new Resource("test");
InputStream testStream = getResourceAsStream(docName);
propReader.extractTemplate(srcDoc, testStream);
JAXBContext jc = JAXBContext.newInstance(Resource.class);
Marshaller marshal = jc.createMarshaller();
StringWriter sw = new StringWriter();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshal.marshal(srcDoc, sw);
log.debug("{}", sw);
Unmarshaller unmarshal = jc.createUnmarshaller();
Resource docIn = (Resource) unmarshal.unmarshal(new StringReader(sw.toString()));
PropWriter.write(docIn, TEST_OUTPUT_DIR, ISO_8859_1);
assertInputAndOutputDocContentSame(docName);
}
@Test
public void roundtripTransPropsToDocXmlToProps() throws Exception
{
String locale = "fr";
String docName = "test_fr.properties";
InputStream targetStream = getResourceAsStream(docName);
TranslationsResource transDoc = new TranslationsResource();
propReader.extractTarget(transDoc, targetStream, new LocaleId(locale), ContentState.New);
JAXBContext jc = JAXBContext.newInstance(TranslationsResource.class);
Marshaller marshal = jc.createMarshaller();
StringWriter sw = new StringWriter();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshal.marshal(transDoc, sw);
log.debug("{}", sw);
Unmarshaller unmarshal = jc.createUnmarshaller();
TranslationsResource docIn = (TranslationsResource) unmarshal.unmarshal(new StringReader(sw.toString()));
PropWriter.write(docIn, TEST_OUTPUT_DIR, "test", locale, ISO_8859_1);
assertInputAndOutputDocContentSame(docName);
}
/**
* Asserts that the content of the input document and output document are
* identical. Assumes filename is the same for both documents and that the
* output file is in TEST_OUTPUT_DIR.
*
* @param docName the name for both input and output files
* @throws FileNotFoundException
* @throws IOException
* @throws MalformedURLException
*/
private void assertInputAndOutputDocContentSame(String docName) throws FileNotFoundException, IOException, MalformedURLException
{
File newFile = new File(TEST_OUTPUT_DIR.getPath() + File.separator + docName);
InputStream newStream = newFile.toURI().toURL().openStream();
InputStream origStream = getResourceAsStream(docName);
String origContent = IOUtils.toString(origStream);
String newContent = IOUtils.toString(newStream);
// note: this does not allow for differences in whitespace, so if tests
// start failing this should be updated to use a less strict comparison
assertEquals(newContent, origContent, "output file should be the same as the input file");
}
private InputStream getResourceAsStream(String relativeResourceName) throws FileNotFoundException
{
InputStream stream = PropReaderTests.class.getResourceAsStream(relativeResourceName);
if (stream == null)
throw new FileNotFoundException(relativeResourceName);
return stream;
}
@Test
public void extractTemplateRemovesNonTranslateableRegions() throws IOException
{
Resource srcDoc = new Resource("test");
InputStream testStream = getResourceAsStream("test_non_trans.properties");
propReader.extractTemplate(srcDoc, testStream);
List<TextFlow> textFlows = srcDoc.getTextFlows();
assertEquals(textFlows.size(), 2, "Unexpected number of textflows");
assertEquals(textFlows.get(0).getId(), "HELLO", "Unexpected textflow id");
assertEquals(textFlows.get(1).getId(), "GOODBYE", "Unexpected textflow id");
// TODO also check comments?
}
@Test
public void extractTemplateNestedNonTranslatableRegions() throws Exception
{
Resource srcDoc = new Resource("test");
InputStream testStream = getResourceAsStream("test_non_trans_nested.properties");
propReader.extractTemplate(srcDoc, testStream);
List<TextFlow> textFlows = srcDoc.getTextFlows();
assertEquals(textFlows.size(), 2, "Unexpected number of textflows");
assertEquals(textFlows.get(0).getId(), "HELLO", "Unexpected textflow id");
assertEquals(textFlows.get(1).getId(), "GOODBYE", "Unexpected textflow id");
// TODO also check comments?
}
@Test(expectedExceptions = InvalidPropertiesFormatException.class)
public void extractTemplateNonTranslatableMismatchException() throws IOException, InvalidPropertiesFormatException
{
Resource srcDoc = new Resource("test");
InputStream testStream = getResourceAsStream("test_non_trans_mismatch.properties");
propReader.extractTemplate(srcDoc, testStream);
}
}