This is an automated email from the ASF dual-hosted git repository. klund pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git
commit ae25c7b8f8db29f498c9c064a340d39200639209 Author: Kirk Lund <[email protected]> AuthorDate: Wed Mar 14 14:48:01 2018 -0700 GEODE-4789: upgrade system-rules from 1.16.1 to 1.17.1 Make DistributedRestoreSystemProperties delegate to RestoreSystemProperties --- .../rules/DistributedRestoreSystemProperties.java | 68 +++++----- ...utedRestoreSystemPropertiesDistributedTest.java | 138 +++++++++++++++++++++ .../DistributedRestoreSystemPropertiesTest.java | 56 +++++++++ geode-junit/build.gradle | 3 + .../AccessibleRestoreSystemProperties.java | 33 +++++ .../geode/test/junit/runners/TestRunner.java | 20 +++ gradle/dependency-versions.properties | 2 +- 7 files changed, 287 insertions(+), 33 deletions(-) diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/DistributedRestoreSystemProperties.java b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/DistributedRestoreSystemProperties.java index 592fe05..2fcf128 100755 --- a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/DistributedRestoreSystemProperties.java +++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/DistributedRestoreSystemProperties.java @@ -14,26 +14,26 @@ */ package org.apache.geode.test.dunit.rules; -import static java.lang.System.*; +import static org.apache.geode.test.dunit.Host.getHost; +import static org.assertj.core.api.Assertions.assertThat; -import java.util.Properties; - -import org.junit.contrib.java.lang.system.RestoreSystemProperties; - -import org.apache.geode.test.dunit.SerializableRunnable; +import org.apache.geode.test.junit.rules.accessible.AccessibleRestoreSystemProperties; import org.apache.geode.test.junit.rules.serializable.SerializableTestRule; /** * Distributed version of RestoreSystemProperties which affects all DUnit JVMs including the Locator * JVM. */ -public class DistributedRestoreSystemProperties extends RestoreSystemProperties +public class DistributedRestoreSystemProperties extends AccessibleRestoreSystemProperties implements SerializableTestRule { - private static volatile Properties originalProperties; + private static final AccessibleRestoreSystemProperties restoreSystemProperties = + new AccessibleRestoreSystemProperties(); private final RemoteInvoker invoker; + private volatile int beforeVmCount; + public DistributedRestoreSystemProperties() { this(new RemoteInvoker()); } @@ -45,35 +45,39 @@ public class DistributedRestoreSystemProperties extends RestoreSystemProperties @Override public void before() throws Throwable { - super.before(); - this.invoker.invokeInEveryVMAndController(new SerializableRunnable() { - @Override - public void run() { - if (originalProperties == null) { - originalProperties = getProperties(); - setProperties(copyOf(originalProperties)); - } - } - }); - } + beforeVmCount = getVMCount(); - private Properties copyOf(Properties source) { - Properties copy = new Properties(); - copy.putAll(source); - return copy; + invoker.invokeInEveryVMAndController(() -> invokeBefore()); } @Override public void after() { - super.after(); - this.invoker.invokeInEveryVMAndController(new SerializableRunnable() { - @Override - public void run() { - if (originalProperties != null) { - setProperties(originalProperties); - originalProperties = null; - } + int afterVmCount = getVMCount(); + assertThat(afterVmCount).isEqualTo(beforeVmCount); + + invoker.invokeInEveryVMAndController(() -> invokeAfter()); + } + + private void invokeBefore() throws Exception { + try { + restoreSystemProperties.before(); + } catch (Throwable throwable) { + if (throwable instanceof Exception) { + throw (Exception) throwable; } - }); + throw new RuntimeException(throwable); + } + } + + private void invokeAfter() { + restoreSystemProperties.after(); + } + + private int getVMCount() { + try { + return getHost(0).getVMCount(); + } catch (IllegalArgumentException e) { + throw new IllegalStateException("DUnit VMs have not been launched"); + } } } diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesDistributedTest.java b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesDistributedTest.java new file mode 100644 index 0000000..5179644 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesDistributedTest.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.test.dunit.rules.tests; + +import static org.apache.geode.test.dunit.Host.getHost; +import static org.apache.geode.test.junit.runners.TestRunner.runTestWithValidation; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.Serializable; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties; +import org.apache.geode.test.dunit.rules.DistributedTestRule; +import org.apache.geode.test.junit.categories.DistributedTest; + +@Category(DistributedTest.class) +public class DistributedRestoreSystemPropertiesDistributedTest { + + private static final String NULL_PROPERTY = "NULL_PROPERTY"; + private static final String PREEXISTING_PROPERTY = "PREEXISTING_PROPERTY"; + private static final String PREEXISTING_VALUE = "PREEXISTING_VALUE"; + + @ClassRule + public static DistributedTestRule distributedTestRule = new DistributedTestRule(); + + @BeforeClass + public static void assertPreconditions() { + assertThat(System.getProperty(NULL_PROPERTY)).isNull(); + assertThat(System.getProperty(PREEXISTING_PROPERTY)).isNull(); + for (int i = 0; i < 4; i++) { + getHost(0).getVM(i).invoke(() -> { + assertThat(System.getProperty(NULL_PROPERTY)).isNull(); + assertThat(System.getProperty(PREEXISTING_PROPERTY)).isNull(); + }); + } + } + + @Before + public void setUp() { + System.setProperty(PREEXISTING_PROPERTY, PREEXISTING_VALUE); + for (int i = 0; i < 4; i++) { + getHost(0).getVM(i).invoke(() -> { + System.setProperty(PREEXISTING_PROPERTY, PREEXISTING_VALUE); + }); + } + } + + @After + public void tearDown() { + System.clearProperty(PREEXISTING_PROPERTY); + for (int i = 0; i < 4; i++) { + getHost(0).getVM(i).invoke(() -> { + System.clearProperty(PREEXISTING_PROPERTY); + }); + } + } + + @Test + public void nullPropertyWithDifferentValues() throws Exception { + runTestWithValidation(NullPropertyWithDifferentValues.class); + + assertThat(System.getProperty(NULL_PROPERTY)).isNull(); + for (int i = 0; i < 4; i++) { + getHost(0).getVM(i).invoke(() -> { + assertThat(System.getProperty(NULL_PROPERTY)).isNull(); + }); + } + } + + @Test + public void preexistingPropertyWithDifferentValues() throws Exception { + runTestWithValidation(NullPropertyWithDifferentValues.class); + + assertThat(System.getProperty(PREEXISTING_PROPERTY)).isEqualTo(PREEXISTING_VALUE); + for (int i = 0; i < 4; i++) { + getHost(0).getVM(i).invoke(() -> { + assertThat(System.getProperty(PREEXISTING_PROPERTY)).isEqualTo(PREEXISTING_VALUE); + }); + } + } + + /** + * Used by test {@link #nullPropertyWithDifferentValues()}. + */ + public static class NullPropertyWithDifferentValues implements Serializable { + + @Rule + public DistributedRestoreSystemProperties restoreSystemProperties = + new DistributedRestoreSystemProperties(); + + @Test + public void nullPropertyWithDifferentValues() throws Exception { + System.setProperty(NULL_PROPERTY, "controller"); + getHost(0).getVM(0).invoke(() -> System.setProperty(NULL_PROPERTY, "vm0")); + getHost(0).getVM(1).invoke(() -> System.setProperty(NULL_PROPERTY, "vm1")); + getHost(0).getVM(2).invoke(() -> System.setProperty(NULL_PROPERTY, "vm2")); + getHost(0).getVM(3).invoke(() -> System.setProperty(NULL_PROPERTY, "vm3")); + } + } + + /** + * Used by test {@link #preexistingPropertyWithDifferentValues()}. + */ + public static class PreexistingPropertyWithDifferentValues implements Serializable { + + @Rule + public DistributedRestoreSystemProperties restoreSystemProperties = + new DistributedRestoreSystemProperties(); + + @Test + public void preexistingPropertyWithDifferentValues() throws Exception { + System.setProperty(PREEXISTING_PROPERTY, "controller"); + getHost(0).getVM(0).invoke(() -> System.setProperty(PREEXISTING_PROPERTY, "vm0")); + getHost(0).getVM(1).invoke(() -> System.setProperty(PREEXISTING_PROPERTY, "vm1")); + getHost(0).getVM(2).invoke(() -> System.setProperty(PREEXISTING_PROPERTY, "vm2")); + getHost(0).getVM(3).invoke(() -> System.setProperty(PREEXISTING_PROPERTY, "vm3")); + } + } +} diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesTest.java b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesTest.java new file mode 100644 index 0000000..01793e9 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/tests/DistributedRestoreSystemPropertiesTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.test.dunit.rules.tests; + +import static org.apache.geode.test.junit.runners.TestRunner.runTestWithExpectedFailure; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; +import org.junit.experimental.categories.Category; +import org.junit.runner.notification.Failure; + +import org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties; +import org.apache.geode.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class DistributedRestoreSystemPropertiesTest { + + @Test + public void withoutDUnitThrowsIllegalStateException() { + Failure failure = runTestWithExpectedFailure(WithoutDUnit.class); + assertThat(failure.getException()).isInstanceOf(IllegalStateException.class); + assertThat(failure.getMessage()).isEqualTo("DUnit VMs have not been launched"); + } + + @Test + public void isaRestoreSystemProperties() { + assertThat(new DistributedRestoreSystemProperties()) + .isInstanceOf(RestoreSystemProperties.class); + } + + public static class WithoutDUnit { + + @Rule + public DistributedRestoreSystemProperties restoreSystemProperties = + new DistributedRestoreSystemProperties(); + + @Test + public void doTest() { + // nothing + } + } +} diff --git a/geode-junit/build.gradle b/geode-junit/build.gradle index 7a7cb5e..9b7ba68 100755 --- a/geode-junit/build.gradle +++ b/geode-junit/build.gradle @@ -16,6 +16,9 @@ */ dependencies { + compile ('com.github.stefanbirkner:system-rules:' + project.'system-rules.version') { + exclude module: 'junit-dep' + } compile 'com.jayway.jsonpath:json-path:' + project.'json-path.version' testCompile 'commons-lang:commons-lang:' + project.'commons-lang.version' testCompile 'com.google.guava:guava:' + project.'guava.version' diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleRestoreSystemProperties.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleRestoreSystemProperties.java new file mode 100644 index 0000000..50f91f0 --- /dev/null +++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleRestoreSystemProperties.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.test.junit.rules.accessible; + +import org.junit.contrib.java.lang.system.RestoreSystemProperties; + +/** + * Subclass RestoreSystemProperties in order to invoke protected methods from different package. + */ +public class AccessibleRestoreSystemProperties extends RestoreSystemProperties { + + @Override + public void before() throws Throwable { + super.before(); + } + + @Override + public void after() { + super.after(); + } +} diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/runners/TestRunner.java b/geode-junit/src/main/java/org/apache/geode/test/junit/runners/TestRunner.java index 6d31f78..ca8a23f 100755 --- a/geode-junit/src/main/java/org/apache/geode/test/junit/runners/TestRunner.java +++ b/geode-junit/src/main/java/org/apache/geode/test/junit/runners/TestRunner.java @@ -49,4 +49,24 @@ public class TestRunner { return result; } + + public static Failure runTestWithExpectedFailure(final Class<?> test) { + JUnitCore junitCore = new JUnitCore(); + Result result = junitCore.run(Request.aClass(test).getRunner()); + + List<Failure> failures = result.getFailures(); + assertThat(failures).hasSize(1); + + return failures.get(0); + } + + public static List<Failure> runTestWithExpectedFailures(final Class<?> test) { + JUnitCore junitCore = new JUnitCore(); + Result result = junitCore.run(Request.aClass(test).getRunner()); + + List<Failure> failures = result.getFailures(); + assertThat(failures).isNotEmpty(); + + return failures; + } } diff --git a/gradle/dependency-versions.properties b/gradle/dependency-versions.properties index e65d709..40cda59 100644 --- a/gradle/dependency-versions.properties +++ b/gradle/dependency-versions.properties @@ -94,7 +94,7 @@ springframework.version = 4.3.14.RELEASE spymemcached.version = 2.12.2 springfox.version=2.8.0 stephenc-findbugs.version = 1.3.9-1 -system-rules.version = 1.16.1 +system-rules.version = 1.17.1 tempus-fugit.version = 1.1 tomcat6.version = 6.0.37 tomcat7.version = 7.0.73 -- To stop receiving notification emails like this one, please contact [email protected].
