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
/
JavaVariablesValidationTest.java
247 lines (206 loc) · 8.63 KB
/
JavaVariablesValidationTest.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
/*
* Copyright 2011, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.shared.validation;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.server.locale.Gwti18nReader;
import org.zanata.webtrans.shared.model.ValidationId;
import org.zanata.webtrans.shared.validation.action.JavaVariablesValidation;
/**
*
* @author David Mason, damason@redhat.com
*
**/
public class JavaVariablesValidationTest {
private JavaVariablesValidation javaVariablesValidation;
private ValidationMessages messages;
@Before
public void init() throws IOException {
MockitoAnnotations.initMocks(this);
messages = Gwti18nReader.create(ValidationMessages.class);
javaVariablesValidation =
new JavaVariablesValidation(ValidationId.JAVA_VARIABLES,
messages);
javaVariablesValidation.getRules().setEnabled(true);
}
@Test
public void idIsSet() {
assertThat(javaVariablesValidation.getId(),
is(ValidationId.JAVA_VARIABLES));
}
@Test
public void noErrorForMatchingVars() {
String source = "Testing string with variable {0} and {1}";
String target = "{1} and {0} included, order not relevant";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void missingVarInTarget() {
String source = "Testing string with variable {0}";
String target = "Testing string with no variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList,
hasItem(messages.varsMissing(Arrays.asList("{0}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void missingVarsThroughoutTarget() {
String source = "{0} variables in all parts {1} of the string {2}";
String target = "Testing string with no variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList, hasItem(messages.varsMissing(Arrays.asList("{0}",
"{1}", "{2}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void addedVarInTarget() {
String source = "Testing string with no variables";
String target = "Testing string with variable {0}";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList, hasItem(messages.varsAdded(Arrays.asList("{0}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void addedVarsThroughoutTarget() {
String source = "Testing string with no variables";
String target = "{0} variables in all parts {1} of the string {2}";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList,
hasItem(messages.varsAdded(Arrays.asList("{0}", "{1}", "{2}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void bothAddedAndMissingVars() {
String source = "String with {0} and {1} only, not 2";
String target = "String with {1} and {2}, not 0";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(
errorList,
hasItems(messages.varsAdded(Arrays.asList("{2}")),
messages.varsMissing(Arrays.asList("{0}"))));
assertThat(errorList.size(), is(2));
}
@Test
public void disturbanceInTheForce() {
String source =
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.";
String target =
"At time on date, there was a disturbance in the force on planet Earth";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList, hasItem(messages.varsMissing(Arrays.asList("{0}",
"{1}", "{2}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void diskContainsFiles() {
String source = "The disk \"{1}\" contains {0} file(s).";
String target = "The disk contains some files";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList,
hasItem(messages.varsMissing(Arrays.asList("{0}", "{1}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void doesNotDetectEscapedVariables() {
String source = "This string does not contain \\{0\\} style variables";
String target = "This string does not contain java style variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void doesNotDetectQuotedVariables() {
String source = "This string does not contain '{0}' style variables";
String target = "This string does not contain java style variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void doesNotDetectVariablesInQuotedText() {
String source = "This 'string does not contain {0} style' variables";
String target = "This string does not contain java style variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void ignoresEscapedQuotes() {
String source =
"This string does not contain \\'{0}\\' style variables";
String target = "This string does not contain java style variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList,
hasItem(messages.varsMissing(Arrays.asList("{0}"))));
assertThat(errorList.size(), is(1));
}
@Test
public void advancedQuoting() {
String source = "'''{'0}'''''{0}'''";
String target =
"From examples on MessageFormat page, should not contain any variables";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void translatedChoicesStillMatch() {
String source =
"There {0,choice,0#are no things|1#is one thing|1<are many things}.";
String target =
"Es gibt {0,choice,0#keine Dinge|1#eine Sache|1<viele Dinge}.";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList.size(), is(0));
}
@Test
public void choiceFormatAndRecursion() {
String source =
"There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.";
String target = "There are 0 files";
List<String> errorList =
javaVariablesValidation.validate(source, target);
assertThat(errorList,
hasItem(messages.varsMissing(Arrays.asList("{0}"))));
assertThat(errorList.size(), is(1));
}
// TODO tests for format type
// TODO test 3 or 4 levels of recursion
}