org.testatoo.container.ContainerConfiguration Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2008 Ovea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testatoo.container;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static org.testatoo.container.TestatooProperties.*;
public final class ContainerConfiguration {
public static final int DEFAULT_PORT = 8080;
public static final String DEFAULT_CONTEXT_PATH = "/";
public static final String DEFAULT_WEBAPP_ROOT = "src/main/webapp";
final Properties properties = new Properties();
private ContainerConfiguration() {
port(DEFAULT_PORT);
context(DEFAULT_CONTEXT_PATH);
webappRoot(DEFAULT_WEBAPP_ROOT);
}
/* Fluent config */
public Container buildContainer(TestatooContainer type) {
return buildContainer(type.serverClass());
}
public Container buildContainer(String containerClass) {
notNull(containerClass, "Container type " + Arrays.deepToString(TestatooContainer.values()) + " or container class");
Class clazz = null;
for (TestatooContainer container : TestatooContainer.values()) {
if (container.name().equalsIgnoreCase(containerClass)) {
clazz = loadServerClass(container.serverClass());
break;
}
}
if (clazz == null) clazz = loadServerClass(containerClass);
try {
return clazz.getConstructor(ContainerConfiguration.class).newInstance(ContainerConfiguration.from(this));
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) throw (RuntimeException) e.getTargetException();
else throw new RuntimeException(e.getTargetException().getMessage(), e.getTargetException());
} catch (Exception e) {
throw new IllegalArgumentException("Cannot instanciate class [" + clazz + "]. Ensure there is a public constructor having a parameter of type " + ContainerConfiguration.class.getName(), e);
}
}
/* fluent config */
public ContainerConfiguration serverClassPath(String serverClassPath) {
notNull(serverClassPath, "Server classpath");
return serverClassPath(toUrl(serverClassPath));
}
public ContainerConfiguration serverClassPath(File... paths) {
notNull(paths, "Server classpath");
List urls = new ArrayList(paths.length);
for (File path : paths) urls.add(pathAsURL(path.getAbsolutePath()));
return serverClassPath(urls.toArray(new URL[urls.size()]));
}
public ContainerConfiguration serverClassPath(URL... locations) {
notNull(locations, "Server classpath");
properties.put(SERVER_CLASSPATH, locations);
return this;
}
public boolean hasServerClassPath() {
return properties.get(SERVER_CLASSPATH) != null;
}
public URL[] serverClassPath() {
URL[] locations = (URL[]) properties.get(SERVER_CLASSPATH);
return locations == null ? new URL[0] : locations;
}
public ContainerConfiguration webappClassPath(String webappClassPath) {
notNull(webappClassPath, "Webapp classpath");
return webappClassPath(toUrl(webappClassPath));
}
public ContainerConfiguration webappClassPath(File... paths) {
notNull(paths, "Webapp classpath");
List urls = new ArrayList(paths.length);
for (File path : paths) urls.add(pathAsURL(path.getAbsolutePath()));
return webappClassPath(urls.toArray(new URL[urls.size()]));
}
public ContainerConfiguration webappClassPath(URL... locations) {
notNull(locations, "Webapp classpath");
properties.put(WEBAPP_CLASSPATH, locations);
return this;
}
public boolean hasWebappClassPath() {
return properties.get(WEBAPP_CLASSPATH) != null;
}
public URL[] webappClassPath() {
URL[] locations = (URL[]) properties.get(WEBAPP_CLASSPATH);
return locations == null ? new URL[0] : locations;
}
public ContainerConfiguration webappRoot(String webappRoot) {
notNull(webappRoot, "Webapp root");
return webappRoot(new File(webappRoot));
}
public ContainerConfiguration webappRoot(File webappRoot) {
notNull(webappRoot, "Webapp root");
properties.put(WEBAPP_ROOT, webappRoot);
return this;
}
public File webappRoot() {
return (File) properties.get(WEBAPP_ROOT);
}
public ContainerConfiguration context(String contextPath) {
notNull(contextPath, "Webapp context path");
properties.put(CONTEXT, contextPath.startsWith("/") ? contextPath : "/" + contextPath);
return this;
}
public String context() {
return (String) properties.get(CONTEXT);
}
public ContainerConfiguration port(int port) {
if (port < 0 || port > 65535) throw new IllegalArgumentException("Invalid port: " + port);
properties.put(PORT, port);
return this;
}
public int port() {
return (Integer) properties.get(PORT);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ContainerConfiguration:\n");
for (Map.Entry