001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/test/junit/org/deegree/ogcwebservices/AbstractServiceTest.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.ogcwebservices;
044:
045: import java.io.BufferedReader;
046: import java.io.File;
047: import java.io.FileReader;
048: import java.io.IOException;
049: import java.net.MalformedURLException;
050: import java.net.URL;
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055:
056: import junit.framework.TestCase;
057:
058: import org.deegree.framework.util.ConvenienceFileFilter;
059:
060: import alltests.AllTests;
061:
062: public class AbstractServiceTest extends TestCase {
063:
064: protected Map<String, String> createKVPMap(URL fileURL)
065: throws IOException {
066: Map<String, String> kvpMap = new HashMap<String, String>();
067: BufferedReader reader = new BufferedReader(new FileReader(
068: fileURL.getFile()));
069: // skip first line
070: String line = reader.readLine();
071: while ((line = reader.readLine()) != null) {
072: int equalsPos = line.indexOf('=');
073: if (equalsPos != -1) {
074: String key = line.substring(0, equalsPos);
075: String value = line.substring(equalsPos + 1);
076: if (value.endsWith("&")) {
077: value = value.substring(0, value.length() - 1);
078: }
079: kvpMap.put(key, value);
080: }
081: }
082: return kvpMap;
083: }
084:
085: /**
086: * Returns a filename that is suitable as a target to write the request result to.
087: * <p>
088: * The file will be placed in the "output"-subdirectory of the services resource directory.
089: *
090: * @param requestFile
091: * @param outputDir
092: * @return
093: * @throws MalformedURLException
094: */
095: protected String getResultFilename(URL requestFile, URL outputDir)
096: throws MalformedURLException {
097: String fileName = requestFile.toString();
098:
099: // request file name extension (kvp / xml)
100: String ext = fileName.substring(fileName.lastIndexOf('.') + 1,
101: fileName.length());
102:
103: // last directory (name of operation)
104: String[] directories = fileName.split("/");
105: String operation = directories[directories.length - 3];
106:
107: // example name
108: String exampleName = fileName.substring(fileName
109: .lastIndexOf('/') + 1, fileName.lastIndexOf('.'));
110:
111: String targetName = operation + ext.toUpperCase() + exampleName
112: + "_result.xml";
113: String resultName = new URL(outputDir, targetName).getFile();
114: return resultName;
115: }
116:
117: /**
118: * Returns all files in the given directory that end with the given extension.
119: *
120: * @param directoryURL
121: * @param ext
122: * @return
123: * @throws MalformedURLException
124: */
125: protected List<URL> scanDirectory(URL directoryURL, String ext)
126: throws MalformedURLException {
127: File directory = new File(directoryURL.getFile());
128: String[] fileNames = directory.list(new ConvenienceFileFilter(
129: false, ext));
130: List<URL> fileURLs = null;
131:
132: if (fileNames == null) {
133: AllTests.LOG.logDebug("Specified directory '"
134: + directory.toString() + "' does not exist.");
135: fileNames = new String[0];
136: } else {
137: fileURLs = new ArrayList<URL>(fileNames.length);
138: for (int i = 0; i < fileNames.length; i++) {
139: fileURLs.add(new URL(directoryURL.toString() + "/"
140: + fileNames[i]));
141: }
142: }
143: return fileURLs;
144: }
145:
146: }
|