From 8e9d2d132cd74beb37ca31dc40cd20ee3d4352d7 Mon Sep 17 00:00:00 2001 From: Ivo Studensky Date: Sun, 18 Aug 2013 22:37:35 +0200 Subject: [PATCH 1/2] WFLY-1896 - CLI 'module remove' command now deletes files recursively --- .../cli/handlers/module/ASModuleHandler.java | 25 ++- testsuite/integration/basic/pom.xml | 8 + .../management/cli/ModuleTestCase.java | 157 ++++++++++++++++++ 3 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/management/cli/ModuleTestCase.java diff --git a/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java b/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java index ba4be1f410a9..ab6e08aab076 100644 --- a/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java +++ b/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java @@ -345,15 +345,10 @@ private void removeModule(ParsedCommandLine parsedCmd) throws CommandLineExcepti throw new CommandLineException("Failed to locate module " + moduleName + " at " + modulePath.getAbsolutePath()); } - final File[] moduleFiles = modulePath.listFiles(); - if(moduleFiles != null) { - for(File f : moduleFiles) { - if(!f.delete()) { - throw new CommandLineException("Failed to delete " + f.getAbsolutePath()); - } - } - } + // delete the whole slot directory + deleteRecursively(modulePath); + modulePath = modulePath.getParentFile(); while(!modulesDir.equals(modulePath)) { if(modulePath.list().length > 0) { break; @@ -365,6 +360,20 @@ private void removeModule(ParsedCommandLine parsedCmd) throws CommandLineExcepti } } + protected void deleteRecursively(final File file) throws CommandLineException { + if (file.isDirectory()) { + final File[] files = file.listFiles(); + if (files != null) { + for (File f : files) { + deleteRecursively(f); + } + } + } + if (!file.delete()) { + throw new CommandLineException("Failed to delete " + file.getAbsolutePath()); + } + } + protected File getModulePath(File modulesDir, final String moduleName, String slot) throws CommandLineException { return new File(modulesDir, moduleName.replace('.', File.separatorChar) + File.separatorChar + (slot == null ? "main" : slot)); } diff --git a/testsuite/integration/basic/pom.xml b/testsuite/integration/basic/pom.xml index 6c0d85bf1917..ac67b5b43cb2 100644 --- a/testsuite/integration/basic/pom.xml +++ b/testsuite/integration/basic/pom.xml @@ -239,6 +239,10 @@ org/jboss/as/test/integration/ee/injection/resource/basic/*TestCase.java + + ${jboss.dist} + + standalone-full.xml @@ -285,6 +289,10 @@ org/jboss/as/test/integration/ee/injection/resource/basic/*TestCase.java + + ${jboss.dist} + + standalone.xml diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/management/cli/ModuleTestCase.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/management/cli/ModuleTestCase.java new file mode 100644 index 000000000000..f65f8a410cff --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/management/cli/ModuleTestCase.java @@ -0,0 +1,157 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2013, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.as.test.integration.management.cli; + +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.as.test.integration.management.base.AbstractCliTestBase; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.impl.base.exporter.zip.ZipExporterImpl; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.xnio.IoUtils; + +import java.io.File; +import java.io.PrintWriter; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * This tests 'module add/remove' CLI command. + * + * @author Ivo Studensky + */ +@RunWith(Arquillian.class) +@RunAsClient +public class ModuleTestCase extends AbstractCliTestBase { + + private static final String MODULE_NAME = "org.jboss.test.cli.climoduletest"; + + private static File jarFile; + + @BeforeClass + public static void beforeClass() throws Exception { + final JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "Dummy.jar"); + jar.addClass(ModuleTestCase.class); + jarFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "Dummy.jar"); + new ZipExporterImpl(jar).exportTo(jarFile, true); + + AbstractCliTestBase.initCLI(); + } + + @AfterClass + public static void afterClass() throws Exception { + jarFile.delete(); + AbstractCliTestBase.closeCLI(); + } + + @Test + public void addRemoveModule() throws Exception { + testAddRemove("main"); + } + + @Test + public void addRemoveModuleNonDefaultSlot() throws Exception { + testAddRemove("2.0"); + } + + @Test + public void addRemoveModuleMetaInf() throws Exception { + testModuleFiles(false, "main"); + testAdd("main"); + testModuleFiles(true, "main"); + + // create a META-INF directory inside the module + final File metaInfDir = new File(getModulePath(), MODULE_NAME.replace('.', File.separatorChar) + File.separator + "main" + File.separator + "META-INF"); + if (!metaInfDir.mkdirs()) { + fail("Could not create " + metaInfDir); + } + PrintWriter out = null; + try { + out = new PrintWriter(new File(metaInfDir, "version.txt")); + out.println("main"); + } finally { + IoUtils.safeClose(out); + } + + testRemove("main"); + testModuleFiles(false, "main"); + } + + private void testAddRemove(String slotName) throws Exception { + testModuleFiles(false, slotName); + testAdd(slotName); + testModuleFiles(true, slotName); + testRemove(slotName); + testModuleFiles(false, slotName); + } + + private void testAdd(String slotName) throws Exception { + // create a module + cli.sendLine("module add --name=" + MODULE_NAME + + ("main".equals(slotName) ? "" : " --slot=" + slotName) + + " --resources=" + jarFile.getAbsolutePath()); + } + + private void testRemove(String slotName) throws Exception { + // remove the module + cli.sendLine("module remove --name=" + MODULE_NAME + + ("main".equals(slotName) ? "" : " --slot=" + slotName)); + } + + private void testModuleFiles(boolean ifExists, String slotName) throws Exception { + File testModuleRoot = new File(getModulePath(), MODULE_NAME.replace('.', File.separatorChar)); + assertTrue("Invalid state of module directory", ifExists == testModuleRoot.exists()); + + File slot = new File(testModuleRoot, slotName); + assertTrue("Invalid state of slot directory", ifExists == slot.exists()); + } + + private File getModulePath() { + String modulePath = System.getProperty("module.path", null); + if (modulePath == null) { + String jbossHome = System.getProperty("jboss.home", null); + if (jbossHome == null) { + throw new IllegalStateException( + "Neither -Dmodule.path nor -Djboss.home were set"); + } + modulePath = jbossHome + File.separatorChar + "modules"; + } else { + modulePath = modulePath.split(File.pathSeparator)[0]; + } + File moduleDir = new File(modulePath); + if (!moduleDir.exists()) { + throw new IllegalStateException( + "Determined module path does not exist"); + } + if (!moduleDir.isDirectory()) { + throw new IllegalStateException( + "Determined module path is not a dir"); + } + return moduleDir; + } + +} From 8998ee5d9a716ba80263a5e33775f0cd0545f505 Mon Sep 17 00:00:00 2001 From: Ivo Studensky Date: Wed, 21 Aug 2013 11:07:05 +0200 Subject: [PATCH 2/2] WFLY-1913 fixing module add command with letter drive in its resource path on Windows --- .../java/org/jboss/as/cli/handlers/module/ASModuleHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java b/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java index ab6e08aab076..4f6084f5a791 100644 --- a/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java +++ b/cli/src/main/java/org/jboss/as/cli/handlers/module/ASModuleHandler.java @@ -243,7 +243,7 @@ protected void addModule(CommandContext ctx, final ParsedCommandLine parsedCmd) final String[] resourceArr = (resourcePaths == null) ? new String[0] : resourcePaths.split(pathDelimiter); File[] resourceFiles = new File[resourceArr.length]; for(int i = 0; i < resourceArr.length; ++i) { - final File f = new File(ctx.getCurrentDir(), resourceArr[i]); + final File f = new File(resourceArr[i]); if(!f.exists()) { throw new CommandLineException("Failed to locate " + f.getAbsolutePath()); }