From dfaff3ec992cfb81116741a64a912472a2345bcc Mon Sep 17 00:00:00 2001 From: Paulo Ricardo Motta Gomes Date: Tue, 22 Jun 2010 22:33:28 +0200 Subject: [PATCH] Adapted BasicSuite for changes in NimbusTestBase (Spring integration) Added example suite that uses @DirtiesContext annotation Signed-off-by: Tim Freeman --- .../testing/suites/basic/BasicSuite.java | 22 ++-- .../basic/DirtiesContextExampleSuite.java | 116 ++++++++++++++++++ 2 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/DirtiesContextExampleSuite.java diff --git a/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/BasicSuite.java b/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/BasicSuite.java index e65b3ed4..037d653e 100644 --- a/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/BasicSuite.java +++ b/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/BasicSuite.java @@ -16,32 +16,30 @@ package org.globus.workspace.testing.suites.basic; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertTrue; + import org.globus.workspace.testing.NimbusTestBase; +import org.globus.workspace.testing.NimbusTestContextLoader; import org.nimbustools.api.repr.Caller; import org.nimbustools.api.repr.CreateResult; import org.nimbustools.api.repr.vm.VM; import org.nimbustools.api.services.rm.Manager; +import org.springframework.test.context.ContextConfiguration; import org.testng.annotations.AfterSuite; -import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertNotNull; -import static org.testng.AssertJUnit.assertTrue; - +@ContextConfiguration( + locations={"file:./service/service/java/tests/suites/basic/home/services/etc/nimbus/workspace-service/other/main.xml"}, + loader=NimbusTestContextLoader.class) public class BasicSuite extends NimbusTestBase { // ----------------------------------------------------------------------------------------- // extends NimbusTestBase // ----------------------------------------------------------------------------------------- - @BeforeSuite - @Override - public void suiteSetup() throws Exception { - super.suiteSetup(); - } - - @AfterSuite + @AfterSuite(alwaysRun=true) @Override public void suiteTeardown() throws Exception { super.suiteTeardown(); diff --git a/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/DirtiesContextExampleSuite.java b/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/DirtiesContextExampleSuite.java new file mode 100644 index 00000000..4e30c4b9 --- /dev/null +++ b/service/service/java/tests/suites/basic/src/org/globus/workspace/testing/suites/basic/DirtiesContextExampleSuite.java @@ -0,0 +1,116 @@ +/* + * Copyright 1999-2010 University of Chicago + * + * Licensed 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.globus.workspace.testing.suites.basic; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertTrue; + +import org.globus.workspace.testing.NimbusTestBase; +import org.globus.workspace.testing.NimbusTestContextLoader; +import org.nimbustools.api.repr.Caller; +import org.nimbustools.api.repr.CreateResult; +import org.nimbustools.api.repr.vm.VM; +import org.nimbustools.api.services.rm.Manager; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.testng.annotations.AfterSuite; +import org.testng.annotations.Test; + + +/** + * Suite to exemplify the use of spring test annotation @DirtiesContext, + * that indicate that a test method dirties the context for the current test. + * + * This annotation can be used if the context needs to be reloaded, + * because a test has modified it (for example, by making the DB dirty + * or replacing a bean definition). + * + * In this example, the test dirtyTest() leases a VM but doesn't + * destroy it at the end of the test. The following test (checkEmpty()) + * checks if there are no VM leases. If the @DirtiesContext annotation + * is not used in the dirtyTest(), the checkEmpty() test fails. + */ +@ContextConfiguration( + locations={"file:./service/service/java/tests/suites/basic/home/services/etc/nimbus/workspace-service/other/main.xml"}, + loader=NimbusTestContextLoader.class) +public class DirtiesContextExampleSuite extends NimbusTestBase { + + // ----------------------------------------------------------------------------------------- + // extends NimbusTestBase + // ----------------------------------------------------------------------------------------- + + @AfterSuite(alwaysRun=true) + @Override + public void suiteTeardown() throws Exception { + super.suiteTeardown(); + } + + /** + * This is how coordinate your Java test suite code with the conf files to use. + * @return absolute path to the value that should be set for $NIMBUS_HOME + * @throws Exception if $NIMBUS_HOME cannot be determined + */ + @Override + protected String getNimbusHome() throws Exception { + return this.determineSuitesPath() + "/basic/home"; + } + + + // ----------------------------------------------------------------------------------------- + // TESTS + // ----------------------------------------------------------------------------------------- + + /** + * Lease a VM and doesn't destroy it + * @throws Exception problem + */ + @DirtiesContext + @Test(groups="dirtyTest") + public void dirtyTest() throws Exception { + logger.debug("dirtyTest"); + final Manager rm = this.locator.getManager(); + + final Caller caller = this.populator().getCaller(); + final CreateResult result = + rm.create(this.populator().getCreateRequest("suite:basic:dirtyTest"), + caller); + + final VM[] vms = result.getVMs(); + assertEquals(1, vms.length); + assertNotNull(vms[0]); + logger.info("Leased vm '" + vms[0].getID() + '\''); + + assertTrue(rm.exists(vms[0].getID(), Manager.INSTANCE)); + } + + + /** + * Check if no VM's are left from previous test + * @throws Exception problem + */ + @Test(dependsOnGroups="dirtyTest") + public void checkEmpty() throws Exception { + logger.debug("checkExistence"); + final Manager rm = this.locator.getManager(); + + final VM[] vms = rm.getGlobalAll(); + + assertEquals(0, vms.length); + } + +}