Skip to content

Commit

Permalink
[resolves #551] Allow standalone tests to get executed against runnin…
Browse files Browse the repository at this point in the history
…g server
  • Loading branch information
jamesnetherton committed Apr 17, 2015
1 parent 58e026f commit b8b564b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
package org.wildfly.camel.test.classloading;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -42,6 +44,7 @@
import org.jboss.modules.management.DependencyInfo;
import org.jboss.modules.management.ModuleLoaderMXBean;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -51,9 +54,10 @@
@RunWith(Arquillian.class)
public class ExportedPathsTest {

private static final String FILE_BASEDIR = "basedir.txt";
private static final String FILE_EXPORTED_PATH_PATTERNS = "exported-path-patterns.txt";
private static final File FILE_EXPORTED_PATHS = new File("../../modules/etc/baseline/exported-paths.txt");
private static final File FILE_MODULE_INFOS = new File("target/module-infos.txt");
private static final Path FILE_EXPORTED_PATHS = Paths.get("../../modules/etc/baseline/exported-paths.txt");
private static final Path FILE_MODULE_INFOS = Paths.get("target/module-infos.txt");

private static final String MODULE_LOADER_OBJECT_NAME = "jboss.modules:type=ModuleLoader,name=LocalModuleLoader-2";
private static final String MODULE_CAMEL_COMPONENT = "org.apache.camel.component";
Expand All @@ -63,6 +67,7 @@ public class ExportedPathsTest {
public static JavaArchive deployment() {
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "exported-paths-tests");
archive.addAsResource("classloading/" + FILE_EXPORTED_PATH_PATTERNS, FILE_EXPORTED_PATH_PATTERNS);
archive.addAsResource(new StringAsset(System.getProperty("basedir")), FILE_BASEDIR);
return archive;
}

Expand All @@ -72,19 +77,31 @@ public void setUp() throws Exception {
ObjectName oname = ObjectNameFactory.create(MODULE_LOADER_OBJECT_NAME);
ModuleLoaderMXBean mbean = JMX.newMXBeanProxy(server, oname, ModuleLoaderMXBean.class);

PrintWriter pw = new PrintWriter(new FileWriter(FILE_MODULE_INFOS));
Path moduleInfos = resolvePath(FILE_MODULE_INFOS);
PrintWriter pw = new PrintWriter(new FileWriter(moduleInfos.toFile()));
for (String module : new String[] { MODULE_CAMEL, MODULE_CAMEL_COMPONENT }) {
pw.println(mbean.dumpModuleInformation(module));
for (DependencyInfo depinfo : mbean.getDependencies(module)) {
String moduleName = depinfo.getModuleName();
if (moduleName != null) {
pw.println(mbean.dumpModuleInformation(moduleName));
pw.println("[Exported Paths: " + moduleName + "]");
List<String> modulePaths = new ArrayList<>(mbean.getModulePathsInfo(moduleName, true).keySet());
Collections.sort(modulePaths);
for (String path : modulePaths) {
if (path.contains("/") && !path.equals("org/apache")) {
pw.println(path);
}
}
pw.println();
}
}
}
pw.flush();
pw.close();

pw = new PrintWriter(new FileWriter(FILE_EXPORTED_PATHS));
Path exportedPaths = resolvePath(FILE_EXPORTED_PATHS);
pw = new PrintWriter(new FileWriter(exportedPaths.toFile()));
for (String module : new String[] { MODULE_CAMEL, MODULE_CAMEL_COMPONENT }) {
pw.println("[Exported Paths: " + module + "]");
List<String> modulePaths = new ArrayList<>(mbean.getModulePathsInfo(module, true).keySet());
Expand Down Expand Up @@ -130,7 +147,8 @@ public void testExportedPaths() throws Exception {
}

// Verify each line
reader = new BufferedReader(new FileReader(FILE_EXPORTED_PATHS));
Path exportedPaths = resolvePath(FILE_EXPORTED_PATHS);
reader = new BufferedReader(new FileReader(exportedPaths.toFile()));
try {
String line = reader.readLine();
while (line != null) {
Expand Down Expand Up @@ -163,4 +181,13 @@ public void testExportedPaths() throws Exception {
reader.close();
}
}

private Path resolvePath(Path other) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/" + FILE_BASEDIR)));
try {
return Paths.get(reader.readLine()).resolve(other);
} finally {
reader.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

package org.wildfly.camel.test.ftp;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
Expand All @@ -34,15 +38,20 @@
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.apache.ftpserver.usermanager.impl.WriteRequest;
import org.apache.mina.filter.executor.OrderedThreadPoolExecutor;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -53,15 +62,17 @@
@RunWith(Arquillian.class)
public class FtpIntegrationTest {

static final File FTP_ROOT_DIR = new File("./target/res/home");
static final File USERS_FILE = new File("./src/test/resources/users.properties");
static final int PORT = AvailablePortFinder.getNextAvailable(21000);
private static final String FILE_BASEDIR = "basedir.txt";
private static final Path FTP_ROOT_DIR = Paths.get(System.getProperty("jboss.server.data.dir") + "/ftp");
private static final Path USERS_FILE = Paths.get(System.getProperty("jboss.server.config.dir") + "/users.properties");
private static final int PORT = AvailablePortFinder.getNextAvailable(21000);

FtpServer ftpServer;
private FtpServer ftpServer;

@Deployment
public static WebArchive createdeployment() throws IOException {
final WebArchive archive = ShrinkWrap.create(WebArchive.class, "camel-ftp-tests.war");
archive.addAsResource(new StringAsset(System.getProperty("basedir")), FILE_BASEDIR);
addJarHolding(archive, PasswordEncryptor.class);
addJarHolding(archive, AvailablePortFinder.class);
addJarHolding(archive, OrderedThreadPoolExecutor.class);
Expand All @@ -70,19 +81,33 @@ public static WebArchive createdeployment() throws IOException {

@Before
public void startFtpServer() throws Exception {
recursiveDelete(FTP_ROOT_DIR);
System.out.println(new File(".").getCanonicalPath());
assertTrue(new File(".").getCanonicalPath(), USERS_FILE.exists());
recursiveDelete(resolvePath(FTP_ROOT_DIR).toFile());

File usersFile = USERS_FILE.toFile();
usersFile.createNewFile();

NativeFileSystemFactory fsf = new NativeFileSystemFactory();
fsf.setCreateHome(true);

PropertiesUserManagerFactory pumf = new PropertiesUserManagerFactory();
pumf.setAdminName("admin");
pumf.setPasswordEncryptor(new ClearTextPasswordEncryptor());
pumf.setFile(USERS_FILE);
pumf.setFile(usersFile);

UserManager userMgr = pumf.createUserManager();

BaseUser user = new BaseUser();
user.setName("admin");
user.setPassword("admin");
user.setHomeDirectory(FTP_ROOT_DIR.toString());

List<Authority> authorities = new ArrayList<>();
WritePermission writePermission = new WritePermission();
writePermission.authorize(new WriteRequest());
authorities.add(writePermission);
user.setAuthorities(authorities);
userMgr.save(user);

ListenerFactory factory1 = new ListenerFactory();
factory1.setPort(PORT);

Expand Down Expand Up @@ -114,7 +139,7 @@ public void stopFtpServer() throws Exception {
@Test
public void testSendFile() throws Exception {

File testFile = new File(FTP_ROOT_DIR, "foo/test.txt");
File testFile = resolvePath(FTP_ROOT_DIR).resolve("foo/test.txt").toFile();

CamelContext camelctx = new DefaultCamelContext();
try {
Expand All @@ -139,7 +164,17 @@ public void testComponentLoads() throws Exception {
}
}

private static void recursiveDelete(File file) {
private static void addJarHolding(WebArchive archive, Class<?> clazz) {
URL location = clazz.getProtectionDomain().getCodeSource().getLocation();
if (location != null && location.getProtocol().equals("file")) {
File path = new File(location.getPath());
if (path.isFile()) {
archive.addAsLibrary(path);
}
}
}

private void recursiveDelete(File file) {
if (file.exists()) {
if (file.isDirectory()) {
File[] files = file.listFiles();
Expand All @@ -153,14 +188,12 @@ private static void recursiveDelete(File file) {
}
}

private static void addJarHolding(WebArchive archive, Class<?> clazz) {
URL location = clazz.getProtectionDomain().getCodeSource().getLocation();
if (location != null && location.getProtocol().equals("file")) {
File path = new File(location.getPath());
if (path.isFile()) {
System.out.println("Adding jar lib to war: " + path);
archive.addAsLibrary(path);
}
private Path resolvePath(Path other) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/" + FILE_BASEDIR)));
try {
return Paths.get(reader.readLine()).resolve(other);
} finally {
reader.close();
}
}
}
23 changes: 0 additions & 23 deletions itests/standalone/src/test/resources/users.properties

This file was deleted.

0 comments on commit b8b564b

Please sign in to comment.