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