Project

General

Profile

1 2134 costa
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis,
5
 *              and the University of New Mexico
6
 *
7
 *  Purpose: To test the HarvestLog class by using JUnit
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
28
package edu.ucsb.nceas.metacattest.harvesterClient;
29
30 2143 costa
import edu.ucsb.nceas.metacat.harvesterClient.HarvestSiteSchedule;
31
import java.sql.Connection;
32
import java.util.Date;
33 4385 daigle
34
import edu.ucsb.nceas.MCTestCase;
35 2134 costa
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
36 2143 costa
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
37
import edu.ucsb.nceas.metacat.harvesterClient.HarvestLog;
38 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
39
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
40 4385 daigle
41 2134 costa
import junit.framework.Test;
42
import junit.framework.TestSuite;
43
44
/**
45 2255 costa
 * Tests HarvestLog code using JUnit.
46 2134 costa
 *
47
 * @author  costa
48
 */
49 4385 daigle
public class HarvestLogTest extends MCTestCase {
50 2134 costa
51 2143 costa
  private Connection conn;
52 2134 costa
  private Harvester harvester;
53 2143 costa
  private HarvestLog harvestLogShallow;
54
  private HarvestLog harvestLogDeep;
55
  private HarvestSiteSchedule harvestSiteSchedule;
56 4127 daigle
  /* Initialize Properties*/
57 3566 tao
  static
58
  {
59
	  try
60
	  {
61
		  MetaCatUtil.pathsForIndexing
62 4213 daigle
		         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
63 3566 tao
	  }
64
	  catch(Exception e)
65
	  {
66
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
67
	  }
68
  }
69 2134 costa
70 2255 costa
  /**
71
   * Constructor for this test.
72
   *
73
   * @param name     name of the test case
74
   */
75 2134 costa
  public HarvestLogTest(String name) {
76
    super(name);
77
  }
78
79 2255 costa
80
  /**
81
   * Sets up the test by instantiating a HarvestSiteSchedule, HarvestDocument,
82
   * and two HarvestLog objects. The harvestLogShallow is a HarvestLog that
83
   * does not have an associated HarvestDetailLog. The harvestLogDeep is a
84
   * HarvestLog that does have an associated HarvestDetailLog.
85
   */
86 2134 costa
  protected void setUp() {
87 2143 costa
    String contactEmail = "jdoe@institution.edu";
88
    String dateLastHarvest = "2004-04-01 00:00:00.0";
89
    String dateNextHarvest = "2004-05-01 00:00:00.0";
90
    int detailLogID;
91
    String documentListURL =
92
                 "http://www.institution.edu/~jdoe/public_html/harvestList.xml";
93
    String documentType = "eml://ecoinformatics.org/eml-2.0.0";
94
    String documentURL =
95
                   "http://www.institution.edu/~jdoe/public_html/document1.xml";
96
    String errorMessage = "JUnit Testing";
97
    HarvestDocument harvestDocument = null;
98
    int harvestLogID;
99 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
100 2143 costa
    Date harvestStartTime = new Date();
101
    int identifier = 1;
102
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
103
    String ldapPwd = "secretpassword";
104
    String message = "JUnit Testing";
105
    int revision = 1;
106
    String scope = "docname";
107
    int siteScheduleID = 1;
108
    int status = 0;
109 2158 costa
    boolean test = true;
110 2143 costa
    String unit = "months";
111
    int updateFrequency = 1;
112
113 2134 costa
    harvester = new Harvester();
114 4127 daigle
    Harvester.loadProperties(test);
115 2143 costa
    conn = harvester.getConnection();  // initializes the database connection
116
    harvester.initLogIDs();
117
    harvestLogID = harvester.getHarvestLogID();
118
    harvestLogShallow = new HarvestLog(harvester, conn, harvestLogID,
119
                                       harvestStartTime, status,
120
                                       message, harvestOperationCode,
121
                                       siteScheduleID);
122
123
    harvestLogID = harvester.getHarvestLogID();
124
    detailLogID = harvester.getDetailLogID();
125
    harvestSiteSchedule = new HarvestSiteSchedule(harvester,
126
                                                  siteScheduleID,
127
                                                  documentListURL,
128
                                                  ldapDN,
129
                                                  ldapPwd,
130
                                                  dateNextHarvest,
131
                                                  dateLastHarvest,
132
                                                  updateFrequency,
133
                                                  unit,
134
                                                  contactEmail
135
                                                 );
136
137
    harvestDocument = new HarvestDocument(harvester,
138
                                          harvestSiteSchedule,
139
                                          scope,
140
                                          identifier,
141
                                          revision,
142
                                          documentType,
143
                                          documentURL
144
                                        );
145
146
147
    harvestLogDeep = new HarvestLog(harvester, conn, harvestLogID, detailLogID,
148
                                    harvestStartTime, status, message,
149
                                    harvestOperationCode, siteScheduleID,
150
                                    harvestDocument, errorMessage);
151 2134 costa
  }
152
153 2255 costa
154
  /**
155
   * Closes the database connection after the test completes.
156
   */
157 2134 costa
  protected void tearDown() {
158 2143 costa
    harvester.closeConnection();
159 2134 costa
  }
160 2143 costa
161 2255 costa
162
  /**
163
   * Tests the getCodeLevelValue() method by comparing the value returned for
164
   * "error" (lowest level) to the value returned for "debug" (highest level).
165
   */
166 2143 costa
  public void testGetCodeLevelValue() {
167
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
168
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
169
170
    assertTrue(lowLevel < highLevel);
171
  }
172
173 2255 costa
174
  /**
175
   * Tests the getExplanation() method. Check that a harvest operation code is
176
   * associated with an appropriate explanation string.
177
   */
178 2143 costa
  public void testGetExplanation() {
179 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
180 2143 costa
    String explanation;
181 2134 costa
182 2143 costa
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
183
    assertTrue(explanation.equals("Harvester start up"));
184
  }
185
186 2255 costa
187
  /**
188
   * Tests the getHarvestOperationCodeLevel() method. Check that the method
189
   * returns an appropriate code level for a given harvest operation code.
190
   */
191 2143 costa
  public void testGetHarvestOperationCodeLevel() {
192 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
193 2143 costa
    String harvestOperationCodeLevel;
194 2134 costa
195 2143 costa
    harvestOperationCodeLevel =
196
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
197
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));
198
  }
199
200
201 2134 costa
  /**
202 2255 costa
   * Tests the construction of HarvestLog object, and a HarvestLog object
203 2143 costa
   * that contains a HarvestDetailLog object.
204 2134 costa
   */
205 2143 costa
  public void testHarvestLogObject() {
206
    assertTrue(harvestLogShallow != null);
207
    assertTrue(harvestLogDeep != null);
208 2134 costa
  }
209
210
211
  /**
212 2143 costa
   * Tests the printOutput() method when the code level of this operation is
213
   * greater than the maximum level we wish to print. This means that the output
214
   * should not be printed.
215
   */
216
  public void testPrintOutputExceedsMax() {
217
    System.out.println("No output should be printed:");
218
    harvestLogDeep.printOutput(System.out, "Error");
219
  }
220
221
222
  /**
223
   * Tests the printOutput() method when the code level of this operation is
224
   * less than the maximum level we wish to print. This means that the output
225
   * should be printed.
226
   */
227
  public void testPrintOutputWithinMax() {
228
    System.out.println("The log output should be printed:");
229
    harvestLogDeep.printOutput(System.out, "Debug");
230
  }
231
232
233
  /**
234 2134 costa
   * Returns the test suite. The test suite consists of all methods in this
235
   * class whose names start with "test".
236
   *
237
   * @return  a TestSuite object
238
   */
239
  public static Test suite() {
240
    return new TestSuite(HarvestLogTest.class);
241
  }
242
243
244
  /**
245
   * The main program. Runs the test suite.
246
   *
247
   * @param args   command line argument array.
248
   */
249
  public static void main(String args[]) {
250
    junit.textui.TestRunner.run(suite());
251
  }
252
253
}