org.coos.messaging.plugin.test.DefaultTest Maven / Gradle / Ivy
The newest version!
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging.plugin.test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.coos.messaging.COContainer;
import org.coos.messaging.COOS;
import org.coos.messaging.COOSBootstrapHelper;
import org.coos.messaging.Plugin;
import org.coos.messaging.PluginFactory;
/**
* A Default testclass to be extended
*
* @author Knut Eilif Husa, Tellu AS
*/
public abstract class DefaultTest extends TestCase implements COContainer {
private COOS coos;
private Plugin[] coosPlugins, testPlugins;
private final static String DEFAULT_COOS_CONFIG_DIR = "./coosConfig";
private static final String COOS_CONFIG_PATH = "/org/coos/config";
public COOS getCoos() {
return coos;
}
public Plugin[] getCOOSPlugins() {
return coosPlugins;
}
public Plugin[] getTestPlugins() {
return testPlugins;
}
public abstract InputStream loadTestPlugin();
protected void setUp() throws Exception {
coos = COOSBootstrapHelper.startCOOS(DEFAULT_COOS_CONFIG_DIR, this);
coosPlugins = COOSBootstrapHelper.startPlugins(DEFAULT_COOS_CONFIG_DIR, this);
InputStream is = loadTestPlugin();
if (is != null) {
testPlugins = PluginFactory.createPlugins(is, this);
for (Plugin testPlugin : testPlugins) {
testPlugin.connect();
}
}
}
protected void tearDown() throws Exception {
// The test plugin
if (testPlugins != null) {
for (int i = 0; i < testPlugins.length; i++) {
Plugin plugin = testPlugins[i];
plugin.disconnect();
}
}
// The test plugin
if (coosPlugins != null) {
for (int i = 0; i < coosPlugins.length; i++) {
Plugin plugin = coosPlugins[i];
plugin.disconnect();
}
}
coos.stop();
}
public Object getObject(String name) {
return null;
}
public InputStream getResource(String resourceName) throws IOException {
InputStream is = null;
try {
File file = new File(DEFAULT_COOS_CONFIG_DIR + resourceName);
is = file.toURI().toURL().openStream();
} catch (Exception e) {
}
// if it is not overridden, use the provided file
if (is == null) {
is = this.getClass().getResourceAsStream(COOS_CONFIG_PATH + resourceName);
}
return is;
}
public Class loadClass(String className) throws ClassNotFoundException {
return Class.forName(className);
}
public void start() {}
public void stop() {}
}