2424
2525import java .util .Arrays ;
2626import java .util .Collections ;
27- import java .util .List ;
2827
2928import javax .script .Bindings ;
3029import javax .script .ScriptEngine ;
@@ -91,13 +90,15 @@ void trivialGroovyScript() {
9190 @ Test
9291 void trivialNonJavaScript () {
9392 EnabledIf annotation = mockEnabledIfAnnotation ("one" , "two" );
94- assertLinesMatchCreatedScript (Arrays .asList ("one" , "two" ), annotation , "unknown language" );
93+ String script = condition .createScript (annotation , "unknown language" );
94+ assertLinesMatch (Arrays .asList ("one" , "two" ), Arrays .asList (script .split ("\\ R" )));
9595 }
9696
9797 @ Test
9898 void createJavaScriptMultipleLines () {
9999 EnabledIf annotation = mockEnabledIfAnnotation ("m1()" , "m2()" );
100- assertLinesMatchCreatedScript (Arrays .asList ("m1()" , "m2()" ), annotation , "ECMAScript" );
100+ String script = condition .createScript (annotation , "ECMAScript" );
101+ assertLinesMatch (Arrays .asList ("m1()" , "m2()" ), Arrays .asList (script .split ("\\ R" )));
101102 }
102103
103104 @ Test
@@ -119,17 +120,22 @@ void createReasonWithCustomMessage() {
119120 void syntaxErrorInScriptFailsTest () {
120121 EnabledIf annotation = mockEnabledIfAnnotation ("syntax error" );
121122 Exception exception = assertThrows (JUnitException .class , () -> evaluate (annotation ));
122- assertTrue (exception .getMessage ().contains ("@EnabledIf" ));
123- assertTrue (exception .getMessage ().contains ("script" ));
124- assertTrue (exception .getMessage ().contains ("bindings" ));
123+ assertThat (exception .getMessage ()).contains ("@EnabledIf" , "script" , "syntax error" , "bindings" );
124+ }
125+
126+ @ Test
127+ @ EnabledIf ("true" )
128+ void defaultAnnotationValues () {
129+ EnabledIf annotation = reflectEnabledIfAnnotation ("defaultAnnotationValues" );
130+ assertEquals ("true" , String .join ("-" , annotation .value ()));
131+ assertEquals ("nashorn" , annotation .engine ());
132+ assertEquals ("Script `{script}` evaluated to: {result}" , annotation .reason ());
125133 }
126134
127135 @ Test
128136 @ EnabledIf (value = "true" , reason = "{annotation}" )
129- void createReasonWithAnnotationPlaceholder () throws Exception {
130- EnabledIf annotation = getClass () //
131- .getDeclaredMethod ("createReasonWithAnnotationPlaceholder" ) //
132- .getDeclaredAnnotation (EnabledIf .class );
137+ void createReasonWithAnnotationPlaceholder () {
138+ EnabledIf annotation = reflectEnabledIfAnnotation ("createReasonWithAnnotationPlaceholder" );
133139 ConditionEvaluationResult result = evaluate (annotation );
134140 assertFalse (result .isDisabled ());
135141 String actual = result .getReason ().orElseThrow (() -> new AssertionError ("causeless" ));
@@ -139,21 +145,53 @@ void createReasonWithAnnotationPlaceholder() throws Exception {
139145 .endsWith (")" );
140146 }
141147
142- private ConditionEvaluationResult evaluate (EnabledIf annotation ) {
143- return condition .evaluate (annotation , this ::mockBinder );
148+ @ Test
149+ void getJUnitConfigurationParameterWithJavaScript () {
150+ EnabledIf annotation = mockEnabledIfAnnotation ("junitConfigurationParameter.get('XXX')" );
151+ ConditionEvaluationResult result = evaluate (annotation );
152+ assertTrue (result .isDisabled ());
153+ String actual = result .getReason ().orElseThrow (() -> new AssertionError ("causeless" ));
154+ assertEquals ("Script `junitConfigurationParameter.get('XXX')` evaluated to: null" , actual );
155+ }
156+
157+ @ Test
158+ void getJUnitConfigurationParameterWithJavaScriptAndCheckForNull () {
159+ EnabledIf annotation = mockEnabledIfAnnotation ("junitConfigurationParameter.get('XXX') != null" );
160+ ConditionEvaluationResult result = evaluate (annotation );
161+ assertTrue (result .isDisabled ());
162+ String actual = result .getReason ().orElseThrow (() -> new AssertionError ("causeless" ));
163+ assertEquals ("Script `junitConfigurationParameter.get('XXX') != null` evaluated to: false" , actual );
164+ }
165+
166+ @ Test
167+ void getJUnitConfigurationParameterWithGroovy () {
168+ EnabledIf annotation = mockEnabledIfAnnotation ("junitConfigurationParameter.get('XXX')" );
169+ when (annotation .engine ()).thenReturn ("Groovy" );
170+ ConditionEvaluationResult result = evaluate (annotation );
171+ assertTrue (result .isDisabled ());
172+ String actual = result .getReason ().orElseThrow (() -> new AssertionError ("causeless" ));
173+ assertEquals ("Script `junitConfigurationParameter.get('XXX')` evaluated to: null" , actual );
174+ }
175+
176+ @ Test
177+ void getJUnitConfigurationParameterWithGroovyAndCheckForNull () {
178+ EnabledIf annotation = mockEnabledIfAnnotation ("junitConfigurationParameter.get('XXX') != null" );
179+ when (annotation .engine ()).thenReturn ("Groovy" );
180+ ConditionEvaluationResult result = evaluate (annotation );
181+ assertTrue (result .isDisabled ());
182+ String actual = result .getReason ().orElseThrow (() -> new AssertionError ("causeless" ));
183+ assertEquals ("Script `junitConfigurationParameter.get('XXX') != null` evaluated to: false" , actual );
144184 }
145185
146- private void assertLinesMatchCreatedScript (List <String > expectedLines , EnabledIf annotation , String language ) {
147- EnabledIfCondition condition = new EnabledIfCondition ();
148- String actual = condition .createScript (annotation , language );
149- assertLinesMatch (expectedLines , Arrays .asList (actual .split ("\\ R" )));
186+ private ConditionEvaluationResult evaluate (EnabledIf annotation ) {
187+ return condition .evaluate (annotation , this ::mockBinder );
150188 }
151189
152190 private void mockBinder (Bindings bindings ) {
153- bindings .put ("jupiterTags" , Collections .emptySet ());
154- bindings .put ("jupiterUniqueId" , "Mock for UniqueId" );
155- bindings .put ("jupiterDisplayName" , "Mock for DisplayName" );
156- bindings .put ("jupiterConfigurationParameter" , Collections .emptyMap ());
191+ bindings .put (EnabledIf . Bind . JUNIT_TAGS , Collections .emptySet ());
192+ bindings .put (EnabledIf . Bind . JUNIT_UNIQUE_ID , "Mock for UniqueId" );
193+ bindings .put (EnabledIf . Bind . JUNIT_DISPLAY_NAME , "Mock for DisplayName" );
194+ bindings .put (EnabledIf . Bind . JUNIT_CONFIGURATION_PARAMETER , Collections .emptyMap ());
157195 }
158196
159197 private EnabledIf mockEnabledIfAnnotation (String ... value ) {
@@ -171,4 +209,13 @@ private EnabledIf mockEnabledIfAnnotation(String... value) {
171209 return annotation ;
172210 }
173211
212+ private EnabledIf reflectEnabledIfAnnotation (String methodName , Class <?>... parameterTypes ) {
213+ try {
214+ return getClass ().getDeclaredMethod (methodName , parameterTypes ).getDeclaredAnnotation (EnabledIf .class );
215+ }
216+ catch (NoSuchMethodException e ) {
217+ throw new AssertionError (e );
218+ }
219+ }
220+
174221}
0 commit comments