-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ModuleTestCase.java
157 lines (136 loc) · 5.43 KB
/
ModuleTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
}
}