1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 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
|
*
|
7
|
* '$Author: jones $'
|
8
|
* '$Date: 2003-08-11 23:28:25 -0700 (Mon, 11 Aug 2003) $'
|
9
|
* '$Revision: 1788 $'
|
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.metacattest.client;
|
27
|
|
28
|
import edu.ucsb.nceas.metacat.client.*;
|
29
|
import edu.ucsb.nceas.utilities.IOUtil;
|
30
|
|
31
|
import java.io.FileReader;
|
32
|
import java.io.InputStreamReader;
|
33
|
import java.io.IOException;
|
34
|
import java.io.Reader;
|
35
|
import java.io.StringWriter;
|
36
|
|
37
|
import junit.framework.Test;
|
38
|
import junit.framework.TestCase;
|
39
|
import junit.framework.TestResult;
|
40
|
import junit.framework.TestSuite;
|
41
|
|
42
|
/**
|
43
|
* A JUnit test for testing Step class processing
|
44
|
*/
|
45
|
public class MetacatClientTest extends TestCase
|
46
|
{
|
47
|
private String metacatUrl =
|
48
|
"http://knb.ecoinformatics.org/knb/servlet/metacat";
|
49
|
private String username = "@mcuser@";
|
50
|
private String password = "@mcpassword@";
|
51
|
private String failpass = "uidfnkj43987yfdn";
|
52
|
private String docid = "jones.204.22";
|
53
|
private String testfile = "test/jones.204.22.xml";
|
54
|
private String queryFile = "test/query.xml";
|
55
|
//private String docid = "Gramling.61.26";
|
56
|
private String testdocument = "";
|
57
|
|
58
|
private Metacat m;
|
59
|
|
60
|
/**
|
61
|
* Constructor to build the test
|
62
|
*
|
63
|
* @param name the name of the test method
|
64
|
*/
|
65
|
public MetacatClientTest(String name)
|
66
|
{
|
67
|
super(name);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Establish a testing framework by initializing appropriate objects
|
72
|
*/
|
73
|
public void setUp()
|
74
|
{
|
75
|
try {
|
76
|
FileReader fr = new FileReader(testfile);
|
77
|
testdocument = IOUtil.getAsString(fr, true);
|
78
|
//StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
|
79
|
//testdocument = buf.toString();
|
80
|
} catch (IOException ioe) {
|
81
|
fail("Can't read test data to run the test: " + testfile);
|
82
|
}
|
83
|
|
84
|
try {
|
85
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
86
|
} catch (MetacatInaccessibleException mie) {
|
87
|
fail("Metacat connection failed." + mie.getMessage());
|
88
|
}
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Release any objects after tests are complete
|
93
|
*/
|
94
|
public void tearDown()
|
95
|
{
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Create a suite of tests to be run together
|
100
|
*/
|
101
|
public static Test suite()
|
102
|
{
|
103
|
TestSuite suite = new TestSuite();
|
104
|
suite.addTest(new MetacatClientTest("initialize"));
|
105
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
106
|
suite.addTest(new MetacatClientTest("login"));
|
107
|
suite.addTest(new MetacatClientTest("read"));
|
108
|
suite.addTest(new MetacatClientTest("query"));
|
109
|
return suite;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Run an initial test that always passes to check that the test
|
114
|
* harness is working.
|
115
|
*/
|
116
|
public void initialize()
|
117
|
{
|
118
|
assertTrue(1 == 1);
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Test the login() function with valid credentials
|
123
|
*/
|
124
|
public void login()
|
125
|
{
|
126
|
// Try a valid login
|
127
|
try {
|
128
|
m.login(username, password);
|
129
|
} catch (MetacatAuthException mae) {
|
130
|
fail("Authorization failed:\n" + mae.getMessage());
|
131
|
} catch (MetacatInaccessibleException mie) {
|
132
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
133
|
}
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Test the login() function with INVALID credentials
|
138
|
*/
|
139
|
public void invalidLogin()
|
140
|
{
|
141
|
// Try an invalid login
|
142
|
try {
|
143
|
m.login(username, failpass);
|
144
|
fail("Authorization should have failed.");
|
145
|
} catch (MetacatAuthException mae) {
|
146
|
assertTrue(1 == 1);
|
147
|
} catch (MetacatInaccessibleException mie) {
|
148
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
149
|
}
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Test the read() function with a known document
|
154
|
*/
|
155
|
public void read()
|
156
|
{
|
157
|
try {
|
158
|
m.login(username, password);
|
159
|
Reader r = m.read(docid);
|
160
|
String doc = IOUtil.getAsString(r, true);
|
161
|
System.err.println(doc);
|
162
|
assertTrue(doc.equals(testdocument));
|
163
|
|
164
|
} catch (MetacatAuthException mae) {
|
165
|
fail("Authorization failed:\n" + mae.getMessage());
|
166
|
} catch (MetacatInaccessibleException mie) {
|
167
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
168
|
} catch (Exception e) {
|
169
|
fail("General exception:\n" + e.getMessage());
|
170
|
}
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Test the query() function with a known document
|
175
|
*/
|
176
|
public void query()
|
177
|
{
|
178
|
try {
|
179
|
FileReader fr = new FileReader(queryFile);
|
180
|
Reader r = m.query(fr);
|
181
|
String result = IOUtil.getAsString(r, true);
|
182
|
assertTrue(result.indexOf("obfs.100.3")!=-1);
|
183
|
} catch (MetacatInaccessibleException mie) {
|
184
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
185
|
} catch (Exception e) {
|
186
|
fail("General exception:\n" + e.getMessage());
|
187
|
}
|
188
|
}
|
189
|
}
|