Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2008 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: daigle $'
8
 *     '$Date: 2008-10-10 17:15:19 -0700 (Fri, 10 Oct 2008) $'
9
 * '$Revision: 4444 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas;
27

    
28
import junit.framework.TestCase;
29

    
30
import java.io.IOException;
31
import java.sql.PreparedStatement;
32
import java.sql.ResultSet;
33
import java.sql.ResultSetMetaData;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36
import java.util.HashMap;
37
import java.util.Hashtable;
38
import java.util.Vector;
39

    
40
import org.apache.commons.httpclient.HttpClient;
41

    
42
import edu.ucsb.nceas.metacat.DBConnection;
43
import edu.ucsb.nceas.metacat.DBConnectionPool;
44
import edu.ucsb.nceas.metacat.service.PropertyService;
45
import edu.ucsb.nceas.metacat.service.ServiceException;
46
import edu.ucsb.nceas.metacat.util.RequestUtil;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48
import edu.ucsb.nceas.utilities.SortedProperties;
49

    
50
/**
51
 * A base JUnit class for Metacat tests
52
 */
53
public class MCTestCase
54
    extends TestCase {
55
	
56
    private static boolean printDebug = false;
57
    
58
	protected static String EML2_0_0 = "EML2_0_0";
59
	protected static String EML2_0_1 = "EML2_0_1";
60
	protected static String EML2_1_0 = "EML2_1_0";
61
	
62
	protected boolean SUCCESS = true;
63
	protected boolean FAILURE = false;
64
	
65
	protected static HttpClient httpClient = null;
66

    
67
	static {
68
		try {
69
			SortedProperties testProperties = 
70
				new SortedProperties("build/tests/test.properties");
71
			testProperties.load();
72
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
73
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
74
		    String printDebugString = PropertyService.getProperty("test.printdebug");
75
		    printDebug = Boolean.parseBoolean(printDebugString);
76
		} catch (IOException ioe) {
77
			System.err.println("Could not read property file in static block: " 
78
					+ ioe.getMessage());
79
		} catch (PropertyNotFoundException pnfe) {
80
			System.err.println("Could not get property in static block: " 
81
					+ pnfe.getMessage());
82
		} catch (ServiceException se) {
83
			System.err.println("Could not get PropertyService instance in static block: " 
84
					+ se.getMessage());
85
		}
86
	}
87

    
88
    /**
89
     * Constructor to build the test
90
     */
91
    public MCTestCase() {
92
        super();
93
    }
94
	
95
    /**
96
     * Constructor to build the test
97
     *
98
     * @param name the name of the test method
99
     */
100
    public MCTestCase(String name) {
101
        super(name);
102
    }
103
    
104
    protected static void debug(String debugMessage) {
105
    	if (printDebug) {
106
    		System.err.println(debugMessage);
107
    	}
108
    }
109
    
110
	protected static Vector<Hashtable<String, Object>> dbSelect(String sqlStatement,
111
			String methodName) throws SQLException {
112
		Vector<Hashtable<String, Object>> resultVector = new Vector<Hashtable<String, Object>>();
113

    
114
		DBConnectionPool connPool = DBConnectionPool.getInstance();
115
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
116
		int serialNumber = dbconn.getCheckOutSerialNumber();
117

    
118
		PreparedStatement pstmt = null;
119

    
120
		debug("Selecting from db: " + sqlStatement);
121
		pstmt = dbconn.prepareStatement(sqlStatement);
122
		pstmt.execute();
123

    
124
		ResultSet resultSet = pstmt.getResultSet();
125
		ResultSetMetaData rsMetaData = resultSet.getMetaData();
126
		int numColumns = rsMetaData.getColumnCount();
127
		debug("Number of data columns: " + numColumns);
128
		while (resultSet.next()) {
129
			Hashtable<String, Object> hashTable = new Hashtable<String, Object>();
130
			for (int i = 1; i <= numColumns; i++) {
131
				if (resultSet.getObject(i) != null) {
132
					hashTable.put(rsMetaData.getColumnName(i), resultSet.getObject(i));
133
				}
134
			}
135
			debug("adding data row to result vector");
136
			resultVector.add(hashTable);
137
		}
138

    
139
		resultSet.close();
140
		pstmt.close();
141
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
142

    
143
		return resultVector;
144
	}
145
	
146
	protected static void dbQuery(String sqlStatement, String methodName)
147
			throws SQLException {
148

    
149
		DBConnectionPool connPool = DBConnectionPool.getInstance();
150
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
151
		int serialNumber = dbconn.getCheckOutSerialNumber();
152

    
153
		Statement statement = dbconn.createStatement();
154

    
155
		debug("Executing against db: " + sqlStatement);
156
		statement.executeQuery(sqlStatement);
157

    
158
		statement.close();
159
		
160
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
161
	}
162
	
163
	protected static void dbUpdate(String sqlStatement, String methodName)
164
			throws SQLException {
165

    
166
		DBConnectionPool connPool = DBConnectionPool.getInstance();
167
		DBConnection dbconn = DBConnectionPool.getDBConnection(methodName);
168
		int serialNumber = dbconn.getCheckOutSerialNumber();
169

    
170
		Statement statement = dbconn.createStatement();
171

    
172
		debug("Executing against db: " + sqlStatement);
173
		statement.executeUpdate(sqlStatement);
174

    
175
		statement.close();
176

    
177
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
178
	}
179
	
180
	protected static void httpPost(String url, HashMap<String,String> paramMap)  throws IOException {
181
		debug("Posting to: " + url);
182
		if (httpClient == null) {
183
			httpClient = new HttpClient();
184
		}
185
		String postResponse = RequestUtil.post(httpClient, url, paramMap);
186
		debug("Post response: " + postResponse);
187
		if (postResponse.contains("<error>")) {
188
			fail("Error posting to metacat: " + postResponse);
189
		}
190
	}
191
	
192
	protected static void resetHttpClient() {
193
		httpClient = null;
194
	}
195
}
    (1-1/1)