Project

General

Profile

« Previous | Next » 

Revision 7379

use CDATA for docname field in docInfo so that XML parser ignores the content that can contain characters like "&

View differences:

test/edu/ucsb/nceas/metacat/replication/ReplicationTest.java
29 29
import java.io.Reader;
30 30
import java.io.StringReader;
31 31
import java.net.URL;
32
import java.util.Hashtable;
32 33

  
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
36

  
33 37
import org.apache.commons.io.IOUtils;
38
import org.xml.sax.InputSource;
39
import org.xml.sax.XMLReader;
34 40

  
35
import junit.framework.Test;
36
import junit.framework.TestSuite;
37 41
import edu.ucsb.nceas.MCTestCase;
42
import edu.ucsb.nceas.metacat.DocInfoHandler;
38 43
import edu.ucsb.nceas.metacat.MetaCatServlet;
39 44
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
40
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
41 45
import edu.ucsb.nceas.metacat.client.Metacat;
42 46
import edu.ucsb.nceas.metacat.client.MetacatFactory;
43 47
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
44 48
import edu.ucsb.nceas.metacat.properties.PropertyService;
45 49
import edu.ucsb.nceas.metacat.util.DocumentUtil;
46 50
import edu.ucsb.nceas.metacat.util.MetacatUtil;
51
import edu.ucsb.nceas.metacat.util.ReplicationUtil;
47 52

  
48 53
/**
49 54
 * A JUnit test for testing Metacat replication
......
102 107
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
103 108
        suite.addTest(new ReplicationTest("testReplicateEML_AtoB"));
104 109
        suite.addTest(new ReplicationTest("testReplicateDataLocking"));
105

  
110
        suite.addTest(new ReplicationTest("testDocumentInfo"));
111
        
106 112
        return suite;
107 113
    }
108 114

  
......
358 364
			fail(e.getMessage());
359 365
		}
360 366
	}
367
	
368
	/**
369
	 * Tests the docInfo parser using reserved characters (&) in CDATA tags
370
	 */
371
	public void testDocumentInfo() {
372
		try {
373
			// the id
374
			String baseDocid = DocumentUtil.generateDocumentId("replicationTest", 0);
375
			String docid = baseDocid + "." + 1;
376
			
377
			// the test data
378
			String object = "test";
379
			String fileName = "testObject with & in filename";
380
			
381
			// insert data locally
382
			m.login(username, password);
383
			m.upload(docid, fileName, IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
384
    		
385
    		// get the docinfo string
386
    		String docInfoStr = ReplicationService.getDocumentInfo(docid);
387
    					
388
    		System.out.println("docInfoStr: " + docInfoStr);
389
    		
390
			// strip out the system metadata portion
391
		    String systemMetadataXML = ReplicationUtil.getSystemMetadataContent(docInfoStr);
392
		   	docInfoStr = ReplicationUtil.getContentWithoutSystemMetadata(docInfoStr);
361 393

  
394
			//dih is the parser for the docinfo xml format
395
			DocInfoHandler dih = new DocInfoHandler();
396
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
397
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
398
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
399
			
400
			// get the docname for testing &
401
			String docName = (String) docinfoHash.get("docname");
402
    		System.out.println("docName: " + docName);
403
    		
404
			assertEquals(fileName, docName);
405

  
406
    		// test user
407
    		String user = (String) docinfoHash.get("user_owner");
408
    		System.out.println("user_owner: " + user);
409
			assertEquals(username, user);
410

  
411
		} catch (Exception e) {
412
			e.printStackTrace();
413
			fail(e.getMessage());
414
		}
415
	}
416

  
362 417
    
363 418
}
364 419

  
src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
1041 1041
		Calendar updated = Calendar.getInstance();
1042 1042
		updated.setTime(doc.getUpdateDate());
1043 1043
		
1044
		sb.append("<docname>").append(doc.getDocname());
1045
		sb.append("</docname><doctype>").append(doc.getDoctype());
1044
		sb.append("<docname><![CDATA[").append(doc.getDocname());
1045
		sb.append("]]></docname><doctype>").append(doc.getDoctype());
1046 1046
		sb.append("</doctype>");
1047 1047
		sb.append("<user_owner>").append(doc.getUserowner());
1048 1048
		sb.append("</user_owner><user_updated>").append(doc.getUserupdated());

Also available in: Unified diff