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.io.File;
32
import java.sql.Connection;
33
import java.sql.DriverManager;
34
import java.sql.SQLException;
35
import java.sql.SQLWarning;
36
import java.util.Date;
37 2134 costa
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
38 2143 costa
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDetailLog;
39
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
40
import edu.ucsb.nceas.metacat.harvesterClient.HarvestLog;
41 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
42
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
43 2134 costa
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46
47
/**
48 2255 costa
 * Tests HarvestLog code using JUnit.
49 2134 costa
 *
50
 * @author  costa
51
 */
52
public class HarvestLogTest extends TestCase {
53
54 2143 costa
  private Connection conn;
55 2134 costa
  private Harvester harvester;
56 2143 costa
  private HarvestLog harvestLogShallow;
57
  private HarvestLog harvestLogDeep;
58
  private HarvestDocument harvestDocument;
59
  private HarvestSiteSchedule harvestSiteSchedule;
60 4127 daigle
  /* Initialize Properties*/
61 3566 tao
  static
62
  {
63
	  try
64
	  {
65 4127 daigle
		  PropertyService.getInstance("build/tests");
66 3566 tao
		  MetaCatUtil.pathsForIndexing
67 4080 daigle
		         = MetaCatUtil.getOptionList(PropertyService.getProperty("indexPaths"));
68 3566 tao
	  }
69
	  catch(Exception e)
70
	  {
71
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
72
	  }
73
  }
74 2134 costa
75 2255 costa
  /**
76
   * Constructor for this test.
77
   *
78
   * @param name     name of the test case
79
   */
80 2134 costa
  public HarvestLogTest(String name) {
81
    super(name);
82
  }
83
84 2255 costa
85
  /**
86
   * Sets up the test by instantiating a HarvestSiteSchedule, HarvestDocument,
87
   * and two HarvestLog objects. The harvestLogShallow is a HarvestLog that
88
   * does not have an associated HarvestDetailLog. The harvestLogDeep is a
89
   * HarvestLog that does have an associated HarvestDetailLog.
90
   */
91 2134 costa
  protected void setUp() {
92 2143 costa
    String contactEmail = "jdoe@institution.edu";
93
    String dateLastHarvest = "2004-04-01 00:00:00.0";
94
    String dateNextHarvest = "2004-05-01 00:00:00.0";
95
    int detailLogID;
96
    String documentListURL =
97
                 "http://www.institution.edu/~jdoe/public_html/harvestList.xml";
98
    String documentType = "eml://ecoinformatics.org/eml-2.0.0";
99
    String documentURL =
100
                   "http://www.institution.edu/~jdoe/public_html/document1.xml";
101
    String errorMessage = "JUnit Testing";
102
    HarvestDocument harvestDocument = null;
103
    int harvestLogID;
104 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
105 2143 costa
    Date harvestStartTime = new Date();
106
    int identifier = 1;
107
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
108
    String ldapPwd = "secretpassword";
109
    String message = "JUnit Testing";
110
    int revision = 1;
111
    String scope = "docname";
112
    int siteScheduleID = 1;
113
    int status = 0;
114 2158 costa
    boolean test = true;
115 2143 costa
    String unit = "months";
116
    int updateFrequency = 1;
117
118 2134 costa
    harvester = new Harvester();
119 4127 daigle
    Harvester.loadProperties(test);
120 2143 costa
    conn = harvester.getConnection();  // initializes the database connection
121
    harvester.initLogIDs();
122
    harvestLogID = harvester.getHarvestLogID();
123
    harvestLogShallow = new HarvestLog(harvester, conn, harvestLogID,
124
                                       harvestStartTime, status,
125
                                       message, harvestOperationCode,
126
                                       siteScheduleID);
127
128
    harvestLogID = harvester.getHarvestLogID();
129
    detailLogID = harvester.getDetailLogID();
130
    harvestSiteSchedule = new HarvestSiteSchedule(harvester,
131
                                                  siteScheduleID,
132
                                                  documentListURL,
133
                                                  ldapDN,
134
                                                  ldapPwd,
135
                                                  dateNextHarvest,
136
                                                  dateLastHarvest,
137
                                                  updateFrequency,
138
                                                  unit,
139
                                                  contactEmail
140
                                                 );
141
142
    harvestDocument = new HarvestDocument(harvester,
143
                                          harvestSiteSchedule,
144
                                          scope,
145
                                          identifier,
146
                                          revision,
147
                                          documentType,
148
                                          documentURL
149
                                        );
150
151
152
    harvestLogDeep = new HarvestLog(harvester, conn, harvestLogID, detailLogID,
153
                                    harvestStartTime, status, message,
154
                                    harvestOperationCode, siteScheduleID,
155
                                    harvestDocument, errorMessage);
156 2134 costa
  }
157
158 2255 costa
159
  /**
160
   * Closes the database connection after the test completes.
161
   */
162 2134 costa
  protected void tearDown() {
163 2143 costa
    harvester.closeConnection();
164 2134 costa
  }
165 2143 costa
166 2255 costa
167
  /**
168
   * Tests the getCodeLevelValue() method by comparing the value returned for
169
   * "error" (lowest level) to the value returned for "debug" (highest level).
170
   */
171 2143 costa
  public void testGetCodeLevelValue() {
172
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
173
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
174
175
    assertTrue(lowLevel < highLevel);
176
  }
177
178 2255 costa
179
  /**
180
   * Tests the getExplanation() method. Check that a harvest operation code is
181
   * associated with an appropriate explanation string.
182
   */
183 2143 costa
  public void testGetExplanation() {
184 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
185 2143 costa
    String explanation;
186 2134 costa
187 2143 costa
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
188
    assertTrue(explanation.equals("Harvester start up"));
189
  }
190
191 2255 costa
192
  /**
193
   * Tests the getHarvestOperationCodeLevel() method. Check that the method
194
   * returns an appropriate code level for a given harvest operation code.
195
   */
196 2143 costa
  public void testGetHarvestOperationCodeLevel() {
197 4172 daigle
    String harvestOperationCode = "harvester.HarvesterStartup";
198 2143 costa
    String harvestOperationCodeLevel;
199 2134 costa
200 2143 costa
    harvestOperationCodeLevel =
201
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
202
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));
203
  }
204
205
206 2134 costa
  /**
207 2255 costa
   * Tests the construction of HarvestLog object, and a HarvestLog object
208 2143 costa
   * that contains a HarvestDetailLog object.
209 2134 costa
   */
210 2143 costa
  public void testHarvestLogObject() {
211
    assertTrue(harvestLogShallow != null);
212
    assertTrue(harvestLogDeep != null);
213 2134 costa
  }
214
215
216
  /**
217 2143 costa
   * Tests the printOutput() method when the code level of this operation is
218
   * greater than the maximum level we wish to print. This means that the output
219
   * should not be printed.
220
   */
221
  public void testPrintOutputExceedsMax() {
222
    System.out.println("No output should be printed:");
223
    harvestLogDeep.printOutput(System.out, "Error");
224
  }
225
226
227
  /**
228
   * Tests the printOutput() method when the code level of this operation is
229
   * less than the maximum level we wish to print. This means that the output
230
   * should be printed.
231
   */
232
  public void testPrintOutputWithinMax() {
233
    System.out.println("The log output should be printed:");
234
    harvestLogDeep.printOutput(System.out, "Debug");
235
  }
236
237
238
  /**
239 2134 costa
   * Returns the test suite. The test suite consists of all methods in this
240
   * class whose names start with "test".
241
   *
242
   * @return  a TestSuite object
243
   */
244
  public static Test suite() {
245
    return new TestSuite(HarvestLogTest.class);
246
  }
247
248
249
  /**
250
   * The main program. Runs the test suite.
251
   *
252
   * @param args   command line argument array.
253
   */
254
  public static void main(String args[]) {
255
    junit.textui.TestRunner.run(suite());
256
  }
257
258
}