Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: daigle $'
7
 *     '$Date: 2008-08-13 08:59:35 -0700 (Wed, 13 Aug 2008) $'
8
 * '$Revision: 4231 $'
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.FileReader;
28
import java.io.IOException;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.GregorianCalendar;
34
import java.util.SimpleTimeZone;
35
import java.util.TimeZone;
36

    
37
import edu.ucsb.nceas.metacat.DocumentImpl;
38
import edu.ucsb.nceas.metacat.McdbException;
39
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
40
import edu.ucsb.nceas.metacat.client.Metacat;
41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
42
import edu.ucsb.nceas.metacat.client.MetacatException;
43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45
import edu.ucsb.nceas.metacat.service.PropertyService;
46
import edu.ucsb.nceas.metacat.service.ServiceException;
47
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
48
import edu.ucsb.nceas.utilities.IOUtil;
49
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
50
import junit.framework.Test;
51
import junit.framework.TestCase;
52
import junit.framework.TestSuite;
53

    
54
/**
55
 * A JUnit test for testing the indexing routines for XML Paths
56
 */
57
public class BuildIndexTest extends TestCase
58
{
59
    private static String metacatUrl;
60
    private static String username;
61
	private static String password;
62
	static {
63
		try {
64
			PropertyService.getInstance("build/tests");
65
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
66
			username = PropertyService.getProperty("test.mcUser");
67
			password = PropertyService.getProperty("test.mcPassword");
68
		} catch (PropertyNotFoundException pnfe) {
69
			System.err.println("Could not get property in static block: " 
70
					+ pnfe.getMessage());
71
		} catch (ServiceException se) {
72
			System.err.println("Could not get PropertyService instance in static block: " 
73
					+ se.getMessage());
74
		}
75
	}
76

    
77
	private String prefix = "test";
78
	private String newdocid = null;
79
	private String testfile = "test/eml-sample.xml";
80
	private String testdocument = "";
81
	private Metacat m;
82
    /* Initialize properties */
83
    static
84
    {
85
  	  try
86
  	  {
87
  		  PropertyService.getInstance("build/tests");
88
  		  MetaCatUtil.pathsForIndexing 
89
  		         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
90
  	  }
91
  	  catch(Exception e)
92
  	  {
93
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
94
  	  }
95
    }
96

    
97
    /**
98
     * Constructor to build the test
99
     *
100
     * @param name the name of the test method
101
     */
102
    public BuildIndexTest(String name)
103
    {
104
        super(name);
105
        newdocid = generateDocid();
106
    }
107

    
108
    /**
109
     * Establish a testing framework by initializing appropriate objects
110
     */
111
    public void setUp()
112
    {
113
        try {
114
        	PropertyService.getInstance("build/tests");
115
            metacatUrl = PropertyService.getProperty("test.metacatUrl");
116
        } catch (ServiceException se) {
117
            fail(se.getMessage());
118
        } catch (PropertyNotFoundException pnfe) {
119
        	fail (pnfe.getMessage());
120
        }
121

    
122
        try {
123
            FileReader fr = new FileReader(testfile);
124
            testdocument = IOUtil.getAsString(fr, true);
125
        } catch (IOException ioe) {
126
            fail("Can't read test data to run the test: " + testfile);
127
        }
128
    }
129

    
130
    /**
131
     * Release any objects after tests are complete
132
     */
133
    public void tearDown()
134
    {
135
    }
136

    
137
    /**
138
     * Create a suite of tests to be run together
139
     */
140
    public static Test suite()
141
    {
142
      TestSuite suite = new TestSuite();
143
        suite.addTest(new BuildIndexTest("initialize"));
144
        suite.addTest(new BuildIndexTest("read"));
145
        suite.addTest(new BuildIndexTest("buildIndex"));
146
        return suite;
147
    }
148

    
149
    /**
150
     * Run an initial test that always passes to check that the test
151
     * harness is working.
152
     */
153
    public void initialize()
154
    {
155
        assertTrue(1 == 1);
156
    }
157

    
158
    /**
159
     * Test the read() function with a known document
160
     */
161
    public void read()
162
    {
163
        try {
164
            System.err.println("Test Metacat: " + metacatUrl);
165
            m = MetacatFactory.createMetacatConnection(metacatUrl);
166
            String identifier = newdocid + ".1";
167
            m.login(username, password);
168
            String response = m.insert(identifier,
169
                    new StringReader(testdocument), null);
170
            System.err.println(response);
171
            Reader r = m.read(newdocid+".1");
172
            String doc = IOUtil.getAsString(r, true);
173
            //doc = doc +"\n";
174
            System.err.println(doc);
175
            //assertTrue(doc.equals(testdocument));
176
        } catch (MetacatInaccessibleException mie) {
177
            System.err.println("Metacat is: " + metacatUrl);
178
            fail("Metacat connection failed." + mie.getMessage());
179
        } catch (MetacatAuthException mae) {
180
            fail("Authorization failed:\n" + mae.getMessage());
181
        } catch (InsufficientKarmaException ike) {
182
            assertTrue(1 == 1);
183
            fail("Insufficient karma:\n" + ike.getMessage());
184
        } catch (MetacatException me) {
185
            fail("Metacat Error:\n" + me.getMessage());
186
        } catch (Exception e) {
187
            fail("General exception:\n" + e.getMessage());
188
        }
189
    }
190

    
191
    /**
192
     * Test the buildIndex() function with a known document
193
     */
194
    public void buildIndex()
195
    {
196
        DocumentImpl d = null;
197
        try {
198
            d = new DocumentImpl(newdocid+".1", false);
199
            d.buildIndex();
200
        } catch (McdbException me) {
201
            System.err.println("Caught McdbException (1): " + me.getMessage());
202
        }
203
    }
204

    
205
    /**
206
     * Create a hopefully unique docid for testing insert and update. Does
207
     * not include the 'revision' part of the id.
208
     *
209
     * @return a String docid based on the current date and time
210
     */
211
    private String generateDocid()
212
    {
213
        StringBuffer docid = new StringBuffer(prefix);
214
        docid.append(".");
215

    
216
        // Create a calendar to get the date formatted properly
217
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
218
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
219
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
220
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
221
        Calendar calendar = new GregorianCalendar(pdt);
222
        Date trialTime = new Date();
223
        calendar.setTime(trialTime);
224
        docid.append(calendar.get(Calendar.YEAR));
225
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
226
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
227
        docid.append(calendar.get(Calendar.MINUTE));
228
        docid.append(calendar.get(Calendar.SECOND));
229

    
230
        return docid.toString();
231
    }
232
}
(3-3/19)