Project

General

Profile

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-12 00:24:36 -0700 (Tue, 12 Aug 2003) $'
9
 * '$Revision: 1789 $'
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.StringReader;
36
import java.io.StringWriter;
37

    
38
import junit.framework.Test;
39
import junit.framework.TestCase;
40
import junit.framework.TestResult;
41
import junit.framework.TestSuite;
42

    
43
/**
44
 * A JUnit test for testing Step class processing
45
 */
46
public class MetacatClientTest extends TestCase
47
{
48
    private String metacatUrl = 
49
        "http://knb.ecoinformatics.org/knb/servlet/metacat";
50
    private String username = "@mcuser@";
51
    private String password = "@mcpassword@";
52
    private String failpass = "uidfnkj43987yfdn";
53
    private String docid = "jones.204.22";
54
    private String newdocid = "@newdocid@";
55
    private String testfile = "test/jones.204.22.xml";
56
    private String queryFile = "test/query.xml";
57
    //private String docid = "Gramling.61.26";
58
    private String testdocument = "";
59
    
60
    private Metacat m;
61

    
62
    /**
63
     * Constructor to build the test
64
     *
65
     * @param name the name of the test method
66
     */
67
    public MetacatClientTest(String name)
68
    {
69
        super(name);
70
    }
71

    
72
    /**
73
     * Establish a testing framework by initializing appropriate objects
74
     */
75
    public void setUp()
76
    {
77
        try {
78
            FileReader fr = new FileReader(testfile);
79
            testdocument = IOUtil.getAsString(fr, true);
80
            //StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
81
            //testdocument = buf.toString();
82
        } catch (IOException ioe) {
83
            fail("Can't read test data to run the test: " + testfile);
84
        }
85

    
86
        try {
87
            m = MetacatFactory.createMetacatConnection(metacatUrl);
88
        } catch (MetacatInaccessibleException mie) {
89
            fail("Metacat connection failed." + mie.getMessage());
90
        }
91
    }
92
  
93
    /**
94
     * Release any objects after tests are complete
95
     */
96
    public void tearDown()
97
    {
98
    }
99
  
100
    /**
101
     * Create a suite of tests to be run together
102
     */
103
    public static Test suite()
104
    {
105
        TestSuite suite = new TestSuite();
106
        suite.addTest(new MetacatClientTest("initialize"));
107
        suite.addTest(new MetacatClientTest("invalidLogin"));
108
        suite.addTest(new MetacatClientTest("login"));
109
        suite.addTest(new MetacatClientTest("insert"));
110
        suite.addTest(new MetacatClientTest("read"));
111
        suite.addTest(new MetacatClientTest("query"));
112
        return suite;
113
    }
114
  
115
    /**
116
     * Run an initial test that always passes to check that the test
117
     * harness is working.
118
     */
119
    public void initialize()
120
    {
121
        assertTrue(1 == 1);
122
    }
123
  
124
    /**
125
     * Test the login() function with valid credentials
126
     */
127
    public void login()
128
    {
129
        // Try a valid login
130
        try {
131
            m.login(username, password);
132
        } catch (MetacatAuthException mae) {
133
            fail("Authorization failed:\n" + mae.getMessage());
134
        } catch (MetacatInaccessibleException mie) {
135
            fail("Metacat Inaccessible:\n" + mie.getMessage());
136
        }
137
    }
138

    
139
    /**
140
     * Test the login() function with INVALID credentials
141
     */
142
    public void invalidLogin()
143
    {
144
        // Try an invalid login
145
        try {
146
            m.login(username, failpass);
147
            fail("Authorization should have failed.");
148
        } catch (MetacatAuthException mae) {
149
            assertTrue(1 == 1);
150
        } catch (MetacatInaccessibleException mie) {
151
            fail("Metacat Inaccessible:\n" + mie.getMessage());
152
        }
153
    }
154

    
155
    /**
156
     * Test the read() function with a known document
157
     */
158
    public void read()
159
    {
160
        try {
161
            m.login(username, password);
162
            Reader r = m.read(docid);
163
            String doc = IOUtil.getAsString(r, true);
164
            System.err.println(doc);
165
            assertTrue(doc.equals(testdocument));
166

    
167
        } catch (MetacatAuthException mae) {
168
            fail("Authorization failed:\n" + mae.getMessage());
169
        } catch (MetacatInaccessibleException mie) {
170
            fail("Metacat Inaccessible:\n" + mie.getMessage());
171
        } catch (Exception e) {
172
            fail("General exception:\n" + e.getMessage());
173
        }
174
    }
175
    
176
    /**
177
     * Test the query() function with a known document
178
     */
179
    public void query()
180
    {
181
        try {
182
            FileReader fr = new FileReader(queryFile);
183
            Reader r = m.query(fr);
184
            String result = IOUtil.getAsString(r, true);
185
            assertTrue(result.indexOf("obfs.100.3")!=-1);
186
        } catch (MetacatInaccessibleException mie) {
187
            fail("Metacat Inaccessible:\n" + mie.getMessage());
188
        } catch (Exception e) {
189
            fail("General exception:\n" + e.getMessage());
190
        }
191
    }
192

    
193
    /**
194
     * Test the insert() function with a known document
195
     */
196
    public void insert()
197
    {
198
        try {
199
            m.login(username, password);
200
            String response = m.insert(newdocid, 
201
                    new StringReader(testdocument), null);
202
            assertTrue(response.indexOf("<success>") != -1);
203
            assertTrue(response.indexOf(newdocid) != -1);
204

    
205
        } catch (MetacatAuthException mae) {
206
            fail("Authorization failed:\n" + mae.getMessage());
207
        } catch (MetacatInaccessibleException mie) {
208
            fail("Metacat Inaccessible:\n" + mie.getMessage());
209
        } catch (InsufficientKarmaException ike) {
210
            fail("Insufficient karma:\n" + ike.getMessage());
211
        } catch (MetacatException me) {
212
            fail("Metacat Error:\n" + me.getMessage());
213
        } catch (Exception e) {
214
            fail("General exception:\n" + e.getMessage());
215
        }
216
    }
217
}
    (1-1/1)