modules/common/ant-bundle/src/main/java/org/rhq/bundle/ant/AntLauncher.java | 23 - modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java | 172 ++++++++-- modules/common/ant-bundle/src/test/resources/file.zip |binary modules/common/ant-bundle/src/test/resources/test-bundle-v1.xml | 4 modules/common/ant-bundle/src/test/resources/test-bundle-v2-noManageRootDir.xml | 46 ++ modules/common/ant-bundle/src/test/resources/test-bundle-v2.xml | 14 6 files changed, 221 insertions(+), 38 deletions(-)
New commits: commit d2a766a39f4e3cf9b6e823d53dd356f8d42c5b1c Author: John Mazzitelli mazz@redhat.com Date: Wed Dec 22 11:39:22 2010 -0500
BZ 659142 - ant unit test shows the manageRootDir=false working
diff --git a/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java b/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java index 8a12139..a9edfc1 100644 --- a/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java +++ b/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java @@ -23,6 +23,7 @@ package org.rhq.bundle.ant;
import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -33,6 +34,7 @@ import java.util.Set; import org.apache.tools.ant.BuildListener; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; +import org.testng.annotations.AfterClass; import org.testng.annotations.Test;
import org.rhq.core.domain.configuration.Configuration; @@ -49,7 +51,15 @@ import org.rhq.core.util.file.FileUtil; public class AntLauncherTest { private static final File DEPLOY_DIR = new File("target/test-ant-bundle").getAbsoluteFile();
+ @AfterClass + public void afterClass() { + FileUtil.purge(DEPLOY_DIR, true); + } + public void testParse() throws Exception { + // We want to test with an empty deploy dir to ensure nothing gets installed there after a parse + FileUtil.purge(DEPLOY_DIR, true); + AntLauncher ant = new AntLauncher();
BundleAntProject project = ant.parseBundleDeployFile(getBuildXml("test-bundle-v1.xml")); @@ -74,12 +84,18 @@ public class AntLauncherTest { assert propDef.getDefaultValue().equals("8080"); assert propDef.getDescription().equals("This is where the product will listen for incoming messages"); assert propDef.isRequired(); + + // all we did was parse, nothing should really have been extracted or installed + assert !DEPLOY_DIR.exists() : "Nothing should have been installed to the deploy dir"; }
public void testInstall() throws Exception { // We want to test a fresh install, so make sure the deploy dir doesn't pre-exist. FileUtil.purge(DEPLOY_DIR, true);
+ // but we do want to add an unrelated file to see that it remains untouched - the install just "goes around" it + File unrelatedFile = writeFile("unrelated content", DEPLOY_DIR, "unrelated-file.txt"); + AntLauncher ant = new AntLauncher(); Properties inputProps = createInputProperties("/test-bundle-v1-input.properties"); List<BuildListener> buildListeners = createBuildListeners(); @@ -116,6 +132,10 @@ public class AntLauncherTest { assert preinstallTargetExecuted.equals("1a"); String postinstallTargetExecuted = (String) project.getProperties().get("postinstallTargetExecuted"); assert postinstallTargetExecuted.equals("1b"); + + assert new File(DEPLOY_DIR, "subdir/test.properties").exists() : "missing file"; + assert new File(DEPLOY_DIR, "archived-bundle-file.txt").exists() : "missing archived bundle file"; + assert unrelatedFile.exists() : "unrelated file was removed during the install"; }
private List<BuildListener> createBuildListeners() { @@ -130,7 +150,9 @@ public class AntLauncherTest {
@Test(dependsOnMethods = "testInstall") public void testUpgrade() throws Exception { - // We want to test an upgrade, so do *not* wipe out the deploy dir. + // We want to test an upgrade, so do *not* wipe out the deploy dir - our test method @dependsOnMethods testInstall + // but we do want to add an unrelated file to see that it gets deleted as part of the upgrade + File unrelatedFile = writeFile("unrelated content", DEPLOY_DIR, "unrelated-file.txt");
AntLauncher ant = new AntLauncher(); Properties inputProps = createInputProperties("/test-bundle-v2-input.properties"); @@ -168,6 +190,61 @@ public class AntLauncherTest { assert preinstallTargetExecuted.equals("2a"); String postinstallTargetExecuted = (String) project.getProperties().get("postinstallTargetExecuted"); assert postinstallTargetExecuted.equals("2b"); + + assert new File(DEPLOY_DIR, "subdir/test.properties").exists() : "missing file"; + assert new File(DEPLOY_DIR, "archived-bundle-file.txt").exists() : "missing archived bundle file"; + assert !unrelatedFile.exists() : "we are managing root dir so unrelated file should be removed during upgrade"; + } + + public void testUpgradeNoManageRootDir() throws Exception { + // We want to test an upgrade, so do *not* wipe out the deploy dir - let's re-invoke testInstall + // to get us to an initial state of the v1 bundle installed + testInstall(); + + // we still want the unrelated file - we want to see that manageRootDir=false works (unrelated files should not be deleted) + File unrelatedFile = new File(DEPLOY_DIR, "unrelated-file.txt"); + assert unrelatedFile.exists() : "our initial install test method should have prepared an unmanaged file"; + + AntLauncher ant = new AntLauncher(); + Properties inputProps = createInputProperties("/test-bundle-v2-input.properties"); + List<BuildListener> buildListeners = createBuildListeners(); + + BundleAntProject project = ant.executeBundleDeployFile(getBuildXml("test-bundle-v2-noManageRootDir.xml"), + inputProps, buildListeners); + assert project != null; + Set<String> bundleFiles = project.getBundleFileNames(); + assert bundleFiles != null; + assert bundleFiles.size() == 4 : bundleFiles; + assert bundleFiles.contains("test-v2.properties") : bundleFiles; + assert bundleFiles.contains("file.zip") : bundleFiles; + assert bundleFiles.contains("foo-script") : bundleFiles; // from install-system-service + assert bundleFiles.contains("foo-config") : bundleFiles; // from install-system-service + + assert project.getBundleName().equals("example.com (JBoss EAP 4.3)"); + assert project.getBundleVersion().equals("2.5"); + assert project.getBundleDescription().equals("updated bundle"); + + ConfigurationDefinition configDef = project.getConfigurationDefinition(); + assert configDef.getPropertyDefinitions().size() == 1 : configDef.getPropertyDefinitions(); + PropertyDefinitionSimple propDef = configDef.getPropertyDefinitionSimple("listener.port"); + assert propDef != null; + assert propDef.getType() == PropertySimpleType.INTEGER; + assert propDef.getDefaultValue().equals("9090"); + assert propDef.getDescription().equals("This is where the product will listen for incoming messages"); + assert propDef.isRequired(); + + Configuration config = project.getConfiguration(); + assert config.getProperties().size() == 1; + assert "20000".equals(config.getSimpleValue("listener.port", null)) : config.getProperties(); + + String preinstallTargetExecuted = (String) project.getProperties().get("preinstallTargetExecuted"); + assert preinstallTargetExecuted.equals("2a"); + String postinstallTargetExecuted = (String) project.getProperties().get("postinstallTargetExecuted"); + assert postinstallTargetExecuted.equals("2b"); + + assert new File(DEPLOY_DIR, "subdir/test.properties").exists() : "missing file"; + assert new File(DEPLOY_DIR, "archived-bundle-file.txt").exists() : "missing archived bundle file"; + assert unrelatedFile.exists() : "we are NOT managing root dir so unrelated file should NOT be removed during upgrade"; }
private Properties createInputProperties(String resourcePath) throws IOException { @@ -189,4 +266,24 @@ public class AntLauncherTest { assert file.exists() : "The test Ant build script doesn't exist: " + file.getAbsolutePath(); return file; } + + private File writeFile(String content, File fileToOverwrite) throws Exception { + FileOutputStream out = null; + + try { + fileToOverwrite.getParentFile().mkdirs(); + out = new FileOutputStream(fileToOverwrite); + out.write(content.getBytes()); + return fileToOverwrite; + } finally { + if (out != null) { + out.close(); + } + } + } + + private File writeFile(String content, File destDir, String fileName) throws Exception { + File destFile = new File(destDir, fileName); + return writeFile(content, destFile); + } } diff --git a/modules/common/ant-bundle/src/test/resources/file.zip b/modules/common/ant-bundle/src/test/resources/file.zip index ea45ad7..922658a 100644 Binary files a/modules/common/ant-bundle/src/test/resources/file.zip and b/modules/common/ant-bundle/src/test/resources/file.zip differ diff --git a/modules/common/ant-bundle/src/test/resources/test-bundle-v2-noManageRootDir.xml b/modules/common/ant-bundle/src/test/resources/test-bundle-v2-noManageRootDir.xml new file mode 100644 index 0000000..0b07e9e --- /dev/null +++ b/modules/common/ant-bundle/src/test/resources/test-bundle-v2-noManageRootDir.xml @@ -0,0 +1,46 @@ +<?xml version="1.0"?> + +<project name="test-bundle" default="main" xmlns:rhq="antlib:org.rhq.bundle"> + + <rhq:bundle name="example.com (JBoss EAP 4.3)" version="2.5" + description="updated bundle"> + + <rhq:input-property + name="listener.port" + description="This is where the product will listen for incoming messages" + required="true" + defaultValue="9090" + type="integer"/> + + <rhq:deployment-unit name="appserver" preinstallTarget="preinstall" postinstallTarget="postinstall" + manageRootDir="false"> <!-- this is the only difference with test-bundle-v2.xml --> + <rhq:system-service name="foo" scriptFile="foo-script" + configFile="foo-config" overwriteScript="true" + startLevels="3,4,5" startPriority="80" stopPriority="20" root="root"/> + <rhq:file name="test-v2.properties" destinationFile="subdir/test.properties" replace="true"/> + <rhq:archive name="file.zip"> + rhq:replace + <rhq:fileset includes="**/*.properties"/> + </rhq:replace> + </rhq:archive> + <!-- the files that should be ignored during upgrades --> + rhq:ignore + <rhq:fileset includes="*.log"/> + </rhq:ignore> + </rhq:deployment-unit> + + </rhq:bundle> + + <target name="main"/> + + <target name="preinstall"> + <echo>Deploying Test Bundle v2.5 to ${rhq.deploy.dir}...</echo> + <property name="preinstallTargetExecuted" value="2a"/> + </target> + + <target name="postinstall"> + <echo>Done deploying Test Bundle v2.5 to ${rhq.deploy.dir}.</echo> + <property name="postinstallTargetExecuted" value="2b"/> + </target> + +</project> \ No newline at end of file
commit 64407320dc3715ce09caf4c6f1f8c067602bddbd Author: John Mazzitelli mazz@redhat.com Date: Wed Dec 22 10:38:24 2010 -0500
put test more things in the ant launcher unit test
diff --git a/modules/common/ant-bundle/src/main/java/org/rhq/bundle/ant/AntLauncher.java b/modules/common/ant-bundle/src/main/java/org/rhq/bundle/ant/AntLauncher.java index bfc8d2e..86fd666 100644 --- a/modules/common/ant-bundle/src/main/java/org/rhq/bundle/ant/AntLauncher.java +++ b/modules/common/ant-bundle/src/main/java/org/rhq/bundle/ant/AntLauncher.java @@ -83,13 +83,18 @@ public class AntLauncher { * @throws InvalidBuildFileException if the build file is invalid */ public BundleAntProject executeBundleDeployFile(File buildFile, Properties buildProperties, - List<BuildListener> buildListeners) - throws InvalidBuildFileException { + List<BuildListener> buildListeners) throws InvalidBuildFileException { // Parse and validate the build file before even attempting to execute it. - parseBundleDeployFile(buildFile); + BundleAntProject parsedProject = parseBundleDeployFile(buildFile);
BundleAntProject project = createProject(buildFile, false);
+ // The parse above got us all the bundle files names. The rest of this method + // will be able to re-determine everything else for 'project' but these filenames. + // Therefore, we need to copy those filenames from the parsedProject to project. + // The rest of project will be filled in later. + project.getBundleFileNames().addAll(parsedProject.getBundleFileNames()); + try { if (buildProperties != null) { for (Map.Entry<Object, Object> property : buildProperties.entrySet()) { @@ -133,10 +138,9 @@ public class AntLauncher { try { // Use the 3-param version of parse(), rather than the 2-param version, or ProjectHelper.configureProject(), // to avoid actually executing the implicit target (which would cause the rhq:bundle task to be executed). - AntXMLContext context = (AntXMLContext) project.getReference(REFID_CONTEXT); - projectHelper.parse(project, buildFile, - new ProjectHelper2.RootHandler(context, - new ProjectHelper2.MainHandler())); + AntXMLContext context = (AntXMLContext) project.getReference(REFID_CONTEXT); + projectHelper.parse(project, buildFile, new ProjectHelper2.RootHandler(context, + new ProjectHelper2.MainHandler())); } catch (BuildException e) { throw new InvalidBuildFileException("Failed to parse bundle Ant build file.", e); } @@ -210,7 +214,7 @@ public class AntLauncher { for (Object targetObj : targets) { Target target = (Target) targetObj; Task[] tasks = target.getTasks(); - for (Task task : tasks) { + for (Task task : tasks) { if (task.getTaskName().equals(BUNDLE_TASK_NAME)) { abortIfTaskWithinTarget(target, task); bundleTaskCount++; @@ -230,7 +234,8 @@ public class AntLauncher { BundleTask bundleTask = (BundleTask) preconfigureTask(unconfiguredBundleTask); Collection<DeploymentUnitType> deployments = bundleTask.getDeploymentUnits().values(); if (deployments.isEmpty()) { - throw new InvalidBuildFileException("The bundle task must contain exactly one rhq:deploymentUnit child element."); + throw new InvalidBuildFileException( + "The bundle task must contain exactly one rhq:deploymentUnit child element."); } DeploymentUnitType deployment = deployments.iterator().next(); Map<File, File> files = deployment.getFiles(); diff --git a/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java b/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java index 831bdf9..8a12139 100644 --- a/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java +++ b/modules/common/ant-bundle/src/test/java/org/rhq/bundle/ant/AntLauncherTest.java @@ -28,6 +28,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Properties; +import java.util.Set;
import org.apache.tools.ant.BuildListener; import org.apache.tools.ant.DefaultLogger; @@ -53,17 +54,26 @@ public class AntLauncherTest {
BundleAntProject project = ant.parseBundleDeployFile(getBuildXml("test-bundle-v1.xml")); assert project != null; - /*Map<String, String> bundleFiles = project.getBundleFiles(); + Set<String> bundleFiles = project.getBundleFileNames(); assert bundleFiles != null; - assert bundleFiles.size() == 2 : bundleFiles; - assert bundleFiles.get("f").equals("test-v2.properties") : bundleFiles; - assert bundleFiles.get("pkg").equals("package.zip") : bundleFiles;*/ + assert bundleFiles.size() == 4 : bundleFiles; + assert bundleFiles.contains("test-v1.properties") : bundleFiles; + assert bundleFiles.contains("file.zip") : bundleFiles; + assert bundleFiles.contains("foo-script") : bundleFiles; // from install-system-service + assert bundleFiles.contains("foo-config") : bundleFiles; // from install-system-service + + assert project.getBundleName().equals("example.com (JBoss EAP 4.3)"); + assert project.getBundleVersion().equals("1.0"); + assert project.getBundleDescription().equals("example.com corporate website hosted on JBoss EAP 4.3");
ConfigurationDefinition configDef = project.getConfigurationDefinition(); assert configDef.getPropertyDefinitions().size() == 1 : configDef.getPropertyDefinitions(); PropertyDefinitionSimple propDef = configDef.getPropertyDefinitionSimple("listener.port"); assert propDef != null; assert propDef.getType() == PropertySimpleType.INTEGER; + assert propDef.getDefaultValue().equals("8080"); + assert propDef.getDescription().equals("This is where the product will listen for incoming messages"); + assert propDef.isRequired(); }
public void testInstall() throws Exception { @@ -75,27 +85,37 @@ public class AntLauncherTest { List<BuildListener> buildListeners = createBuildListeners();
BundleAntProject project = ant.executeBundleDeployFile(getBuildXml("test-bundle-v1.xml"), inputProps, - buildListeners); - /*Map<String, String> bundleFiles = project.getBundleFiles(); + buildListeners); + assert project != null; + Set<String> bundleFiles = project.getBundleFileNames(); assert bundleFiles != null; - assert bundleFiles.size() == 2 : bundleFiles; - assert bundleFiles.get("f").equals("test-v2.properties") : bundleFiles; - assert bundleFiles.get("pkg").equals("package.zip") : bundleFiles;*/ + assert bundleFiles.size() == 4 : bundleFiles; + assert bundleFiles.contains("test-v1.properties") : bundleFiles; + assert bundleFiles.contains("file.zip") : bundleFiles; + assert bundleFiles.contains("foo-script") : bundleFiles; // from install-system-service + assert bundleFiles.contains("foo-config") : bundleFiles; // from install-system-service + + assert project.getBundleName().equals("example.com (JBoss EAP 4.3)"); + assert project.getBundleVersion().equals("1.0"); + assert project.getBundleDescription().equals("example.com corporate website hosted on JBoss EAP 4.3");
ConfigurationDefinition configDef = project.getConfigurationDefinition(); assert configDef.getPropertyDefinitions().size() == 1 : configDef.getPropertyDefinitions(); PropertyDefinitionSimple propDef = configDef.getPropertyDefinitionSimple("listener.port"); assert propDef != null; assert propDef.getType() == PropertySimpleType.INTEGER; + assert propDef.getDefaultValue().equals("8080"); + assert propDef.getDescription().equals("This is where the product will listen for incoming messages"); + assert propDef.isRequired();
Configuration config = project.getConfiguration(); assert config.getProperties().size() == 1 : config.getProperties(); assert "10000".equals(config.getSimpleValue("listener.port", null)) : config.getProperties(); - + String preinstallTargetExecuted = (String) project.getProperties().get("preinstallTargetExecuted"); - assert preinstallTargetExecuted.equals("true"); + assert preinstallTargetExecuted.equals("1a"); String postinstallTargetExecuted = (String) project.getProperties().get("postinstallTargetExecuted"); - assert postinstallTargetExecuted.equals("true"); + assert postinstallTargetExecuted.equals("1b"); }
private List<BuildListener> createBuildListeners() { @@ -117,28 +137,43 @@ public class AntLauncherTest { List<BuildListener> buildListeners = createBuildListeners();
BundleAntProject project = ant.executeBundleDeployFile(getBuildXml("test-bundle-v2.xml"), inputProps, - buildListeners); - /*Map<String, String> bundleFiles = project.getBundleFiles(); + buildListeners); + assert project != null; + Set<String> bundleFiles = project.getBundleFileNames(); assert bundleFiles != null; - assert bundleFiles.size() == 2 : bundleFiles; - assert bundleFiles.get("f").equals("test-v2.properties") : bundleFiles; - assert bundleFiles.get("pkg").equals("package.zip") : bundleFiles;*/ + assert bundleFiles.size() == 4 : bundleFiles; + assert bundleFiles.contains("test-v2.properties") : bundleFiles; + assert bundleFiles.contains("file.zip") : bundleFiles; + assert bundleFiles.contains("foo-script") : bundleFiles; // from install-system-service + assert bundleFiles.contains("foo-config") : bundleFiles; // from install-system-service + + assert project.getBundleName().equals("example.com (JBoss EAP 4.3)"); + assert project.getBundleVersion().equals("2.5"); + assert project.getBundleDescription().equals("updated bundle");
ConfigurationDefinition configDef = project.getConfigurationDefinition(); - assert configDef.getPropertyDefinitions().size() == 1; + assert configDef.getPropertyDefinitions().size() == 1 : configDef.getPropertyDefinitions(); PropertyDefinitionSimple propDef = configDef.getPropertyDefinitionSimple("listener.port"); assert propDef != null; assert propDef.getType() == PropertySimpleType.INTEGER; + assert propDef.getDefaultValue().equals("9090"); + assert propDef.getDescription().equals("This is where the product will listen for incoming messages"); + assert propDef.isRequired();
Configuration config = project.getConfiguration(); assert config.getProperties().size() == 1; assert "20000".equals(config.getSimpleValue("listener.port", null)) : config.getProperties(); + + String preinstallTargetExecuted = (String) project.getProperties().get("preinstallTargetExecuted"); + assert preinstallTargetExecuted.equals("2a"); + String postinstallTargetExecuted = (String) project.getProperties().get("postinstallTargetExecuted"); + assert postinstallTargetExecuted.equals("2b"); }
private Properties createInputProperties(String resourcePath) throws IOException { Properties inputProps = new Properties(); inputProps.setProperty(DeployPropertyNames.DEPLOY_DIR, DEPLOY_DIR.getPath()); - inputProps.setProperty(DeployPropertyNames.DEPLOY_ID, "100"); + inputProps.setProperty(DeployPropertyNames.DEPLOY_ID, "100"); inputProps.setProperty(DeployPropertyNames.DEPLOY_PHASE, DeploymentPhase.INSTALL.name()); InputStream inputStream = this.getClass().getResourceAsStream(resourcePath); try { diff --git a/modules/common/ant-bundle/src/test/resources/test-bundle-v1.xml b/modules/common/ant-bundle/src/test/resources/test-bundle-v1.xml index 76820bf..a6dcfec 100644 --- a/modules/common/ant-bundle/src/test/resources/test-bundle-v1.xml +++ b/modules/common/ant-bundle/src/test/resources/test-bundle-v1.xml @@ -34,12 +34,12 @@
<target name="preinstall"> <echo>Deploying Test Bundle v1.0 to ${rhq.deploy.dir}...</echo> - <property name="preinstallTargetExecuted" value="true"/> + <property name="preinstallTargetExecuted" value="1a"/> </target>
<target name="postinstall"> <echo>Done deploying Test Bundle v1.0 to ${rhq.deploy.dir}.</echo> - <property name="postinstallTargetExecuted" value="true"/> + <property name="postinstallTargetExecuted" value="1b"/> </target>
</project> \ No newline at end of file diff --git a/modules/common/ant-bundle/src/test/resources/test-bundle-v2.xml b/modules/common/ant-bundle/src/test/resources/test-bundle-v2.xml index 6764c13..1bbe0d1 100644 --- a/modules/common/ant-bundle/src/test/resources/test-bundle-v2.xml +++ b/modules/common/ant-bundle/src/test/resources/test-bundle-v2.xml @@ -2,14 +2,14 @@
<project name="test-bundle" default="main" xmlns:rhq="antlib:org.rhq.bundle">
- <rhq:bundle name="example.com (JBoss EAP 4.3)" version="1.0" - description="example.com corporate website hosted on JBoss EAP 4.3"> + <rhq:bundle name="example.com (JBoss EAP 4.3)" version="2.5" + description="updated bundle">
<rhq:input-property name="listener.port" description="This is where the product will listen for incoming messages" required="true" - defaultValue="8080" + defaultValue="9090" type="integer"/>
<rhq:deployment-unit name="appserver" preinstallTarget="preinstall" postinstallTarget="postinstall"> @@ -33,13 +33,13 @@ <target name="main"/>
<target name="preinstall"> - <echo>Deploying Test Bundle v1.0 to ${rhq.deploy.dir}...</echo> - <property name="preinstallTargetExecuted" value="true"/> + <echo>Deploying Test Bundle v2.5 to ${rhq.deploy.dir}...</echo> + <property name="preinstallTargetExecuted" value="2a"/> </target>
<target name="postinstall"> - <echo>Done deploying Test Bundle v1.0 to ${rhq.deploy.dir}.</echo> - <property name="postinstallTargetExecuted" value="true"/> + <echo>Done deploying Test Bundle v2.5 to ${rhq.deploy.dir}.</echo> + <property name="postinstallTargetExecuted" value="2b"/> </target>
</project> \ No newline at end of file