1 |
3244
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2007 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
*
|
6 |
|
|
* '$Author$'
|
7 |
|
|
* '$Date$'
|
8 |
|
|
* '$Revision$'
|
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 |
|
|
|
25 |
|
|
package edu.ucsb.nceas.metacattest;
|
26 |
|
|
|
27 |
|
|
import java.io.File;
|
28 |
|
|
import java.io.FileNotFoundException;
|
29 |
|
|
import java.io.IOException;
|
30 |
4510
|
daigle
|
import java.util.Vector;
|
31 |
3244
|
jones
|
|
32 |
4384
|
daigle
|
import edu.ucsb.nceas.MCTestCase;
|
33 |
3244
|
jones
|
import edu.ucsb.nceas.metacat.DBConnectionPool;
|
34 |
|
|
import edu.ucsb.nceas.metacat.Sitemap;
|
35 |
4510
|
daigle
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
36 |
|
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
37 |
4735
|
daigle
|
import edu.ucsb.nceas.utilities.FileUtil;
|
38 |
3244
|
jones
|
|
39 |
|
|
/**
|
40 |
|
|
* Test the Sitemap class by generating the sitemaps in a separate directory.
|
41 |
|
|
*
|
42 |
|
|
* @author Matt Jones
|
43 |
|
|
*/
|
44 |
4384
|
daigle
|
public class SitemapTest extends MCTestCase {
|
45 |
3244
|
jones
|
|
46 |
|
|
private String directoryName = "/tmp/sitemaps";
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* Initialize the Metacat environment so the test can run.
|
50 |
|
|
*/
|
51 |
|
|
protected void setUp() throws Exception {
|
52 |
|
|
super.setUp();
|
53 |
4384
|
daigle
|
|
54 |
|
|
DBConnectionPool pool = DBConnectionPool.getInstance();
|
55 |
4510
|
daigle
|
|
56 |
|
|
metacatConnectionNeeded = true;
|
57 |
|
|
super.setUp();
|
58 |
3244
|
jones
|
}
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Test the static generateSitemaps() method.
|
62 |
|
|
*/
|
63 |
4510
|
daigle
|
public void testGenerateSitemaps() {
|
64 |
|
|
try {
|
65 |
|
|
debug("\nRunning: testGenerateSitemaps()");
|
66 |
|
|
|
67 |
|
|
// login
|
68 |
|
|
debug("logging in as: username=" + username + " password=" + password);
|
69 |
|
|
m.login(username, password);
|
70 |
|
|
|
71 |
|
|
// insert 2.0.1 document
|
72 |
|
|
String docid1 = generateDocumentId();
|
73 |
|
|
debug("inserting docid: " + docid1 + ".1 which has no access section");
|
74 |
|
|
testdocument = getTestEmlDoc("Doc with no access section", EML2_0_1, null,
|
75 |
|
|
null, null, null, null, null, null, null, null);
|
76 |
|
|
insertDocumentId(docid1 + ".1", testdocument, SUCCESS, false);
|
77 |
|
|
readDocumentIdWhichEqualsDoc(docid1, testdocument, SUCCESS, false);
|
78 |
|
|
|
79 |
|
|
String docid2 = generateDocumentId();
|
80 |
|
|
debug("inserting docid: " + docid2 + ".1 which has public read/write section");
|
81 |
|
|
Vector<String> accessRules1 = new Vector<String>();
|
82 |
|
|
String accessRule1 = generateOneAccessRule("public", true, true, true, false, false);
|
83 |
|
|
accessRules1.add(accessRule1);
|
84 |
|
|
String accessBlock = getAccessBlock(accessRules1, ALLOWFIRST);
|
85 |
|
|
testdocument = getTestEmlDoc(
|
86 |
|
|
"Doc with public read and write", EML2_0_1,
|
87 |
|
|
null, null, null, null, accessBlock, null, null, null, null);
|
88 |
|
|
insertDocumentId(docid2 + ".1", testdocument, SUCCESS, false);
|
89 |
|
|
|
90 |
|
|
String docid3 = generateDocumentId();
|
91 |
|
|
debug("inserting docid: " + docid3 + ".1 which has which has " + username + " read/write section");
|
92 |
|
|
Vector<String> accessRules2 = new Vector<String>();
|
93 |
|
|
String accessRule2 = generateOneAccessRule(username, true, true, true, false, false);
|
94 |
|
|
accessRules2.add(accessRule2);
|
95 |
|
|
String accessBlock2 = getAccessBlock(accessRules2, ALLOWFIRST);
|
96 |
|
|
testdocument = getTestEmlDoc(
|
97 |
|
|
"Doc with public read and write", EML2_0_1,
|
98 |
|
|
null, null, null, null, accessBlock2, null, null, null, null);
|
99 |
|
|
insertDocumentId(docid3 + ".1", testdocument, SUCCESS, false);
|
100 |
|
|
|
101 |
|
|
debug("logging out");
|
102 |
|
|
m.logout();
|
103 |
|
|
|
104 |
4735
|
daigle
|
// create the directory if it does not exist
|
105 |
|
|
FileUtil.createDirectory(directoryName);
|
106 |
|
|
|
107 |
4510
|
daigle
|
File directory = new File(directoryName);
|
108 |
|
|
String urlRoot = "http://foo.example.com/ctx/metacat";
|
109 |
|
|
String skin = "testskin";
|
110 |
|
|
Sitemap smap = new Sitemap(directory, urlRoot, skin);
|
111 |
|
|
smap.generateSitemaps();
|
112 |
4735
|
daigle
|
|
113 |
4510
|
daigle
|
File sitemap1 = new File(directory, "metacat1.xml");
|
114 |
|
|
assertTrue(sitemap1.exists() && sitemap1.isFile());
|
115 |
4735
|
daigle
|
|
116 |
|
|
String doc = FileUtil.readFileToString(directoryName + FileUtil.getFS() + "metacat1.xml");
|
117 |
|
|
debug("**** sitemap doc *** \n");
|
118 |
|
|
debug(doc);
|
119 |
4510
|
daigle
|
assertTrue(doc.indexOf("<?xml") >= 0);
|
120 |
|
|
assertTrue(doc.indexOf("<urlset") >= 0);
|
121 |
|
|
assertTrue(doc.indexOf("<url>") >= 0);
|
122 |
|
|
assertTrue(doc.indexOf("http:") >= 0);
|
123 |
|
|
|
124 |
|
|
// only docid 2 should show up in the sitemap.
|
125 |
|
|
assertTrue(doc.indexOf(docid1) == -1);
|
126 |
|
|
assertTrue(doc.indexOf(docid2) >= 0);
|
127 |
|
|
assertTrue(doc.indexOf(docid3) == -1);
|
128 |
|
|
} catch (FileNotFoundException e) {
|
129 |
|
|
fail("Failed to read the sitemap file." + e.getMessage());
|
130 |
|
|
} catch (IOException ioe) {
|
131 |
|
|
fail("Failed while reading sitemap file." + ioe.getMessage());
|
132 |
|
|
} catch (MetacatAuthException mae) {
|
133 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
134 |
|
|
} catch (MetacatInaccessibleException mie) {
|
135 |
|
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
136 |
|
|
} catch (Exception e) {
|
137 |
|
|
fail("General exception:\n" + e.getMessage());
|
138 |
|
|
}
|
139 |
3244
|
jones
|
}
|
140 |
|
|
}
|