Project

General

Profile

« Previous | Next » 

Revision 2143

Initial suite of JUnit tests for Harvester code

View differences:

test/edu/ucsb/nceas/metacattest/harvesterClient/HarvestDetailLogTest.java
28 28
package edu.ucsb.nceas.metacattest.harvesterClient;
29 29

  
30 30
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
31
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDetailLog;
32
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
33
import edu.ucsb.nceas.metacat.harvesterClient.HarvestSiteSchedule;
34
import java.io.File;
35
import java.sql.Connection;
31 36
import junit.framework.Test;
32 37
import junit.framework.TestCase;
33 38
import junit.framework.TestSuite;
......
42 47
public class HarvestDetailLogTest extends TestCase {
43 48

  
44 49
  private Harvester harvester;
50
  private HarvestDetailLog harvestDetailLog;
51
  private HarvestDocument harvestDocument;
52
  private HarvestSiteSchedule harvestSiteSchedule;
45 53
  
46 54

  
47 55
  public HarvestDetailLogTest(String name) {
......
50 58
  
51 59
  
52 60
  protected void setUp() {
61
    File configfile = new File("./lib/harvester", "harvester.properties");
62
    Connection conn;
63
    String contactEmail = "jdoe@institution.edu";
64
    String dateLastHarvest = "2004-04-01 00:00:00.0";
65
    String dateNextHarvest = "2004-05-01 00:00:00.0";
66
    int detailLogID;
67
    String documentListURL = 
68
                 "http://www.institution.edu/~jdoe/public_html/harvestList.xml";
69
    String documentType = "eml://ecoinformatics.org/eml-2.0.0";
70
    String documentURL = 
71
                   "http://www.institution.edu/~jdoe/public_html/document1.xml";
72
    String errorMessage = "JUnit testing";
73
    int harvestLogID;
74
    int identifier = 1;
75
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
76
    String ldapPwd = "secretpassword";
77
    int revision = 1;
78
    String scope = "docname";
79
    int siteScheduleID = 1;
80
    String unit = "months";
81
    int updateFrequency = 1;
82
  
53 83
    harvester = new Harvester();
84
    Harvester.loadProperties(configfile);
85
    conn = harvester.getConnection();  // initializes the database connection
86
    harvester.initLogIDs();
87
    harvestLogID = harvester.getHarvestLogID();
88
    detailLogID = harvester.getDetailLogID();
89

  
90
    harvestSiteSchedule = new HarvestSiteSchedule(harvester,
91
                                                  siteScheduleID,
92
                                                  documentListURL,
93
                                                  ldapDN,
94
                                                  ldapPwd,
95
                                                  dateNextHarvest,
96
                                                  dateLastHarvest,
97
                                                  updateFrequency,
98
                                                  unit,
99
                                                  contactEmail
100
                                                 );
101

  
102
    harvestDocument = new HarvestDocument(harvester,
103
                                          harvestSiteSchedule,
104
                                          scope,
105
                                          identifier,
106
                                          revision,
107
                                          documentType,
108
                                          documentURL
109
                                        );
110
    
111
    harvestDetailLog = new HarvestDetailLog(harvester, conn, harvestLogID, 
112
                                            detailLogID, harvestDocument, 
113
                                            errorMessage);
54 114
  }
55 115
  
56 116
  
57 117
  protected void tearDown() {
118
    harvester.closeConnection();
58 119
  }
59 120

  
60 121

  
61 122
  /**
62
   * Dummy test. Will be replaced by the actual Harvester tests when 
63
   * they are implemented.
123
   * Tests that the harvestDetailLog object was constructed.
64 124
   */
65
  public void testHarvesterObject() {
66
    assertTrue(harvester.equals(harvester));
125
  public void testHarvestDetailLogObject() {
126
    assertTrue(harvestDetailLog != null);
67 127
  }
68 128
  
69 129
  
70 130
  /**
131
   * Tests the printOutput() method.
132
   */
133
  public void testPrintOutput() {
134
    harvestDetailLog.printOutput(System.out);
135
  }
136
  
137
  
138
  /**
71 139
   * Returns the test suite. The test suite consists of all methods in this
72 140
   * class whose names start with "test".
73 141
   * 
test/edu/ucsb/nceas/metacattest/harvesterClient/HarvesterTest.java
62 62
  }
63 63
  
64 64
  
65
  public void testDequoteText() {
66
    String singleQuoteString = "I can't see how it's done!\n";
67
    String compareString = "I can\"t see how it\"s done!";
68
    String dequotedString = "";
69
    
70
    dequotedString = harvester.dequoteText(singleQuoteString);
71
    assertTrue(dequotedString.equals(compareString));
72
    System.out.println("Dequoted string: " + dequotedString);
73
  }
74
  
75
  
65 76
  /**
77
   * Test that the Harvester object was created successfully.
78
   */
79
  public void testHarvesterObject() {
80
    assertTrue(harvester != null);
81
  }
82
  
83
  
84
  /**
66 85
   * Tests loading of Harvester properties from a configuration file.
67 86
   */
68 87
  public void testLoadProperties() {
69 88
    File configfile = new File("./lib/harvester", "harvester.properties");
70 89
    Properties properties = new Properties();
71 90

  
72
    String[] dir = new java.io.File(".").list(); // Get list of names
73
    java.util.Arrays.sort(dir);             // Sort it (Data Structuring chapter))
74
    for (int i=0; i<dir.length; i++)
75
      System.out.println(dir[i]);     // Print the list
76

  
77 91
    try {
78 92
      properties.load(new FileInputStream(configfile));
79 93
    }
......
84 98
  
85 99
  
86 100
  /**
101
   * Prints the files in the current working directory. This may be useful
102
   * for determining which directory all other tests are running in.
103
   */
104
  public void testWorkingDirectory() {
105
    String[] dir = new java.io.File(".").list(); // Get files in current dir
106

  
107
    java.util.Arrays.sort(dir);                  // Sort the directory listing
108

  
109
    for (int i=0; i<dir.length; i++)
110
      System.out.println(dir[i]);                // Print the list
111
  }
112
  
113
  
114
  /**
87 115
   * Returns the test suite. The test suite consists of all methods in this
88 116
   * class whose names start with "test".
89 117
   * 
test/edu/ucsb/nceas/metacattest/harvesterClient/HarvestDocumentTest.java
27 27

  
28 28
package edu.ucsb.nceas.metacattest.harvesterClient;
29 29

  
30
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
31
import edu.ucsb.nceas.metacat.harvesterClient.HarvestSiteSchedule;
30 32
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
33
import java.io.File;
34
import java.io.StringReader;
35
import java.util.Date;
31 36
import junit.framework.Test;
32 37
import junit.framework.TestCase;
33 38
import junit.framework.TestSuite;
......
42 47
public class HarvestDocumentTest extends TestCase {
43 48

  
44 49
  private Harvester harvester;
50
  private HarvestDocument harvestDocument;
51
  private HarvestSiteSchedule harvestSiteSchedule;
45 52
  
46 53

  
47 54
  public HarvestDocumentTest(String name) {
......
50 57
  
51 58
  
52 59
  protected void setUp() {
60
    File configfile = new File("./lib/harvester", "harvester.properties");
61
    String contactEmail = "jdoe@institution.edu";
62
    String dateLastHarvest = "2004-04-01 00:00:00.0";
63
    String dateNextHarvest = "2004-05-01 00:00:00.0";
64
    int detailLogID;
65
    String documentListURL = 
66
                 "http://www.institution.edu/~jdoe/public_html/harvestList.xml";
67
    String documentType = "eml://ecoinformatics.org/eml-2.0.0";
68
    String documentURL = "http://www.lternet.edu/~dcosta/document1.xml";
69
    String errorMessage = "JUnit Testing";
70
    int harvestLogID;
71
    String harvestOperationCode = "HarvesterStartup";
72
    Date harvestStartTime = new Date();
73
    int identifier = 1;
74
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
75
    String ldapPwd = "secretpassword";
76
    String message = "JUnit Testing";
77
    int revision = 1;
78
    String scope = "docname";
79
    int siteScheduleID = 1;
80
    int status = 0;
81
    String unit = "months";
82
    int updateFrequency = 1;
83

  
53 84
    harvester = new Harvester();
85
    Harvester.loadProperties(configfile);
86
    harvester.getConnection();  // initializes the database connection
87
    harvester.initLogIDs();
88
    harvester.setHarvestStartTime(new Date());
89

  
90
    harvestSiteSchedule = new HarvestSiteSchedule(harvester,
91
                                                  siteScheduleID,
92
                                                  documentListURL,
93
                                                  ldapDN,
94
                                                  ldapPwd,
95
                                                  dateNextHarvest,
96
                                                  dateLastHarvest,
97
                                                  updateFrequency,
98
                                                  unit,
99
                                                  contactEmail
100
                                                 );
101

  
102
    harvestDocument = new HarvestDocument(harvester,
103
                                          harvestSiteSchedule,
104
                                          scope,
105
                                          identifier,
106
                                          revision,
107
                                          documentType,
108
                                          documentURL
109
                                        );
54 110
  }
55 111
  
56 112
  
57 113
  protected void tearDown() {
114
    harvester.closeConnection();
58 115
  }
116
  
117
  
118
  public void testGetSiteDocument() {
119
    StringReader stringReader = null;
120
    stringReader = harvestDocument.getSiteDocument();
121
    assertTrue(stringReader != null);
122
  }
123
  
124
  
125
  /**
126
   * Test that the harvesterDocument object was created successfully.
127
   */
128
  public void testHarvestDocumentObject() {
129
    assertTrue(harvestDocument != null);
130
  }
131
  
132
  
133
  public void testMetacatHighestRevision() {
134
    int highestRevision;
135
    
136
    highestRevision = harvestDocument.metacatHighestRevision();
137
    assertTrue(highestRevision == -1);
138
  }
59 139

  
60 140

  
61 141
  /**
62
   * Dummy test. Will be replaced by the actual HarvestDocument tests when 
63
   * they are implemented.
142
   * Tests the printOutput() method.
64 143
   */
65
  public void testHarvesterObject() {
66
    assertTrue(harvester.equals(harvester));
144
  public void testPrintOutput() {
145
    harvestDocument.printOutput(System.out);
67 146
  }
68 147
  
69 148
  
test/edu/ucsb/nceas/metacattest/harvesterClient/HarvestLogTest.java
27 27

  
28 28
package edu.ucsb.nceas.metacattest.harvesterClient;
29 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 java.util.Properties;
30 38
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
39
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDetailLog;
40
import edu.ucsb.nceas.metacat.harvesterClient.HarvestDocument;
41
import edu.ucsb.nceas.metacat.harvesterClient.HarvestLog;
31 42
import junit.framework.Test;
32 43
import junit.framework.TestCase;
33 44
import junit.framework.TestSuite;
......
41 52
 */
42 53
public class HarvestLogTest extends TestCase {
43 54

  
55
  private Connection conn;
44 56
  private Harvester harvester;
57
  private HarvestLog harvestLogShallow;
58
  private HarvestLog harvestLogDeep;
59
  private HarvestDocument harvestDocument;
60
  private HarvestSiteSchedule harvestSiteSchedule;
45 61
  
46 62

  
47 63
  public HarvestLogTest(String name) {
......
50 66
  
51 67
  
52 68
  protected void setUp() {
69
    File configfile = new File("./lib/harvester", "harvester.properties");
70
    String contactEmail = "jdoe@institution.edu";
71
    String dateLastHarvest = "2004-04-01 00:00:00.0";
72
    String dateNextHarvest = "2004-05-01 00:00:00.0";
73
    int detailLogID;
74
    String documentListURL = 
75
                 "http://www.institution.edu/~jdoe/public_html/harvestList.xml";
76
    String documentType = "eml://ecoinformatics.org/eml-2.0.0";
77
    String documentURL = 
78
                   "http://www.institution.edu/~jdoe/public_html/document1.xml";
79
    String errorMessage = "JUnit Testing";
80
    HarvestDocument harvestDocument = null;
81
    int harvestLogID;
82
    String harvestOperationCode = "HarvesterStartup";
83
    Date harvestStartTime = new Date();
84
    int identifier = 1;
85
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
86
    String ldapPwd = "secretpassword";
87
    String message = "JUnit Testing";
88
    int revision = 1;
89
    String scope = "docname";
90
    int siteScheduleID = 1;
91
    int status = 0;
92
    String unit = "months";
93
    int updateFrequency = 1;
94

  
53 95
    harvester = new Harvester();
96
    Harvester.loadProperties(configfile);
97
    conn = harvester.getConnection();  // initializes the database connection
98
    harvester.initLogIDs();
99
    harvestLogID = harvester.getHarvestLogID();
100
    harvestLogShallow = new HarvestLog(harvester, conn, harvestLogID, 
101
                                       harvestStartTime, status, 
102
                                       message, harvestOperationCode, 
103
                                       siteScheduleID);
104

  
105
    harvestLogID = harvester.getHarvestLogID();
106
    detailLogID = harvester.getDetailLogID();
107
    harvestSiteSchedule = new HarvestSiteSchedule(harvester,
108
                                                  siteScheduleID,
109
                                                  documentListURL,
110
                                                  ldapDN,
111
                                                  ldapPwd,
112
                                                  dateNextHarvest,
113
                                                  dateLastHarvest,
114
                                                  updateFrequency,
115
                                                  unit,
116
                                                  contactEmail
117
                                                 );
118

  
119
    harvestDocument = new HarvestDocument(harvester,
120
                                          harvestSiteSchedule,
121
                                          scope,
122
                                          identifier,
123
                                          revision,
124
                                          documentType,
125
                                          documentURL
126
                                        );
127
    
128

  
129
    harvestLogDeep = new HarvestLog(harvester, conn, harvestLogID, detailLogID, 
130
                                    harvestStartTime, status, message,
131
                                    harvestOperationCode, siteScheduleID,
132
                                    harvestDocument, errorMessage);    
54 133
  }
55 134
  
56 135
  
57 136
  protected void tearDown() {
137
    harvester.closeConnection();
58 138
  }
139
  
140
  
141
  public void testGetCodeLevelValue() {
142
    int lowLevel = harvestLogShallow.getCodeLevelValue("error");
143
    int highLevel = harvestLogShallow.getCodeLevelValue("debug");
144
    
145
    assertTrue(lowLevel < highLevel);
146
  }
147
  
148
  
149
  public void testGetExplanation() {
150
    String harvestOperationCode = "HarvesterStartup";
151
    String explanation;
59 152

  
153
    explanation = harvestLogShallow.getExplanation(harvestOperationCode);
154
    assertTrue(explanation.equals("Harvester start up"));    
155
  }
156
  
157
  
158
  public void testGetHarvestOperationCodeLevel() {
159
    String harvestOperationCode = "HarvesterStartup";
160
    String harvestOperationCodeLevel;
60 161

  
162
    harvestOperationCodeLevel = 
163
           harvestLogShallow.getHarvestOperationCodeLevel(harvestOperationCode);
164
    assertTrue(harvestOperationCodeLevel.equalsIgnoreCase("Info"));    
165
  }
166

  
167

  
61 168
  /**
62
   * Dummy test. Will be replaced by the actual Harvester tests when 
63
   * they are implemented.
169
   * Test the construction of HarvestLog object, and a HarvestLog object
170
   * that contains a HarvestDetailLog object.
64 171
   */
65
  public void testHarvesterObject() {
66
    assertTrue(harvester.equals(harvester));
172
  public void testHarvestLogObject() {
173
    assertTrue(harvestLogShallow != null);
174
    assertTrue(harvestLogDeep != null);
67 175
  }
68 176
  
69 177
  
70 178
  /**
179
   * Tests the printOutput() method when the code level of this operation is
180
   * greater than the maximum level we wish to print. This means that the output
181
   * should not be printed.
182
   */
183
  public void testPrintOutputExceedsMax() {
184
    System.out.println("No output should be printed:");
185
    harvestLogDeep.printOutput(System.out, "Error");
186
  }
187
  
188
  
189
  /**
190
   * Tests the printOutput() method when the code level of this operation is
191
   * less than the maximum level we wish to print. This means that the output
192
   * should be printed.
193
   */
194
  public void testPrintOutputWithinMax() {
195
    System.out.println("The log output should be printed:");
196
    harvestLogDeep.printOutput(System.out, "Debug");
197
  }
198
  
199
  
200
  /**
71 201
   * Returns the test suite. The test suite consists of all methods in this
72 202
   * class whose names start with "test".
73 203
   * 
test/edu/ucsb/nceas/metacattest/harvesterClient/HarvestSiteScheduleTest.java
28 28
package edu.ucsb.nceas.metacattest.harvesterClient;
29 29

  
30 30
import edu.ucsb.nceas.metacat.harvesterClient.Harvester;
31
import edu.ucsb.nceas.metacat.harvesterClient.HarvestSiteSchedule;
32
import java.io.File;
33
import java.util.Date;
34
import javax.xml.parsers.ParserConfigurationException;
31 35
import junit.framework.Test;
32 36
import junit.framework.TestCase;
33 37
import junit.framework.TestSuite;
......
42 46
public class HarvestSiteScheduleTest extends TestCase {
43 47

  
44 48
  private Harvester harvester;
49
  private HarvestSiteSchedule harvestSiteScheduleFuture; // future date next har
50
  private HarvestSiteSchedule harvestSiteSchedulePast; // past date next harvest
45 51
  
46 52

  
47 53
  public HarvestSiteScheduleTest(String name) {
......
50 56
  
51 57
  
52 58
  protected void setUp() {
59
    File configfile = new File("./lib/harvester", "harvester.properties");
60
    String contactEmail = "jdoe@institution.edu";
61
    String dateLastHarvest = "2004-04-01 00:00:00.0";
62
    String dateNextHarvestFuture = "2056-01-01 00:00:00.0";
63
    String dateNextHarvestPast = "2000-01-01 00:00:00.0";
64
    String documentListURL = "http://www.lternet.edu/~dcosta/harvestList.xml";
65
    String errorMessage = "JUnit Testing";
66
    String ldapDN = "uid=jdoe,o=lter,dc=ecoinformatics,dc=org";
67
    String ldapPwd = "secretpassword";
68
    int siteScheduleID = 1;
69
    String unit = "months";
70
    int updateFrequency = 1;
71

  
53 72
    harvester = new Harvester();
73
    Harvester.loadProperties(configfile);
74
    harvester.getConnection();  // initializes the database connection
75
    harvester.initLogIDs();
76
    harvester.setHarvestStartTime(new Date());
77

  
78
    harvestSiteScheduleFuture = new HarvestSiteSchedule(harvester,
79
                                                  siteScheduleID,
80
                                                  documentListURL,
81
                                                  ldapDN,
82
                                                  ldapPwd,
83
                                                  dateNextHarvestFuture,
84
                                                  dateLastHarvest,
85
                                                  updateFrequency,
86
                                                  unit,
87
                                                  contactEmail
88
                                                 );
89

  
90
    harvestSiteSchedulePast = new HarvestSiteSchedule(harvester,
91
                                                  siteScheduleID,
92
                                                  documentListURL,
93
                                                  ldapDN,
94
                                                  ldapPwd,
95
                                                  dateNextHarvestPast,
96
                                                  dateLastHarvest,
97
                                                  updateFrequency,
98
                                                  unit,
99
                                                  contactEmail
100
                                                 );
101

  
54 102
  }
55 103
  
56 104
  
57 105
  protected void tearDown() {
106
    harvester.closeConnection();
58 107
  }
108
  
109
  
110
  public void testDueForHarvestFalse() {
111
    boolean dueForHarvest;
112
    
113
    dueForHarvest = harvestSiteScheduleFuture.dueForHarvest();
114
    assertTrue(dueForHarvest == false);
115
  }
116
  
117
  
118
  public void testParseHarvestList() {
119
    boolean success = false;
120
    String schemaLocation =
121
         "eml://ecoinformatics.org/harvestList ./lib/harvester/harvestList.xsd";
122
         
123
    harvestSiteScheduleFuture.setSchemaLocation(schemaLocation);
124
    
125
    try {
126
      success = harvestSiteScheduleFuture.parseHarvestList();
127
      assertTrue(success == true);
128
    }
129
    catch (ParserConfigurationException e) {
130
      fail("ParserConfigurationException: " + e.getMessage());
131
    }    
132
  }
59 133

  
60 134

  
61 135
  /**
62
   * Dummy test. Will be replaced by the actual Harvester tests when 
63
   * they are implemented.
136
   * Tests the printOutput() method.
64 137
   */
65
  public void testHarvesterObject() {
66
    assertTrue(harvester.equals(harvester));
138
  public void testPrintOutput() {
139
    harvestSiteScheduleFuture.printOutput(System.out);
67 140
  }
68 141
  
69 142
  
143
  public void testDueForHarvestTrue() {
144
    boolean dueForHarvest;
145
    
146
    dueForHarvest = harvestSiteSchedulePast.dueForHarvest();
147
    assertTrue(dueForHarvest == true);
148
  }
149

  
150

  
70 151
  /**
152
   * Test that the harvesterDocument object was created successfully.
153
   */
154
  public void testHarvestSiteScheduleObject() {
155
    assertTrue(harvestSiteScheduleFuture != null);
156
    assertTrue(harvestSiteSchedulePast != null);
157
  }
158
  
159
  
160
  /**
71 161
   * Returns the test suite. The test suite consists of all methods in this
72 162
   * class whose names start with "test".
73 163
   * 

Also available in: Unified diff