Skip to content

Commit

Permalink
Add suitable config for running tests incontainer from an IDE
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1688 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Feb 24, 2009
1 parent 2a7752b commit 685f315
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 22 deletions.
Expand Up @@ -2,7 +2,6 @@

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -16,6 +15,7 @@
import org.jboss.jsr299.tck.api.Configurable;
import org.jboss.jsr299.tck.api.Configuration;
import org.jboss.jsr299.tck.spi.Containers;
import org.jboss.webbeans.tck.integration.jbossas.util.DeploymentProperties;

/**
*
Expand All @@ -36,12 +36,19 @@ public abstract class AbstractContainersImpl implements Configurable, Containers

private static Logger log = Logger.getLogger(AbstractContainersImpl.class);

private final DeploymentProperties properties;

private Configuration configuration;
protected String jbossHome;
private String jbossHttpUrl;
private boolean jbossWasStarted;
private long bootTimeout;
private String javaOpts;

public AbstractContainersImpl()
{
this.properties = new DeploymentProperties();
}

protected static void copy(InputStream inputStream, File file) throws IOException
{
Expand Down Expand Up @@ -94,9 +101,10 @@ protected boolean isJBossUp()

public void setup() throws IOException
{
if (System.getProperty(JBOSS_AS_DIR_PROPERTY_NAME) != null)
String jbossAsPath = properties.getStringValue(JBOSS_AS_DIR_PROPERTY_NAME, "../jboss-as", false);
if (jbossAsPath != null)
{
File jbossAsDir = new File(System.getProperty(JBOSS_AS_DIR_PROPERTY_NAME));
File jbossAsDir = new File(jbossAsPath);
if (jbossAsDir.isDirectory())
{
File buildProperties = new File(jbossAsDir, "build.properties");
Expand All @@ -111,26 +119,14 @@ public void setup() throws IOException
}
}
}
jbossHome = System.getProperty(JBOSS_HOME_PROPERTY_NAME);
javaOpts = System.getProperty(JAVA_OPTS_PROPERTY_NAME);
if (javaOpts == null)
{
javaOpts = "";
}
jbossHome = properties.getStringValue(JBOSS_HOME_PROPERTY_NAME, null, true);
javaOpts = properties.getStringValue(JAVA_OPTS_PROPERTY_NAME, "", false);
javaOpts = javaOpts + JAVA_OPTS;
if (jbossHome == null)
{
throw new IllegalArgumentException("-D" + JBOSS_HOME_PROPERTY_NAME + " must be set");
}
else
{
jbossHome = System.getProperty(JBOSS_HOME_PROPERTY_NAME);
File jbossHomeFile = new File(jbossHome);
jbossHome = jbossHomeFile.getPath();
log.info("Using JBoss instance in " + jbossHome + " at URL " + configuration.getHost());
}
this.bootTimeout = Long.getLong(JBOSS_BOOT_TIMEOUT_PROPERTY_NAME, 240000);
if (Boolean.getBoolean(FORCE_RESTART_PROPERTY_NAME))
File jbossHomeFile = new File(jbossHome);
jbossHome = jbossHomeFile.getPath();
log.info("Using JBoss instance in " + jbossHome + " at URL " + configuration.getHost());
this.bootTimeout = properties.getLongValue(JBOSS_BOOT_TIMEOUT_PROPERTY_NAME, 240000, false);
if (properties.getBooleanValue(FORCE_RESTART_PROPERTY_NAME, false, false))
{
if (isJBossUp())
{
Expand Down
@@ -0,0 +1,233 @@
package org.jboss.webbeans.tck.integration.jbossas.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

/**
* Utility class to load deployment properties
*
* @author Pete Muir
*/
public class DeploymentProperties
{
// The resource bundle used to control Web Beans RI deployment
public static final String RESOURCE_BUNDLE = "META-INF/web-beans-tck.properties";

private static final Logger log = Logger.getLogger(DeploymentProperties.class);

// The class to work from
private SimpleResourceLoader resourceLoader;

/**
* Constructor
*
* @param classLoader The classloader to work on
*/
public DeploymentProperties()
{
this.resourceLoader = new SimpleResourceLoader();
}

/**
* Get a list of possible values for a given key.
*
* First, System properties are tried, followed by the specified resource
* bundle (first in classpath only).
*
* @param key The key to search for
* @return A list of possible values. An empty list is returned if there are
* no matches.
*/
public List<String> getPropertyValues(String key)
{
List<String> values = new ArrayList<String>();
addPropertiesFromSystem(key, values);
addPropertiesFromResourceBundle(key, values);
return values;
}

/**
* Adds matches from system properties
*
* @param key The key to match
* @param values The currently found values
*/
private void addPropertiesFromSystem(String key, List<String> values)
{
addProperty(key, System.getProperty(key), values);
}

/**
* Adds matches from detected resource bundles
*
* @param key The key to match
* @param values The currently found values
*/
private void addPropertiesFromResourceBundle(String key, List<String> values)
{
try
{
for (URL url : resourceLoader.getResources(RESOURCE_BUNDLE))
{
Properties properties = new Properties();
InputStream propertyStream = url.openStream();
try
{
properties.load(propertyStream);
addProperty(key, properties.getProperty(key), values);
}
finally
{
if (propertyStream != null)
{
propertyStream.close();
}
}
}
}
catch (IOException e)
{
// No - op, file is optional
}
}

/**
* Add the property to the set of properties only if it hasn't already been
* added
*
* @param key The key searched for
* @param value The value of the property
* @param values The currently found values
*/
private void addProperty(String key, String value, List<String> values)
{
if (value != null)
{
values.add(value);
}

}

/**
* Gets the possible implementation class for a given property for which the
* values are classanames
*
* @param deploymentProperties The deployment properties object to use
* @param resourceLoader The resource laoder to use to attempt
* @param propertyName The name of the property to load
* @return A set of classes specified
*/
@SuppressWarnings("unchecked")
public <T> Set<Class<T>> getClasses(String propertyName, Class<T> expectedType)
{
Set<Class<T>> classes = new HashSet<Class<T>>();
for (String className : getPropertyValues(propertyName))
{
try
{
classes.add((Class<T>) resourceLoader.classForName(className));
}
catch (ResourceLoadingException e)
{
//log.debug("Unable to load class " + className + " for property " + propertyName, e);
}
}
return classes;
}

public <T> Class<T> getClassValue(String propertyName, Class<T> expectedType, boolean required)
{
Set<Class<T>> classes = getClasses(propertyName, expectedType);
if (classes.size() == 0)
{
if (required)
{
throw new IllegalArgumentException("Cannot find any implementations of " + expectedType.getSimpleName() + ", check that " + propertyName + " is specified");
}
else
{
return null;
}
}
else if (classes.size() > 1)
{
throw new IllegalArgumentException("More than one implementation of " + expectedType.getSimpleName() + " specified by " + propertyName + ", not sure which one to use!");
}
else
{
return classes.iterator().next();
}
}

public <T> T getInstanceValue(String propertyName, Class<T> expectedType, boolean required)
{
Class<T> clazz = getClassValue(propertyName, expectedType, required);
if (clazz != null)
{
try
{
return clazz.newInstance();
}
catch (InstantiationException e)
{
throw new IllegalStateException("Error instantiating " + clazz + " specified by " + propertyName, e);
}
catch (IllegalAccessException e)
{
throw new IllegalStateException("Error instantiating " + clazz + " specified by " + propertyName, e);
}
}
else
{
return null;
}
}

public boolean getBooleanValue(String propertyName, boolean _default, boolean required)
{
return Boolean.valueOf(getStringValue(propertyName, _default ? "true" : "false", required));
}

public int getIntValue(String propertyName, int _default, boolean required)
{
return Integer.valueOf(getStringValue(propertyName, Integer.toString(_default), required)).intValue();
}

public long getLongValue(String propertyName, long _default, boolean required)
{
return Long.valueOf(getStringValue(propertyName, Long.toString(_default), required)).longValue();
}

public String getStringValue(String propertyName, String _default, boolean required)
{
List<String> values = getPropertyValues(propertyName);
if (values.size() == 0)
{
if (required)
{
throw new IllegalArgumentException("Cannot find required property " + propertyName + ", check that it is specified");
}
else
{
return _default;
}
}
else if (values.size() > 1)
{
throw new IllegalArgumentException("More than one value given for " + propertyName + ", not sure which one to use!");
}
else
{
return values.iterator().next();
}
}

}
@@ -0,0 +1,37 @@
package org.jboss.webbeans.tck.integration.jbossas.util;

import java.util.Enumeration;
import java.util.Iterator;

/**
* An Enumeration -> Iteratble adaptor
*
* @author Pete Muir
* @see org.jboss.webbeans.util.EnumerationIterator
*/
class EnumerationIterable<T> implements Iterable<T>
{
// The enumeration-iteartor
private EnumerationIterator<T> iterator;

/**
* Constructor
*
* @param enumeration The enumeration
*/
public EnumerationIterable(Enumeration<T> enumeration)
{
this.iterator = new EnumerationIterator<T>(enumeration);
}

/**
* Gets an iterator
*
* @return The iterator
*/
public Iterator<T> iterator()
{
return iterator;
}

}

0 comments on commit 685f315

Please sign in to comment.