001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.test;
017:
018: import java.io.BufferedInputStream;
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.InputStream;
022: import java.util.Properties;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Test support for test cases which require an "online" resource, such as an
028: * external server or database.
029: * <p>
030: * Online tests work off of a "fixture". A fixture is a properties file which
031: * defines connection parameters for some remote service. Each online test case
032: * must define the id of the fixture is uses with {@link #getFixtureId()}.
033: * </p>
034: * <p>
035: * Fixtures are stored under the users home directory, under the "{@code .geotools}"
036: * directory. In the event that a fixture does not exist, the test case is
037: * aborted.
038: * </p>
039: * <p>
040: * Online tests connect to remote / online resources. Test cases should do all
041: * connection / disconnection in the {@link #connect} and {@link #disconnect()}
042: * methods.
043: * </p>
044: *
045: * @since 2.4
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/sample-data/src/main/java/org/geotools/test/OnlineTestCase.java $
047: * @version $Id: OnlineTestCase.java 25806 2007-06-11 22:04:55Z chorner $
048: * @author Justin Deoliveira, The Open Planning Project
049: */
050: public abstract class OnlineTestCase extends TestCase {
051: /**
052: * The test fixture, {@code null} if the fixture is not available.
053: */
054: protected Properties fixture;
055:
056: /**
057: * Loads the test fixture for the test case.
058: * <p>
059: * The fixture is obtained via {@link #getFixtureId()}.
060: * </p>
061: */
062: protected void setUp() throws Exception {
063: super .setUp();
064:
065: // load the fixture
066: File base = new File(System.getProperty("user.home")
067: + File.separator + ".geotools");
068: String fixtureId = getFixtureId();
069: if (fixtureId == null) {
070: fixture = null; // not available (turn test off)
071: return;
072: }
073: File fixtureFile = new File(base, fixtureId.replace('.',
074: File.separatorChar).concat(".properties"));
075:
076: if (fixtureFile.exists()) {
077: InputStream input = new BufferedInputStream(
078: new FileInputStream(fixtureFile));
079: try {
080: fixture = new Properties();
081: fixture.load(input);
082: } finally {
083: input.close();
084: }
085:
086: // call the setUp template method
087: try {
088: connect();
089: } catch (Throwable t) {
090: // abort the test
091: fixture = null;
092:
093: if (once) {
094: // print out connection failure message once
095: // (for people debugging a single test case)
096: t.printStackTrace();
097: once = false;
098: }
099: }
100: }
101: }
102:
103: static boolean once = true;
104:
105: /**
106: * Tear down method for test, calls through to {@link #disconnect()} if the
107: * test is active.
108: */
109: protected final void tearDown() throws Exception {
110: if (fixture != null) {
111: try {
112: disconnect();
113: } catch (Throwable t) {
114: // do nothing
115: }
116: }
117: }
118:
119: /**
120: * Connection method, called from {@link #setUp()}.
121: * <p>
122: * Subclasses should do all initialization / connection here. In the event
123: * of a connection not being available, this method should throw an
124: * exception to abort the test case.
125: * </p>
126: *
127: * @throws Exception if the connection failed.
128: */
129: protected void connect() throws Exception {
130: }
131:
132: /**
133: * Disconnection method, called from {@link #tearDown()}.
134: * <p>
135: * Subclasses should do all cleanup here.
136: * </p>
137: *
138: * @throws Exception if the disconnection failed.
139: */
140: protected void disconnect() throws Exception {
141: }
142:
143: /**
144: * Override which checks if the fixture is available. If not the test is not
145: * executed.
146: */
147: protected void runTest() throws Throwable {
148: // if the fixture was loaded, run
149: if (fixture != null) {
150: super .runTest();
151: }
152:
153: // otherwise do nothing
154: }
155:
156: /**
157: * The fixture id for the test case.
158: * <p>
159: * This name is hierachical, similar to a java package name. Example:
160: * {@code "postgis.demo_bc"}.
161: * </p>
162: *
163: * @return The fixture id.
164: */
165: protected abstract String getFixtureId();
166: }
|