Project

General

Profile

« Previous | Next » 

Revision 4384

Added by daigle over 15 years ago

Convert remainder of tests that use PropertyService to extend the MCTestCase class

View differences:

BuildIndexTest.java
34 34
import java.util.SimpleTimeZone;
35 35
import java.util.TimeZone;
36 36

  
37
import edu.ucsb.nceas.MCTestCase;
37 38
import edu.ucsb.nceas.metacat.DocumentImpl;
38 39
import edu.ucsb.nceas.metacat.McdbException;
39 40
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
......
49 50
import edu.ucsb.nceas.utilities.IOUtil;
50 51
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51 52
import junit.framework.Test;
52
import junit.framework.TestCase;
53 53
import junit.framework.TestSuite;
54 54

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

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

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

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

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

  
132
    /**
133
     * Release any objects after tests are complete
134
     */
135
    public void tearDown()
136
    {
137
    }
123
	/**
124
	 * Release any objects after tests are complete
125
	 */
126
	public void tearDown() {
127
	}
138 128

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

  
151
    /**
152
     * Run an initial test that always passes to check that the test
153
     * harness is working.
154
     */
155
    public void initialize()
156
    {
157
        assertTrue(1 == 1);
158
    }
140
	/**
141
	 * Run an initial test that always passes to check that the test harness is
142
	 * working.
143
	 */
144
	public void initialize() {
145
		assertTrue(1 == 1);
146
	}
159 147

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

  
193
    /**
194
     * Test the buildIndex() function with a known document
195
     */
196
    public void buildIndex()
197
    {
198
        DocumentImpl d = null;
199
        try {
200
            d = new DocumentImpl(newdocid+".1", false);
201
            d.buildIndex();
202
        } catch (McdbException me) {
203
            System.err.println("Caught McdbException (1): " + me.getMessage());
204
        }
205
    }
179
	/**
180
	 * Test the buildIndex() function with a known document
181
	 */
182
	public void buildIndex() {
183
		DocumentImpl d = null;
184
		try {
185
			d = new DocumentImpl(newdocid + ".1", false);
186
			d.buildIndex();
187
		} catch (McdbException me) {
188
			System.err.println("Caught McdbException (1): " + me.getMessage());
189
		}
190
	}
206 191

  
207
    /**
208
     * Create a hopefully unique docid for testing insert and update. Does
209
     * not include the 'revision' part of the id.
210
     *
211
     * @return a String docid based on the current date and time
212
     */
213
    private String generateDocid()
214
    {
215
        StringBuffer docid = new StringBuffer(prefix);
216
        docid.append(".");
192
	/**
193
	 * Create a hopefully unique docid for testing insert and update. Does not
194
	 * include the 'revision' part of the id.
195
	 * 
196
	 * @return a String docid based on the current date and time
197
	 */
198
	private String generateDocid() {
199
		StringBuffer docid = new StringBuffer(prefix);
200
		docid.append(".");
217 201

  
218
        // Create a calendar to get the date formatted properly
219
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
220
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
221
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
222
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
223
        Calendar calendar = new GregorianCalendar(pdt);
224
        Date trialTime = new Date();
225
        calendar.setTime(trialTime);
226
        docid.append(calendar.get(Calendar.YEAR));
227
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
228
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
229
        docid.append(calendar.get(Calendar.MINUTE));
230
        docid.append(calendar.get(Calendar.SECOND));
202
		// Create a calendar to get the date formatted properly
203
		String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
204
		SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
205
		pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
206
		pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
207
		Calendar calendar = new GregorianCalendar(pdt);
208
		Date trialTime = new Date();
209
		calendar.setTime(trialTime);
210
		docid.append(calendar.get(Calendar.YEAR));
211
		docid.append(calendar.get(Calendar.DAY_OF_YEAR));
212
		docid.append(calendar.get(Calendar.HOUR_OF_DAY));
213
		docid.append(calendar.get(Calendar.MINUTE));
214
		docid.append(calendar.get(Calendar.SECOND));
231 215

  
232
        return docid.toString();
233
    }
216
		return docid.toString();
217
	}
234 218
}

Also available in: Unified diff