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: 2009-02-23 11:29:07 -0800 (Mon, 23 Feb 2009) $'
11
 * '$Revision: 4816 $'
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.sql.Connection;
32
import java.util.Date;
33

    
34
import edu.ucsb.nceas.MCTestCase;
35
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
36
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
37
import edu.ucsb.nceas.metacat.harvesterClient.HarvestLog;
38
import edu.ucsb.nceas.metacat.service.PropertyService;
39
import edu.ucsb.nceas.metacat.util.MetacatUtil;
40

    
41
import junit.framework.Test;
42
import junit.framework.TestSuite;
43

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

    
51
  private Connection conn;
52
  private Harvester harvester;
53
  private HarvestLog harvestLogShallow;
54
  private HarvestLog harvestLogDeep;
55
  private HarvestSiteSchedule harvestSiteSchedule;
56
  /* Initialize Properties*/
57
  static
58
  {
59
	  try
60
	  {
61
		  PropertyService.getInstance();
62
	  }
63
	  catch(Exception e)
64
	  {
65
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
66
	  }
67
  }
68

    
69
  /**
70
   * Constructor for this test.
71
   * 
72
   * @param name     name of the test case
73
   */
74
  public HarvestLogTest(String name) {
75
    super(name);
76
  }
77
  
78

    
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
  protected void setUp() {
86
    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
    String harvestOperationCode = "harvester.HarvesterStartup";
99
    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
    boolean test = true;
109
    String unit = "months";
110
    int updateFrequency = 1;
111

    
112
    harvester = new Harvester();
113
    Harvester.loadProperties(test);
114
    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
  }
151
  
152

    
153
  /** 
154
   * Closes the database connection after the test completes.
155
   */
156
  protected void tearDown() {
157
    harvester.closeConnection();
158
  }
159
  
160

    
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
  public void testGetCodeLevelValue() {
166
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
167
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
168
    
169
    assertTrue(lowLevel < highLevel);
170
  }
171
  
172

    
173
  /**
174
   * Tests the getExplanation() method. Check that a harvest operation code is
175
   * associated with an appropriate explanation string.
176
   */
177
  public void testGetExplanation() {
178
    String harvestOperationCode = "harvester.HarvesterStartup";
179
    String explanation;
180

    
181
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
182
    assertTrue(explanation.equals("Harvester start up"));    
183
  }
184
  
185

    
186
  /**
187
   * Tests the getHarvestOperationCodeLevel() method. Check that the method
188
   * returns an appropriate code level for a given harvest operation code.
189
   */
190
  public void testGetHarvestOperationCodeLevel() {
191
    String harvestOperationCode = "harvester.HarvesterStartup";
192
    String harvestOperationCodeLevel;
193

    
194
    harvestOperationCodeLevel = 
195
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
196
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));    
197
  }
198

    
199

    
200
  /**
201
   * Tests the construction of HarvestLog object, and a HarvestLog object
202
   * that contains a HarvestDetailLog object.
203
   */
204
  public void testHarvestLogObject() {
205
    assertTrue(harvestLogShallow != null);
206
    assertTrue(harvestLogDeep != null);
207
  }
208
  
209
  
210
  /**
211
   * 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
   * 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
}
(3-3/5)