Project

General

Profile

« Previous | Next » 

Revision 1783

Added by Matt Jones over 21 years ago

Wrote the login() function, the first of the series of API calls for
the metacat client to be implemented. Also wrote a test class to
test the API functionality.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
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$'
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

  
30
import junit.framework.Test;
31
import junit.framework.TestCase;
32
import junit.framework.TestResult;
33
import junit.framework.TestSuite;
34

  
35
/**
36
 * A JUnit test for testing Step class processing
37
 */
38
public class MetacatClientTest extends TestCase
39
{
40
    private String metacatUrl = 
41
        "http://knb.ecoinformatics.org/knb/servlet/metacat";
42
    private String username = "@mcuser@";
43
    private String password = "@mcpassword@";
44
    private String failpass = "uidfnkj43987yfdn";
45
    
46
    private Metacat m;
47

  
48
    /**
49
     * Constructor to build the test
50
     *
51
     * @param name the name of the test method
52
     */
53
    public MetacatClientTest(String name)
54
    {
55
        super(name);
56
    }
57

  
58
    /**
59
     * Establish a testing framework by initializing appropriate objects
60
     */
61
    public void setUp()
62
    {
63
        try {
64
            m = MetacatFactory.createMetacatConnection(metacatUrl);
65
        } catch (MetacatInaccessibleException mie) {
66
            fail("Metacat connection failed." + mie.getMessage());
67
        }
68
    }
69
  
70
    /**
71
     * Release any objects after tests are complete
72
     */
73
    public void tearDown()
74
    {
75
    }
76
  
77
    /**
78
     * Create a suite of tests to be run together
79
     */
80
    public static Test suite()
81
    {
82
        TestSuite suite = new TestSuite();
83
        suite.addTest(new MetacatClientTest("initialize"));
84
        suite.addTest(new MetacatClientTest("invalidLogin"));
85
        suite.addTest(new MetacatClientTest("login"));
86
        return suite;
87
    }
88
  
89
    /**
90
     * Run an initial test that always passes to check that the test
91
     * harness is working.
92
     */
93
    public void initialize()
94
    {
95
        assertTrue(1 == 1);
96
    }
97
  
98
    /**
99
     * Test the login() function with valid credentials
100
     */
101
    public void login()
102
    {
103
        // Try a valid login
104
        try {
105
            m.login(username, password);
106
        } catch (MetacatAuthException mae) {
107
            fail("Authorization failed:\n" + mae.getMessage());
108
        } catch (MetacatInaccessibleException mie) {
109
            fail("Metacat Inaccessible:\n" + mie.getMessage());
110
        }
111
    }
112

  
113
    /**
114
     * Test the login() function with INVALID credentials
115
     */
116
    public void invalidLogin()
117
    {
118
        // Try an invalid login
119
        try {
120
            System.err.println("Username: " + username);
121
            m.login(username, failpass);
122
            fail("Authorization should have failed.");
123
        } catch (MetacatAuthException mae) {
124
            assertTrue(1 == 1);
125
        } catch (MetacatInaccessibleException mie) {
126
            fail("Metacat Inaccessible:\n" + mie.getMessage());
127
        }
128
    }
129
}
0 130

  
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
25 25
package edu.ucsb.nceas.metacat.client;
26 26

  
27 27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
import java.io.StringWriter;
28 30
import java.io.Reader;
29 31
import java.net.URL;
30 32
import java.util.Properties;
......
63 65
    public void login(String username, String password) 
64 66
           throws MetacatAuthException, MetacatInaccessibleException
65 67
    {
68
        Properties prop = new Properties();
69
        prop.put("action", "login");
70
        prop.put("qformat", "xml");
71
        prop.put("username", username);
72
        prop.put("password", password);
73

  
74
        String response = null;
75
        try {
76
            response = sendDataForString(prop);
77
        } catch (Exception e) {
78
            throw new MetacatInaccessibleException(e.getMessage());
79
        }
80

  
81
        if (response.indexOf("<login>") == -1) {
82
            HttpMessage.setCookie(null);
83
            throw new MetacatAuthException(response);
84
        }
66 85
    }
67 86

  
68 87
    /**
......
143 162
     */
144 163
    public void setMetacatUrl(String metacatUrl)
145 164
    {
165
        this.metacatUrl = metacatUrl;
146 166
    }
147 167

  
148 168
    /************************************************************************
149 169
     * PRIVATE METHODS
150 170
     ************************************************************************/
151 171

  
172
    /**
173
     * Send a request to metacat.
174
     *
175
     * @param prop the properties to be URL encoded and sent
176
     */
152 177
    synchronized private InputStream sendDataOnce(Properties prop) 
153 178
        throws Exception
154 179
    {
......
194 219
            }
195 220
        }
196 221
    }
222

  
223
    /**
224
     * Send a request to Metacat
225
     *
226
     * @param prop  the properties to be sent to Metacat
227
     * @return      a string as returned by Metacat
228
     */
229
    synchronized private String sendDataForString(Properties prop) 
230
        throws Exception
231
    {
232
        String response = null;
233

  
234
        try {
235
            InputStreamReader returnStream =
236
                    new InputStreamReader(sendData(prop));
237
            StringWriter sw = new StringWriter();
238
            int len;
239
            char[] characters = new char[512];
240
            while ((len = returnStream.read(characters, 0, 512)) != -1) {
241
                sw.write(characters, 0, len);
242
            }
243
            returnStream.close();
244
            response = sw.toString();
245
            sw.close();
246
        } catch (Exception e) {
247
            throw e;
248
        }
249
        return response;
250
    }
197 251
}
build.xml
79 79
      <property name="context" value="tao" />
80 80
      <property name="user" value="tao"/>
81 81
      <property name="password" value=""/>
82
      <property name="mcuser" value="uid=jones,o=NCEAS,dc=ecoinformatics,dc=org"/>
83
      <property name="mcpassword" value=""/>
82 84
      <!-- Tomcat version, if you use tomcat 3, please fill it "tomcat3". If you using tomcat4, please fill it "tomcat4"-->
83 85
      <property name="tomcatversion" value="tomcat4"/>
84 86

  
......
171 173
      <filter token="html-path" value="${html-path}"/>
172 174
      <filter token="user" value="${user}"/>
173 175
      <filter token="password" value="${password}"/>
176
      <filter token="mcuser" value="${mcuser}"/>
177
      <filter token="mcpassword" value="${mcpassword}"/>
174 178
      <filter token="image-path" value="${image-path}"/>
175 179
      <filter token="style-path" value="${style-path}"/>
176 180
      <filter token="web-base-url" value="${web-base-url}"/>
......
189 193
      <filter token="debugprefix" value="${debugprefix}"/>
190 194

  
191 195
      <property name="srcdir" value="./src" />
196
      <property name="testdir" value="./test" />
197
      <property name="testtorun" value="MetacatClientTest" />
192 198
      <property name="junittestsdir" value="./test/edu/ucsb/nceas/metacattest" />
193 199
      <property name="junitnettestsdir" value="./test/edu/ucsb/nceas/metacatnettest" />
194 200
      <property name="build.dir" value="./build"/>
......
434 440
     
435 441
   </target>
436 442
   
437
   
443
   <target name="testprep" depends="install">
444
      <mkdir dir="${build.tests}"/>
445
      <copy todir="${build.tests}" filtering="yes">
446
        <fileset dir="${testdir}">
447
          <include name="edu/**"/>
448
        </fileset>
449
      </copy>
450
   </target>
438 451

  
439
   <target name="test" depends="install">
452
   <target name="test" depends="testprep">
440 453
      <!-- copy and compile the tests into a jar file -->
441
      <mkdir dir="${build.metacattest}"/>
442
      <javac srcdir="${junittestsdir}"
443
             destdir="${build.metacattest}"
454
      <javac srcdir="${build.tests}"
455
             destdir="${build.tests}"
444 456
             classpath="${cpath}:${build.dir}/${name}.jar"
445 457
             debug="on"
446 458
       includes="**/*.java" />
447 459

  
448 460
      <jar jarfile="${build.dir}/${name}-junittests.jar"
449
        basedir="${build.metacattest}"
461
        basedir="${build.tests}"
450 462
                includes="**/*.class" />
451 463

  
452 464
      <!-- use the ant "junit" task to run JUnit tests. -->
453
      <junit printsummary="yes" haltonfailure="no" fork="no"
465
      <junit printsummary="yes" haltonfailure="no" fork="yes"
454 466
             haltonerror="no">
467
        <jvmarg value="-Djava.protocol.handler.pkgs=HTTPClient"/>
455 468
        <classpath>
456
          <pathelement path="${cpath}:${build.dir}/${name}.jar:${build.dir}/${name}-junittests.jar" />
469
          <pathelement path="${cpath}:${build.dir}/${name}.jar:${build.dir}/${name}-client.jar:${build.dir}/${name}-junittests.jar" />
457 470
        </classpath>
458 471

  
459 472
        <formatter type="plain" />
460 473

  
461 474
        <batchtest fork="yes" todir="${build.dir}">
462
          <fileset dir="${build.metacattest}">
475
          <fileset dir="${build.tests}">
463 476
            <include name="**/*.class" />
464 477
          </fileset>
465 478
        </batchtest>
466 479
     </junit>
467 480
   </target>
468 481

  
469
    <target name="runonetest" depends="install">
482
    <target name="runonetest" depends="testprep">
470 483
      <!-- copy and compile the tests into a jar file -->
471 484
      <!--<property name="testtorun" value="SubTreeTest"/> -->
472 485
      <echo>testtorun: ${testtorun}</echo>
473
      <mkdir dir="${build.metacattest}"/>
474
      <javac srcdir="${junittestsdir}"
475
             destdir="${build.metacattest}"
486
      <javac srcdir="${build.tests}"
487
             destdir="${build.tests}"
476 488
             classpath="${cpath}:${build.dir}/${name}.jar"
477 489
       includes="**/*.java" />
478 490

  
479 491
      <jar jarfile="${build.dir}/${name}-junittests.jar"
480
        basedir="${build.metacattest}"
492
        basedir="${build.tests}"
481 493
                includes="**/*.class" />
482 494

  
483 495
      <!-- use the ant "junit" task to run JUnit tests. -->
484
      <junit printsummary="yes" haltonfailure="no" fork="no"
496
      <junit printsummary="yes" haltonfailure="no" fork="yes"
485 497
             haltonerror="no">
498
        <jvmarg value="-Djava.protocol.handler.pkgs=HTTPClient"/>
486 499
        <classpath>
487
          <pathelement path="${cpath}:${build.dir}/${name}.jar:${build.dir}/${name}-junittests.jar" />
500
          <pathelement path="${cpath}:${build.dir}/${name}.jar:${build.dir}/${name}-client.jar:${build.dir}/${name}-junittests.jar" />
488 501
        </classpath>
489 502

  
490 503
        <formatter type="plain" />
491 504

  
492 505
        <batchtest fork="yes" todir="${build.dir}">
493
          <fileset dir="${build.metacattest}">
494

  
506
          <fileset dir="${build.tests}">
495 507
            <include name="**/${testtorun}.class" />
496 508
          </fileset>
497 509
        </batchtest>

Also available in: Unified diff