package de.is24.playground;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

/* 
 * System properties can be set via different ways:
 *   - using -D on the command line (usually the Maven command line, since we don't run java directly any more)
 *   - in the "parameters" section of a TeamCity build config
 *   - in the <properties> section of pom.xml 
 *   - surefire/execution/configuration/systemPropertyVariables
 *  
 * Teamcity calls Maven somewhat like so: 
 * java -Dlots.of.properties maven.Launcher goal args
 *  
 * "args" are specified in field "Additional Maven command line parameters" of the Maven "Build Step".
 * "args" can include properties set via -Dprop=val
 * 
 * System properties set on the "Parameters" page in TeamCity will be passed on the "java" command line before the 
 * maven.Launcher and are treated somewhat differently than the ones passed after. Both will be interpreted by
 * Maven itself and its plugins (for instance, skipTests, tests, groups, ...), but only the later ones (additional cmd 
 * line params) will be propagated by surefire into the test runner!
 * 
 */
public class SystemPropertyTest {
	@Rule
	public ErrorCollector collector = new ErrorCollector();

	@Test
	public void testPropertiesAvailable() throws Exception {
		// set in TeamCity
		checkPropertyNotSet("proptest.tcy.param");  // set as "system.proptest.tcy.param" in Teamcity
		checkPropertyIsSet("proptest.tcy.param.passthru");  // special inclusion in surefire config
		checkPropertyIsSet("proptest.cmd.arg");

		// set in POM
		checkPropertyNotSet("proptest.maven.general");
		checkPropertyIsSet("proptest.maven.surefire.config");
	}

	void checkPropertyNotSet(String prop) {
		collector.checkThat(prop, System.getProperty(prop), nullValue());
	}

	void checkPropertyIsSet(String prop) {
		collector.checkThat(prop, System.getProperty(prop), is("t"));
	}

	@Test
	public void testEnv() throws Exception {
		// this environment variable should be set everywhere Java tests can run...
		assertThat(System.getenv("JAVA_HOME"), containsString("jdk"));
	}
}

@@ -177,1185 +177,513 @@
 ers.
-containsString;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.CoreMatchers.nullValue;%0Aimport static org.junit.Assert.assertThat;%0A%0A/* %0A * System properties can be set via different ways:%0A *   - using -D on the 
+*;%0Aimport static org.junit.Assert.assertThat;%0A%0A/* %0A * Background knowledge: %0A * https://github.
 com
+/
 ma
-nd line (usually the Maven command line, since we don't run java directly any more)%0A *   - in the %22parameters%22 section of a TeamCity build config%0A *   - in the %3Cproperties%3E section of pom.xml %0A *   - surefire/execution/configuration/systemPropertyVariable
+tey-jack/grouping-tests/wiki/Maven,-TeamCity,-and-passing-system-properties-to-test
 s%0A * 
- 
 %0A * 
-Teamcity calls Maven somewhat like so: %0A * java -Dlots.of.properties maven.Launcher goal args%0A *  %0A * %22args%22 are specified in field %22Additional Maven command line parameters%22 of the Maven %22Build Step%22.%0A * %22args%22 can include properties set via -Dprop=val%0A * %0A * System properties set on the %22Parameters%22 page in TeamCity will be passed on the %22java%22 command line before the %0A * maven.Launcher and are treated somewhat differently than the ones passed after. Both will be interpreted by%0A * Maven itself and its plugins (for instance, skipTests, tests, groups, ...), but only the later ones (additional cmd %0A * line params) will be propagated by surefire into the test runner!
+You can simulate the way, our TeamCity example config runs this test with the following command line:%0A * MAVEN_OPTS=%22-Dproptest.tcy.param=t -Dproptest.tcy.param.passthru=t%22 mvn test -Pproptest -Dproptest.cmd.arg=t%0A * %0A * See also the %22proptest%22 profile in pom.xml for a complete picture of what's happening here.
 %0A * 
@@ -1496,24 +1496,87 @@
 Exception %7B%0A
+%09%09// simple example of accessing Unix environment directly ...%0A
 %09%09// this en
@@ -1682,16 +1682,22 @@
 HOME%22), 
+anyOf(
 contains
@@ -1709,16 +1709,64 @@
 g(%22jdk%22)
+, containsString(%22jvm%22), containsString(%22java%22))
 );%0A%09%7D%0A%7D%0A

move comment to GitHub wiki and generalize ENV assertion