Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.domain.management.util;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
 
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
 import java.util.Map;
 
Utilities for running tests of domain mode.

Author(s):
Brian Stansberry (c) 2011 Red Hat Inc.
 
 public class DomainTestSupport {
 
 
     private static final Logger log = Logger.getLogger("org.jboss.as.test.integration.domain");
 
     public static final String masterAddress = System.getProperty("jboss.test.host.master.address""127.0.0.1");
     public static final String slaveAddress = System.getProperty("jboss.test.host.slave.address""127.0.0.1");
     public static final long domainBootTimeout = Long.valueOf(System.getProperty("jboss.test.domain.boot.timeout""60000"));
     public static final long domainShutdownTimeout = Long.valueOf(System.getProperty("jboss.test.domain.shutdown.timeout""20000"));
     public static final String masterJvmHome = System.getProperty("jboss.test.host.master.jvmhome");
     public static final String slaveJvmHome = System.getProperty("jboss.test.host.slave.jvmhome");
     public static final String masterControllerJvmHome = System.getProperty("jboss.test.host.master.controller.jvmhome");
     public static final String slaveControllerJvmHome = System.getProperty("jboss.test.host.slave.controller.jvmhome");

    
Create and start a default configuration for the domain tests.

Parameters:
testName the test name
Returns:
a started domain test support
 
     public static DomainTestSupport createAndStartDefaultSupport(final String testName) {
         try {
             final Configuration configuration = DomainTestSupport.Configuration.create(testName,
                     "domain-configs/domain-standard.xml""host-configs/host-master.xml""host-configs/host-slave.xml");
             final DomainTestSupport testSupport = DomainTestSupport.create(configuration);
             // Start!
             testSupport.start();
             return testSupport;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }

    
Create and start a configuration for the domain tests

Parameters:
configuration the configuration specification
Returns:
a started domain test support
 
     public static DomainTestSupport createAndStartSupport(Configuration configuration) {
         try {
             final DomainTestSupport testSupport = DomainTestSupport.create(configuration);
             // Start!
             testSupport.start();
             return testSupport;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static WildFlyManagedConfiguration getMasterConfiguration(String domainConfigPathString hostConfigPath,
                                                                     String testNameboolean readOnlyDomainboolean readOnlyHost) {
        return getMasterConfiguration(domainConfigPathhostConfigPathtestNamenullreadOnlyDomainreadOnlyHost);
    }
    public static WildFlyManagedConfiguration getMasterConfiguration(String domainConfigPathString hostConfigPath,
                String testNameWildFlyManagedConfiguration baseConfig,
                boolean readOnlyDomainboolean readOnlyHost) {
        return Configuration.getMasterConfiguration(domainConfigPathhostConfigPathtestNamebaseConfigreadOnlyDomainreadOnlyHost);
    }
    public static WildFlyManagedConfiguration getSlaveConfiguration(String hostConfigPathString testName,
                                                                    boolean readOnlyHost) {
        return getSlaveConfiguration("slave"hostConfigPathtestNamenew WildFlyManagedConfiguration(), readOnlyHost);
    }
    public static WildFlyManagedConfiguration getSlaveConfiguration(String hostNameString hostConfigPathString testName,
                                                                    boolean readOnlyHost) {
        return getSlaveConfiguration(hostNamehostConfigPathtestNamenew WildFlyManagedConfiguration(), readOnlyHost);
    }
    public static WildFlyManagedConfiguration getSlaveConfiguration(String hostConfigPathString testName,
                                                                    WildFlyManagedConfiguration baseConfig,
                                                                    boolean readOnlyHost) {
        return getSlaveConfiguration("slave"hostConfigPathtestNamebaseConfigreadOnlyHost);
    }
    public static WildFlyManagedConfiguration getSlaveConfiguration(String hostNameString hostConfigPathString testName,
                                                                    WildFlyManagedConfiguration baseConfig,
                                                                    boolean readOnlyHost) {
        return Configuration.getSlaveConfiguration(hostConfigPathtestNamehostNamebaseConfigreadOnlyHost);
    }
    private static URI toURI(URL url) {
        try {
            return url.toURI();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    public static void startHosts(long timeoutDomainLifecycleUtil... hoststhrows Exception {
        Future<?>[] futures = new Future<?>[hosts.length];
        for (int i = 0; i < hosts.lengthi++) {
            futures[i] = hosts[i].startAsync();
        }
        processFutures(futurestimeout);
    }
    public static void stopHosts(long timeoutDomainLifecycleUtil... hoststhrows Exception {
        Future<?>[] futures = new Future<?>[hosts.length];
        for (int i = 0; i < hosts.lengthi++) {
            futures[i] = hosts[i].stopAsync();
        }
        processFutures(futurestimeout);
    }
    public static File getBaseDir(String testName) {
        return new File("target" + . + "domains" + . + testName);
    }
    public static File getHostDir(String testNameString hostName) {
        return new File(getBaseDir(testName), hostName);
    }
    public static File getAddedModulesDir(String testName) {
        File f = new File(getBaseDir(testName), "added-modules");
        f.mkdirs();
        return f;
    }
    public static File getHostOverrideModulesDir(String testNameString hostName) {
        final File f = new File(getHostDir(testNamehostName), "added-modules");
        f.mkdirs();
        return f;
    }
    public static ModelNode createOperationNode(String addressString operation) {
        ModelNode op = new ModelNode();
        // set address
        ModelNode list = op.get("address").setEmptyList();
        if (address != null) {
            String [] pathSegments = address.split("/");
            for (String segment : pathSegments) {
                String[] elements = segment.split("=");
                list.add(elements[0], elements[1]);
            }
        }
        op.get("operation").set(operation);
        return op;
    }
    public static ModelNode validateResponse(ModelNode response) {
        return validateResponse(responsetrue);
    }
    public static ModelNode validateResponse(ModelNode responseboolean getResult) {
        if(! .equals(response.get().asString())) {
            ..println("Failed response:");
            ..println(response);
            Assert.fail(response.get().toString());
        }
        if (getResult) {
            Assert.assertTrue("result exists"response.has());
            return response.get();
        }
        return null;
    }
    public static ModelNode validateFailedResponse(ModelNode response) {
        if(! .equals(response.get().asString())) {
            ..println("Response succeeded:");
            ..println(response);
            Assert.fail(response.get().toString());
        }
        Assert.assertTrue("failure description exists"response.has());
        return response.get();
    }
    public static void cleanFile(File file) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                for (File child : file.listFiles()) {
                    cleanFile(child);
                }
            }
            if (!file.delete()) {
                file.deleteOnExit();
            }
        }
    }
    public static void safeClose(final Closeable closeable) {
        if (closeable != nulltry {
            closeable.close();
        } catch (Throwable t) {
            .errorf(t"Failed to close resource %s"closeable);
        }
    }
    private static void processFutures(Future<?>[] futureslong timeoutthrows Exception {
        try {
            for (int i = 0; i < futures.lengthi++) {
                try {
                    futures[i].get(timeout.);
                }  catch (ExecutionException e){
                    throw e.getCause();
                }
            }
        } catch (Exception e) {
            throw e;
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }
    private static void configureModulePath(WildFlyManagedConfiguration configFile... extraModules) {
        String basePath = config.getModulePath();
        if (basePath == null || basePath.isEmpty()) {
            basePath = config.getJbossHome() + . + "modules";
        }
        final StringBuilder path = new StringBuilder();
        for(final File extraModule : extraModules) {
            path.append(extraModule.getAbsolutePath()).append(.);
        }
        path.append(basePath);
        config.setModulePath(path.toString());
    }
    private final String testClass;
    protected DomainTestSupport(final String testClassfinal String domainConfigfinal String masterConfig,
                                final String slaveConfigWildFlyManagedConfiguration masterBase,
                                final WildFlyManagedConfiguration slaveBasethrows Exception {
        this(testClassdomainConfigmasterConfigslaveConfigmasterBaseslaveBasefalsefalsefalse);
    }
    protected DomainTestSupport(final String testClassfinal String domainConfigfinal String masterConfig,
                                final String slaveConfigWildFlyManagedConfiguration masterBase,
                                final WildFlyManagedConfiguration slaveBasefinal boolean readOnlyDomainConfig,
                                final boolean readOnlyMasterHostConfigfinal boolean readOnlySlaveHostConfigthrows Exception {
        this(testClassgetMasterConfiguration(domainConfigmasterConfigtestClassmasterBasereadOnlyDomainConfigreadOnlyMasterHostConfig),
                slaveConfig == null ? null : getSlaveConfiguration(slaveConfigtestClassslaveBasereadOnlySlaveHostConfig));
    }
    protected DomainTestSupport(final String testClassfinal WildFlyManagedConfiguration masterConfiguration,
                                final WildFlyManagedConfiguration slaveConfigurationthrows Exception {
        this. = testClass;
        this. = DomainControllerClientConfig.create();
        this. = masterConfiguration;
        this. = new DomainLifecycleUtil(masterConfiguration);
        this. = slaveConfiguration;
        if (slaveConfiguration != null) {
            this. = new DomainLifecycleUtil(slaveConfiguration);
        } else {
            this. = null;
        }
    }
    public static DomainTestSupport create(final Configuration configurationthrows Exception {
        return new DomainTestSupport(configuration.getTestName(), configuration.getMasterConfiguration(), configuration.getSlaveConfiguration());
    }
    public static DomainTestSupport create(final String testClassfinal Configuration configurationthrows Exception {
        return new DomainTestSupport(testClassconfiguration.getMasterConfiguration(), configuration.getSlaveConfiguration());
    }
    public static DomainTestSupport create(final String testClassfinal WildFlyManagedConfiguration masterConfiguration,
                                           final WildFlyManagedConfiguration slaveConfigurationthrows Exception {
        return new DomainTestSupport(testClassmasterConfigurationslaveConfiguration);
    }
        return ;
    }
        return ;
    }
        return ;
    }
        return ;
    }
    public void start() {
        if ( != null) {
            .start();
        }
    }
    public void addTestModule(String moduleNameInputStream moduleXmlMap<StringStreamExportercontentsthrows IOException {
        File modulesDir = getAddedModulesDir();
        addModule(modulesDirmoduleNamemoduleXmlcontents);
    }
    public void addOverrideModule(String hostNameString moduleNameInputStream moduleXmlMap<StringStreamExportercontentsthrows IOException {
        File modulesDir = getHostOverrideModulesDir(hostName);
        addModule(modulesDirmoduleNamemoduleXmlcontents);
    }
    static void addModule(final File modulesDirString moduleNameInputStream moduleXmlMap<StringStreamExporterresourcesthrows IOException {
        String modulePath = moduleName.replace('.'.) + . + "main";
        File moduleDir = new File(modulesDirmodulePath);
        moduleDir.mkdirs();
        FileUtils.copyFile(moduleXmlnew File(moduleDir"module.xml"));
        for (Map.Entry<StringStreamExporterentry : resources.entrySet()) {
            entry.getValue().exportTo(new File(moduleDirentry.getKey()), true);
        }
    }
    public void stop() {
        try {
            try {
                if ( != null) {
                    .stop();
                }
            } finally {
                .stop();
            }
        } finally {
            StreamUtils.safeClose();
        }
    }
    public static class Configuration {
        private final String testName;
        private final WildFlyManagedConfiguration masterConfiguration;
        private final WildFlyManagedConfiguration slaveConfiguration;
        protected Configuration(final String testNameWildFlyManagedConfiguration masterConfiguration,
                                WildFlyManagedConfiguration slaveConfiguration) {
            this. = testName;
            this. = masterConfiguration;
            this. = slaveConfiguration;
        }
        public String getTestName() {
            return ;
        }
            return ;
        }
            return ;
        }
        public static Configuration create(final String testNamefinal String domainConfigfinal String masterConfigfinal String slaveConfig) {
            return create(testNamedomainConfigmasterConfigslaveConfigfalsefalsefalse);
        }
        public static Configuration create(final String testNamefinal String domainConfigfinal String masterConfig,
                                           final String slaveConfig,
                                           boolean readOnlyMasterDomainboolean readOnlyMasterHost,
                                           boolean readOnlySlaveHost) {
            WildFlyManagedConfiguration masterConfiguration = getMasterConfiguration(domainConfigmasterConfigtestNamenullreadOnlyMasterDomainreadOnlyMasterHost);
            WildFlyManagedConfiguration slaveConfiguration = slaveConfig == null ? null : getSlaveConfiguration(slaveConfigtestName"slave"nullreadOnlySlaveHost);
            return new Configuration(testNamemasterConfigurationslaveConfiguration);
        }
        private static WildFlyManagedConfiguration getMasterConfiguration(String domainConfigPathString hostConfigPath,
                                                                         String testNameWildFlyManagedConfiguration baseConfig,
                                                                         boolean readOnlyDomainboolean readOnlyHost) {
            final String hostName = "master";
            File domains = getBaseDir(testName);
            File extraModules = getAddedModulesDir(testName);
            File overrideModules = getHostOverrideModulesDir(testNamehostName);
            ClassLoader tccl = Thread.currentThread().getContextClassLoader();
            final WildFlyManagedConfiguration masterConfig = baseConfig == null ? new WildFlyManagedConfiguration() : baseConfig;
            configureModulePath(masterConfigoverrideModulesextraModules);
            masterConfig.setHostControllerManagementAddress();
            masterConfig.setHostCommandLineProperties("-Djboss.test.host.master.address=" + );
            masterConfig.setReadOnlyDomain(readOnlyDomain);
            masterConfig.setReadOnlyHost(readOnlyHost);
            URL url = tccl.getResource(domainConfigPath);
            assert url != null : "cannot find domainConfigPath";
            masterConfig.setDomainConfigFile(new File(toURI(url)).getAbsolutePath());
            url = tccl.getResource(hostConfigPath);
            assert url != null : "cannot find hostConfigPath";
            masterConfig.setHostConfigFile(new File(toURI(url)).getAbsolutePath());
            File masterDir = new File(domainshostName);
            // TODO this should not be necessary
            new File(masterDir"configuration").mkdirs();
            masterConfig.setDomainDirectory(masterDir.getAbsolutePath());
            if ( != nullmasterConfig.setJavaHome();
            if ( != nullmasterConfig.setControllerJavaHome();
            return masterConfig;
        }
        private static WildFlyManagedConfiguration getSlaveConfiguration(String hostConfigPathString testName,
                                                                         String hostNameWildFlyManagedConfiguration baseConfig,
                                                                         boolean readOnlyHost) {
            File domains = getBaseDir(testName);
            File extraModules = getAddedModulesDir(testName);
            File overrideModules = getHostOverrideModulesDir(testNamehostName);
            ClassLoader tccl = Thread.currentThread().getContextClassLoader();
            final WildFlyManagedConfiguration slaveConfig = baseConfig == null ? new WildFlyManagedConfiguration() : baseConfig;
            configureModulePath(slaveConfigoverrideModulesextraModules);
            slaveConfig.setHostName(hostName);
            slaveConfig.setHostControllerManagementAddress();
            slaveConfig.setHostControllerManagementPort(19999);
            slaveConfig.setHostCommandLineProperties("-Djboss.test.host.master.address=" +  +
                    " -Djboss.test.host.slave.address=" + );
            slaveConfig.setReadOnlyHost(readOnlyHost);
            URL url = tccl.getResource(hostConfigPath);
            slaveConfig.setHostConfigFile(new File(toURI(url)).getAbsolutePath());
            File slaveDir = new File(domainshostName);
            // TODO this should not be necessary
            new File(slaveDir"configuration").mkdirs();
            slaveConfig.setDomainDirectory(slaveDir.getAbsolutePath());
            if ( != nullslaveConfig.setJavaHome();
            if ( != nullslaveConfig.setControllerJavaHome();
            return slaveConfig;
        }
    }
New to GrepCode? Check out our FAQ X