Project

General

Profile

« Previous | Next » 

Revision 4444

Added by daigle over 15 years ago

Add common test case db select, query and update methods. Add http post method.

View differences:

MCTestCase.java
28 28
import junit.framework.TestCase;
29 29

  
30 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;
31 39

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

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

  
......
48 61
	
49 62
	protected boolean SUCCESS = true;
50 63
	protected boolean FAILURE = false;
64
	
65
	protected static HttpClient httpClient = null;
51 66

  
52 67
	static {
53 68
		try {
......
91 106
    		System.err.println(debugMessage);
92 107
    	}
93 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
	}
94 195
}

Also available in: Unified diff