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: daigle $'
10
 *     '$Date: 2008-08-05 17:50:14 -0700 (Tue, 05 Aug 2008) $'
11
 * '$Revision: 4213 $'
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 edu.ucsb.nceas.metacat.service.PropertyService;
42
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46

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

    
54
  private Connection conn;
55
  private Harvester harvester;
56
  private HarvestLog harvestLogShallow;
57
  private HarvestLog harvestLogDeep;
58
  private HarvestDocument harvestDocument;
59
  private HarvestSiteSchedule harvestSiteSchedule;
60
  /* Initialize Properties*/
61
  static
62
  {
63
	  try
64
	  {
65
		  PropertyService.getInstance("build/tests");
66
		  MetaCatUtil.pathsForIndexing 
67
		         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
68
	  }
69
	  catch(Exception e)
70
	  {
71
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
72
	  }
73
  }
74

    
75
  /**
76
   * Constructor for this test.
77
   * 
78
   * @param name     name of the test case
79
   */
80
  public HarvestLogTest(String name) {
81
    super(name);
82
  }
83
  
84

    
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
  protected void setUp() {
92
    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
    String harvestOperationCode = "harvester.HarvesterStartup";
105
    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
    boolean test = true;
115
    String unit = "months";
116
    int updateFrequency = 1;
117

    
118
    harvester = new Harvester();
119
    Harvester.loadProperties(test);
120
    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
  }
157
  
158

    
159
  /** 
160
   * Closes the database connection after the test completes.
161
   */
162
  protected void tearDown() {
163
    harvester.closeConnection();
164
  }
165
  
166

    
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
  public void testGetCodeLevelValue() {
172
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
173
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
174
    
175
    assertTrue(lowLevel < highLevel);
176
  }
177
  
178

    
179
  /**
180
   * Tests the getExplanation() method. Check that a harvest operation code is
181
   * associated with an appropriate explanation string.
182
   */
183
  public void testGetExplanation() {
184
    String harvestOperationCode = "harvester.HarvesterStartup";
185
    String explanation;
186

    
187
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
188
    assertTrue(explanation.equals("Harvester start up"));    
189
  }
190
  
191

    
192
  /**
193
   * Tests the getHarvestOperationCodeLevel() method. Check that the method
194
   * returns an appropriate code level for a given harvest operation code.
195
   */
196
  public void testGetHarvestOperationCodeLevel() {
197
    String harvestOperationCode = "harvester.HarvesterStartup";
198
    String harvestOperationCodeLevel;
199

    
200
    harvestOperationCodeLevel = 
201
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
202
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));    
203
  }
204

    
205

    
206
  /**
207
   * Tests the construction of HarvestLog object, and a HarvestLog object
208
   * that contains a HarvestDetailLog object.
209
   */
210
  public void testHarvestLogObject() {
211
    assertTrue(harvestLogShallow != null);
212
    assertTrue(harvestLogDeep != null);
213
  }
214
  
215
  
216
  /**
217
   * 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
   * 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
}
(3-3/5)