Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFLY-1896, WFLY1913 - CLI 'module remove' command now deletes files recursively #4934

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down
8 changes: 8 additions & 0 deletions testsuite/integration/basic/pom.xml
Expand Up @@ -239,6 +239,10 @@
<include>org/jboss/as/test/integration/ee/injection/resource/basic/*TestCase.java</include>
</includes>

<environmentVariables>
<JBOSS_HOME>${jboss.dist}</JBOSS_HOME>
</environmentVariables>

<!-- Parameters to test cases. -->
<systemPropertyVariables combine.children="append">
<jboss.server.config.file.name>standalone-full.xml</jboss.server.config.file.name>
Expand Down Expand Up @@ -285,6 +289,10 @@
<exclude>org/jboss/as/test/integration/ee/injection/resource/basic/*TestCase.java</exclude>
</excludes>

<environmentVariables>
<JBOSS_HOME>${jboss.dist}</JBOSS_HOME>
</environmentVariables>

<!-- Parameters to test cases. -->
<systemPropertyVariables combine.children="append">
<jboss.server.config.file.name>standalone.xml</jboss.server.config.file.name>
Expand Down
@@ -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;
}

}