Revision 4510
Added by daigle about 16 years ago
test/edu/ucsb/nceas/metacattest/SitemapTest.java | ||
---|---|---|
29 | 29 |
import java.io.FileNotFoundException; |
30 | 30 |
import java.io.FileReader; |
31 | 31 |
import java.io.IOException; |
32 |
import java.util.Vector; |
|
32 | 33 |
|
33 | 34 |
import edu.ucsb.nceas.MCTestCase; |
34 | 35 |
import edu.ucsb.nceas.metacat.DBConnectionPool; |
35 | 36 |
import edu.ucsb.nceas.metacat.Sitemap; |
37 |
import edu.ucsb.nceas.metacat.client.MetacatAuthException; |
|
38 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
36 | 39 |
|
37 | 40 |
/** |
38 | 41 |
* Test the Sitemap class by generating the sitemaps in a separate directory. |
... | ... | |
50 | 53 |
super.setUp(); |
51 | 54 |
|
52 | 55 |
DBConnectionPool pool = DBConnectionPool.getInstance(); |
56 |
|
|
57 |
metacatConnectionNeeded = true; |
|
58 |
super.setUp(); |
|
53 | 59 |
} |
54 | 60 |
|
55 | 61 |
/** |
56 | 62 |
* Test the static generateSitemaps() method. |
57 | 63 |
*/ |
58 |
public void testGenerateSitemaps() { |
|
59 |
File directory = new File(directoryName); |
|
60 |
directory.mkdirs(); |
|
61 |
String urlRoot = "http://foo.example.com/ctx/metacat"; |
|
62 |
String skin = "testskin"; |
|
63 |
Sitemap smap = new Sitemap(directory, urlRoot, skin); |
|
64 |
smap.generateSitemaps(); |
|
65 |
File sitemap1 = new File(directory, "metacat1.xml"); |
|
66 |
assertTrue(sitemap1.exists() && sitemap1.isFile()); |
|
67 |
try { |
|
68 |
FileReader r = new FileReader(sitemap1); |
|
69 |
BufferedReader br = new BufferedReader(r); |
|
70 |
char[] buf = new char[1024]; |
|
71 |
br.read(buf, 0, 1024); |
|
72 |
br.close(); |
|
73 |
String doc = new String(buf); |
|
74 |
assertTrue(doc.indexOf("<?xml") >= 0); |
|
75 |
assertTrue(doc.indexOf("<urlset") >= 0); |
|
76 |
assertTrue(doc.indexOf("<url>") >= 0); |
|
77 |
assertTrue(doc.indexOf("http:") >= 0); |
|
78 |
} catch (FileNotFoundException e) { |
|
79 |
fail("Failed to read the sitemap file." + e.getMessage()); |
|
80 |
} catch (IOException ioe) { |
|
81 |
fail("Failed while reading sitemap file." + ioe.getMessage()); |
|
82 |
} |
|
64 |
public void testGenerateSitemaps() { |
|
65 |
try { |
|
66 |
debug("\nRunning: testGenerateSitemaps()"); |
|
67 |
|
|
68 |
// login |
|
69 |
debug("logging in as: username=" + username + " password=" + password); |
|
70 |
m.login(username, password); |
|
71 |
|
|
72 |
// insert 2.0.1 document |
|
73 |
String docid1 = generateDocumentId(); |
|
74 |
debug("inserting docid: " + docid1 + ".1 which has no access section"); |
|
75 |
testdocument = getTestEmlDoc("Doc with no access section", EML2_0_1, null, |
|
76 |
null, null, null, null, null, null, null, null); |
|
77 |
insertDocumentId(docid1 + ".1", testdocument, SUCCESS, false); |
|
78 |
readDocumentIdWhichEqualsDoc(docid1, testdocument, SUCCESS, false); |
|
79 |
|
|
80 |
String docid2 = generateDocumentId(); |
|
81 |
debug("inserting docid: " + docid2 + ".1 which has public read/write section"); |
|
82 |
Vector<String> accessRules1 = new Vector<String>(); |
|
83 |
String accessRule1 = generateOneAccessRule("public", true, true, true, false, false); |
|
84 |
accessRules1.add(accessRule1); |
|
85 |
String accessBlock = getAccessBlock(accessRules1, ALLOWFIRST); |
|
86 |
testdocument = getTestEmlDoc( |
|
87 |
"Doc with public read and write", EML2_0_1, |
|
88 |
null, null, null, null, accessBlock, null, null, null, null); |
|
89 |
insertDocumentId(docid2 + ".1", testdocument, SUCCESS, false); |
|
90 |
|
|
91 |
String docid3 = generateDocumentId(); |
|
92 |
debug("inserting docid: " + docid3 + ".1 which has which has " + username + " read/write section"); |
|
93 |
Vector<String> accessRules2 = new Vector<String>(); |
|
94 |
String accessRule2 = generateOneAccessRule(username, true, true, true, false, false); |
|
95 |
accessRules2.add(accessRule2); |
|
96 |
String accessBlock2 = getAccessBlock(accessRules2, ALLOWFIRST); |
|
97 |
testdocument = getTestEmlDoc( |
|
98 |
"Doc with public read and write", EML2_0_1, |
|
99 |
null, null, null, null, accessBlock2, null, null, null, null); |
|
100 |
insertDocumentId(docid3 + ".1", testdocument, SUCCESS, false); |
|
101 |
|
|
102 |
debug("logging out"); |
|
103 |
m.logout(); |
|
104 |
|
|
105 |
File directory = new File(directoryName); |
|
106 |
directory.mkdirs(); |
|
107 |
String urlRoot = "http://foo.example.com/ctx/metacat"; |
|
108 |
String skin = "testskin"; |
|
109 |
Sitemap smap = new Sitemap(directory, urlRoot, skin); |
|
110 |
smap.generateSitemaps(); |
|
111 |
File sitemap1 = new File(directory, "metacat1.xml"); |
|
112 |
assertTrue(sitemap1.exists() && sitemap1.isFile()); |
|
113 |
|
|
114 |
FileReader r = new FileReader(sitemap1); |
|
115 |
BufferedReader br = new BufferedReader(r); |
|
116 |
char[] buf = new char[1024]; |
|
117 |
br.read(buf, 0, 1024); |
|
118 |
br.close(); |
|
119 |
String doc = new String(buf); |
|
120 |
//debug("**** sitemap doc *** \n"); |
|
121 |
//debug(doc); |
|
122 |
assertTrue(doc.indexOf("<?xml") >= 0); |
|
123 |
assertTrue(doc.indexOf("<urlset") >= 0); |
|
124 |
assertTrue(doc.indexOf("<url>") >= 0); |
|
125 |
assertTrue(doc.indexOf("http:") >= 0); |
|
126 |
|
|
127 |
// only docid 2 should show up in the sitemap. |
|
128 |
assertTrue(doc.indexOf(docid1) == -1); |
|
129 |
assertTrue(doc.indexOf(docid2) >= 0); |
|
130 |
assertTrue(doc.indexOf(docid3) == -1); |
|
131 |
} catch (FileNotFoundException e) { |
|
132 |
fail("Failed to read the sitemap file." + e.getMessage()); |
|
133 |
} catch (IOException ioe) { |
|
134 |
fail("Failed while reading sitemap file." + ioe.getMessage()); |
|
135 |
} catch (MetacatAuthException mae) { |
|
136 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
137 |
} catch (MetacatInaccessibleException mie) { |
|
138 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
139 |
} catch (Exception e) { |
|
140 |
fail("General exception:\n" + e.getMessage()); |
|
141 |
} |
|
83 | 142 |
} |
84 | 143 |
} |
Also available in: Unified diff
Insert documents with different permissions and make sure only the publically readable ones get pulled into the sitemap file.