29
29
*/
30
30
package com .jcabi .xml ;
31
31
32
- import com .google .common .collect .Iterables ;
33
32
import com .yegor256 .OnlineMeans ;
33
+ import com .yegor256 .Together ;
34
34
import com .yegor256 .WeAreOnline ;
35
35
import java .io .IOException ;
36
36
import java .net .InetAddress ;
37
- import java .net .SocketException ;
38
- import java .security .SecureRandom ;
39
- import java .util .Collections ;
40
- import java .util .Random ;
41
- import java .util .concurrent .Callable ;
42
- import java .util .concurrent .CountDownLatch ;
43
- import java .util .concurrent .ExecutorService ;
44
- import java .util .concurrent .Executors ;
45
- import java .util .concurrent .TimeUnit ;
46
- import java .util .concurrent .atomic .AtomicInteger ;
47
- import javax .xml .transform .Source ;
48
- import javax .xml .validation .Validator ;
49
37
import org .apache .commons .lang3 .StringUtils ;
50
38
import org .hamcrest .MatcherAssert ;
51
39
import org .hamcrest .Matchers ;
52
40
import org .junit .jupiter .api .Assertions ;
53
41
import org .junit .jupiter .api .Assumptions ;
54
42
import org .junit .jupiter .api .BeforeEach ;
55
- import org .junit .jupiter .api .Disabled ;
43
+ import org .junit .jupiter .api .RepeatedTest ;
56
44
import org .junit .jupiter .api .Test ;
57
45
import org .junit .jupiter .api .extension .ExtendWith ;
58
- import org .mockito .ArgumentMatchers ;
59
- import org .mockito .Mockito ;
60
- import org .mockito .stubbing .Answer ;
61
46
62
47
/**
63
48
* Test case for {@link StrictXML}.
68
53
*/
69
54
@ SuppressWarnings ({ "PMD.TooManyMethods" , "PMD.AvoidDuplicateLiterals" })
70
55
final class StrictXMLTest {
71
-
72
56
@ BeforeEach
73
57
void weAreOnline () throws IOException {
74
58
Assumptions .assumeTrue (
@@ -78,15 +62,18 @@ void weAreOnline() throws IOException {
78
62
79
63
@ Test
80
64
void passesValidXmlThrough () {
81
- new StrictXML (
82
- new XMLDocument ("<root>passesValidXmlThrough</root>" ),
83
- new XMLDocument (
84
- StringUtils .join (
85
- "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
86
- "<xs:element name='root' type='xs:string'/>" ,
87
- "</xs:schema>"
65
+ Assertions .assertDoesNotThrow (
66
+ new StrictXML (
67
+ new XMLDocument ("<root>RootXML</root>" ),
68
+ new XMLDocument (
69
+ StringUtils .join (
70
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
71
+ "<xs:element name='root' type='xs:string'/>" ,
72
+ "</xs:schema>"
73
+ )
88
74
)
89
- )
75
+ )::inner ,
76
+ "XML should be validated without errors"
90
77
);
91
78
}
92
79
@@ -95,11 +82,12 @@ void rejectsInvalidXmlThrough() {
95
82
Assertions .assertThrows (
96
83
IllegalArgumentException .class ,
97
84
new StrictXML (
98
- new XMLDocument ("<root>not an integer </root>" ),
85
+ new XMLDocument ("<root>string </root>" ),
99
86
new XMLDocument (
100
87
StringUtils .join (
101
- "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' >" ,
102
- "<xs:element name='root' type='xs:integer'/></xs:schema>"
88
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
89
+ "<xs:element name='root' type='xs:integer'/>" ,
90
+ "</xs:schema>"
103
91
)
104
92
)
105
93
)::inner ,
@@ -111,10 +99,13 @@ void rejectsInvalidXmlThrough() {
111
99
@ ExtendWith (WeAreOnline .class )
112
100
@ OnlineMeans (url = "http://maven.apache.org" )
113
101
void passesValidXmlUsingXsiSchemaLocation () throws Exception {
114
- new StrictXML (
115
- new XMLDocument (
116
- this .getClass ().getResource ("xsi-schemalocation-valid.xml" )
117
- )
102
+ Assertions .assertDoesNotThrow (
103
+ new StrictXML (
104
+ new XMLDocument (
105
+ this .getClass ().getResource ("xsi-schemalocation-valid.xml" )
106
+ )
107
+ )::inner ,
108
+ "XML with path to schema should be validated without errors"
118
109
);
119
110
}
120
111
@@ -142,121 +133,82 @@ void printsInnerXmlToString() {
142
133
new XMLDocument (xml ),
143
134
new XMLDocument (
144
135
StringUtils .join (
145
- "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' >" ,
146
- "<xs:element name='root' type='xs:string'/></xs:schema>"
136
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
137
+ "<xs:element name='root' type='xs:string'/>" ,
138
+ "</xs:schema>"
147
139
)
148
140
)
149
141
).toString (),
150
142
Matchers .containsString (xml )
151
143
);
152
144
}
153
145
154
- @ Test
155
- @ Disabled
156
- void validatesMultipleXmlsInThreads () throws Exception {
157
- final XML xsd = new XMLDocument (
158
- StringUtils .join (
159
- "<xs:schema xmlns:xs ='http://www.w3.org/2001/XMLSchema' >" ,
160
- "<xs:element name='r'><xs:complexType><xs:sequence>" ,
161
- "<xs:element name='x' maxOccurs='unbounded'><xs:simpleType>" ,
162
- "<xs:restriction base='xs:integer'>" ,
163
- "<xs:maxInclusive value='100'/></xs:restriction>" ,
164
- "</xs:simpleType></xs:element>" ,
165
- "</xs:sequence></xs:complexType></xs:element></xs:schema>"
146
+ @ RepeatedTest (60 )
147
+ void doesNotFailOnFetchingInMultipleThreadsFromTheSameDocument () {
148
+ final XML xml = new StrictXML (
149
+ new XMLDocument ("<root>RootXML</root>" ),
150
+ new XMLDocument (
151
+ StringUtils .join (
152
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
153
+ "<xs:element name='root' type='xs:string'/>" ,
154
+ "</xs:schema>"
155
+ )
166
156
)
167
157
);
168
- final Random rnd = new SecureRandom ();
169
- final XML xml = new XMLDocument (
170
- StringUtils .join (
171
- Iterables .concat (
172
- Collections .singleton ("<r>" ),
173
- Iterables .transform (
174
- Collections .nCopies (10 , 0 ),
175
- pos -> String .format (
176
- "<x>%d</x>" , rnd .nextInt (100 )
177
- )
178
- ),
179
- Collections .singleton ("<x>101</x></r>" )
180
- ),
181
- " "
182
- )
158
+ Assertions .assertDoesNotThrow (
159
+ new Together <>(
160
+ thread -> xml .nodes ("/root" )
161
+ )::asList ,
162
+ "StrictXML must not fail on fetching in multiple threads from the same document"
183
163
);
184
- final AtomicInteger done = new AtomicInteger ();
185
- final int threads = Runtime .getRuntime ().availableProcessors () * 10 ;
186
- final CountDownLatch latch = new CountDownLatch (threads );
187
- final Callable <Void > callable = () -> {
188
- try {
189
- new StrictXML (xml , xsd );
190
- } catch (final IllegalArgumentException ex ) {
191
- done .incrementAndGet ();
192
- } finally {
193
- latch .countDown ();
194
- }
195
- return null ;
196
- };
197
- final ExecutorService service = Executors .newFixedThreadPool (5 );
198
- try {
199
- for (int count = 0 ; count < threads ; count += 1 ) {
200
- service .submit (callable );
201
- }
202
- latch .await (1L , TimeUnit .SECONDS );
203
- MatcherAssert .assertThat (done .get (), Matchers .equalTo (threads ));
204
- } finally {
205
- service .shutdown ();
206
- MatcherAssert .assertThat (
207
- service .awaitTermination (10L , TimeUnit .SECONDS ),
208
- Matchers .is (true )
209
- );
210
- service .shutdownNow ();
211
- }
212
164
}
213
165
214
- @ Test
215
- @ Disabled
216
- void passesValidXmlWithNetworkProblems () throws Exception {
217
- final Validator validator = Mockito .mock (Validator .class );
218
- final AtomicInteger counter = new AtomicInteger (0 );
219
- // @checkstyle IllegalThrowsCheck (5 lines)
220
- Mockito .doAnswer (
221
- (Answer <Void >) invocation -> {
222
- final int attempt = counter .incrementAndGet ();
223
- if (attempt == 1 || attempt == 2 ) {
224
- throw new SocketException (
225
- String .format ("Attempt #%s failed" , attempt )
166
+ @ RepeatedTest (60 )
167
+ void doesNotFailOnFetchingInMultipleThreadsFromDifferentDocuments () {
168
+ Assertions .assertDoesNotThrow (
169
+ new Together <>(
170
+ thread -> {
171
+ final XML xml = new StrictXML (
172
+ new XMLDocument ("<root>RootXML</root>" ),
173
+ new XMLDocument (
174
+ StringUtils .join (
175
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" ,
176
+ "<xs:element name='root' type='xs:string'/>" ,
177
+ "</xs:schema>"
178
+ )
179
+ )
226
180
);
181
+ return xml .nodes ("/root" );
227
182
}
228
- return null ;
229
- }
230
- ).when (validator ).validate (ArgumentMatchers .any (Source .class ));
231
- new StrictXML (
232
- new XMLDocument (
233
- "<root>passesValidXmlWithNetworkProblems</root>"
234
- ),
235
- validator
183
+ )::asList ,
184
+ "StrictXML must not fail on fetching in multiple threads from different document"
236
185
);
237
186
}
238
187
239
188
@ Test
240
189
void lookupXsdsFromClasspath () {
241
- new StrictXML (
242
- new XMLDocument (
243
- StringUtils .join (
244
- "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" ,
245
- "<payment xmlns=\" http://jcabi.com/schema/xml\" " ,
246
- "xmlns:xsi=\" " ,
247
- "http://www.w3.org/2001/XMLSchema-instance" ,
248
- "\" " ,
249
- "xsi:schemaLocation=\" " ,
250
- "http://jcabi.com/schema/xml " ,
251
- "com/jcabi/xml/sample-namespaces.xsd" ,
252
- "\" >" ,
253
- "<id>333</id>" ,
254
- "<date>1-Jan-2013</date>" ,
255
- "<debit>test-1</debit>" ,
256
- "<credit>test-2</credit>" ,
257
- "</payment>"
190
+ Assertions .assertDoesNotThrow (
191
+ new StrictXML (
192
+ new XMLDocument (
193
+ StringUtils .join (
194
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" ,
195
+ "<payment xmlns=\" http://jcabi.com/schema/xml\" " ,
196
+ "xmlns:xsi=\" " ,
197
+ "http://www.w3.org/2001/XMLSchema-instance" ,
198
+ "\" " ,
199
+ "xsi:schemaLocation=\" " ,
200
+ "http://jcabi.com/schema/xml " ,
201
+ "com/jcabi/xml/sample-namespaces.xsd" ,
202
+ "\" >" ,
203
+ "<id>333</id>" ,
204
+ "<date>1-Jan-2013</date>" ,
205
+ "<debit>test-1</debit>" ,
206
+ "<credit>test-2</credit>" ,
207
+ "</payment>"
208
+ )
258
209
)
259
- )
210
+ )::inner ,
211
+ "StrictXML should not have failed on validation via classpath"
260
212
);
261
213
}
262
214
0 commit comments