001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.net.URL;
018:
019: import junit.framework.TestCase;
020:
021: import org.apache.avalon.framework.container.ContainerUtil;
022: import org.apache.avalon.framework.logger.ConsoleLogger;
023: import org.apache.avalon.framework.logger.Logger;
024: import org.rapla.client.ClientService;
025: import org.rapla.components.util.IOUtil;
026: import org.rapla.components.util.SerializableDateTimeFormat;
027: import org.rapla.facade.ClientFacade;
028: import org.rapla.framework.Container;
029: import org.rapla.framework.RaplaContext;
030: import org.rapla.framework.RaplaLocale;
031: import org.rapla.gui.toolkit.ErrorDialog;
032:
033: public abstract class RaplaTestCase extends TestCase {
034: protected Container raplaContainer;
035: private Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_WARN)
036: .getChildLogger("test");
037: public static String TEST_FOLDER_NAME = "temp/test";
038: RaplaStartupEnvironment env = new RaplaStartupEnvironment();
039:
040: public RaplaTestCase(String name) {
041: super (name);
042: try {
043: new File("temp").mkdir();
044: File testFolder = new File(TEST_FOLDER_NAME);
045: testFolder.mkdir();
046: IOUtil.copy("test-src/test.xconf", TEST_FOLDER_NAME
047: + "/test.xconf");
048: IOUtil.copy("test-src/test.xlog", TEST_FOLDER_NAME
049: + "/test.xlog");
050: } catch (IOException ex) {
051: throw new RuntimeException(
052: "Can't initialize config-files: " + ex.getMessage());
053: }
054: }
055:
056: public void copyDataFile(String testFile) throws IOException {
057: try {
058: IOUtil.copy(testFile, TEST_FOLDER_NAME + "/test.xml");
059: } catch (IOException ex) {
060: throw new IOException("Failed to copy TestFile '"
061: + testFile + "': " + ex.getMessage());
062: }
063: }
064:
065: protected RaplaContext getContext() {
066: return raplaContainer.getContext();
067: }
068:
069: protected SerializableDateTimeFormat formater() {
070: return new SerializableDateTimeFormat();
071: }
072:
073: protected Logger getLogger() {
074: return logger;
075: }
076:
077: protected void setUp(String testFile) throws Exception {
078: ErrorDialog.THROW_ERROR_DIALOG_EXCEPTION = true;
079:
080: URL configURL = new URL("file:./" + TEST_FOLDER_NAME
081: + "/test.xconf");
082: URL logConfigURL = new URL(IOUtil.getBase(configURL),
083: "test.xlog");
084: env.setConfigURL(configURL);
085: env.setLogConfigURL(logConfigURL);
086: copyDataFile("test-src/" + testFile);
087: raplaContainer = new RaplaMainContainer(env);
088: assertNotNull("Container not initialized.", raplaContainer);
089: }
090:
091: protected void setUp() throws Exception {
092: setUp("testdefault.xml");
093: }
094:
095: protected ClientService getClientService() throws Exception {
096: return (ClientService) getContext().lookup(ClientService.ROLE);
097: }
098:
099: protected ClientFacade getFacade() throws Exception {
100: return (ClientFacade) getContext().lookup(ClientFacade.ROLE);
101: }
102:
103: protected RaplaLocale getRaplaLocale() throws Exception {
104: return (RaplaLocale) getContext().lookup(RaplaLocale.ROLE);
105: }
106:
107: protected void tearDown() throws Exception {
108: if (raplaContainer != null)
109: ContainerUtil.dispose(raplaContainer);
110: }
111:
112: }
|