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-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
8
 * '$Revision: 4080 $'
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.Sitemap;
35
import edu.ucsb.nceas.metacat.util.SystemUtil;
36
import edu.ucsb.nceas.utilities.Options;
37
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38

    
39
import junit.framework.TestCase;
40

    
41
/**
42
 * Test the Sitemap class by generating the sitemaps in a separate directory.
43
 * 
44
 * @author Matt Jones
45
 */
46
public class SitemapTest extends TestCase {
47
	
48
	private static String propertyFileName;
49
	static {
50
		try {
51
			propertyFileName = SystemUtil.getContextDir() + "/WEB-INF/metacat.properties";
52
			// "/usr/share/tomcat5.5/webapps/knb/WEB-INF/metacat.properties";
53
		} catch (PropertyNotFoundException pnfe) {
54

    
55
		}
56
	}
57

    
58
    private String directoryName = "/tmp/sitemaps";
59

    
60
    /**
61
     * Initialize the Metacat environment so the test can run.
62
     */
63
    protected void setUp() throws Exception {
64
        super.setUp();
65
        try {
66
            File propertyFile = new File(propertyFileName);
67
            Options options = Options.initialize(propertyFile);
68
            DBConnectionPool pool = DBConnectionPool.getInstance();
69
        } catch (FileNotFoundException e) {
70
            fail(e.getMessage());
71
        } catch (IOException e) {
72
            fail(e.getMessage());
73
        }
74
    }
75

    
76
    /**
77
     * Test the static generateSitemaps() method.
78
     */
79
    public void testGenerateSitemaps() {
80
        File directory = new File(directoryName);
81
        directory.mkdirs();
82
        String urlRoot = "http://foo.example.com/ctx/metacat";
83
        String skin = "testskin";
84
        Sitemap smap = new Sitemap(directory, urlRoot, skin);
85
        smap.generateSitemaps();
86
        File sitemap1 = new File(directory, "metacat1.xml");
87
        assertTrue(sitemap1.exists() && sitemap1.isFile());
88
        try {
89
            FileReader r = new FileReader(sitemap1);
90
            BufferedReader br = new BufferedReader(r);
91
            char[] buf = new char[1024];
92
            br.read(buf, 0, 1024);
93
            br.close();
94
            String doc = new String(buf);
95
            assertTrue(doc.indexOf("<?xml") >= 0);
96
            assertTrue(doc.indexOf("<urlset") >= 0);
97
            assertTrue(doc.indexOf("<url>") >= 0);
98
            assertTrue(doc.indexOf("http:") >= 0);
99
        } catch (FileNotFoundException e) {
100
            fail("Failed to read the sitemap file." + e.getMessage());
101
        } catch (IOException ioe) {
102
            fail("Failed while reading sitemap file." + ioe.getMessage());
103
        }
104
    }
105
}
(15-15/18)