/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.extensions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.EnabledIf;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

/**
 * Script-based execution condition evaluation tests.
 *
 * @since 1.1
 */
@EnabledIf("true")
class EnabledIfTests {

	@Test
	@EnabledIf("true")
	void booleanTrue() {
	}

	@Test
	@EnabledIf("java.lang.Boolean.TRUE")
	void booleanWrapperTrue() {
	}

	@Test
	@EnabledIf("'TrUe'")
	void stringTrue() {
	}

	@Test
	@EnabledIf("false")
	void booleanFalse() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("java.lang.Boolean.FALSE")
	void booleanWrapperFalse() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("'FaLsE'")
	void stringFalse() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("1 == 2")
	void oneEqualsTwo() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled('Go!')")
	void customResultEnabled() {
	}

	@Test
	@EnabledIf("org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled('No go.')")
	void customResultDisabled() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("java.lang.Boolean.getBoolean('is-not-set')")
	void getBoolean() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf({ //
			"load('nashorn:mozilla_compat.js')", //
			"importPackage(java.nio.file)", //
			"", //
			"var path = Files.createTempFile('volatile-', '.temp')", //
			"java.lang.System.getProperties().put('volatile', path)", //
			"Files.exists(path)" //
	})
	void multiLineAndImportJavaPackage() {
		assertTrue(Files.exists((Path) System.getProperties().get("volatile")));
	}

	@Test
	@EnabledIf("jupiterConfigurationParameter.get('some.value.or.null')")
	void getJupiterConfigurationParameterWithJavaScript() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf(engine = "groovy", value = "jupiterConfigurationParameter.get('some.value.or.null')")
	void getJupiterConfigurationParameterWithGroovy() {
		fail("test must not be executed");
	}

	@Test
	@EnabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('win')")
	void onMicrosoftWindows() {
		assertTrue(System.getProperty("os.name").toLowerCase().contains("win"));
	}

	@Test
	@EnabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')")
	void onMacOs() {
		assertTrue(System.getProperty("os.name").toLowerCase().contains("mac"));
	}

	@Test
	@EnabledIf("/64/.test(systemProperty.get('os.arch'))")
	void osArch() {
		assertTrue(System.getProperty("os.arch").contains("64"));
	}

	@Test
	@EnabledIf(engine = "groovy", value = { "System.properties['jsr'] = '233'", "'233' == System.properties['jsr']" })
	void groovy() {
		assertEquals("233", System.getProperty("jsr"));
	}

	@Test
	@EnabledIf("true")
	@Disabled
	void enabledAndDisabled() {
		fail("test must not be executed");
	}

	@Test
	@Disabled
	@EnabledIf("true")
	void disabledAndEnabled() {
		fail("test must not be executed");
	}

	@RepeatedTest(2)
	@CoinToss
	void gamble() {
	}

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	// @CoinToss was originally annotated as follows:
	// @EnabledIf("Math.random() >= 0.5")
	// ... but we've replaced the randomness in order to ensure stability in the
	// number of tests executed within our test suite.
	@EnabledIf("Math.random() >= 0.0")
	@interface CoinToss {
	}

}

@@ -2471,34 +2471,19 @@
 er.get('
-some.value.or.null
+XXX
 ')%22)%0A%09vo
@@ -2600,91 +2600,452 @@
 dIf(
-engine = %22groovy%22, value = %22jupiterConfigurationParameter.get('some.value.or.
+%22jupiterConfigurationParameter.get('XXX') != null%22)%0A%09void getJupiterConfigurationParameterWithJavaScriptAndCheckForNull() %7B%0A%09%09fail(%22test must not be executed%22);%0A%09%7D%0A%0A%09@Test%0A%09@EnabledIf(engine = %22groovy%22, value = %22jupiterConfigurationParameter.get('XXX')%22)%0A%09void getJupiterConfigurationParameterWithGroovy() %7B%0A%09%09fail(%22test must not be executed%22);%0A%09%7D%0A%0A%09@Test%0A%09@EnabledIf(engine = %22groovy%22, value = %22jupiterConfigurationParameter.get('XXX') != 
 null
-')
 %22)%0A%09
@@ -3083,32 +3083,47 @@
 ameterWithGroovy
+AndCheckForNull
 () %7B%0A%09%09fail(%22tes

Introduce regression tests for bugfix in previous commit