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: tao $'
|
7
|
* '$Date: 2008-05-15 18:25:04 -0700 (Thu, 15 May 2008) $'
|
8
|
* '$Revision: 3875 $'
|
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
|
|
33
|
import edu.ucsb.nceas.metacat.DBConnectionPool;
|
34
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
35
|
import edu.ucsb.nceas.metacat.Sitemap;
|
36
|
import edu.ucsb.nceas.utilities.Options;
|
37
|
|
38
|
import junit.framework.TestCase;
|
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 TestCase {
|
46
|
/* Initialize Options*/
|
47
|
static
|
48
|
{
|
49
|
try
|
50
|
{
|
51
|
Options.initialize(new File("build/tests/metacat.properties"));
|
52
|
MetaCatUtil.pathsForIndexing
|
53
|
= MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
|
54
|
}
|
55
|
catch(Exception e)
|
56
|
{
|
57
|
System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
|
58
|
}
|
59
|
}
|
60
|
private String propertyFileName =
|
61
|
MetaCatUtil.getOption("installDir") + "/WEB-INF/metacat.properties";
|
62
|
// "/usr/share/tomcat5.5/webapps/knb/WEB-INF/metacat.properties";
|
63
|
|
64
|
private String directoryName = "/tmp/sitemaps";
|
65
|
|
66
|
/**
|
67
|
* Initialize the Metacat environment so the test can run.
|
68
|
*/
|
69
|
protected void setUp() throws Exception {
|
70
|
super.setUp();
|
71
|
try {
|
72
|
File propertyFile = new File(propertyFileName);
|
73
|
Options options = Options.initialize(propertyFile);
|
74
|
MetaCatUtil util = new MetaCatUtil();
|
75
|
DBConnectionPool pool = DBConnectionPool.getInstance();
|
76
|
} catch (FileNotFoundException e) {
|
77
|
fail(e.getMessage());
|
78
|
} catch (IOException e) {
|
79
|
fail(e.getMessage());
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Test the static generateSitemaps() method.
|
85
|
*/
|
86
|
public void testGenerateSitemaps() {
|
87
|
File directory = new File(directoryName);
|
88
|
directory.mkdirs();
|
89
|
String urlRoot = "http://foo.example.com/ctx/metacat";
|
90
|
String skin = "testskin";
|
91
|
Sitemap smap = new Sitemap(directory, urlRoot, skin);
|
92
|
smap.generateSitemaps();
|
93
|
File sitemap1 = new File(directory, "metacat1.xml");
|
94
|
assertTrue(sitemap1.exists() && sitemap1.isFile());
|
95
|
try {
|
96
|
FileReader r = new FileReader(sitemap1);
|
97
|
BufferedReader br = new BufferedReader(r);
|
98
|
char[] buf = new char[1024];
|
99
|
br.read(buf, 0, 1024);
|
100
|
br.close();
|
101
|
String doc = new String(buf);
|
102
|
assertTrue(doc.indexOf("<?xml") >= 0);
|
103
|
assertTrue(doc.indexOf("<urlset") >= 0);
|
104
|
assertTrue(doc.indexOf("<url>") >= 0);
|
105
|
assertTrue(doc.indexOf("http:") >= 0);
|
106
|
} catch (FileNotFoundException e) {
|
107
|
fail("Failed to read the sitemap file." + e.getMessage());
|
108
|
} catch (IOException ioe) {
|
109
|
fail("Failed while reading sitemap file." + ioe.getMessage());
|
110
|
}
|
111
|
}
|
112
|
}
|