Project

General

Profile

1
/**
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: costa $'
10
 *     '$Date: 2004-08-26 14:56:11 -0700 (Thu, 26 Aug 2004) $'
11
 * '$Revision: 2255 $'
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
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
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
38
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDetailLog;
39
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
40
import edu.ucsb.nceas.metacat.harvesterClient.HarvestLog;
41
import junit.framework.Test;
42
import junit.framework.TestCase;
43
import junit.framework.TestSuite;
44

    
45
/**
46
 * Tests HarvestLog code using JUnit.
47
 *
48
 * @author  costa
49
 */
50
public class HarvestLogTest extends TestCase {
51

    
52
  private Connection conn;
53
  private Harvester harvester;
54
  private HarvestLog harvestLogShallow;
55
  private HarvestLog harvestLogDeep;
56
  private HarvestDocument harvestDocument;
57
  private HarvestSiteSchedule harvestSiteSchedule;
58
  
59

    
60
  /**
61
   * Constructor for this test.
62
   * 
63
   * @param name     name of the test case
64
   */
65
  public HarvestLogTest(String name) {
66
    super(name);
67
  }
68
  
69

    
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
  protected void setUp() {
77
    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
    boolean test = true;
100
    String unit = "months";
101
    int updateFrequency = 1;
102

    
103
    harvester = new Harvester();
104
    Harvester.loadOptions(test);
105
    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
  }
142
  
143

    
144
  /** 
145
   * Closes the database connection after the test completes.
146
   */
147
  protected void tearDown() {
148
    harvester.closeConnection();
149
  }
150
  
151

    
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
  public void testGetCodeLevelValue() {
157
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
158
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
159
    
160
    assertTrue(lowLevel < highLevel);
161
  }
162
  
163

    
164
  /**
165
   * Tests the getExplanation() method. Check that a harvest operation code is
166
   * associated with an appropriate explanation string.
167
   */
168
  public void testGetExplanation() {
169
    String harvestOperationCode = "HarvesterStartup";
170
    String explanation;
171

    
172
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
173
    assertTrue(explanation.equals("Harvester start up"));    
174
  }
175
  
176

    
177
  /**
178
   * Tests the getHarvestOperationCodeLevel() method. Check that the method
179
   * returns an appropriate code level for a given harvest operation code.
180
   */
181
  public void testGetHarvestOperationCodeLevel() {
182
    String harvestOperationCode = "HarvesterStartup";
183
    String harvestOperationCodeLevel;
184

    
185
    harvestOperationCodeLevel = 
186
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
187
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));    
188
  }
189

    
190

    
191
  /**
192
   * Tests the construction of HarvestLog object, and a HarvestLog object
193
   * that contains a HarvestDetailLog object.
194
   */
195
  public void testHarvestLogObject() {
196
    assertTrue(harvestLogShallow != null);
197
    assertTrue(harvestLogDeep != null);
198
  }
199
  
200
  
201
  /**
202
   * 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
   * 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
}
(3-3/5)