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-15 10:16:18 -0700 (Tue, 15 Jul 2008) $'
8
 * '$Revision: 4127 $'
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.service.PropertyService;
36
import edu.ucsb.nceas.metacat.service.ServiceException;
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

    
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
        try {
55
  		  	PropertyService.getInstance("build/tests");
56
            DBConnectionPool pool = DBConnectionPool.getInstance();
57
        } catch (ServiceException se) {
58
            fail(se.getMessage());
59
        }
60
    }
61

    
62
    /**
63
     * Test the static generateSitemaps() method.
64
     */
65
    public void testGenerateSitemaps() {
66
        File directory = new File(directoryName);
67
        directory.mkdirs();
68
        String urlRoot = "http://foo.example.com/ctx/metacat";
69
        String skin = "testskin";
70
        Sitemap smap = new Sitemap(directory, urlRoot, skin);
71
        smap.generateSitemaps();
72
        File sitemap1 = new File(directory, "metacat1.xml");
73
        assertTrue(sitemap1.exists() && sitemap1.isFile());
74
        try {
75
            FileReader r = new FileReader(sitemap1);
76
            BufferedReader br = new BufferedReader(r);
77
            char[] buf = new char[1024];
78
            br.read(buf, 0, 1024);
79
            br.close();
80
            String doc = new String(buf);
81
            assertTrue(doc.indexOf("<?xml") >= 0);
82
            assertTrue(doc.indexOf("<urlset") >= 0);
83
            assertTrue(doc.indexOf("<url>") >= 0);
84
            assertTrue(doc.indexOf("http:") >= 0);
85
        } catch (FileNotFoundException e) {
86
            fail("Failed to read the sitemap file." + e.getMessage());
87
        } catch (IOException ioe) {
88
            fail("Failed while reading sitemap file." + ioe.getMessage());
89
        }
90
    }
91
}
(16-16/19)