1 |
1783
|
jones
|
/**
|
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$'
|
8 |
|
|
* '$Date$'
|
9 |
|
|
* '$Revision$'
|
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 |
1784
|
jones
|
import edu.ucsb.nceas.utilities.IOUtil;
|
30 |
1783
|
jones
|
|
31 |
1784
|
jones
|
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 |
1783
|
jones
|
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 |
1784
|
jones
|
private String docid = "jones.204.22";
|
53 |
|
|
private String testfile = "test/jones.204.22.xml";
|
54 |
|
|
//private String docid = "Gramling.61.26";
|
55 |
|
|
private String testdocument = "";
|
56 |
1783
|
jones
|
|
57 |
|
|
private Metacat m;
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* Constructor to build the test
|
61 |
|
|
*
|
62 |
|
|
* @param name the name of the test method
|
63 |
|
|
*/
|
64 |
|
|
public MetacatClientTest(String name)
|
65 |
|
|
{
|
66 |
|
|
super(name);
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Establish a testing framework by initializing appropriate objects
|
71 |
|
|
*/
|
72 |
|
|
public void setUp()
|
73 |
|
|
{
|
74 |
|
|
try {
|
75 |
1784
|
jones
|
FileReader fr = new FileReader(testfile);
|
76 |
|
|
StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
|
77 |
|
|
testdocument = buf.toString();
|
78 |
|
|
} catch (IOException ioe) {
|
79 |
|
|
fail("Can't read test data to run the test: " + testfile);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
try {
|
83 |
1783
|
jones
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
84 |
|
|
} catch (MetacatInaccessibleException mie) {
|
85 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Release any objects after tests are complete
|
91 |
|
|
*/
|
92 |
|
|
public void tearDown()
|
93 |
|
|
{
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* Create a suite of tests to be run together
|
98 |
|
|
*/
|
99 |
|
|
public static Test suite()
|
100 |
|
|
{
|
101 |
|
|
TestSuite suite = new TestSuite();
|
102 |
|
|
suite.addTest(new MetacatClientTest("initialize"));
|
103 |
|
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
104 |
|
|
suite.addTest(new MetacatClientTest("login"));
|
105 |
1784
|
jones
|
suite.addTest(new MetacatClientTest("read"));
|
106 |
1783
|
jones
|
return suite;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Run an initial test that always passes to check that the test
|
111 |
|
|
* harness is working.
|
112 |
|
|
*/
|
113 |
|
|
public void initialize()
|
114 |
|
|
{
|
115 |
|
|
assertTrue(1 == 1);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/**
|
119 |
|
|
* Test the login() function with valid credentials
|
120 |
|
|
*/
|
121 |
|
|
public void login()
|
122 |
|
|
{
|
123 |
|
|
// Try a valid login
|
124 |
|
|
try {
|
125 |
|
|
m.login(username, password);
|
126 |
|
|
} catch (MetacatAuthException mae) {
|
127 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
128 |
|
|
} catch (MetacatInaccessibleException mie) {
|
129 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/**
|
134 |
|
|
* Test the login() function with INVALID credentials
|
135 |
|
|
*/
|
136 |
|
|
public void invalidLogin()
|
137 |
|
|
{
|
138 |
|
|
// Try an invalid login
|
139 |
|
|
try {
|
140 |
|
|
m.login(username, failpass);
|
141 |
|
|
fail("Authorization should have failed.");
|
142 |
|
|
} catch (MetacatAuthException mae) {
|
143 |
|
|
assertTrue(1 == 1);
|
144 |
|
|
} catch (MetacatInaccessibleException mie) {
|
145 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
1784
|
jones
|
|
149 |
|
|
/**
|
150 |
|
|
* Test the read() function with a known document
|
151 |
|
|
*/
|
152 |
|
|
public void read()
|
153 |
|
|
{
|
154 |
|
|
try {
|
155 |
|
|
m.login(username, password);
|
156 |
|
|
Reader r = m.read(docid);
|
157 |
|
|
String doc = readerToString(r);
|
158 |
|
|
System.err.println(doc);
|
159 |
|
|
assertTrue(doc.equals(testdocument));
|
160 |
|
|
|
161 |
|
|
} catch (MetacatAuthException mae) {
|
162 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
163 |
|
|
} catch (MetacatInaccessibleException mie) {
|
164 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
165 |
|
|
} catch (Exception e) {
|
166 |
|
|
fail("General exception:\n" + e.getMessage());
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
/*
|
171 |
|
|
* PRIVATE METHODS
|
172 |
|
|
*/
|
173 |
|
|
|
174 |
|
|
/**
|
175 |
|
|
* Utility method to convert a reader to a String.
|
176 |
|
|
*
|
177 |
|
|
* @param reader the Reader to be converted
|
178 |
|
|
* @return a String representation fo the data read
|
179 |
|
|
*/
|
180 |
|
|
private String readerToString(Reader reader) throws Exception
|
181 |
|
|
{
|
182 |
|
|
String response = null;
|
183 |
|
|
|
184 |
|
|
try {
|
185 |
|
|
StringWriter sw = new StringWriter();
|
186 |
|
|
int len;
|
187 |
|
|
char[] characters = new char[512];
|
188 |
|
|
while ((len = reader.read(characters, 0, 512)) != -1) {
|
189 |
|
|
sw.write(characters, 0, len);
|
190 |
|
|
}
|
191 |
|
|
response = sw.toString();
|
192 |
|
|
sw.close();
|
193 |
|
|
reader.close();
|
194 |
|
|
} catch (Exception e) {
|
195 |
|
|
throw e;
|
196 |
|
|
}
|
197 |
|
|
return response;
|
198 |
|
|
}
|
199 |
1783
|
jones
|
}
|