You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/03/18 22:54:49 UTC
[03/11] incubator-geode git commit: GEODE-1050: add JUnit 4 versions
of DistributedTestCase and CacheTestCase
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/JUnit4DistributedTestCase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/JUnit4DistributedTestCase.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/JUnit4DistributedTestCase.java
new file mode 100755
index 0000000..809fbd3
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/JUnit4DistributedTestCase.java
@@ -0,0 +1,610 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal;
+
+import static org.junit.Assert.*;
+
+import java.io.Serializable;
+import java.text.DecimalFormat;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import com.gemstone.gemfire.admin.internal.AdminDistributedSystemImpl;
+import com.gemstone.gemfire.cache.Cache;
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.cache.hdfs.internal.hoplog.HoplogConfig;
+import com.gemstone.gemfire.cache.query.QueryTestUtils;
+import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
+import com.gemstone.gemfire.cache30.ClientServerTestCase;
+import com.gemstone.gemfire.cache30.GlobalLockingDUnitTest;
+import com.gemstone.gemfire.cache30.MultiVMRegionTestCase;
+import com.gemstone.gemfire.cache30.RegionTestCase;
+import com.gemstone.gemfire.distributed.DistributedSystem;
+import com.gemstone.gemfire.distributed.internal.DistributionConfig;
+import com.gemstone.gemfire.distributed.internal.DistributionMessageObserver;
+import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
+import com.gemstone.gemfire.internal.SocketCreator;
+import com.gemstone.gemfire.internal.admin.ClientStatsManager;
+import com.gemstone.gemfire.internal.cache.DiskStoreObserver;
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.internal.cache.HARegion;
+import com.gemstone.gemfire.internal.cache.InitialImageOperation;
+import com.gemstone.gemfire.internal.cache.PartitionedRegion;
+import com.gemstone.gemfire.internal.cache.tier.InternalClientMembership;
+import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerTestUtil;
+import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
+import com.gemstone.gemfire.internal.cache.xmlcache.CacheCreation;
+import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.management.internal.cli.LogWrapper;
+import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.IgnoredException;
+import com.gemstone.gemfire.test.dunit.Invoke;
+import com.gemstone.gemfire.test.dunit.LogWriterUtils;
+import com.gemstone.gemfire.test.dunit.standalone.DUnitLauncher;
+import com.gemstone.gemfire.test.junit.rules.serializable.SerializableTestName;
+import org.apache.logging.log4j.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+
+/**
+ * This class is the base class for all distributed tests using JUnit 4.
+ *
+ * TODO: make this class abstract when JUnit3DistributedTestCase is deleted
+ */
+public class JUnit4DistributedTestCase implements DistributedTestFixture, Serializable {
+
+ private static final Logger logger = LogService.getLogger();
+
+ private static final Set<String> testHistory = new LinkedHashSet<String>();
+
+ /** This VM's connection to the distributed system */
+ private static InternalDistributedSystem system;
+ private static Class lastSystemCreatedInTest;
+ private static Properties lastSystemProperties;
+ private static volatile String testMethodName;
+
+ /** For formatting timing info */
+ private static final DecimalFormat format = new DecimalFormat("###.###");
+
+ private static boolean reconnect = false;
+
+ private static final boolean logPerTest = Boolean.getBoolean("dunitLogPerTest");
+
+ private final DistributedTestFixture distributedTestFixture;
+
+ /**
+ * Constructs a new distributed test. All JUnit 4 test classes need to have a
+ * no-arg constructor.
+ */
+ public JUnit4DistributedTestCase() {
+ this((DistributedTestFixture)null);
+ }
+
+ /**
+ * This constructor should only be used by {@link JUnit3DistributedTestCase}.
+ */
+ protected JUnit4DistributedTestCase(final DistributedTestFixture distributedTestFixture) {
+ if (distributedTestFixture == null) {
+ this.distributedTestFixture = this;
+ } else {
+ this.distributedTestFixture = distributedTestFixture;
+ }
+ }
+
+ @Rule
+ public SerializableTestName testName = new SerializableTestName();
+
+ @BeforeClass
+ public static final void initializeDistributedTestCase() {
+ DUnitLauncher.launchIfNeeded();
+ }
+
+ public final String getName() {
+ if (this.distributedTestFixture != this) {
+ return this.distributedTestFixture.getName();
+ }
+ return this.testName.getMethodName();
+ }
+
+ public final Class<? extends DistributedTestFixture> getTestClass() {
+ return this.distributedTestFixture.getClass();
+ }
+
+ //---------------------------------------------------------------------------
+ // methods for tests
+ //---------------------------------------------------------------------------
+
+ /**
+ * @deprecated Please override {@link #getDistributedSystemProperties()} instead.
+ */
+ @Deprecated
+ public final void setSystem(final Properties props, final DistributedSystem ds) { // TODO: override getDistributedSystemProperties and then delete
+ system = (InternalDistributedSystem)ds;
+ lastSystemProperties = props;
+ lastSystemCreatedInTest = getTestClass(); // used to be getDeclaringClass()
+ }
+
+ /**
+ * Returns this VM's connection to the distributed system. If necessary, the
+ * connection will be lazily created using the given {@code Properties}.
+ *
+ * <p>Do not override this method. Override {@link #getDistributedSystemProperties()}
+ * instead.
+ *
+ * <p>Note: "final" was removed so that WANTestBase can override this method.
+ * This was part of the xd offheap merge.
+ *
+ * @since 3.0
+ */
+ public final InternalDistributedSystem getSystem(final Properties props) {
+ // Setting the default disk store name is now done in setUp
+ if (system == null) {
+ system = InternalDistributedSystem.getAnyInstance();
+ }
+ if (system == null || !system.isConnected()) {
+ // Figure out our distributed system properties
+ Properties p = DistributedTestUtils.getAllDistributedSystemProperties(props);
+ lastSystemCreatedInTest = getTestClass(); // used to be getDeclaringClass()
+ if (logPerTest) {
+ String testMethod = getTestMethodName();
+ String testName = lastSystemCreatedInTest.getName() + '-' + testMethod;
+ String oldLogFile = p.getProperty(DistributionConfig.LOG_FILE_NAME);
+ p.put(DistributionConfig.LOG_FILE_NAME,
+ oldLogFile.replace("system.log", testName+".log"));
+ String oldStatFile = p.getProperty(DistributionConfig.STATISTIC_ARCHIVE_FILE_NAME);
+ p.put(DistributionConfig.STATISTIC_ARCHIVE_FILE_NAME,
+ oldStatFile.replace("statArchive.gfs", testName+".gfs"));
+ }
+ system = (InternalDistributedSystem)DistributedSystem.connect(p);
+ lastSystemProperties = p;
+ } else {
+ boolean needNewSystem = false;
+ if(!getTestClass().equals(lastSystemCreatedInTest)) { // used to be getDeclaringClass()
+ Properties newProps = DistributedTestUtils.getAllDistributedSystemProperties(props);
+ needNewSystem = !newProps.equals(lastSystemProperties);
+ if(needNewSystem) {
+ LogWriterUtils.getLogWriter().info(
+ "Test class has changed and the new DS properties are not an exact match. "
+ + "Forcing DS disconnect. Old props = "
+ + lastSystemProperties + "new props=" + newProps);
+ }
+ } else {
+ Properties activeProps = system.getProperties();
+ for (Iterator iter = props.entrySet().iterator();
+ iter.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ String key = (String) entry.getKey();
+ String value = (String) entry.getValue();
+ if (!value.equals(activeProps.getProperty(key))) {
+ needNewSystem = true;
+ LogWriterUtils.getLogWriter().info("Forcing DS disconnect. For property " + key
+ + " old value = " + activeProps.getProperty(key)
+ + " new value = " + value);
+ break;
+ }
+ }
+ }
+ if(needNewSystem) {
+ // the current system does not meet our needs to disconnect and
+ // call recursively to get a new system.
+ LogWriterUtils.getLogWriter().info("Disconnecting from current DS in order to make a new one");
+ disconnectFromDS();
+ getSystem(props);
+ }
+ }
+ return system;
+ }
+
+ /**
+ * Returns this VM's connection to the distributed system. If necessary, the
+ * connection will be lazily created using the {@code Properties} returned by
+ * {@link #getDistributedSystemProperties()}.
+ *
+ * <p>Do not override this method. Override {@link #getDistributedSystemProperties()}
+ * instead.
+ *
+ * @see #getSystem(Properties)
+ *
+ * @since 3.0
+ */
+ public final InternalDistributedSystem getSystem() {
+ return getSystem(getDistributedSystemProperties());
+ }
+
+ public final InternalDistributedSystem basicGetSystem() {
+ return system;
+ }
+
+ public final void nullSystem() { // TODO: delete
+ system = null;
+ }
+
+ public static final InternalDistributedSystem getSystemStatic() {
+ return system;
+ }
+
+ /**
+ * Returns a loner distributed system that isn't connected to other vms.
+ *
+ * @since 6.5
+ */
+ public final InternalDistributedSystem getLonerSystem() {
+ Properties props = getDistributedSystemProperties();
+ props.put(DistributionConfig.MCAST_PORT_NAME, "0");
+ props.put(DistributionConfig.LOCATORS_NAME, "");
+ return getSystem(props);
+ }
+
+ /**
+ * Returns whether or this VM is connected to a {@link DistributedSystem}.
+ */
+ public final boolean isConnectedToDS() {
+ return system != null && system.isConnected();
+ }
+
+ /**
+ * Returns a {@code Properties} object used to configure a connection to a
+ * {@link DistributedSystem}. Unless overridden, this method will return an
+ * empty {@code Properties} object.
+ *
+ * <p>Override this as needed. Default implementation returns empty {@code Properties}.
+ *
+ * @since 3.0
+ */
+ @Override
+ public Properties getDistributedSystemProperties() {
+ if (this.distributedTestFixture != this) {
+ return this.distributedTestFixture.getDistributedSystemProperties();
+ }
+ return defaultGetDistributedSystemProperties();
+ }
+
+ final Properties defaultGetDistributedSystemProperties() {
+ return new Properties();
+ }
+
+ public static final void disconnectAllFromDS() {
+ disconnectFromDS();
+ Invoke.invokeInEveryVM(()->disconnectFromDS());
+ }
+
+ /**
+ * Disconnects this VM from the distributed system
+ */
+ public static final void disconnectFromDS() {
+ //setTestMethodName(null);
+ GemFireCacheImpl.testCacheXml = null;
+ if (system != null) {
+ system.disconnect();
+ system = null;
+ }
+
+ for (;;) {
+ DistributedSystem ds = InternalDistributedSystem.getConnectedInstance();
+ if (ds == null) {
+ break;
+ }
+ try {
+ ds.disconnect();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+
+ AdminDistributedSystemImpl ads = AdminDistributedSystemImpl.getConnectedInstance();
+ if (ads != null) {// && ads.isConnected()) {
+ ads.disconnect();
+ }
+ }
+
+ //---------------------------------------------------------------------------
+ // name methods
+ //---------------------------------------------------------------------------
+
+ public static final String getTestMethodName() {
+ return testMethodName;
+ }
+
+ private static final void setTestMethodName(final String testMethodName) {
+ JUnit4DistributedTestCase.testMethodName = testMethodName;
+ }
+
+ /**
+ * Returns a unique name for this test method. It is based on the
+ * name of the class as well as the name of the method.
+ */
+ public final String getUniqueName() {
+ return getTestClass().getSimpleName() + "_" + getName();
+ }
+
+ //---------------------------------------------------------------------------
+ // setup methods
+ //---------------------------------------------------------------------------
+
+ /**
+ * Sets up the DistributedTestCase.
+ *
+ * <p>Do not override this method. Override {@link #preSetUp()} with work that
+ * needs to occur before setUp() or override {@link #postSetUp()} with work
+ * that needs to occur after setUp().
+ */
+ @Before
+ public final void setUp() throws Exception {
+ preSetUp();
+ setUpDistributedTestCase();
+ postSetUp();
+ }
+
+ /**
+ * Sets up DistributedTest in controller and remote VMs. This includes the
+ * defining the test name, setting the default disk store name, logging the
+ * test history, and capturing a creation stack for detecting the source of
+ * incompatible DistributedSystem connections.
+ *
+ * <p>Do not override this method.
+ */
+ private final void setUpDistributedTestCase() {
+ final String className = getTestClass().getCanonicalName();
+ final String methodName = getName();
+
+ logTestHistory();
+
+ setUpVM(methodName, getDefaultDiskStoreName(0, -1, className, methodName));
+
+ for (int hostIndex = 0; hostIndex < Host.getHostCount(); hostIndex++) {
+ Host host = Host.getHost(hostIndex);
+ for (int vmIndex = 0; vmIndex < host.getVMCount(); vmIndex++) {
+ final String vmDefaultDiskStoreName = getDefaultDiskStoreName(hostIndex, vmIndex, className, methodName);
+ host.getVM(vmIndex).invoke(()->setUpVM(methodName, vmDefaultDiskStoreName));
+ }
+ }
+
+ logTestStart();
+ }
+
+ /**
+ * {@code preSetUp()} is invoked before {@link #setUpDistributedTestCase()}.
+ *
+ * <p>Override this as needed. Default implementation is empty.
+ */
+ @Override
+ public void preSetUp() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.preSetUp();
+ }
+ }
+
+ /**
+ * {@code postSetUp()} is invoked after {@link #setUpDistributedTestCase()}.
+ *
+ * <p>Override this as needed. Default implementation is empty.
+ */
+ @Override
+ public void postSetUp() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.postSetUp();
+ }
+ }
+
+ private static final String getDefaultDiskStoreName(final int hostIndex, final int vmIndex, final String className, final String methodName) {
+ return "DiskStore-" + String.valueOf(hostIndex) + "-" + String.valueOf(vmIndex) + "-" + className + "." + methodName; // used to be getDeclaringClass()
+ }
+
+ private static final void setUpVM(final String methodName, final String defaultDiskStoreName) {
+ assertNotNull("methodName must not be null", methodName);
+ assertNotNull("defaultDiskStoreName must not be null", defaultDiskStoreName);
+ setTestMethodName(methodName);
+ GemFireCacheImpl.setDefaultDiskStoreName(defaultDiskStoreName);
+ System.setProperty(HoplogConfig.ALLOW_LOCAL_HDFS_PROP, "true");
+ setUpCreationStackGenerator();
+ }
+
+ private final void logTestStart() {
+ System.out.println("\n\n[setup] START TEST " + getTestClass().getSimpleName()+"."+testMethodName+"\n\n");
+ }
+
+ private static final void setUpCreationStackGenerator() {
+ // the following is moved from InternalDistributedSystem to fix #51058
+ InternalDistributedSystem.TEST_CREATION_STACK_GENERATOR.set(
+ new InternalDistributedSystem.CreationStackGenerator() {
+ @Override
+ public Throwable generateCreationStack(final DistributionConfig config) {
+ final StringBuilder sb = new StringBuilder();
+ final String[] validAttributeNames = config.getAttributeNames();
+ for (int i = 0; i < validAttributeNames.length; i++) {
+ final String attName = validAttributeNames[i];
+ final Object actualAtt = config.getAttributeObject(attName);
+ String actualAttStr = actualAtt.toString();
+ sb.append(" ");
+ sb.append(attName);
+ sb.append("=\"");
+ if (actualAtt.getClass().isArray()) {
+ actualAttStr = InternalDistributedSystem.arrayToString(actualAtt);
+ }
+ sb.append(actualAttStr);
+ sb.append("\"");
+ sb.append("\n");
+ }
+ return new Throwable("Creating distributed system with the following configuration:\n" + sb.toString());
+ }
+ });
+ }
+
+ /**
+ * Write a message to the log about what tests have ran previously. This
+ * makes it easier to figure out if a previous test may have caused problems
+ */
+ private final void logTestHistory() {
+ String classname = getTestClass().getSimpleName();
+ testHistory.add(classname);
+ System.out.println("Previously run tests: " + testHistory);
+ }
+
+ //---------------------------------------------------------------------------
+ // teardown methods
+ //---------------------------------------------------------------------------
+
+ /**
+ * Tears down the DistributedTestCase.
+ *
+ * <p>Do not override this method. Override {@link #preTearDown()} with work that
+ * needs to occur before tearDown() or override {@link #postTearDown()} with work
+ * that needs to occur after tearDown().
+ */
+ @After
+ public final void tearDown() throws Exception {
+ try {
+ preTearDownAssertions();
+ } finally {
+ preTearDown();
+ tearDownDistributedTestCase();
+ postTearDown();
+ }
+ postTearDownAssertions();
+ }
+
+ private final void tearDownDistributedTestCase() throws Exception {
+ Invoke.invokeInEveryVM(()->tearDownCreationStackGenerator());
+ if (logPerTest) {
+ disconnectAllFromDS();
+ }
+ cleanupAllVms();
+ if (!getDistributedSystemProperties().isEmpty()) {
+ disconnectAllFromDS();
+ }
+ }
+
+ /**
+ * {@code preTearDown()} is invoked before {@link #tearDownDistributedTestCase()}.
+ *
+ * <p>Override this as needed. Default implementation is empty.
+ */
+ @Override
+ public void preTearDown() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.preTearDown();
+ }
+ }
+
+ /**
+ * {@code postTearDown()} is invoked after {@link #tearDownDistributedTestCase()}.
+ *
+ * <p>Override this as needed. Default implementation is empty.
+ */
+ @Override
+ public void postTearDown() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.postTearDown();
+ }
+ }
+
+ @Override
+ public void preTearDownAssertions() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.preTearDownAssertions();
+ }
+ }
+
+ @Override
+ public void postTearDownAssertions() throws Exception {
+ if (this.distributedTestFixture != this) {
+ this.distributedTestFixture.postTearDownAssertions();
+ }
+ }
+
+ private static final void cleanupAllVms() {
+ tearDownVM();
+ Invoke.invokeInEveryVM(()->tearDownVM());
+ Invoke.invokeInLocator(()->{
+ DistributionMessageObserver.setInstance(null);
+ DistributedTestUtils.unregisterInstantiatorsInThisVM();
+ });
+ DUnitLauncher.closeAndCheckForSuspects();
+ }
+
+ private static final void tearDownVM() {
+ closeCache();
+
+ // keep alphabetized to detect duplicate lines
+ CacheCreation.clearThreadLocals();
+ CacheServerTestUtil.clearCacheReference();
+ ClientProxyMembershipID.system = null;
+ ClientServerTestCase.AUTO_LOAD_BALANCE = false;
+ ClientStatsManager.cleanupForTests();
+ DiskStoreObserver.setInstance(null);
+ DistributedTestUtils.unregisterInstantiatorsInThisVM();
+ DistributionMessageObserver.setInstance(null);
+ GlobalLockingDUnitTest.region_testBug32356 = null;
+ InitialImageOperation.slowImageProcessing = 0;
+ InternalClientMembership.unregisterAllListeners();
+ LogWrapper.close();
+ MultiVMRegionTestCase.CCRegion = null;
+ QueryObserverHolder.reset();
+ QueryTestUtils.setCache(null);
+ RegionTestCase.preSnapshotRegion = null;
+ SocketCreator.resetHostNameCache();
+ SocketCreator.resolve_dns = true;
+
+ // clear system properties -- keep alphabetized
+ System.clearProperty("gemfire.log-level");
+ System.clearProperty(HoplogConfig.ALLOW_LOCAL_HDFS_PROP);
+ System.clearProperty("jgroups.resolve_dns");
+
+ if (InternalDistributedSystem.systemAttemptingReconnect != null) {
+ InternalDistributedSystem.systemAttemptingReconnect.stopReconnecting();
+ }
+
+ IgnoredException.removeAllExpectedExceptions();
+ }
+
+ private static final void closeCache() { // TODO: this should move to CacheTestCase
+ GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
+ if (cache != null && !cache.isClosed()) {
+ destroyRegions(cache);
+ cache.close();
+ }
+ }
+
+ protected static final void destroyRegions(final Cache cache) { // TODO: this should move to CacheTestCase
+ if (cache != null && !cache.isClosed()) {
+ // try to destroy the root regions first so that we clean up any persistent files.
+ for (Iterator itr = cache.rootRegions().iterator(); itr.hasNext();) {
+ Region root = (Region)itr.next();
+ String regionFullPath = root == null ? null : root.getFullPath();
+ // for colocated regions you can't locally destroy a partitioned region.
+ if(root.isDestroyed() || root instanceof HARegion || root instanceof PartitionedRegion) {
+ continue;
+ }
+ try {
+ root.localDestroyRegion("teardown");
+ } catch (Throwable t) {
+ logger.error("Failure during tearDown destroyRegions for " + regionFullPath, t);
+ }
+ }
+ }
+ }
+
+ private static final void tearDownCreationStackGenerator() {
+ InternalDistributedSystem.TEST_CREATION_STACK_GENERATOR.set(InternalDistributedSystem.DEFAULT_CREATION_STACK_GENERATOR);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3BasicDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3BasicDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3BasicDUnitTest.java
new file mode 100644
index 0000000..5130b5c
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3BasicDUnitTest.java
@@ -0,0 +1,189 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static com.gemstone.gemfire.test.dunit.Invoke.*;
+
+import java.util.Properties;
+
+import com.gemstone.gemfire.test.dunit.Assert;
+import com.gemstone.gemfire.test.dunit.AsyncInvocation;
+import com.gemstone.gemfire.test.dunit.DUnitEnv;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.RMIException;
+import com.gemstone.gemfire.test.dunit.VM;
+import com.gemstone.gemfire.test.dunit.internal.JUnit3DistributedTestCase;
+
+/**
+ * This class tests the basic functionality of the distributed unit
+ * test framework.
+ */
+public class JUnit3BasicDUnitTest extends JUnit3DistributedTestCase {
+
+ private static Properties bindings;
+
+ public JUnit3BasicDUnitTest(String name) {
+ super(name);
+ }
+
+ @Override
+ public void postSetUp() throws Exception {
+ bindings = new Properties();
+ invokeInEveryVM(() -> bindings = new Properties());
+ }
+
+ @Override
+ public void postTearDown() throws Exception {
+ bindings = null;
+ invokeInEveryVM(() -> bindings = null);
+ }
+
+ public void testPreconditions() {
+ invokeInEveryVM(() -> assertNotNull("getUniqueName() must not return null", getUniqueName()));
+ invokeInEveryVM(() -> assertNotNull("bindings must not be null", bindings));
+ }
+
+ /**
+ * Tests how the Hydra framework handles an error
+ */
+ public void ignore_testDontCatchRemoteException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ vm.invoke(() -> remoteThrowException());
+ }
+
+ public void testRemoteInvocationWithException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ try {
+ vm.invoke(() -> remoteThrowException());
+ fail("Should have thrown a BasicTestException");
+
+ } catch (RMIException ex) {
+ assertTrue(ex.getCause() instanceof BasicTestException);
+ }
+ }
+
+ public void testInvokeWithLambda() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+ VM vm1 = host.getVM(1);
+
+ int vm0Num = vm0.invoke(() -> DUnitEnv.get().getVMID());
+ int vm1Num = vm1.invoke(() -> DUnitEnv.get().getVMID());
+
+ assertEquals(0, vm0Num);
+ assertEquals(1, vm1Num);
+ }
+
+ public void testInvokeLambdaAsync() throws Throwable {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+
+ AsyncInvocation<Integer> async0 = vm0.invokeAsync(() -> DUnitEnv.get().getVMID());
+ int vm0num = async0.getResult();
+
+ assertEquals(0, vm0num);
+ }
+
+ public void testInvokeWithNamedLambda() {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+ VM vm1 = host.getVM(1);
+
+ int vm0Num = vm0.invoke("getVMID", () -> DUnitEnv.get().getVMID());
+ int vm1Num = vm1.invoke("getVMID", () -> DUnitEnv.get().getVMID());
+
+ assertEquals(0, vm0Num);
+ assertEquals(1, vm1Num);
+ }
+
+ public void testInvokeNamedLambdaAsync() throws Throwable {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+
+ AsyncInvocation<Integer> async0 = vm0.invokeAsync("getVMID", () -> DUnitEnv.get().getVMID());
+ int vm0num = async0.getResult();
+
+ assertEquals(0, vm0num);
+ }
+
+ // Test was never implemented
+ public void ignore_testRemoteInvocationBoolean() {
+ }
+
+ public void testRemoteInvokeAsync() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ String name = getUniqueName();
+ String value = "Hello";
+
+ AsyncInvocation ai = vm.invokeAsync(() -> remoteBind(name, value));
+ ai.join();
+ // TODO shouldn't we call fail() here?
+ if (ai.exceptionOccurred()) {
+ Assert.fail("remoteBind failed", ai.getException());
+ }
+
+ ai = vm.invokeAsync(() -> remoteValidateBind(name, value ));
+ ai.join();
+ if (ai.exceptionOccurred()) {
+ Assert.fail("remoteValidateBind failed", ai.getException());
+ }
+ }
+
+ public void testRemoteInvokeAsyncWithException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+
+ AsyncInvocation ai = vm.invokeAsync(() -> remoteThrowException());
+ ai.join();
+ assertTrue(ai.exceptionOccurred());
+ Throwable ex = ai.getException();
+ assertTrue(ex instanceof BasicTestException);
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static void remoteThrowException() {
+ String s = "Test exception. Please ignore.";
+ throw new BasicTestException(s);
+ }
+
+ private static void remoteBind(String name, String value) {
+ assertNotNull("name must not be null", name);
+ assertNotNull("value must not be null", value);
+ assertNotNull("bindings must not be null", bindings);
+
+ new JUnit3BasicDUnitTest("").getSystem(); // forces connection
+ bindings.setProperty(name, value);
+ }
+
+ private static void remoteValidateBind(String name, String expected) {
+ assertEquals(expected, bindings.getProperty(name));
+ }
+
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetDefaultDiskStoreNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetDefaultDiskStoreNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetDefaultDiskStoreNameDUnitTest.java
new file mode 100644
index 0000000..e89f66b
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetDefaultDiskStoreNameDUnitTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static org.assertj.core.api.Assertions.*;
+
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.internal.JUnit3DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.experimental.categories.Category;
+
+@SuppressWarnings("serial")
+@Category(DistributedTest.class)
+public class JUnit3GetDefaultDiskStoreNameDUnitTest extends JUnit3DistributedTestCase {
+
+ public JUnit3GetDefaultDiskStoreNameDUnitTest(final String name) {
+ super(name);
+ }
+
+ public void testGetTestMethodName() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodName");
+ assertGetDefaultDiskStoreName(expected);
+ }
+
+ public void testGetTestMethodNameChanges() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameChanges");
+ assertGetDefaultDiskStoreName(expected);
+ }
+
+ public void testGetTestMethodNameInAllVMs() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameInAllVMs");
+ assertGetDefaultDiskStoreName(expected);
+
+ for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
+ String expectedInVM = createDefaultDiskStoreName(0, vmIndex, "testGetTestMethodNameInAllVMs");
+ Host.getHost(0).getVM(vmIndex).invoke(()->assertGetDefaultDiskStoreName(expectedInVM));
+ }
+ }
+
+ private void assertGetDefaultDiskStoreName(final String expected) {
+ assertThat(getDefaultDiskStoreName()).isEqualTo(expected);
+ }
+
+ private String createDefaultDiskStoreName(final int hostIndex, final int vmIndex, final String methodName) {
+ return "DiskStore-" + hostIndex + "-" + vmIndex + "-" + getClass().getCanonicalName() + "." + methodName;
+ }
+
+ private String getDefaultDiskStoreName() {
+ return GemFireCacheImpl.DEFAULT_DS_NAME; // TODO: not thread safe
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetTestMethodNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetTestMethodNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetTestMethodNameDUnitTest.java
new file mode 100644
index 0000000..c77843d
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3GetTestMethodNameDUnitTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static org.assertj.core.api.Assertions.*;
+
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.internal.JUnit3DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.experimental.categories.Category;
+
+@SuppressWarnings("serial")
+@Category(DistributedTest.class)
+public class JUnit3GetTestMethodNameDUnitTest extends JUnit3DistributedTestCase {
+
+ public JUnit3GetTestMethodNameDUnitTest(final String name) {
+ super(name);
+ }
+
+ public void testGetTestMethodName() {
+ assertGetTestMethodName("testGetTestMethodName");
+ }
+
+ public void testGetTestMethodNameChanges() {
+ assertGetTestMethodName("testGetTestMethodNameChanges");
+ }
+
+ public void testGetTestMethodNameInAllVMs() {
+ assertGetTestMethodName("testGetTestMethodNameInAllVMs");
+
+ for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
+ Host.getHost(0).getVM(vmIndex).invoke(()->assertGetTestMethodName("testGetTestMethodNameInAllVMs"));
+ }
+ }
+
+ private void assertGetTestMethodName(final String expected) {
+ assertThat(getTestMethodName()).isEqualTo(expected);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3VMDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3VMDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3VMDUnitTest.java
new file mode 100644
index 0000000..ac2ed7e
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit3VMDUnitTest.java
@@ -0,0 +1,192 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.gemstone.gemfire.test.dunit.AsyncInvocation;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.RMIException;
+import com.gemstone.gemfire.test.dunit.SerializableRunnableIF;
+import com.gemstone.gemfire.test.dunit.VM;
+import com.gemstone.gemfire.test.dunit.internal.JUnit3DistributedTestCase;
+
+/**
+ * This class tests the functionality of the {@link VM} class.
+ */
+public class JUnit3VMDUnitTest extends JUnit3DistributedTestCase {
+
+ private static final AtomicInteger COUNTER = new AtomicInteger();
+ private static final boolean BOOLEAN_VALUE = true;
+ private static final byte BYTE_VALUE = (byte) 40;
+ private static final long LONG_VALUE = 42L;
+ private static final String STRING_VALUE = "BLAH BLAH BLAH";
+
+ public JUnit3VMDUnitTest(String name) {
+ super(name);
+ }
+
+ public void testInvokeStaticBoolean() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(BOOLEAN_VALUE, (boolean) vm.invoke(() -> remoteBooleanMethod()));
+ }
+
+ public void testInvokeStaticByte() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(BYTE_VALUE, (byte) vm.invoke(() -> remoteByteMethod()));
+ }
+
+ public void testInvokeStaticLong() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(LONG_VALUE, (long) vm.invoke(() -> remoteLongMethod()));
+ }
+
+ public void testInvokeInstance() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(STRING_VALUE, vm.invoke(new ClassWithString(), "getString"));
+ }
+
+ public void testInvokeRunnableWithException() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ try {
+ vm.invoke(new InvokeRunnable());
+ fail("Should have thrown a BasicTestException");
+ } catch (RMIException ex) {
+ assertTrue(ex.getCause() instanceof BasicTestException);
+ }
+ }
+
+ public void testReturnValue() throws Exception {
+ final Host host = Host.getHost(0);
+ final VM vm = host.getVM(0);
+ // Assert class static invocation works
+ AsyncInvocation a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(0), a1.getReturnValue());
+ // Assert class static invocation with args works
+ a1 = vm.invokeAsync(() -> incrementStaticCount(new Integer(2)));
+ a1.join();
+ assertEquals(new Integer(3), a1.getReturnValue());
+ // Assert that previous values are not returned when invoking method w/ no return val
+ a1 = vm.invokeAsync(() -> incStaticCount());
+ a1.join();
+ assertNull(a1.getReturnValue());
+ // Assert that previous null returns are over-written
+ a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(4), a1.getReturnValue());
+
+ // Assert object method invocation works with zero arg method
+ final VMTestObject o = new VMTestObject(0);
+ a1 = vm.invokeAsync(o, "incrementAndGet", new Object[] {});
+ a1.join();
+ assertEquals(new Integer(1), a1.getReturnValue());
+ // Assert object method invocation works with no return
+ a1 = vm.invokeAsync(o, "set", new Object[] {new Integer(3)});
+ a1.join();
+ assertNull(a1.getReturnValue());
+ }
+
+ private static Integer getAndIncStaticCount() {
+ return new Integer(COUNTER.getAndIncrement());
+ }
+
+ private static Integer incrementStaticCount(Integer inc) {
+ return new Integer(COUNTER.addAndGet(inc.intValue()));
+ }
+
+ private static void incStaticCount() {
+ COUNTER.incrementAndGet();
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static byte remoteByteMethod() {
+ return BYTE_VALUE;
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static boolean remoteBooleanMethod() {
+ return BOOLEAN_VALUE;
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static long remoteLongMethod() {
+ return LONG_VALUE;
+ }
+
+ private static class ClassWithLong implements Serializable {
+ public long getLong() {
+ return LONG_VALUE;
+ }
+ }
+
+ private static class ClassWithByte implements Serializable {
+ public byte getByte() {
+ return BYTE_VALUE;
+ }
+ }
+
+ private static class InvokeRunnable implements SerializableRunnableIF {
+ public void run() {
+ throw new BasicTestException();
+ }
+ }
+
+ private static class ClassWithString implements Serializable {
+ public String getString() {
+ return STRING_VALUE;
+ }
+ }
+
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
+ }
+ }
+
+ private static class VMTestObject implements Serializable {
+ private static final long serialVersionUID = 1L;
+ private final AtomicInteger val;
+ public VMTestObject(int init) {
+ this.val = new AtomicInteger(init);
+ }
+ public Integer get() {
+ return new Integer(this.val.get());
+ }
+ public Integer incrementAndGet() {
+ return new Integer(this.val.incrementAndGet());
+ }
+ public void set(Integer newVal) {
+ this.val.set(newVal.intValue());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4BasicDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4BasicDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4BasicDUnitTest.java
new file mode 100644
index 0000000..5cac3ba
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4BasicDUnitTest.java
@@ -0,0 +1,202 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static com.gemstone.gemfire.test.dunit.Assert.*;
+import static com.gemstone.gemfire.test.dunit.Invoke.*;
+
+import java.util.Properties;
+
+import com.gemstone.gemfire.test.dunit.AsyncInvocation;
+import com.gemstone.gemfire.test.dunit.DUnitEnv;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.RMIException;
+import com.gemstone.gemfire.test.dunit.VM;
+import com.gemstone.gemfire.test.dunit.internal.JUnit4DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * This class tests the basic functionality of the distributed unit
+ * test framework.
+ */
+@Category(DistributedTest.class)
+public class JUnit4BasicDUnitTest extends JUnit4DistributedTestCase {
+
+ private static Properties bindings;
+
+ @BeforeClass
+ public static void setUpJUnit4BasicDUnitTest() throws Exception {
+ invokeInEveryVM(() -> bindings = new Properties());
+ }
+
+ @AfterClass
+ public static void tearDownJUnit4BasicDUnitTest() {
+ invokeInEveryVM(() -> bindings = null);
+ }
+
+ @Test
+ public void testPreconditions() {
+ invokeInEveryVM(() -> assertNotNull("getUniqueName() must not return null", getUniqueName()));
+ invokeInEveryVM(() -> assertNotNull("bindings must not be null", bindings));
+ }
+
+ /**
+ * Tests how the Hydra framework handles an error
+ */
+ @Ignore
+ @Test
+ public void testDontCatchRemoteException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ vm.invoke(() -> remoteThrowException());
+ }
+
+ @Test
+ public void testRemoteInvocationWithException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ try {
+ vm.invoke(() -> remoteThrowException());
+ fail("Should have thrown a BasicTestException");
+
+ } catch (RMIException ex) {
+ assertTrue(ex.getCause() instanceof BasicTestException);
+ }
+ }
+
+ @Test
+ public void testInvokeWithLambda() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+ VM vm1 = host.getVM(1);
+
+ int vm0Num = vm0.invoke(() -> DUnitEnv.get().getVMID());
+ int vm1Num = vm1.invoke(() -> DUnitEnv.get().getVMID());
+
+ assertEquals(0, vm0Num);
+ assertEquals(1, vm1Num);
+ }
+
+ @Test
+ public void testInvokeLambdaAsync() throws Throwable {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+
+ AsyncInvocation<Integer> async0 = vm0.invokeAsync(() -> DUnitEnv.get().getVMID());
+ int vm0num = async0.getResult();
+
+ assertEquals(0, vm0num);
+ }
+
+ @Test
+ public void testInvokeWithNamedLambda() {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+ VM vm1 = host.getVM(1);
+
+ int vm0Num = vm0.invoke("getVMID", () -> DUnitEnv.get().getVMID());
+ int vm1Num = vm1.invoke("getVMID", () -> DUnitEnv.get().getVMID());
+
+ assertEquals(0, vm0Num);
+ assertEquals(1, vm1Num);
+ }
+
+ @Test
+ public void testInvokeNamedLambdaAsync() throws Throwable {
+ Host host = Host.getHost(0);
+ VM vm0 = host.getVM(0);
+
+ AsyncInvocation<Integer> async0 = vm0.invokeAsync("getVMID", () -> DUnitEnv.get().getVMID());
+ int vm0num = async0.getResult();
+
+ assertEquals(0, vm0num);
+ }
+
+ @Ignore("Test was never implemented")
+ @Test
+ public void testRemoteInvocationBoolean() {
+ }
+
+ @Test
+ public void testRemoteInvokeAsync() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ String name = this.getUniqueName();
+ String value = "Hello";
+
+ AsyncInvocation ai =
+ vm.invokeAsync(() -> this.remoteBind( name, value ));
+ ai.join();
+ // TODO shouldn't we call fail() here?
+ if (ai.exceptionOccurred()) {
+ fail("remoteBind failed", ai.getException());
+ }
+
+ ai = vm.invokeAsync(() -> this.remoteValidateBind(name, value ));
+ ai.join();
+ if (ai.exceptionOccurred()) {
+ fail("remoteValidateBind failed", ai.getException());
+ }
+ }
+
+ @Test
+ public void testRemoteInvokeAsyncWithException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+
+ AsyncInvocation ai = vm.invokeAsync(() -> this.remoteThrowException());
+ ai.join();
+ assertTrue(ai.exceptionOccurred());
+ Throwable ex = ai.getException();
+ assertTrue(ex instanceof BasicTestException);
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static void remoteThrowException() {
+ String s = "Test exception. Please ignore.";
+ throw new BasicTestException(s);
+ }
+
+ private static void remoteBind(String name, String value) {
+ assertNotNull("name must not be null", name);
+ assertNotNull("value must not be null", value);
+ assertNotNull("bindings must not be null", bindings);
+
+ new JUnit4BasicDUnitTest().getSystem(); // forces connection
+ bindings.setProperty(name, value);
+ }
+
+ private static void remoteValidateBind(String name, String expected) {
+ assertEquals(expected, bindings.getProperty(name));
+ }
+
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetDefaultDiskStoreNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetDefaultDiskStoreNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetDefaultDiskStoreNameDUnitTest.java
new file mode 100644
index 0000000..7f99bc1
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetDefaultDiskStoreNameDUnitTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static org.assertj.core.api.Assertions.*;
+
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.internal.JUnit4DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(DistributedTest.class)
+public class JUnit4GetDefaultDiskStoreNameDUnitTest extends JUnit4DistributedTestCase {
+
+ @Test
+ public void testGetTestMethodName() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodName");
+ assertGetDefaultDiskStoreName(expected);
+ }
+
+ @Test
+ public void testGetTestMethodNameChanges() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameChanges");
+ assertGetDefaultDiskStoreName(expected);
+ }
+
+ @Test
+ public void testGetTestMethodNameInAllVMs() {
+ String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameInAllVMs");
+ assertGetDefaultDiskStoreName(expected);
+
+ for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
+ String expectedInVM = createDefaultDiskStoreName(0, vmIndex, "testGetTestMethodNameInAllVMs");
+ Host.getHost(0).getVM(vmIndex).invoke(()->assertGetDefaultDiskStoreName(expectedInVM));
+ }
+ }
+
+ private void assertGetDefaultDiskStoreName(final String expected) {
+ assertThat(getDefaultDiskStoreName()).isEqualTo(expected);
+ }
+
+ private String createDefaultDiskStoreName(final int hostIndex, final int vmIndex, final String methodName) {
+ return "DiskStore-" + hostIndex + "-" + vmIndex + "-" + getClass().getCanonicalName() + "." + methodName;
+ }
+
+ private String getDefaultDiskStoreName() {
+ return GemFireCacheImpl.DEFAULT_DS_NAME; // TODO: not thread safe
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetTestMethodNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetTestMethodNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetTestMethodNameDUnitTest.java
new file mode 100644
index 0000000..93b2e86
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4GetTestMethodNameDUnitTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static org.assertj.core.api.Assertions.*;
+
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.internal.JUnit4DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(DistributedTest.class)
+public class JUnit4GetTestMethodNameDUnitTest extends JUnit4DistributedTestCase {
+
+ @Test
+ public void testGetTestMethodName() {
+ assertGetTestMethodName("testGetTestMethodName");
+ }
+
+ @Test
+ public void testGetTestMethodNameChanges() {
+ assertGetTestMethodName("testGetTestMethodNameChanges");
+ }
+
+ @Test
+ public void testGetTestMethodNameInAllVMs() {
+ assertGetTestMethodName("testGetTestMethodNameInAllVMs");
+
+ for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
+ Host.getHost(0).getVM(vmIndex).invoke(()->assertGetTestMethodName("testGetTestMethodNameInAllVMs"));
+ }
+ }
+
+ private void assertGetTestMethodName(final String expected) {
+ assertThat(getTestMethodName()).isEqualTo(expected);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4VMDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4VMDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4VMDUnitTest.java
new file mode 100644
index 0000000..c9d9d3c
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/internal/tests/JUnit4VMDUnitTest.java
@@ -0,0 +1,200 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.internal.tests;
+
+import static com.gemstone.gemfire.test.dunit.Assert.*;
+
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.gemstone.gemfire.test.dunit.AsyncInvocation;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.RMIException;
+import com.gemstone.gemfire.test.dunit.SerializableRunnableIF;
+import com.gemstone.gemfire.test.dunit.VM;
+import com.gemstone.gemfire.test.dunit.internal.JUnit4DistributedTestCase;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * This class tests the functionality of the {@link VM} class.
+ */
+@Category(DistributedTest.class)
+public class JUnit4VMDUnitTest extends JUnit4DistributedTestCase {
+
+ private static final AtomicInteger COUNTER = new AtomicInteger();
+ private static final boolean BOOLEAN_VALUE = true;
+ private static final byte BYTE_VALUE = (byte) 40;
+ private static final long LONG_VALUE = 42L;
+ private static final String STRING_VALUE = "BLAH BLAH BLAH";
+
+ @Test
+ public void testInvokeStaticBoolean() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(BOOLEAN_VALUE, (boolean) vm.invoke(() -> remoteBooleanMethod()));
+ }
+
+ @Test
+ public void testInvokeStaticByte() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(BYTE_VALUE, (byte) vm.invoke(() -> remoteByteMethod()));
+ }
+
+ @Test
+ public void testInvokeStaticLong() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(LONG_VALUE, (long) vm.invoke(() -> remoteLongMethod()));
+ }
+
+ @Test
+ public void testInvokeInstance() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(STRING_VALUE, vm.invoke(new ClassWithString(), "getString"));
+ }
+
+ @Test
+ public void testInvokeRunnableWithException() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ try {
+ vm.invoke(new InvokeRunnable());
+ fail("Should have thrown a BasicTestException");
+ } catch (RMIException ex) {
+ assertTrue(ex.getCause() instanceof BasicTestException);
+ }
+ }
+
+ @Test
+ public void testReturnValue() throws Exception {
+ final Host host = Host.getHost(0);
+ final VM vm = host.getVM(0);
+ // Assert class static invocation works
+ AsyncInvocation a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(0), a1.getReturnValue());
+ // Assert class static invocation with args works
+ a1 = vm.invokeAsync(() -> incrementStaticCount(new Integer(2)));
+ a1.join();
+ assertEquals(new Integer(3), a1.getReturnValue());
+ // Assert that previous values are not returned when invoking method w/ no return val
+ a1 = vm.invokeAsync(() -> incStaticCount());
+ a1.join();
+ assertNull(a1.getReturnValue());
+ // Assert that previous null returns are over-written
+ a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(4), a1.getReturnValue());
+
+ // Assert object method invocation works with zero arg method
+ final VMTestObject o = new VMTestObject(0);
+ a1 = vm.invokeAsync(o, "incrementAndGet", new Object[] {});
+ a1.join();
+ assertEquals(new Integer(1), a1.getReturnValue());
+ // Assert object method invocation works with no return
+ a1 = vm.invokeAsync(o, "set", new Object[] {new Integer(3)});
+ a1.join();
+ assertNull(a1.getReturnValue());
+ }
+
+ private static Integer getAndIncStaticCount() {
+ return new Integer(COUNTER.getAndIncrement());
+ }
+
+ private static Integer incrementStaticCount(Integer inc) {
+ return new Integer(COUNTER.addAndGet(inc.intValue()));
+ }
+
+ private static void incStaticCount() {
+ COUNTER.incrementAndGet();
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static byte remoteByteMethod() {
+ return BYTE_VALUE;
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static boolean remoteBooleanMethod() {
+ return BOOLEAN_VALUE;
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static long remoteLongMethod() {
+ return LONG_VALUE;
+ }
+
+ private static class ClassWithLong implements Serializable {
+ public long getLong() {
+ return LONG_VALUE;
+ }
+ }
+
+ private static class ClassWithByte implements Serializable {
+ public byte getByte() {
+ return BYTE_VALUE;
+ }
+ }
+
+ private static class InvokeRunnable implements SerializableRunnableIF {
+ public void run() {
+ throw new BasicTestException();
+ }
+ }
+
+ private static class ClassWithString implements Serializable {
+ public String getString() {
+ return STRING_VALUE;
+ }
+ }
+
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
+ }
+ }
+
+ private static class VMTestObject implements Serializable {
+ private static final long serialVersionUID = 1L;
+ private final AtomicInteger val;
+ public VMTestObject(int init) {
+ this.val = new AtomicInteger(init);
+ }
+ public Integer get() {
+ return new Integer(this.val.get());
+ }
+ public Integer incrementAndGet() {
+ return new Integer(this.val.incrementAndGet());
+ }
+ public void set(Integer newVal) {
+ this.val.set(newVal.intValue());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/BasicDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/BasicDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/BasicDUnitTest.java
index 3a98188..b10f610 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/BasicDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/BasicDUnitTest.java
@@ -16,6 +16,8 @@
*/
package com.gemstone.gemfire.test.dunit.tests;
+import static com.gemstone.gemfire.test.dunit.Invoke.invokeInEveryVM;
+
import java.util.Properties;
import com.gemstone.gemfire.test.dunit.Assert;
@@ -32,86 +34,164 @@ import com.gemstone.gemfire.test.dunit.VM;
*/
public class BasicDUnitTest extends DistributedTestCase {
+ private static Properties bindings;
+
public BasicDUnitTest(String name) {
super(name);
}
- //////// Test Methods
+ @Override
+ public final void postSetUp() throws Exception {
+ bindings = new Properties();
+ invokeInEveryVM(() -> bindings = new Properties());
+ }
+
+ @Override
+ public final void postTearDown() throws Exception {
+ bindings = null;
+ invokeInEveryVM(() -> bindings = null);
+ }
+
+ public void testPreconditions() {
+ invokeInEveryVM(() -> assertNotNull("getUniqueName() must not return null", getUniqueName()));
+ invokeInEveryVM(() -> assertNotNull("bindings must not be null", bindings));
+ }
/**
* Tests how the Hydra framework handles an error
*/
- public void _testDontCatchRemoteException() {
+ public void ignore_testDontCatchRemoteException() throws Exception {
Host host = Host.getHost(0);
VM vm = host.getVM(0);
- vm.invoke(() -> this.remoteThrowException());
+ vm.invoke(() -> remoteThrowException());
}
- public void testRemoteInvocationWithException() {
+ public void testRemoteInvocationWithException() throws Exception {
Host host = Host.getHost(0);
VM vm = host.getVM(0);
try {
- vm.invoke(() -> this.remoteThrowException());
+ vm.invoke(() -> remoteThrowException());
fail("Should have thrown a BasicTestException");
} catch (RMIException ex) {
assertTrue(ex.getCause() instanceof BasicTestException);
}
}
-
- public void testInvokeWithLambda() {
+
+ public void testInvokeWithLambda() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
-
+
int vm0Num = vm0.invoke(() -> DUnitEnv.get().getVMID());
int vm1Num = vm1.invoke(() -> DUnitEnv.get().getVMID());
-
+
assertEquals(0, vm0Num);
assertEquals(1, vm1Num);
-
}
-
+
public void testInvokeLambdaAsync() throws Throwable {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
-
+
AsyncInvocation<Integer> async0 = vm0.invokeAsync(() -> DUnitEnv.get().getVMID());
int vm0num = async0.getResult();
-
+
assertEquals(0, vm0num);
-
}
public void testInvokeWithNamedLambda() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
-
+
int vm0Num = vm0.invoke("getVMID", () -> DUnitEnv.get().getVMID());
int vm1Num = vm1.invoke("getVMID", () -> DUnitEnv.get().getVMID());
-
+
assertEquals(0, vm0Num);
assertEquals(1, vm1Num);
-
}
-
+
public void testInvokeNamedLambdaAsync() throws Throwable {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
-
+
AsyncInvocation<Integer> async0 = vm0.invokeAsync("getVMID", () -> DUnitEnv.get().getVMID());
int vm0num = async0.getResult();
-
+
assertEquals(0, vm0num);
-
}
-
+
+ // Test was never implemented
+ public void ignore_testRemoteInvocationBoolean() {
+ }
+
+ public void testRemoteInvokeAsync() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ String name = getUniqueName();
+ String value = "Hello";
+
+ AsyncInvocation ai = vm.invokeAsync(() -> remoteBind(name, value));
+ ai.join();
+ // TODO shouldn't we call fail() here?
+ if (ai.exceptionOccurred()) {
+ Assert.fail("remoteBind failed", ai.getException());
+ }
+
+ ai = vm.invokeAsync(() -> remoteValidateBind(name, value ));
+ ai.join();
+ if (ai.exceptionOccurred()) {
+ Assert.fail("remoteValidateBind failed", ai.getException());
+ }
+ }
+
+ public void testRemoteInvokeAsyncWithException() throws Exception {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+
+ AsyncInvocation ai = vm.invokeAsync(() -> remoteThrowException());
+ ai.join();
+ assertTrue(ai.exceptionOccurred());
+ Throwable ex = ai.getException();
+ assertTrue(ex instanceof BasicTestException);
+ }
+
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static void remoteThrowException() {
+ String s = "Test exception. Please ignore.";
+ throw new BasicTestException(s);
+ }
+
+ private static void remoteBind(String name, String value) {
+ assertNotNull("name must not be null", name);
+ assertNotNull("value must not be null", value);
+ assertNotNull("bindings must not be null", bindings);
+
+ new BasicDUnitTest("").getSystem(); // forces connection
+ bindings.setProperty(name, value);
+ }
+
+ private static void remoteValidateBind(String name, String expected) {
+ assertEquals(expected, bindings.getProperty(name));
+ }
+
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
+ }
+ }
+
static class BasicDUnitException extends RuntimeException {
public BasicDUnitException() {
}
}
-
+
public static void throwException() throws BasicDUnitException {
throw new BasicDUnitException();
}
@@ -119,7 +199,7 @@ public class BasicDUnitTest extends DistributedTestCase {
public void testInvokeNamedRunnableLambdaAsync() throws Throwable {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
-
+
AsyncInvocation<Integer> async0 = vm0.invokeAsync("throwSomething", () -> BasicDUnitTest.throwException());
try {
async0.getResult();
@@ -138,7 +218,7 @@ public class BasicDUnitTest extends DistributedTestCase {
public void testInvokeNamedRunnableLambda() throws Throwable {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
-
+
try {
vm0.invoke("throwSomething", () -> BasicDUnitTest.throwException());
throw new Error("expected an exception to be thrown");
@@ -148,75 +228,4 @@ public class BasicDUnitTest extends DistributedTestCase {
}
}
}
-
- static class BasicTestException extends RuntimeException {
- BasicTestException() {
- this("Test exception. Please ignore.");
- }
-
- BasicTestException(String s) {
- super(s);
- }
- }
-
- /**
- * Accessed via reflection. DO NOT REMOVE
- *
- */
- protected static void remoteThrowException() {
- String s = "Test exception. Please ignore.";
- throw new BasicTestException(s);
- }
-
- public void _testRemoteInvocationBoolean() {
-
- }
-
- public void testRemoteInvokeAsync() throws InterruptedException {
- Host host = Host.getHost(0);
- VM vm = host.getVM(0);
- String name = this.getUniqueName();
- String value = "Hello";
-
- AsyncInvocation ai =
- vm.invokeAsync(() -> this.remoteBind( name, value ));
- ai.join();
- // TODO shouldn't we call fail() here?
- if (ai.exceptionOccurred()) {
- Assert.fail("remoteBind failed", ai.getException());
- }
-
- ai = vm.invokeAsync(() -> this.remoteValidateBind(name, value ));
- ai.join();
- if (ai.exceptionOccurred()) {
- Assert.fail("remoteValidateBind failed", ai.getException());
- }
- }
-
- private static Properties bindings = new Properties();
- private static void remoteBind(String name, String s) {
- new BasicDUnitTest("bogus").getSystem(); // forces connection
- bindings.setProperty(name, s);
- }
-
- private static void remoteValidateBind(String name, String expected)
- {
- assertEquals(expected, bindings.getProperty(name));
- }
-
- public void testRemoteInvokeAsyncWithException()
- throws InterruptedException {
-
- Host host = Host.getHost(0);
- VM vm = host.getVM(0);
-// String name = this.getUniqueName();
-// String value = "Hello";
-
- AsyncInvocation ai =
- vm.invokeAsync(() -> this.remoteThrowException());
- ai.join();
- assertTrue(ai.exceptionOccurred());
- Throwable ex = ai.getException();
- assertTrue(ex instanceof BasicTestException);
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetDefaultDiskStoreNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetDefaultDiskStoreNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetDefaultDiskStoreNameDUnitTest.java
index 99dcc29..c92ab3d 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetDefaultDiskStoreNameDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetDefaultDiskStoreNameDUnitTest.java
@@ -37,30 +37,30 @@ public class GetDefaultDiskStoreNameDUnitTest extends DistributedTestCase {
String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodName");
assertGetDefaultDiskStoreName(expected);
}
-
+
public void testGetTestMethodNameChanges() {
String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameChanges");
assertGetDefaultDiskStoreName(expected);
}
-
+
public void testGetTestMethodNameInAllVMs() {
String expected = createDefaultDiskStoreName(0, -1, "testGetTestMethodNameInAllVMs");
assertGetDefaultDiskStoreName(expected);
-
+
for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
String expectedInVM = createDefaultDiskStoreName(0, vmIndex, "testGetTestMethodNameInAllVMs");
Host.getHost(0).getVM(vmIndex).invoke(()->assertGetDefaultDiskStoreName(expectedInVM));
}
}
-
+
private void assertGetDefaultDiskStoreName(final String expected) {
assertThat(getDefaultDiskStoreName()).isEqualTo(expected);
}
-
+
private String createDefaultDiskStoreName(final int hostIndex, final int vmIndex, final String methodName) {
return "DiskStore-" + hostIndex + "-" + vmIndex + "-" + getClass().getCanonicalName() + "." + methodName;
}
-
+
private String getDefaultDiskStoreName() {
return GemFireCacheImpl.DEFAULT_DS_NAME; // TODO: not thread safe
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetTestMethodNameDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetTestMethodNameDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetTestMethodNameDUnitTest.java
index 9bad472..13550f6 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetTestMethodNameDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/GetTestMethodNameDUnitTest.java
@@ -35,19 +35,19 @@ public class GetTestMethodNameDUnitTest extends DistributedTestCase {
public void testGetTestMethodName() {
assertGetTestMethodName("testGetTestMethodName");
}
-
+
public void testGetTestMethodNameChanges() {
assertGetTestMethodName("testGetTestMethodNameChanges");
}
-
+
public void testGetTestMethodNameInAllVMs() {
assertGetTestMethodName("testGetTestMethodNameInAllVMs");
-
+
for (int vmIndex = 0; vmIndex < Host.getHost(0).getVMCount(); vmIndex++) {
Host.getHost(0).getVM(vmIndex).invoke(()->assertGetTestMethodName("testGetTestMethodNameInAllVMs"));
}
}
-
+
private void assertGetTestMethodName(final String expected) {
assertThat(getTestMethodName()).isEqualTo(expected);
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/OverridingGetPropertiesDisconnectsAllDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/OverridingGetPropertiesDisconnectsAllDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/OverridingGetPropertiesDisconnectsAllDUnitTest.java
new file mode 100644
index 0000000..776fb3d
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/OverridingGetPropertiesDisconnectsAllDUnitTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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 com.gemstone.gemfire.test.dunit.tests;
+
+import static com.gemstone.gemfire.test.dunit.Invoke.*;
+
+import java.util.Properties;
+
+import com.gemstone.gemfire.distributed.internal.DistributionConfig;
+import com.gemstone.gemfire.test.dunit.DistributedTestCase;
+
+/**
+ * Verifies that overriding {@code getDistributedSystemProperties} results
+ * in {@code disconnectAllFromDS} during tear down.
+ */
+public class OverridingGetPropertiesDisconnectsAllDUnitTest extends DistributedTestCase {
+
+ public OverridingGetPropertiesDisconnectsAllDUnitTest(final String name) {
+ super(name);
+ }
+
+ @Override
+ public final void preTearDownAssertions() throws Exception {
+ invokeInEveryVM(() -> assertNotNull(basicGetSystem()));
+ }
+
+ @Override
+ public final void postTearDownAssertions() throws Exception {
+ invokeInEveryVM(() -> assertNull(basicGetSystem()));
+ }
+
+ @Override
+ public final Properties getDistributedSystemProperties() {
+ Properties props = new Properties();
+ props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
+ return props;
+ }
+
+ public void testDisconnects() throws Exception {
+ invokeInEveryVM(() -> assertFalse(getDistributedSystemProperties().isEmpty()));
+ invokeInEveryVM(() -> assertNotNull(getSystem()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/VMDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/VMDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/VMDUnitTest.java
index ce6cb36..c0275d2 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/VMDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/test/dunit/tests/VMDUnitTest.java
@@ -31,6 +31,7 @@ import com.gemstone.gemfire.test.dunit.VM;
*/
public class VMDUnitTest extends DistributedTestCase {
+ private static final AtomicInteger COUNTER = new AtomicInteger();
private static final boolean BOOLEAN_VALUE = true;
private static final byte BYTE_VALUE = (byte) 40;
private static final long LONG_VALUE = 42L;
@@ -40,124 +41,139 @@ public class VMDUnitTest extends DistributedTestCase {
super(name);
}
- /**
- * Accessed via reflection. DO NOT REMOVE
- * @return
- */
- protected static byte remoteByteMethod() {
- return BYTE_VALUE;
+ public void testInvokeStaticBoolean() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(BOOLEAN_VALUE, (boolean) vm.invoke(() -> remoteBooleanMethod()));
}
- public void notestInvokeStaticBoolean() {
+ public void testInvokeStaticByte() {
Host host = Host.getHost(0);
VM vm = host.getVM(0);
- assertEquals(BOOLEAN_VALUE,
- (boolean) vm.invoke(() -> VMDUnitTest.remoteBooleanMethod()));
+ assertEquals(BYTE_VALUE, (byte) vm.invoke(() -> remoteByteMethod()));
}
- /**
- * Accessed via reflection. DO NOT REMOVE
- * @return
- */
- protected static boolean remoteBooleanMethod() {
- return BOOLEAN_VALUE;
+ public void testInvokeStaticLong() {
+ Host host = Host.getHost(0);
+ VM vm = host.getVM(0);
+ assertEquals(LONG_VALUE, (long) vm.invoke(() -> remoteLongMethod()));
}
- public void notestInvokeStaticBooleanNotBoolean() {
+ public void testInvokeInstance() {
Host host = Host.getHost(0);
VM vm = host.getVM(0);
- try {
- vm.invoke(() -> VMDUnitTest.remoteByteMethod());
- fail("Should have thrown an IllegalArgumentException");
-
- } catch (IllegalArgumentException ex) {
-
- }
+ assertEquals(STRING_VALUE, vm.invoke(new ClassWithString(), "getString"));
}
- public void notestInvokeStaticLong() {
+ public void testInvokeRunnableWithException() {
Host host = Host.getHost(0);
VM vm = host.getVM(0);
- assertEquals(LONG_VALUE,
- (long) vm.invoke(() -> VMDUnitTest.remoteLongMethod()));
+ try {
+ vm.invoke(new InvokeRunnable());
+ fail("Should have thrown a BasicTestException");
+ } catch (RMIException ex) {
+ assertTrue(ex.getCause() instanceof BasicTestException);
+ }
+ }
+
+ public void testReturnValue() throws Exception {
+ final Host host = Host.getHost(0);
+ final VM vm = host.getVM(0);
+ // Assert class static invocation works
+ AsyncInvocation a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(0), a1.getReturnValue());
+ // Assert class static invocation with args works
+ a1 = vm.invokeAsync(() -> incrementStaticCount(new Integer(2)));
+ a1.join();
+ assertEquals(new Integer(3), a1.getReturnValue());
+ // Assert that previous values are not returned when invoking method w/ no return val
+ a1 = vm.invokeAsync(() -> incStaticCount());
+ a1.join();
+ assertNull(a1.getReturnValue());
+ // Assert that previous null returns are over-written
+ a1 = vm.invokeAsync(() -> getAndIncStaticCount());
+ a1.join();
+ assertEquals(new Integer(4), a1.getReturnValue());
+
+ // Assert object method invocation works with zero arg method
+ final VMTestObject o = new VMTestObject(0);
+ a1 = vm.invokeAsync(o, "incrementAndGet", new Object[] {});
+ a1.join();
+ assertEquals(new Integer(1), a1.getReturnValue());
+ // Assert object method invocation works with no return
+ a1 = vm.invokeAsync(o, "set", new Object[] {new Integer(3)});
+ a1.join();
+ assertNull(a1.getReturnValue());
+ }
+
+ private static Integer getAndIncStaticCount() {
+ return new Integer(COUNTER.getAndIncrement());
+ }
+
+ private static Integer incrementStaticCount(Integer inc) {
+ return new Integer(COUNTER.addAndGet(inc.intValue()));
+ }
+
+ private static void incStaticCount() {
+ COUNTER.incrementAndGet();
}
/**
* Accessed via reflection. DO NOT REMOVE
- * @return
*/
- protected static long remoteLongMethod() {
- return LONG_VALUE;
+ private static byte remoteByteMethod() {
+ return BYTE_VALUE;
}
- public void notestInvokeStaticLongNotLong() {
- Host host = Host.getHost(0);
- VM vm = host.getVM(0);
- try {
- vm.invoke(() -> VMDUnitTest.remoteByteMethod());
- fail("Should have thrown an IllegalArgumentException");
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static boolean remoteBooleanMethod() {
+ return BOOLEAN_VALUE;
+ }
- } catch (IllegalArgumentException ex) {
-
- }
+ /**
+ * Accessed via reflection. DO NOT REMOVE
+ */
+ private static long remoteLongMethod() {
+ return LONG_VALUE;
}
- protected static class ClassWithLong implements Serializable {
+ private static class ClassWithLong implements Serializable {
public long getLong() {
return LONG_VALUE;
}
}
- protected static class ClassWithByte implements Serializable {
+ private static class ClassWithByte implements Serializable {
public byte getByte() {
return BYTE_VALUE;
}
}
- protected static class InvokeRunnable
- implements SerializableRunnableIF {
-
+ private static class InvokeRunnable implements SerializableRunnableIF {
public void run() {
- throw new BasicDUnitTest.BasicTestException();
+ throw new BasicTestException();
}
}
- protected static class ClassWithString implements Serializable {
+ private static class ClassWithString implements Serializable {
public String getString() {
return STRING_VALUE;
}
}
- public void notestInvokeInstance() {
- Host host = Host.getHost(0);
- VM vm = host.getVM(0);
- assertEquals(STRING_VALUE,
- vm.invoke(new ClassWithString(), "getString"));
- }
-
- public void notestInvokeRunnable() {
- Host host = Host.getHost(0);
- VM vm = host.getVM(0);
- try {
- vm.invoke(new InvokeRunnable());
- fail("Should have thrown a BasicTestException");
-
- } catch (RMIException ex) {
- assertTrue(ex.getCause() instanceof BasicDUnitTest.BasicTestException);
+ private static class BasicTestException extends RuntimeException {
+ BasicTestException() {
+ this("Test exception. Please ignore.");
+ }
+ BasicTestException(String s) {
+ super(s);
}
}
-
- private static final AtomicInteger COUNTER = new AtomicInteger();
- public static Integer getAndIncStaticCount() {
- return new Integer(COUNTER.getAndIncrement());
- }
- public static Integer incrementStaticCount(Integer inc) {
- return new Integer(COUNTER.addAndGet(inc.intValue()));
- }
- public static void incStaticCount() {
- COUNTER.incrementAndGet();
- }
- public static class VMTestObject implements Serializable {
+
+ private static class VMTestObject implements Serializable {
private static final long serialVersionUID = 1L;
private final AtomicInteger val;
public VMTestObject(int init) {
@@ -173,34 +189,4 @@ public class VMDUnitTest extends DistributedTestCase {
this.val.set(newVal.intValue());
}
}
- public void testReturnValue() throws Exception {
- final Host host = Host.getHost(0);
- final VM vm = host.getVM(0);
- // Assert class static invocation works
- AsyncInvocation a1 = vm.invokeAsync(() -> getAndIncStaticCount());
- a1.join();
- assertEquals(new Integer(0), a1.getReturnValue());
- // Assert class static invocation with args works
- a1 = vm.invokeAsync(() -> incrementStaticCount(new Integer(2)));
- a1.join();
- assertEquals(new Integer(3), a1.getReturnValue());
- // Assert that previous values are not returned when invoking method w/ no return val
- a1 = vm.invokeAsync(() -> incStaticCount());
- a1.join();
- assertNull(a1.getReturnValue());
- // Assert that previous null returns are over-written
- a1 = vm.invokeAsync(() -> getAndIncStaticCount());
- a1.join();
- assertEquals(new Integer(4), a1.getReturnValue());
-
- // Assert object method invocation works with zero arg method
- final VMTestObject o = new VMTestObject(0);
- a1 = vm.invokeAsync(o, "incrementAndGet", new Object[] {});
- a1.join();
- assertEquals(new Integer(1), a1.getReturnValue());
- // Assert object method invocation works with no return
- a1 = vm.invokeAsync(o, "set", new Object[] {new Integer(3)});
- a1.join();
- assertNull(a1.getReturnValue());
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/57c8600b/geode-cq/src/test/java/com/gemstone/gemfire/cache/query/cq/dunit/CqDataDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-cq/src/test/java/com/gemstone/gemfire/cache/query/cq/dunit/CqDataDUnitTest.java b/geode-cq/src/test/java/com/gemstone/gemfire/cache/query/cq/dunit/CqDataDUnitTest.java
index ebc6f53..bd473b3 100644
--- a/geode-cq/src/test/java/com/gemstone/gemfire/cache/query/cq/dunit/CqDataDUnitTest.java
+++ b/geode-cq/src/test/java/com/gemstone/gemfire/cache/query/cq/dunit/CqDataDUnitTest.java
@@ -63,10 +63,9 @@ public class CqDataDUnitTest extends CacheTestCase {
public CqDataDUnitTest(String name) {
super(name);
}
-
- public void setUp() throws Exception {
- super.setUp();
-
+
+ @Override
+ public final void postSetUp() throws Exception {
// avoid IllegalStateException from HandShake by connecting all vms tor
// system before creating ConnectionPools
getSystem();
@@ -75,7 +74,10 @@ public class CqDataDUnitTest extends CacheTestCase {
getSystem();
}
});
-
+ postSetUpCqDataDUnitTest();
+ }
+
+ protected void postSetUpCqDataDUnitTest() throws Exception {
}
/**