1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2007 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: daigle $'
|
7
|
* '$Date: 2009-08-06 10:59:40 -0700 (Thu, 06 Aug 2009) $'
|
8
|
* '$Revision: 5016 $'
|
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
|
import java.util.Vector;
|
31
|
|
32
|
import edu.ucsb.nceas.MCTestCase;
|
33
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
34
|
import edu.ucsb.nceas.metacat.Sitemap;
|
35
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
36
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
37
|
import edu.ucsb.nceas.utilities.FileUtil;
|
38
|
|
39
|
/**
|
40
|
* Test the Sitemap class by generating the sitemaps in a separate directory.
|
41
|
*
|
42
|
* @author Matt Jones
|
43
|
*/
|
44
|
public class SitemapTest extends MCTestCase {
|
45
|
|
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
|
|
54
|
DBConnectionPool pool = DBConnectionPool.getInstance();
|
55
|
|
56
|
metacatConnectionNeeded = true;
|
57
|
super.setUp();
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Test the static generateSitemaps() method.
|
62
|
*/
|
63
|
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
|
// create the directory if it does not exist
|
105
|
FileUtil.createDirectory(directoryName);
|
106
|
|
107
|
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
|
|
113
|
File sitemap1 = new File(directory, "metacat1.xml");
|
114
|
assertTrue(sitemap1.exists() && sitemap1.isFile());
|
115
|
|
116
|
String doc = FileUtil.readFileToString(directoryName + FileUtil.getFS() + "metacat1.xml");
|
117
|
debug("**** sitemap doc *** \n");
|
118
|
debug(doc);
|
119
|
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 (MetacatAuthException mae) {
|
129
|
fail("Authorization failed:\n" + mae.getMessage());
|
130
|
} catch (MetacatInaccessibleException mie) {
|
131
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
132
|
} catch (Exception e) {
|
133
|
fail("General exception:\n" + e.getMessage());
|
134
|
}
|
135
|
}
|
136
|
}
|