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