Showing posts with label system properties. Show all posts
Showing posts with label system properties. Show all posts

Wednesday, 24 February 2021

RestoreSystemProperties: Restore system properties

RestoreSystemProperties rule undoes changes of system properties when the test finishes.

 

Example

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

Find the below working application.

 

RestoreSystemPropertiesTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;

public class RestoreSystemPropertiesTest {

	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	@Rule
	public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

	@BeforeClass
	public static void beforeAllTests() {
		System.out.println("In beforeAllTests()");

		System.setProperty(COUNTRY_KEY, "no_country");
		System.setProperty(STATE_KEY, "no_state");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Before
	public void setup() {
		System.out.println("\nIn setup()");

		System.setProperty(COUNTRY_KEY, "India");
		System.setProperty(STATE_KEY, "Andhra Pradesh");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Test
	public void sysPropTest() {
		System.out.println("\nIn sysPropTest()");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));

		assertEquals(System.getProperty(COUNTRY_KEY), "India");
		assertEquals(System.getProperty(STATE_KEY), "Andhra Pradesh");
	}

	@After
	public void cleanUp() {
		System.out.println("\nIn cleanUp()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@AfterClass
	public static void afterAllTests() {
		System.out.println("\nIn afterAllTests()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}
}


Output

In beforeAllTests()
	country-> no_country
	state-> no_state

In setup()
	country-> India
	state-> Andhra Pradesh

In sysPropTest()
	country-> India
	state-> Andhra Pradesh

In cleanUp()
	country-> India
	state-> Andhra Pradesh

In afterAllTests()
	country-> no_country
	state-> no_state


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 



 

 

Previous                                                    Next                                                    Home

Friday, 19 February 2021

ClearSystemProperties: Clear System Properties

'ClearSystemProperties' rule is used to clear set of system properties when the test starts and restore the system properties when the test finishes.

 

Example

@Rule

public final ClearSystemProperties PROPS_TO_BE_CLEARED = new ClearSystemProperties("country", "state");

 

Find the below working application.

 

ClearSystemPropertiesTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertNull;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ClearSystemProperties;

public class ClearSystemPropertiesTest {

	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	@Rule
	public final ClearSystemProperties PROPS_TO_BE_CLEARED = new ClearSystemProperties(COUNTRY_KEY, STATE_KEY);

	@BeforeClass
	public static void beforeAllTests() {
		System.setProperty(COUNTRY_KEY, "India");
		System.setProperty(STATE_KEY, "Andhra Pradesh");

		System.out.println("In beforeAllTests()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Before
	public void setup() {
		System.out.println("\nIn setup()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Test
	public void clearSysPropTest() {
		System.out.println("\nIn clearSysPropTest()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));

		assertNull(System.getProperty(COUNTRY_KEY));
		assertNull(System.getProperty(STATE_KEY));
	}

	@After
	public void cleanUp() {
		System.out.println("\nIn cleanUp()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@AfterClass
	public static void afterAllTests() {
		System.out.println("\nIn afterAllTests()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}
}

 

Output


In beforeAllTests()
	country-> India
	state-> Andhra Pradesh

In setup()
	country-> null
	state-> null

In clearSysPropTest()
	country-> null
	state-> null

In cleanUp()
	country-> null
	state-> null

In afterAllTests()
	country-> India
	state-> Andhra Pradesh


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules


Previous                                                    Next                                                    Home

Tuesday, 9 July 2019

Maven: Access java system properties in pom.xml file


You can access all the properties that are exposed by 'System.getProperties()' API in pom.xml file.

Below program print all the system properties.
package com.sample.app;

import java.util.Properties;
import java.util.Set;

public class Test {

 public static void main(String args[]) {
  Properties properties = System.getProperties();

  Set<Object> keys = properties.keySet();

  for (Object key : keys) {
   System.out.println(key + " : " + properties.getProperty(key.toString()));
  }
 }
}

Below are some of the examples of the properties that are exposed by 'System.getProperties()' API.

os.name
sun.jnu.encoding
java.library.path
user.home
user.language

You can access above java system properties by using $ notation like below.

${os.name}
${sun.jnu.encoding}
${java.library.path}
${user.home}
${user.language}


Previous                                                    Next                                                    Home