Project

General

Profile

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: 2008-11-04 16:03:44 -0800 (Tue, 04 Nov 2008) $'
8
 * '$Revision: 4510 $'
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.BufferedReader;
28
import java.io.File;
29
import java.io.FileNotFoundException;
30
import java.io.FileReader;
31
import java.io.IOException;
32
import java.util.Vector;
33

    
34
import edu.ucsb.nceas.MCTestCase;
35
import edu.ucsb.nceas.metacat.DBConnectionPool;
36
import edu.ucsb.nceas.metacat.Sitemap;
37
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
38
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
39

    
40
/**
41
 * Test the Sitemap class by generating the sitemaps in a separate directory.
42
 * 
43
 * @author Matt Jones
44
 */
45
public class SitemapTest extends MCTestCase {
46

    
47
    private String directoryName = "/tmp/sitemaps";
48

    
49
    /**
50
     * Initialize the Metacat environment so the test can run.
51
     */
52
    protected void setUp() throws Exception {
53
        super.setUp();
54

    
55
        DBConnectionPool pool = DBConnectionPool.getInstance();
56
        
57
		metacatConnectionNeeded = true;
58
		super.setUp();
59
    }
60

    
61
    /**
62
     * Test the static generateSitemaps() method.
63
     */
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
		}
142
    }
143
}
(17-17/20)