Project

General

Profile

1 1292 tao
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5 3080 jones
 *    Purpose: To test the ReplicationServerList class by JUnit
6
 *    Authors: Jing Tao
7 1292 tao
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacattest;
28
29 4384 daigle
import edu.ucsb.nceas.MCTestCase;
30 1292 tao
import edu.ucsb.nceas.metacat.*;
31 5027 daigle
import edu.ucsb.nceas.metacat.database.*;
32
import edu.ucsb.nceas.metacat.replication.*;
33 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
34 4701 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
35 1292 tao
import junit.framework.Test;
36
import junit.framework.TestSuite;
37 2917 jones
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39 1292 tao
40
import java.io.*;
41
import java.net.*;
42 6118 leinfelder
import java.util.Date;
43 1292 tao
44
/**
45
 * A JUnit test for testing Step class processing
46
 */
47 4384 daigle
public class ReplicationServerListTest extends MCTestCase
48 1292 tao
{
49 4080 daigle
    private static String metacatReplicationURL;
50 4127 daigle
	 /* Initialize properties*/
51 3567 tao
	  static
52
	  {
53
		  try
54
		  {
55 4816 daigle
			 PropertyService.getInstance();
56 4080 daigle
			 metacatReplicationURL=
57
                     PropertyService.getProperty("junitreplicationurl");
58 3567 tao
		  }
59
		  catch(Exception e)
60
		  {
61
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
62
		  }
63
	  }
64 4080 daigle
65 3567 tao
     private ReplicationServerList serverList = null;
66
     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
67
68 1292 tao
  /**
69
   * Constructor to build the test
70
   *
71
   * @param name the name of the test method
72
   */
73
  public ReplicationServerListTest(String name)
74
  {
75
    super(name);
76
  }
77
78
  /**
79
   * Constructor to build the test
80
   *
81
   * @param name the name of the test method
82
   * @param list the ReplicationServerList will be passed int
83
   */
84
  public ReplicationServerListTest(String name, ReplicationServerList list)
85
  {
86
    super(name);
87
    serverList=list;
88
  }
89
90
  /**
91
   * Establish a testing framework by initializing appropriate objects
92
   */
93
  public void setUp()
94
 {
95
96
 }
97
98
  /**
99
   * Release any objects after tests are complete
100
   */
101
  public void tearDown()
102
  {
103
    //DBConnectionPool will be release
104
    DBConnectionPool.release();
105
  }
106
107
  /**
108
   * Create a suite of tests to be run together
109
   */
110
  public static Test suite()
111
  {
112
     //Get DBConnection pool, this is only for junit test.
113
    //Because DBConnection is singleton class. So there is only one DBConnection
114
    //pool in the program
115
    try
116
    {
117
      DBConnectionPool pool = DBConnectionPool.getInstance();
118
    }//try
119
    catch (Exception e)
120
    {
121 2917 jones
      log.debug("Error in ReplicationServerList() to get" +
122
                        " DBConnection pool"+e.getMessage());
123 1292 tao
    }//catch
124
125
    TestSuite suite = new TestSuite();
126
    suite.addTest(new ReplicationServerListTest("initialize"));
127
128
    try
129
    {
130
131
    //Add two server into xml_replication table
132
    URL dev = new URL(metacatReplicationURL+"?action=servercontrol&server=dev"+
133
                        "&subaction=add&replicate=1&datareplicate=1&hub=1");
134
    URL epsilon = new URL(metacatReplicationURL+"?action=servercontrol"+
135
             "&server=epsilon&subaction=add&replicate=0&datareplicate=1&hub=0");
136
    InputStream input = dev.openStream();
137
    input.close();
138
    input = epsilon.openStream();
139
    input.close();
140
141
    //create a new server list
142
    ReplicationServerList list = new ReplicationServerList();
143
144
    //Doing test test cases
145
    suite.addTest(new ReplicationServerListTest("testSize", list));
146
147
    suite.addTest(new ReplicationServerListTest("testServerAt0", list));
148
    suite.addTest(new ReplicationServerListTest("testServerAt1", list));
149
150
    suite.addTest(new ReplicationServerListTest
151
                                              ("testNonEmptyServerList", list));
152
153
    suite.addTest(new ReplicationServerListTest("testServerIsNotInList", list));
154
    suite.addTest(new ReplicationServerListTest("testServerIsInList", list));
155
156
    suite.addTest(new ReplicationServerListTest
157
                                              ("testGetLastCheckedDate", list));
158
159
    suite.addTest(new ReplicationServerListTest
160
                                        ("testGetReplicationValueFalse", list));
161
162
    suite.addTest(new ReplicationServerListTest
163
                                         ("testGetReplicationValueTrue", list));
164
    suite.addTest(new ReplicationServerListTest
165
                                    ("testGetDataReplicationValueFalse", list));
166
167
    suite.addTest(new ReplicationServerListTest("testGetHubValueTrue", list));
168
    suite.addTest(new ReplicationServerListTest("testGetHubValueFalse", list));
169
170
    //Delete this two server
171
    URL deleteDev = new URL(metacatReplicationURL+"?action=servercontrol" +
172
                                        "&server=dev&subaction=delete");
173
    URL deleteEpsilon = new URL(metacatReplicationURL+"?action=servercontrol" +
174
                                        "&server=epsilon&subaction=delete");
175
    input = deleteDev.openStream();
176
    input.close();
177
    input = deleteEpsilon.openStream();
178
    input.close();
179
    }//try
180
    catch (Exception e)
181
    {
182 2917 jones
      log.debug("Error in ReplicationServerListTest.suite: "+
183
                                e.getMessage());
184 1292 tao
    }//catch
185
186
187
188
189
    return suite;
190
  }
191
192
193
194
  /**
195
   * Run an initial test that always passes to check that the test
196
   * harness is working.
197
   */
198
  public void initialize()
199
  {
200 1506 tao
    assertTrue(1 == 1);
201 1292 tao
  }
202
203
204
  /**
205
   * Test the a empty server list is empty
206
   */
207
  public void testEmptyServerList()
208
  {
209
210 1506 tao
    assertTrue(serverList.isEmpty());
211 1292 tao
  }
212
213
  /**
214
   * Test the a non-empty server list is non-empty
215
   */
216
  public void testNonEmptyServerList()
217
  {
218
219 1506 tao
    assertTrue(!serverList.isEmpty());
220 1292 tao
  }
221
222
  /**
223
   * Test the size() method
224
   */
225
  public void testSize()
226
  {
227
    int size = serverList.size();
228 1506 tao
    assertTrue(size ==2);
229 1292 tao
  }
230
231
  /**
232
   * Test the method serverAt(0)
233
   */
234
  public void testServerAt0()
235
  {
236
    ReplicationServer server = serverList.serverAt(0);
237
    String serverName = server.getServerName();
238 1506 tao
    assertTrue(serverName.equals("dev"));
239 1292 tao
  }
240
241
  /**
242
   * Test the method serverAt(1)
243
   */
244
  public void testServerAt1()
245
  {
246
    ReplicationServer server = serverList.serverAt(1);
247
    String serverName = server.getServerName();
248 1506 tao
    assertTrue(serverName.equals("epsilon"));
249 1292 tao
  }
250
251
252
  /**
253
   * Test the a given server is not in the server list
254
   */
255
  public void testServerIsNotInList()
256
  {
257 1506 tao
    assertTrue(!serverList.isGivenServerInList("localhost"));
258 1292 tao
  }
259
260
  /**
261
   * Test the a given server is in the server list
262
   */
263
  public void testServerIsInList()
264
  {
265 1506 tao
    assertTrue(serverList.isGivenServerInList("dev"));
266 1292 tao
  }
267
268
  /**
269
   * Test the method getLastCheckedDate
270
   */
271
  public void testGetLastCheckedDate()
272
  {
273 6118 leinfelder
    Date lastCheckedDate = serverList.getLastCheckedDate("dev");
274 1506 tao
    assertTrue(lastCheckedDate.equals("0001-01-01 BC"));
275 1292 tao
  }
276
277
  /**
278
   * Test the method getReplicationValue(resulst is true)
279
   */
280
  public void testGetReplicationValueTrue()
281
  {
282 1506 tao
    assertTrue(serverList.getReplicationValue("dev"));
283 1292 tao
  }
284
285
  /**
286
   * Test the method getReplicationValue(result is false)
287
   */
288
  public void testGetReplicationValueFalse()
289
  {
290 1506 tao
    assertTrue(!serverList.getReplicationValue("epsilon"));
291 1292 tao
  }
292
293
  /**
294
   * Test the method getDataReplicationValue(result is true)
295
   */
296
  public void testGetDataReplicationValueTrue()
297
  {
298 1506 tao
    assertTrue(serverList.getDataReplicationValue("dev"));
299 1292 tao
  }
300
301
  /**
302
   * Test the method getDataReplicationValue(result is false)
303
   */
304
  public void testGetDataReplicationValueFalse()
305
  {
306 1506 tao
    assertTrue(!serverList.getDataReplicationValue("epsilon"));
307 1292 tao
  }
308
309
  /**
310
   * Test the method getHubValue(result is true)
311
   */
312
  public void testGetHubValueTrue()
313
  {
314 1506 tao
    assertTrue(serverList.getHubValue("dev"));
315 1292 tao
  }
316
317
  /**
318
   * Test the method getHubValue(result is false)
319
   */
320
  public void testGetHubValueFalse()
321
  {
322 1506 tao
    assertTrue(!serverList.getHubValue("epsilon"));
323 1292 tao
  }
324
325
326
}//ReplicationServerListTest