1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the MetaCatURL class by JUnit
|
6
|
* Authors: @Jing Tao@
|
7
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: tao $'
|
10
|
* '$Date: 2002-05-03 17:03:49 -0700 (Fri, 03 May 2002) $'
|
11
|
* '$Revision: 1050 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacatjunittests;
|
29
|
|
30
|
import edu.ucsb.nceas.metacat.*;
|
31
|
//import edu.ucsb.nceas.morpho.framework.*;
|
32
|
import junit.framework.Test;
|
33
|
import junit.framework.TestCase;
|
34
|
import junit.framework.TestResult;
|
35
|
import junit.framework.TestSuite;
|
36
|
|
37
|
import java.io.*;
|
38
|
import java.net.*;
|
39
|
import java.util.*;
|
40
|
|
41
|
|
42
|
/**
|
43
|
* A JUnit test for testing Step class processing
|
44
|
*/
|
45
|
public class MetaCatServletTest extends TestCase
|
46
|
{
|
47
|
private String metacatURL=
|
48
|
"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
|
49
|
/**
|
50
|
* Constructor to build the test
|
51
|
*
|
52
|
* @param name the name of the test method
|
53
|
*/
|
54
|
public MetaCatServletTest(String name)
|
55
|
{
|
56
|
super(name);
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Establish a testing framework by initializing appropriate objects
|
61
|
*/
|
62
|
public void setUp()
|
63
|
{
|
64
|
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Release any objects after tests are complete
|
69
|
*/
|
70
|
public void tearDown()
|
71
|
{
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Create a suite of tests to be run together
|
76
|
*/
|
77
|
public static Test suite()
|
78
|
{
|
79
|
TestSuite suite = new TestSuite();
|
80
|
suite.addTest(new MetaCatServletTest("initialize"));
|
81
|
suite.addTest(new MetaCatServletTest("testNCEASLogin"));
|
82
|
suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
|
83
|
suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
|
84
|
suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
|
85
|
suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
|
86
|
suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
|
87
|
return suite;
|
88
|
}
|
89
|
|
90
|
|
91
|
|
92
|
/**
|
93
|
* Run an initial test that always passes to check that the test
|
94
|
* harness is working.
|
95
|
*/
|
96
|
public void initialize()
|
97
|
{
|
98
|
assert(1 == 1);
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Test if the getProtocol() function works given a url
|
103
|
*/
|
104
|
public void testNCEASLogin()
|
105
|
{
|
106
|
String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
|
107
|
String passwd="123456";
|
108
|
assert(logIn(user,passwd));
|
109
|
//assert( withProtocol.getProtocol().equals("http"));
|
110
|
}
|
111
|
|
112
|
public void testNCEASLoginFail()
|
113
|
{
|
114
|
String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
|
115
|
String passwd="12345678";
|
116
|
assert(!logIn(user,passwd));
|
117
|
//assert( withProtocol.getProtocol().equals("http"));
|
118
|
}
|
119
|
public void testLterReferralLogin()
|
120
|
{
|
121
|
String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
|
122
|
String passwd="qVyGpVeb";
|
123
|
assert(logIn(user,passwd));
|
124
|
//assert( withProtocol.getProtocol().equals("http"));
|
125
|
}
|
126
|
|
127
|
public void testLterReferralLoginFail()
|
128
|
{
|
129
|
String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
|
130
|
String passwd="qVyGpveb";
|
131
|
assert(!logIn(user,passwd));
|
132
|
//assert( withProtocol.getProtocol().equals("http"));
|
133
|
}
|
134
|
|
135
|
public void testPiscoReferralLogin()
|
136
|
{
|
137
|
String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
|
138
|
String passwd="purple";
|
139
|
assert(logIn(user,passwd));
|
140
|
//assert( withProtocol.getProtocol().equals("http"));
|
141
|
}
|
142
|
|
143
|
public void testPiscoReferralLoginFail()
|
144
|
{
|
145
|
String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
|
146
|
String passwd="hello";
|
147
|
assert(!logIn(user,passwd));
|
148
|
//assert( withProtocol.getProtocol().equals("http"));
|
149
|
}
|
150
|
|
151
|
public boolean logIn(String userName, String passWord)
|
152
|
{
|
153
|
Properties prop = new Properties();
|
154
|
prop.put("action", "login");
|
155
|
prop.put("qformat", "xml");
|
156
|
prop.put("username", userName);
|
157
|
prop.put("password", passWord);
|
158
|
|
159
|
// Now contact metacat
|
160
|
String response = getMetacatString(prop);
|
161
|
boolean connected = false;
|
162
|
if (response.indexOf("<login>") != -1) {
|
163
|
connected = true;
|
164
|
} else {
|
165
|
|
166
|
connected = false;
|
167
|
}
|
168
|
|
169
|
return connected;
|
170
|
}
|
171
|
|
172
|
public String getMetacatString(Properties prop)
|
173
|
{
|
174
|
String response = null;
|
175
|
|
176
|
// Now contact metacat and send the request
|
177
|
try
|
178
|
{
|
179
|
InputStreamReader returnStream =
|
180
|
new InputStreamReader(getMetacatInputStream(prop));
|
181
|
StringWriter sw = new StringWriter();
|
182
|
int len;
|
183
|
char[] characters = new char[512];
|
184
|
while ((len = returnStream.read(characters, 0, 512)) != -1)
|
185
|
{
|
186
|
sw.write(characters, 0, len);
|
187
|
}
|
188
|
returnStream.close();
|
189
|
response = sw.toString();
|
190
|
sw.close();
|
191
|
}
|
192
|
catch(Exception e)
|
193
|
{
|
194
|
|
195
|
}
|
196
|
|
197
|
return response;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Send a request to Metacat
|
202
|
*
|
203
|
* @param prop the properties to be sent to Metacat
|
204
|
* @return InputStream as returned by Metacat
|
205
|
*/
|
206
|
public InputStream getMetacatInputStream(Properties prop)
|
207
|
{
|
208
|
InputStream returnStream = null;
|
209
|
// Now contact metacat and send the request
|
210
|
try
|
211
|
{
|
212
|
|
213
|
URL url = new URL(metacatURL);
|
214
|
HttpMessage msg = new HttpMessage(url);
|
215
|
returnStream = msg.sendPostMessage(prop);
|
216
|
return returnStream;
|
217
|
}
|
218
|
catch(Exception e)
|
219
|
{
|
220
|
e.printStackTrace(System.err);
|
221
|
|
222
|
}
|
223
|
return returnStream;
|
224
|
|
225
|
}
|
226
|
|
227
|
|
228
|
|
229
|
}
|