This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ResourceUtilsTest.java
439 lines (361 loc) · 15.7 KB
/
ResourceUtilsTest.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package org.zanata.rest.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.persistence.EntityManager;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.ZanataTest;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.MergeType;
import org.zanata.dao.LocaleDAO;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HSimpleComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.rest.dto.extensions.gettext.PoTargetHeader;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.seam.SeamAutowire;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(DataProviderRunner.class)
public class ResourceUtilsTest extends ZanataTest {
private static final Logger log = LoggerFactory
.getLogger(ResourceUtilsTest.class);
private static ResourceUtils resourceUtils;
@Mock
private EntityManager mockEm;
@Mock
private LocaleDAO mockLocaleDAO;
@BeforeClass
public static void logMemoryForTests() {
// FIXME why is this here?
Runtime runtime = Runtime.getRuntime();
log.info("total memory :" + runtime.totalMemory());
log.info("unit tests free memory :" + runtime.freeMemory());
}
@Before
public void initializeResourceUtils() {
MockitoAnnotations.initMocks(this);
resourceUtils =
SeamAutowire.instance().reset()
.use("entityManager", mockEm)
.use("localeDAO", mockLocaleDAO)
.autowire(ResourceUtils.class);
}
@Test
public void mergeNoTextFlows() {
List<TextFlow> from = new ArrayList<TextFlow>();
HDocument to = new HDocument();
boolean changed =
resourceUtils.transferFromTextFlows(from, to,
new HashSet<String>(), 1);
assertThat(changed, is(false));
}
@Test
public void mergeTextFlowWithOneFromChange() {
List<TextFlow> from = new ArrayList<TextFlow>();
TextFlow tf1 = new TextFlow("id", LocaleId.EN, "text1");
from.add(tf1);
HDocument to = new HDocument();
boolean changed =
resourceUtils.transferFromTextFlows(from, to,
new HashSet<String>(), 1);
assertThat(changed, is(true));
}
@Test
public void mergeChangedTextFlow() {
// set up HDocument with a text flow
HDocument to = new HDocument();
int originalTFRevision = 1;
to.setRevision(originalTFRevision);
HTextFlow originalTF = new HTextFlow(to, "id", "original text");
originalTF.setRevision(originalTFRevision);
// target locales that will have new, fuzzy and approved targets
HLocale newLoc, fuzzyLoc, apprLoc;
newLoc = new HLocale(LocaleId.DE);
fuzzyLoc = new HLocale(LocaleId.FR);
apprLoc = new HLocale(LocaleId.ES);
// Target Locale ids
Long newLocId = 1L, fuzzyLocId = 2L, apprLocId = 3L;
HTextFlowTarget newTarg, fuzzyTarg, apprTarg;
newTarg = new HTextFlowTarget(originalTF, newLoc);
fuzzyTarg = new HTextFlowTarget(originalTF, fuzzyLoc);
apprTarg = new HTextFlowTarget(originalTF, apprLoc);
int newTargVersionBefore = 1;
int fuzzyTargVersionBefore = 1;
int apprTargVersionBefore = 1;
newTarg.setVersionNum(newTargVersionBefore);
fuzzyTarg.setVersionNum(fuzzyTargVersionBefore);
apprTarg.setVersionNum(apprTargVersionBefore);
newTarg.setState(ContentState.New);
fuzzyTarg.setState(ContentState.NeedReview);
apprTarg.setState(ContentState.Approved);
originalTF.getTargets().put(newLocId, newTarg);
originalTF.getTargets().put(fuzzyLocId, fuzzyTarg);
originalTF.getTargets().put(apprLocId, apprTarg);
to.getAllTextFlows().put("id", originalTF);
// set up a textflow with the same id and different content
TextFlow changedTF =
new TextFlow(originalTF.getResId(), LocaleId.EN, "changed text");
List<TextFlow> from = new ArrayList<TextFlow>();
from.add(changedTF);
int newTFRevision = 2;
boolean changed =
resourceUtils.transferFromTextFlows(from, to,
new HashSet<String>(), newTFRevision);
Map<Long, HTextFlowTarget> targets =
to.getAllTextFlows().get("id").getTargets();
newTarg = targets.get(newLocId);
assertThat(newTarg.getState(), is(ContentState.New));
assertThat(newTarg.getVersionNum(), is(newTargVersionBefore));
assertThat(newTarg.getTextFlowRevision(), is(originalTFRevision));
fuzzyTarg = targets.get(fuzzyLocId);
assertThat(fuzzyTarg.getState(), is(ContentState.NeedReview));
assertThat(fuzzyTarg.getVersionNum(), is(fuzzyTargVersionBefore));
assertThat(fuzzyTarg.getTextFlowRevision(), is(originalTFRevision));
apprTarg = targets.get(apprLocId);
assertThat(
"approved targets should be set to fuzzy when source content changes",
apprTarg.getState(), is(ContentState.NeedReview));
assertThat(apprTarg.getVersionNum(), is(apprTargVersionBefore + 1));
// Note: TFTRevision should be updated when target content or state is
// changed in editor, not here.
assertThat(apprTarg.getTextFlowRevision(), is(originalTFRevision));
assertThat(changed, is(true));
}
@Test
public void pushCommentInitialImport() {
PoTargetHeader fromHeader = new PoTargetHeader();
String comment = "comment to import\nsecond line";
fromHeader.setComment(comment);
HPoTargetHeader toHeader = new HPoTargetHeader();
resourceUtils.pushPoTargetComment(fromHeader, toHeader, MergeType.AUTO);
assertThat("", toHeader.getComment().getComment(), is(comment));
}
@Test
public void pushCommentSkip() {
PoTargetHeader fromHeader = new PoTargetHeader();
String comment = "comment to import\nskip this #zanata\nlast line";
String expectedComment = "comment to import\nlast line";
fromHeader.setComment(comment);
HPoTargetHeader toHeader = new HPoTargetHeader();
resourceUtils.pushPoTargetComment(fromHeader, toHeader,
MergeType.IMPORT);
assertThat("", toHeader.getComment().getComment(), is(expectedComment));
}
@Test
public void pushCommentMerge() {
PoTargetHeader fromHeader = new PoTargetHeader();
String importedComment = "initial comment\nAlice #zanata\nCharlie";
String expectedComment = "initial comment\nBob\nCharlie";
fromHeader.setComment(importedComment);
HPoTargetHeader toHeader = new HPoTargetHeader();
toHeader.setComment(new HSimpleComment("initial comment\nBob"));
resourceUtils.pushPoTargetComment(fromHeader, toHeader, MergeType.AUTO);
assertThat("", toHeader.getComment().getComment(), is(expectedComment));
}
@Test
public void pushCommentImport() {
PoTargetHeader fromHeader = new PoTargetHeader();
String importedComment = "initial comment\nAlice #zanata\nCharlie";
String expectedComment = "initial comment\nCharlie";
fromHeader.setComment(importedComment);
HPoTargetHeader toHeader = new HPoTargetHeader();
toHeader.setComment(new HSimpleComment("initial comment\nBob"));
resourceUtils.pushPoTargetComment(fromHeader, toHeader,
MergeType.IMPORT);
assertThat("", toHeader.getComment().getComment(), is(expectedComment));
}
@Test
public void pullCommentEmpty() {
HPoTargetHeader fromHeader = new HPoTargetHeader();
PoTargetHeader toHeader = new PoTargetHeader();
List<HTextFlowTarget> hTargets = Collections.emptyList();
resourceUtils.pullPoTargetComment(fromHeader, toHeader, hTargets);
assertThat("", toHeader.getComment(), is(""));
}
@Test
public void pullCommentInitial() {
HPoTargetHeader fromHeader = new HPoTargetHeader();
fromHeader.setComment(new HSimpleComment("initial comment"));
String expectedComment = "initial comment";
PoTargetHeader toHeader = new PoTargetHeader();
List<HTextFlowTarget> hTargets = Collections.emptyList();
resourceUtils.pullPoTargetComment(fromHeader, toHeader, hTargets);
assertThat("", toHeader.getComment(), is(expectedComment));
}
@Test
public void pullCommentWithCredits() {
HPoTargetHeader fromHeader = new HPoTargetHeader();
fromHeader.setComment(new HSimpleComment("initial comment"));
String expectedComment =
"initial comment\n"
+ "Alice <alice@example.org>, 2010. #zanata\n"
+ "Alice <alice@example.org>, 2011. #zanata";
PoTargetHeader toHeader = new PoTargetHeader();
HPerson alice = new HPerson();
alice.setName("Alice");
alice.setEmail("alice@example.org");
List<HTextFlowTarget> hTargets = new ArrayList<HTextFlowTarget>();
HTextFlowTarget tft1 = new HTextFlowTarget();
tft1.setLastChanged(new Date(1302671654000L)); // 13 Apr 2011
tft1.setLastModifiedBy(alice);
hTargets.add(tft1);
HTextFlowTarget tft2 = new HTextFlowTarget();
tft2.setLastChanged(new Date(1304329523000L)); // 2 May 2011
tft2.setLastModifiedBy(alice);
hTargets.add(tft2);
HTextFlowTarget tft3 = new HTextFlowTarget();
tft3.setLastChanged(new Date(1262419384000L)); // 2 Jan 2010
tft3.setLastModifiedBy(alice);
hTargets.add(tft3);
resourceUtils.pullPoTargetComment(fromHeader, toHeader, hTargets);
assertThat("", toHeader.getComment(), is(expectedComment));
}
@Test
public void splitLinesSimple() {
String s = "1\n2\n3";
List<String> expected = Arrays.asList("1", "2", "3");
List<String> lines = ResourceUtils.splitLines(s, null);
assertThat("", lines, is(expected));
}
@Test
public void splitLinesEmpty() {
String s = "";
List<String> expected = Collections.emptyList();
List<String> lines = ResourceUtils.splitLines(s, null);
assertThat("", lines, is(expected));
}
@Test
public void splitLinesSkip() {
String s = "1\n2 #zanata\n3";
List<String> expected = Arrays.asList("1", "3");
List<String> lines = ResourceUtils.splitLines(s, "#zanata");
assertThat("", lines, is(expected));
}
@Test
public void splitLinesSkipAll() {
String s = "1 #zanata\n2 #zanata\n3 #zanata";
List<String> expected = Collections.emptyList();
List<String> lines = ResourceUtils.splitLines(s, "#zanata");
assertThat("", lines, is(expected));
}
@DataProvider
public static Object[][] urlPatterns() {
return new Object[][] {
{ ",my,doc,id", "/my/doc/id" },
{ ",my,,doc,id", "/my//doc/id" },
{ "x+y", "x y" }
};
}
@Test
@UseDataProvider("urlPatterns")
public void decodeDocIds(String encoded, String decoded) {
assertThat("Decoding " + encoded, resourceUtils.decodeDocId(encoded),
is(decoded));
}
@Test
@UseDataProvider("urlPatterns")
public void encodeDocIds(String encoded, String decoded) {
assertThat("Encoding " + decoded, resourceUtils.encodeDocId(decoded),
is(encoded));
}
/**
* Tests that all plural information is readable
*/
@Test
public void readPluralForms() {
Properties properties = ResourceUtils.getPluralForms();
for (Object key : properties.keySet()) {
String propKey = (String) key;
LocaleId localeId = LocaleId.fromJavaName(propKey);
resourceUtils.getPluralForms(localeId, true, false);
verify(mockLocaleDAO).findByLocaleId(localeId);
resourceUtils.getNPluralForms(null, localeId);
}
}
@Test
public void pluralFormsAlternateSeparators() {
// Plural forms for "es"
String esPluralForm = resourceUtils.getPluralForms(LocaleId.ES);
verify(mockLocaleDAO).findByLocaleId(LocaleId.ES);
assertThat(esPluralForm, notNullValue());
// Alternate forms that should match the "es" plurals
// "es_ES"
assertThat(resourceUtils.getPluralForms(new LocaleId("es-ES")),
is(esPluralForm));
// "es.ES"
assertThat(resourceUtils.getPluralForms(new LocaleId("es.ES")),
is(esPluralForm));
// "es@ES"
assertThat(resourceUtils.getPluralForms(new LocaleId("es@ES")),
is(esPluralForm));
// "es.ES@Latin"
assertThat(resourceUtils.getPluralForms(new LocaleId("es.ES@Latin")),
is(esPluralForm));
}
@Test
public void pluralFormsTest() {
//given mock data
String testPluralForms = "testPluralForms";
HLocale mockHLocale = new HLocale(LocaleId.ES);
mockHLocale.setPluralForms(testPluralForms);
when(mockLocaleDAO.findByLocaleId(LocaleId.ES)).thenReturn(mockHLocale);
//execute
String pluralForms = resourceUtils.getPluralForms(LocaleId.ES);
//verify and assert
verify(mockLocaleDAO).findByLocaleId(LocaleId.ES);
assertThat(pluralForms, is(testPluralForms));
}
@Test
public void pluralFormsTestUseDBEntryTest() {
//given mock data
HLocale mockHLocale = new HLocale(LocaleId.ES);
when(mockLocaleDAO.findByLocaleId(LocaleId.ES)).thenReturn(mockHLocale);
//execute
String pluralForms = resourceUtils.getPluralForms(LocaleId.ES);
//verify and assert
verify(mockLocaleDAO).findByLocaleId(LocaleId.ES);
assertThat(pluralForms, notNullValue());
}
@Test
public void isValidPluralFormsTest() {
String invalidPluralForms = "testPluralForms";
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(false));
invalidPluralForms = "nplurals=notinteger";
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(false));
invalidPluralForms = "nplurals=-1";
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(false));
invalidPluralForms = "nplurals=" + ResourceUtils.MAX_TARGET_CONTENTS + 1;
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(false));
invalidPluralForms = "nplurals=0";
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(false));
invalidPluralForms = "nplurals=1;plural=0";
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(true));
invalidPluralForms = "nplurals=5; plural=10" +
ResourceUtils.MAX_TARGET_CONTENTS;
assertThat(resourceUtils.isValidPluralForms(invalidPluralForms), is(true));
}
}