Project

General

Profile

« Previous | Next » 

Revision 5340

Added by berkley almost 14 years ago

refactored XMLSchemaService to not have static methods. made the CrudServiceTest more robust.

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
50 50
 * A JUnit test for testing the dataone CrudService class
51 51
 */
52 52
public class CrudServiceTest extends MCTestCase 
53
{
54
  /**
55
   * consstructor for the test
56
   */
57
  public CrudServiceTest(String name)
58
  {
59
    super(name);
60
  }
53
{   
54
    /**
55
    * consstructor for the test
56
    */
57
    public CrudServiceTest(String name)
58
    {
59
        super(name);
60
        init();
61
    }
61 62
  
62
  /**
63
    /**
63 64
	 * Establish a testing framework by initializing appropriate objects
64 65
	 */
65 66
	public void setUp() throws Exception 
......
73 74
	public void tearDown() 
74 75
	{
75 76
	}
77
	
78
	public void init()
79
	{
80
	    try
81
	    {
82
            
83
        }
84
        catch(Exception e)
85
        {
86
            e.printStackTrace();
87
            fail("Could not initialize CrudServiceTest: " + e.getMessage());
88
        }
89
	}
76 90

  
77 91
	/**
78 92
	 * Create a suite of tests to be run together
......
82 96
		TestSuite suite = new TestSuite();
83 97
		suite.addTest(new CrudServiceTest("initialize"));
84 98
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
85
		suite.addTest(new CrudServiceTest("testCreate"));
99
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
86 100
		//suite.addTest(new CrudServiceTest(""));
87 101
		//suite.addTest(new CrudServiceTest(""));
88 102
		//suite.addTest(new CrudServiceTest(""));
89 103
		//suite.addTest(new CrudServiceTest(""));
90
		//suite.addTest(new CrudServiceTest(""));
91 104
		return suite;
92 105
	}
93 106
	
......
95 108
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
96 109
     *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
97 110
     *       InsufficientResources, InvalidSystemMetadata, NotImplemented
111
     *
112
     * public InputStream get(AuthToken token, Identifier guid)
113
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
114
     *       NotImplemented
98 115
	 */
99
	public void testCreate()
116
	public void testCreateAndGet()
100 117
	{
101 118
	    try
102 119
	    {
120
	        Identifier id;
121
            String testDoc;
122
            AuthToken token;
123
	        testDoc = "<?xml version=\"1.0\"?><test></test>\n";
124
            
103 125
            CrudService cs = CrudService.getInstance();
104
            
105 126
            //login and get a sessionid
106 127
            System.out.println("creating MetacatRestClient with url " + cs.getContextUrl());
107 128
            MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
108 129
            String username = PropertyService.getProperty("test.mcUser");
109
			String password = PropertyService.getProperty("test.mcPassword");
110
			System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
111
			String response = restClient.login(username, password);
112
			//System.out.println("response to login: " + response);
113
			String sessionid = restClient.getSessionId();
114
			SessionService sessionService = SessionService.getInstance();
115
			sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
116
			System.out.println("sessionid: " + sessionid);
117
			AuthToken token = new AuthToken(sessionid);
118
			
119
            String s = "<?xml version=\"1.0\"?><test></test>\n";
130
            String password = PropertyService.getProperty("test.mcPassword");
131
            System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
132
            String response = restClient.login(username, password);
133
            //System.out.println("response to login: " + response);
134
            String sessionid = restClient.getSessionId();
135
            SessionService sessionService = SessionService.getInstance();
136
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
137
            System.out.println("sessionid: " + sessionid);
138
            token = new AuthToken(sessionid);
120 139
            
121
            //create the system metadata then run the create method
122
            StringBufferInputStream sbis = new StringBufferInputStream(s);
140
            id = new Identifier();
123 141
            String docid = generateDocumentId();
124
            String smdocid = generateDocumentId();
125
            Identifier id = new Identifier();
126 142
            id.setValue(docid);
143
	                    
144
            //create the system metadata then run the create method
145
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
127 146
            SystemMetadata sm = new SystemMetadata();
128 147
            //set the id
129 148
            sm.setIdentifier(id);
130
            System.out.println("sm id is " + id);
149
            System.out.println("sm id is " + id.getValue());
131 150
            sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
132 151
            System.out.println("sm objectformat: " + sm.getObjectFormat());
133 152
            //create the checksum
134
            String checksumS = checksum(s);
153
            String checksumS = checksum(testDoc);
135 154
            ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
136 155
            Checksum checksum = new Checksum();
137 156
            checksum.setValue(checksumS);
......
139 158
            sm.setChecksum(checksum);
140 159
            System.out.println("sm checksum is " + checksumS);
141 160
            //set the size
142
            sm.setSize(s.getBytes().length);
143
            System.out.println("sm size: " + s.getBytes().length);
161
            sm.setSize(testDoc.getBytes().length);
162
            System.out.println("sm size: " + testDoc.getBytes().length);
144 163
            //submitter
145 164
            Principal p = new Principal();
146 165
            p.setValue("joe");
......
152 171
            nr.setValue("metacat");
153 172
            sm.setOriginMemberNode(nr);
154 173
            sm.setAuthoritativeMemberNode(nr);
174
            //create the doc
175
            cs.create(token, id, sbis, sm);
155 176
            
177
            //try to get the same doc then compare them
178
            InputStream gotDocStream = cs.get(token, id);
179
            StringBuffer sb = new StringBuffer();
180
            byte[] b = new byte[1024];
181
            int numread = gotDocStream.read(b, 0, 1024);
182
            while(numread != -1)
183
            {
184
                sb.append(new String(b, 0, numread));
185
                numread = gotDocStream.read(b, 0, 1024);
186
            }
156 187
            
157
            cs.create(token, id, sbis, sm);
188
            String gotDoc = sb.toString();
189
            System.out.println("got doc: " + gotDoc);
190
            assertTrue(gotDoc.trim().equals(testDoc.trim()));
158 191
        }
159 192
        catch(Exception e)
160 193
        {
src/edu/ucsb/nceas/metacat/DocumentImpl.java
3393 3393
            // Get an instance of the parser
3394 3394
            String parserName = PropertyService.getProperty("xml.saxparser");
3395 3395
            parser = XMLReaderFactory.createXMLReader(parserName);
3396
            XMLSchemaService.populateRegisteredSchemaList();
3396
            XMLSchemaService.getInstance().populateRegisteredSchemaList();
3397 3397
            if (ruleBase != null && ruleBase.equals(EML200)) {
3398 3398
                logMetacat.info("DocumentImpl.initalizeParser - Using eml 2.0.0 parser");
3399 3399
                chandler = new Eml200SAXHandler(dbconn, action, docid, rev,
......
3411 3411
                // From DB to find the register external schema location
3412 3412
                String externalSchemaLocation = null;
3413 3413
//                SchemaLocationResolver resolver = new SchemaLocationResolver();
3414
                externalSchemaLocation = XMLSchemaService.getNameSpaceAndLocationString();
3414
                externalSchemaLocation = XMLSchemaService.getInstance().getNameSpaceAndLocationString();
3415 3415
                logMetacat.debug("DocumentImpl.initalizeParser - 2.0.0 external schema location: " + externalSchemaLocation);
3416 3416
                // Set external schemalocation.
3417 3417
                if (externalSchemaLocation != null
......
3436 3436
                parser.setFeature(SCHEMAVALIDATIONFEATURE, true);
3437 3437
                // From DB to find the register external schema location
3438 3438
                String externalSchemaLocation = null;
3439
                externalSchemaLocation = XMLSchemaService.getNameSpaceAndLocationString();
3439
                externalSchemaLocation = XMLSchemaService.getInstance().getNameSpaceAndLocationString();
3440 3440
                logMetacat.debug("DocumentImpl.initalizeParser - 2.1.0 external schema location: " + externalSchemaLocation);
3441 3441
                // Set external schemalocation.
3442 3442
                if (externalSchemaLocation != null
src/edu/ucsb/nceas/metacat/service/XMLSchemaService.java
128 128
	 * 
129 129
	 * @return a list of XMLSchema objects holding registered schema information
130 130
	 */
131
	public static Vector<XMLSchema> getRegisteredSchemaList() {
131
	public Vector<XMLSchema> getRegisteredSchemaList() {
132 132
		return registeredSchemaList;
133 133
	}
134 134
	
......
140 140
	 * @return a string that holds space delimited registered namespaces and
141 141
	 *         locations.
142 142
	 */
143
	public static String getNameSpaceAndLocationString() {
143
	public String getNameSpaceAndLocationString() {
144 144
		return nameSpaceAndLocationString;
145 145
	}
146 146
	
......
150 150
	 * 
151 151
	 * @return a list that holds registered namespaces.
152 152
	 */
153
	public static Vector<String> getNameSpaceList() {
153
	public Vector<String> getNameSpaceList() {
154 154
		return nameSpaceList;
155 155
	}
156 156
	
......
162 162
	 * @return true if the xml.useFullSchemaValidation property is set to true,
163 163
	 *         false otherwise.
164 164
	 */
165
	public static boolean useFullSchemaValidation() {
165
	public boolean useFullSchemaValidation() {
166 166
		return useFullSchemaValidation;
167 167
	}
168 168
	
......
170 170
	 * sets the UseFullSchemaValidation variable.  The only way this should be
171 171
	 * set is in the constructor or the refresh methods.
172 172
	 */
173
	private static void setUseFullSchemaValidation() throws PropertyNotFoundException {
173
	private void setUseFullSchemaValidation() throws PropertyNotFoundException {
174 174
		String strUseFullSchemaValidation = 
175 175
			PropertyService.getProperty("xml.useFullSchemaValidation");
176 176
		useFullSchemaValidation = Boolean.valueOf(strUseFullSchemaValidation);
......
181 181
	 * xml_catalog table and then makes sure the schema actually exists and is
182 182
	 * readable on disk.
183 183
	 */
184
	public static void populateRegisteredSchemaList() {
184
	public void populateRegisteredSchemaList() {
185 185
		DBConnection conn = null;
186 186
		int serialNumber = -1;
187 187
		PreparedStatement pstmt = null;
src/edu/ucsb/nceas/metacat/SchemaLocationResolver.java
98 98
  
99 99
    // if name space is not in table
100 100
    if (nameSpace !=null && schemaLocation != null &&
101
        !XMLSchemaService.getNameSpaceList().contains(nameSpace))
101
        !XMLSchemaService.getInstance().getNameSpaceList().contains(nameSpace))
102 102
    {
103 103
       try
104 104
       {
......
113 113
        // because the schema was in the db but there was no file on disk.  If that's 
114 114
        // the case, it will show up in the name space list now.  If it doesn't we need
115 115
        // to register the schema in the database.
116
        if (!XMLSchemaService.getNameSpaceList().contains(nameSpace)) {
116
        if (!XMLSchemaService.getInstance().getNameSpaceList().contains(nameSpace)) {
117 117
        	registerSchema(newURLInMetacat);
118 118
        	ServiceService.refreshService("XMLSchemaService");
119 119
        }
......
259 259
      DBConnectionPool.returnDBConnection(conn, serialNumber);
260 260

  
261 261
    }//finally
262
    XMLSchemaService.populateRegisteredSchemaList();
262
    XMLSchemaService.getInstance().populateRegisteredSchemaList();
263 263
  }
264 264
  
265 265
  /*
......
295 295
       // Print out a empty schema list
296 296
       SchemaLocationResolver schema = new SchemaLocationResolver();
297 297
       logMetacat.warn("Namespace and Location String: "+ 
298
                                XMLSchemaService.getNameSpaceAndLocationString());
298
                                XMLSchemaService.getInstance().getNameSpaceAndLocationString());
299 299
       // input a schemalocation
300 300
       SchemaLocationResolver schema2 = new SchemaLocationResolver(
301 301
                                        "eml://ecoinformatics.org/eml-2.0.0 " +
......
309 309
       // print out new schema list in db
310 310
       SchemaLocationResolver schema4 = new SchemaLocationResolver();
311 311
       logMetacat.warn("Namespace and Location String: "+ 
312
    		   XMLSchemaService.getNameSpaceAndLocationString());
312
    		   XMLSchemaService.getInstance().getNameSpaceAndLocationString());
313 313
     }
314 314
     catch(Exception e)
315 315
     {

Also available in: Unified diff