This articles describes how to write geode Dunit tests using Junit rules.
Use
...
ClusterStartupRule
This rule would ease the steps to start up locator/server in specific VMs with specified gemfire properties. The rule will take care of tearing down and cleaning up the VMs and file systems after tests are done so you don't need to worry about closing anything.
...
Code Block | ||
---|---|---|
| ||
@Category(DistributedTest.class) public class ExampleTest { @Rule public LocatorServerStartupRuleClusterStartupRule lsRulecluster = new LocatorServerStartupRuleClusterStartupRule(); //See Note 1 @Test public void simpleUsage() throws Exception { // start up a default locator in vm0 MemberVM locator = lsRulecluster.startLocatorVM(0); //See Note 2 // start up a default server in vm1, joining the locator MemberVM server = lsRulecluster.startServerVM(1, locator.getPort()); // see Note 3 // you can use these attributes of the locator/server locator.getName(); // by default this would locator-0 locator.getPort(); // these are the random ports by default. locator.getJmxPort(); locator.getHttpPort(); locator.getWorkingDir(); // this should be dunit/vm0 unless the rule is constructed using temp folder. // you can also do code invocation inside the vms locator.invoke(()->{ // access the locator started in this VM InternalLocator internalLocator = LocatorServerStartupRule.locatorStarterClusterStartupRule.getLocator(); // operations and assertions here }); server.invoke(()->{ // access the cache and server in this VM InternalCache cache = LocatorServerStartupRuleClusterStartupRule.serverStarter.getCache(); CacheServer cacheServer = LocatorServerStartupRule.serverStarter.getServer(); // operations and assertions here. }); } } |
...
This is the most common way to create the rule. There are two more variations of it
Code Block language java // this will use a temporary folder for the working dir of the locator/server created by this rule. // use this if you want to examine the content of the workingdir of the server/locator and do not // want it to be contaminated with dunit test launcher's own logs. public LocatorServerStartupRuleClusterStartupRule lsRulecluster = new LocatorServerStartupRuleClusterStartupRule().withTempWorkingDir(); // Or // this will have the server/locators logs go into the log file instead of go into the console. public LocatorServerStartupRuleClusterStartupRule lsRulecluster = new LocatorServerStartupRuleClusterStartupRule().withLogFile(); //or both.
When you start up a locator/server, you can also pass in a properties object that would configure the server/locator
Code Block language java Properties locatorProps = new Properties(); locatorProps.setProperty(ConfigurationProperties.GROUPS, "group1,group2"); locatorProps.setProperty(ConfigurationProperties.HTTP_SERVICE_PORT, "8080"); locatorProps.setProperty(ConfigurationProperties.ANY_PROPERTIES, value); // set any gemfire properties that are in ConfigurationPropertiesMemberVMConfigurationProperties MemberVM locator = lsRulecluster.startLocatorVM(0, locatorProps); Properties serverProps = new Properties();// set any gemfire properties that are in ConfigurationPropertiesMemberVMConfigurationProperties locatorProps.setProperty(ConfigurationProperties.ANY_PROPERTIES, value); MemberVM server = lsRulecluster.startServer(1, serverProps, locator.getPort);
...
Code Block | ||
---|---|---|
| ||
@Rule public ServerStarterRule serverRule = new ServerStarterRule() // simplest way to create the rule, you can call one or more of the following to configure the rule .withProperty(ConfigurationProperties.ANY_PROPERITES, value) // configure the locator with a single property .withProperties(properties) // configure the locator with a property object .withJMXManager() // start the server with JMX manager .withConnectionToLocator() // connect this server with another locator. .withSecurityManager(SimpleTestSecurityManager.class) // a convenient way to start the locator with a security manager, same effect as a .withProperty call. .withEmbeddedLocator() // start an embedded locator on this server .withPDXPersistent() // with pdx persistent .withRestService() // start the rest service on this server .withAutoStart(); // this will start the server before executing any test code. .withRegion(REGION_SHORTCUT, regionName);// this will create the region before executing any test code @Test public void test() throws Exception { // if the server is started, then we can use it to get these attributes: serverRule.getName(); // by default this would be server, if not configured by the properties serverRule.getPort(); // these are the random ports by default if not configured by the properties serverRule.getJmxPort(); serverRule.getHttpPort(); InternalCache cache = serverRule.getCache(); // if the locator is not auto started, you can start the locator by calling serverRule.startServer(); // operations and assertions here. } |
Use
...
GfshCommandRule
This is the rule that's useful if you would like to test out some gfsh commands and verify the output. To use this, you will need to have a jmxManager running (either a locator or a server with JmxManager started).
...
Code Block | ||
---|---|---|
| ||
@Category(IntegrationTest.class) public class ExampleTest { @Rule public LocatorStarterRule locator = new LocatorStarterRule().withAutoStart(); @Rule public GfshShellConnectionRuleGfshCommandRule gfshRule = new GfshShellConnectionRuleGfshCommandRule(); @Before public void before() throws Exception { gfshRule.connectAndVerify(locator); } @Test public void simpleUsage() throws Exception { // gfshRule already connect, ready to execute some command and verify output. CommandResult result = gfshRule.executeAndVerifyCommandexecuteAndAssertThat("list members"); // examine the result and do some assertions // get the string representation of the gfsh result String resultString = gfshRule.getGfshOutput( .statusIsOK() .containsOutput("blah,blah"); } } |
This example starts a server with JMXManager and then use the gfshRule to connect to the jmxManager of the server
Code Block | ||
---|---|---|
| ||
@Category(IntegrationTest.class) public class ExampleTest { @Rule public ServerStarterRule serverRule = new ServerStarterRule().withJMXManager().withRegion(RegionShortcut.REPLICATE, "testRegion"); @Rule public GfshShellConnectionRuleGfshCommandRule gfshRule = new GfshShellConnectionRuleGfshCommandRule(); @Before public void before() throws Exception { gfshRule.connectAndVerify(serverRule.getJmxPort(), GfshShellConnectionRuleGfshCommandRule.PortType.jmxManger); } @Test public void simpleUsage() throws Exception { // gfshRule already connect, ready to execute some command String result = gfshRule.execute("list members"); // examine the result and do some assertions } } |
...
Code Block | ||
---|---|---|
| ||
@Category(IntegrationTest.class) public class ExampleTest { public LocatorStarterRule locator = new LocatorStarterRule().withAutoStart(); public GfshShellConnectionRuleGfshCommandRule gfshRule = new GfshShellConnectionRuleGfshCommandRule(locator::getJmxPort, GfshShellConnectionRule.PortType.jmxManger); @Rule public RuleChain ruleChain = RuleChain.outerRule(locator).around(gfshRule); @Test public void simpleUsage() throws Exception { // gfshRule already connect, ready to execute some command gfshRule.executeAndVerifyCommandexecuteAndAssertThat("list members").statusIsOK(); } } |
Info |
---|
Related articles
...