1 |
9914
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2010 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
*
|
6 |
|
|
* '$Author: tao $'
|
7 |
|
|
* '$Date: 2016-09-02 17:58:12 -0900 $'
|
8 |
|
|
* '$Revision: 5211 $'
|
9 |
|
|
*
|
10 |
|
|
* This program is free software; you can redistribute it and/or modify
|
11 |
|
|
* it under the terms of the GNU General Public License as published by
|
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
13 |
|
|
* (at your option) any later version.
|
14 |
|
|
*
|
15 |
|
|
* This program is distributed in the hope that it will be useful,
|
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
* GNU General Public License for more details.
|
19 |
|
|
*
|
20 |
|
|
* You should have received a copy of the GNU General Public License
|
21 |
|
|
* along with this program; if not, write to the Free Software
|
22 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23 |
|
|
*/
|
24 |
|
|
package edu.ucsb.nceas.metacattest;
|
25 |
|
|
|
26 |
|
|
import java.io.File;
|
27 |
|
|
import java.io.StringReader;
|
28 |
|
|
|
29 |
|
|
import junit.framework.Test;
|
30 |
|
|
import junit.framework.TestSuite;
|
31 |
|
|
|
32 |
|
|
import org.xml.sax.*;
|
33 |
|
|
|
34 |
|
|
import edu.ucsb.nceas.MCTestCase;
|
35 |
|
|
import edu.ucsb.nceas.metacat.AccessionNumber;
|
36 |
|
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
37 |
|
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
38 |
|
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
39 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
40 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
41 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
42 |
|
|
|
43 |
|
|
import org.apache.commons.io.FileUtils;
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
public class MetacatValidationAlgorithmTest extends MCTestCase {
|
47 |
|
|
|
48 |
|
|
public MetacatValidationAlgorithmTest (String name) {
|
49 |
|
|
super(name);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Create a suite of tests to be run together
|
54 |
|
|
*/
|
55 |
|
|
public static Test suite() {
|
56 |
|
|
TestSuite suite = new TestSuite();
|
57 |
|
|
// Test basic functions
|
58 |
|
|
suite.addTest(new MetacatValidationAlgorithmTest("testRegisteredDTDDucoment"));
|
59 |
|
|
suite.addTest(new MetacatValidationAlgorithmTest("testRegisteredDTDInvalidDucoment"));
|
60 |
|
|
suite.addTest(new MetacatValidationAlgorithmTest("testUnRegisteredDTDDucoment"));
|
61 |
|
|
suite.addTest(new MetacatValidationAlgorithmTest("testValidationUnneededDucoment"));
|
62 |
|
|
return suite;
|
63 |
|
|
}
|
64 |
|
|
/**
|
65 |
|
|
* Initialize the connection to metacat, and insert a document to be
|
66 |
|
|
* used for testing with a known docid.
|
67 |
|
|
*/
|
68 |
|
|
public void setUp() throws Exception{
|
69 |
|
|
metacatConnectionNeeded = true;
|
70 |
|
|
super.setUp();
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Insert a test document which's dtd was registered, returning the docid that was used.
|
79 |
|
|
*/
|
80 |
|
|
public void testRegisteredDTDDucoment() throws Exception {
|
81 |
|
|
String xml = FileUtils.readFileToString(new File("./test/jones.204.22.xml"), "UTF-8");
|
82 |
|
|
String docid = generateDocumentId() + ".1";
|
83 |
|
|
try {
|
84 |
|
|
m.login(username, password);
|
85 |
|
|
String response = insertDocumentId(docid, xml, true, false);
|
86 |
|
|
assertTrue(response.contains(docid));
|
87 |
|
|
} catch (MetacatAuthException e) {
|
88 |
|
|
fail(e.getMessage());
|
89 |
|
|
} catch (MetacatInaccessibleException e) {
|
90 |
|
|
fail(e.getMessage());
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Insert a test document which's dtd was registered. But the xml doesn't follow the dta and returning an error.
|
96 |
|
|
*/
|
97 |
|
|
public void testRegisteredDTDInvalidDucoment() throws Exception {
|
98 |
|
|
String xml = FileUtils.readFileToString(new File("./test/jones.204.22.xml.invalid"), "UTF-8");
|
99 |
|
|
String docid = generateDocumentId() + ".1";
|
100 |
|
|
m.login(username, password);
|
101 |
|
|
try {
|
102 |
|
|
String response = m.insert(docid, new StringReader(xml), null);
|
103 |
|
|
fail("we shouldn't get get since we inserted an invalid xml");
|
104 |
|
|
} catch (MetacatException e) {
|
105 |
|
|
assertTrue(e.getMessage().contains("<error>"));
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
/**
|
112 |
|
|
* Insert a test document which's dtd was registered. But the xml doesn't follow the dta and returning an error.
|
113 |
|
|
*/
|
114 |
|
|
public void testUnRegisteredDTDDucoment() throws Exception {
|
115 |
|
|
String xml = FileUtils.readFileToString(new File("./test/doc_with_unregister_dtd.xml"), "UTF-8");
|
116 |
|
|
String docid = generateDocumentId() + ".1";
|
117 |
|
|
m.login(username, password);
|
118 |
|
|
try {
|
119 |
|
|
String response = m.insert(docid, new StringReader(xml), null);
|
120 |
|
|
fail("We can't get since the inserted xml has a unregistered dtd");
|
121 |
|
|
} catch (MetacatException e) {
|
122 |
|
|
assertTrue(e.getMessage().contains("<error>"));
|
123 |
|
|
assertTrue(e.getMessage().contains("isn't registered in Metacat"));
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Insert test documents which don't need to be validated: with embeded dtd or no declaration at all.
|
129 |
|
|
*/
|
130 |
|
|
public void testValidationUnneededDucoment() throws Exception {
|
131 |
|
|
//with embedded dtd
|
132 |
|
|
String xml = FileUtils.readFileToString(new File("./test/doc_with_embedded_dtd.xml"), "UTF-8");
|
133 |
|
|
String docid = generateDocumentId() + ".1";
|
134 |
|
|
try {
|
135 |
|
|
m.login(username, password);
|
136 |
|
|
String response = insertDocumentId(docid, xml, true, false);
|
137 |
|
|
assertTrue(response.contains(docid));
|
138 |
|
|
} catch (MetacatAuthException e) {
|
139 |
|
|
fail(e.getMessage());
|
140 |
|
|
} catch (MetacatInaccessibleException e) {
|
141 |
|
|
fail(e.getMessage());
|
142 |
|
|
}
|
143 |
|
|
//without dtd and scheam declaration
|
144 |
|
|
xml = FileUtils.readFileToString(new File("./test/doc_without_declaration.xml"), "UTF-8");
|
145 |
|
|
docid = generateDocumentId() + ".1";
|
146 |
|
|
try {
|
147 |
|
|
m.login(username, password);
|
148 |
|
|
String response = insertDocumentId(docid, xml, true, false);
|
149 |
|
|
assertTrue(response.contains(docid));
|
150 |
|
|
} catch (MetacatAuthException e) {
|
151 |
|
|
fail(e.getMessage());
|
152 |
|
|
} catch (MetacatInaccessibleException e) {
|
153 |
|
|
fail(e.getMessage());
|
154 |
|
|
}
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
}
|