1
1
package com .tngtech .archunit .lang ;
2
2
3
+ import java .io .File ;
4
+ import java .io .IOException ;
5
+ import java .util .Iterator ;
6
+ import java .util .List ;
7
+ import java .util .SortedSet ;
8
+ import java .util .TreeSet ;
9
+
3
10
import com .google .common .base .Joiner ;
4
11
import com .google .common .base .Splitter ;
5
12
import com .google .common .io .Files ;
13
+ import com .tngtech .archunit .ArchConfiguration ;
6
14
import com .tngtech .archunit .core .domain .JavaClass ;
7
15
import com .tngtech .archunit .core .domain .JavaClasses ;
8
16
import com .tngtech .archunit .core .domain .JavaClassesTest ;
17
+ import com .tngtech .archunit .core .importer .testexamples .SomeClass ;
9
18
import com .tngtech .archunit .lang .ArchConditionTest .ConditionWithInitAndFinish ;
10
19
import com .tngtech .archunit .lang .syntax .ArchRuleDefinition ;
20
+ import com .tngtech .archunit .testutil .ArchConfigurationRule ;
11
21
import org .hamcrest .Description ;
12
22
import org .hamcrest .TypeSafeMatcher ;
13
23
import org .junit .After ;
16
26
import org .junit .Test ;
17
27
import org .junit .rules .ExpectedException ;
18
28
19
- import java .io .File ;
20
- import java .io .IOException ;
21
- import java .util .Iterator ;
22
- import java .util .List ;
23
- import java .util .SortedSet ;
24
- import java .util .TreeSet ;
25
-
26
29
import static com .google .common .collect .Lists .newArrayList ;
27
30
import static com .tngtech .archunit .core .domain .TestUtils .importClasses ;
28
31
import static com .tngtech .archunit .core .domain .TestUtils .importClassesWithContext ;
31
34
import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .all ;
32
35
import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .classes ;
33
36
import static com .tngtech .archunit .testutil .TestUtils .toUri ;
37
+ import static java .lang .Boolean .FALSE ;
34
38
import static java .nio .charset .StandardCharsets .UTF_8 ;
35
39
import static org .assertj .core .api .Assertions .assertThat ;
36
40
37
41
public class ArchRuleTest {
42
+ private static final String FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME = "archRule.failOnEmptyShould" ;
43
+
38
44
@ Rule
39
45
public final ExpectedException thrown = ExpectedException .none ();
40
46
47
+ @ Rule
48
+ public final ArchConfigurationRule archConfigurationRule = new ArchConfigurationRule ();
49
+
41
50
@ Before
42
51
public void setUp () {
43
52
ignoreFile ().delete ();
@@ -159,6 +168,35 @@ public void finish(ConditionEvents events) {
159
168
assertThat (condition .eventsFromFinish .getViolating ()).hasSize (1 );
160
169
}
161
170
171
+ @ Test
172
+ public void evaluation_fails_because_of_empty_set_of_classes_with_default_fail_on_empty_should () {
173
+ archConfigurationRule .removeProperty (FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME );
174
+
175
+ thrown .expect (AssertionError .class );
176
+ thrown .expectMessage ("Rule failed to check any classes" );
177
+ thrown .expectMessage (FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME );
178
+
179
+ classes ().should (ALWAYS_BE_VALID ).evaluate (importClasses ());
180
+ }
181
+
182
+ @ Test
183
+ public void evaluation_fails_because_of_empty_set_of_classes_after_filter_with_default_fail_on_empty_should () {
184
+ archConfigurationRule .removeProperty (FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME );
185
+
186
+ thrown .expect (AssertionError .class );
187
+ thrown .expectMessage ("Rule failed to check any classes" );
188
+ thrown .expectMessage (FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME );
189
+
190
+ classes ().that ().doNotHaveSimpleName (SomeClass .class .getSimpleName ()).should (ALWAYS_BE_VALID ).evaluate (importClasses (SomeClass .class ));
191
+ }
192
+
193
+ @ Test
194
+ public void evaluation_passes_on_empty_set_of_classes_with_deactivated_fail_on_empty_should () {
195
+ ArchConfiguration .get ().setProperty (FAIL_ON_EMPTY_SHOULD_PROPERTY_NAME , FALSE .toString ());
196
+
197
+ classes ().should (ALWAYS_BE_VALID ).evaluate (importClasses ());
198
+ }
199
+
162
200
private ClassesTransformer <String > strings () {
163
201
return new AbstractClassesTransformer <String >("strings" ) {
164
202
@ Override
@@ -250,10 +288,18 @@ public void check(JavaClass item, ConditionEvents events) {
250
288
}
251
289
};
252
290
291
+ private static final ArchCondition <JavaClass > ALWAYS_BE_VALID =
292
+ new ArchCondition <JavaClass >("always be valid" ) {
293
+ @ Override
294
+ public void check (JavaClass item , ConditionEvents events ) {
295
+ }
296
+ };
297
+
298
+ @ SuppressWarnings ({"unused" , "ResultOfMethodCallIgnored" })
253
299
private static class ClassAccessingStringTwoTimes {
254
300
void execute () {
255
301
"foo" .length ();
256
302
"bar" .replaceAll ("a" , "b" );
257
303
}
258
304
}
259
- }
305
+ }
0 commit comments