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 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
32
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
33 1292 tao
import junit.framework.Test;
34
import junit.framework.TestSuite;
35 2917 jones
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37 1292 tao
38
import java.io.*;
39
import java.net.*;
40
41
/**
42
 * A JUnit test for testing Step class processing
43
 */
44 4384 daigle
public class ReplicationServerListTest extends MCTestCase
45 1292 tao
{
46 4080 daigle
    private static String metacatReplicationURL;
47 4127 daigle
	 /* Initialize properties*/
48 3567 tao
	  static
49
	  {
50
		  try
51
		  {
52 4080 daigle
			 metacatReplicationURL=
53
                     PropertyService.getProperty("junitreplicationurl");
54 3567 tao
			  MetaCatUtil.pathsForIndexing
55 4213 daigle
			         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
56 3567 tao
		  }
57
		  catch(Exception e)
58
		  {
59
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
60
		  }
61
	  }
62 4080 daigle
63 3567 tao
     private ReplicationServerList serverList = null;
64
     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
65
66 1292 tao
  /**
67
   * Constructor to build the test
68
   *
69
   * @param name the name of the test method
70
   */
71
  public ReplicationServerListTest(String name)
72
  {
73
    super(name);
74
  }
75
76
  /**
77
   * Constructor to build the test
78
   *
79
   * @param name the name of the test method
80
   * @param list the ReplicationServerList will be passed int
81
   */
82
  public ReplicationServerListTest(String name, ReplicationServerList list)
83
  {
84
    super(name);
85
    serverList=list;
86
  }
87
88
  /**
89
   * Establish a testing framework by initializing appropriate objects
90
   */
91
  public void setUp()
92
 {
93
94
 }
95
96
  /**
97
   * Release any objects after tests are complete
98
   */
99
  public void tearDown()
100
  {
101
    //DBConnectionPool will be release
102
    DBConnectionPool.release();
103
  }
104
105
  /**
106
   * Create a suite of tests to be run together
107
   */
108
  public static Test suite()
109
  {
110
     //Get DBConnection pool, this is only for junit test.
111
    //Because DBConnection is singleton class. So there is only one DBConnection
112
    //pool in the program
113
    try
114
    {
115
      DBConnectionPool pool = DBConnectionPool.getInstance();
116
    }//try
117
    catch (Exception e)
118
    {
119 2917 jones
      log.debug("Error in ReplicationServerList() to get" +
120
                        " DBConnection pool"+e.getMessage());
121 1292 tao
    }//catch
122
123
    TestSuite suite = new TestSuite();
124
    suite.addTest(new ReplicationServerListTest("initialize"));
125
126
    try
127
    {
128
129
    //Add two server into xml_replication table
130
    URL dev = new URL(metacatReplicationURL+"?action=servercontrol&server=dev"+
131
                        "&subaction=add&replicate=1&datareplicate=1&hub=1");
132
    URL epsilon = new URL(metacatReplicationURL+"?action=servercontrol"+
133
             "&server=epsilon&subaction=add&replicate=0&datareplicate=1&hub=0");
134
    InputStream input = dev.openStream();
135
    input.close();
136
    input = epsilon.openStream();
137
    input.close();
138
139
    //create a new server list
140
    ReplicationServerList list = new ReplicationServerList();
141
142
    //Doing test test cases
143
    suite.addTest(new ReplicationServerListTest("testSize", list));
144
145
    suite.addTest(new ReplicationServerListTest("testServerAt0", list));
146
    suite.addTest(new ReplicationServerListTest("testServerAt1", list));
147
148
    suite.addTest(new ReplicationServerListTest
149
                                              ("testNonEmptyServerList", list));
150
151
    suite.addTest(new ReplicationServerListTest("testServerIsNotInList", list));
152
    suite.addTest(new ReplicationServerListTest("testServerIsInList", list));
153
154
    suite.addTest(new ReplicationServerListTest
155
                                              ("testGetLastCheckedDate", list));
156
157
    suite.addTest(new ReplicationServerListTest
158
                                        ("testGetReplicationValueFalse", list));
159
160
    suite.addTest(new ReplicationServerListTest
161
                                         ("testGetReplicationValueTrue", list));
162
    suite.addTest(new ReplicationServerListTest
163
                                    ("testGetDataReplicationValueFalse", list));
164
165
    suite.addTest(new ReplicationServerListTest("testGetHubValueTrue", list));
166
    suite.addTest(new ReplicationServerListTest("testGetHubValueFalse", list));
167
168
    //Delete this two server
169
    URL deleteDev = new URL(metacatReplicationURL+"?action=servercontrol" +
170
                                        "&server=dev&subaction=delete");
171
    URL deleteEpsilon = new URL(metacatReplicationURL+"?action=servercontrol" +
172
                                        "&server=epsilon&subaction=delete");
173
    input = deleteDev.openStream();
174
    input.close();
175
    input = deleteEpsilon.openStream();
176
    input.close();
177
    }//try
178
    catch (Exception e)
179
    {
180 2917 jones
      log.debug("Error in ReplicationServerListTest.suite: "+
181
                                e.getMessage());
182 1292 tao
    }//catch
183
184
185
186
187
    return suite;
188
  }
189
190
191
192
  /**
193
   * Run an initial test that always passes to check that the test
194
   * harness is working.
195
   */
196
  public void initialize()
197
  {
198 1506 tao
    assertTrue(1 == 1);
199 1292 tao
  }
200
201
202
  /**
203
   * Test the a empty server list is empty
204
   */
205
  public void testEmptyServerList()
206
  {
207
208 1506 tao
    assertTrue(serverList.isEmpty());
209 1292 tao
  }
210
211
  /**
212
   * Test the a non-empty server list is non-empty
213
   */
214
  public void testNonEmptyServerList()
215
  {
216
217 1506 tao
    assertTrue(!serverList.isEmpty());
218 1292 tao
  }
219
220
  /**
221
   * Test the size() method
222
   */
223
  public void testSize()
224
  {
225
    int size = serverList.size();
226 1506 tao
    assertTrue(size ==2);
227 1292 tao
  }
228
229
  /**
230
   * Test the method serverAt(0)
231
   */
232
  public void testServerAt0()
233
  {
234
    ReplicationServer server = serverList.serverAt(0);
235
    String serverName = server.getServerName();
236 1506 tao
    assertTrue(serverName.equals("dev"));
237 1292 tao
  }
238
239
  /**
240
   * Test the method serverAt(1)
241
   */
242
  public void testServerAt1()
243
  {
244
    ReplicationServer server = serverList.serverAt(1);
245
    String serverName = server.getServerName();
246 1506 tao
    assertTrue(serverName.equals("epsilon"));
247 1292 tao
  }
248
249
250
  /**
251
   * Test the a given server is not in the server list
252
   */
253
  public void testServerIsNotInList()
254
  {
255 1506 tao
    assertTrue(!serverList.isGivenServerInList("localhost"));
256 1292 tao
  }
257
258
  /**
259
   * Test the a given server is in the server list
260
   */
261
  public void testServerIsInList()
262
  {
263 1506 tao
    assertTrue(serverList.isGivenServerInList("dev"));
264 1292 tao
  }
265
266
  /**
267
   * Test the method getLastCheckedDate
268
   */
269
  public void testGetLastCheckedDate()
270
  {
271
    String lastCheckedDate = serverList.getLastCheckedDate("dev");
272 1506 tao
    assertTrue(lastCheckedDate.equals("0001-01-01 BC"));
273 1292 tao
  }
274
275
  /**
276
   * Test the method getReplicationValue(resulst is true)
277
   */
278
  public void testGetReplicationValueTrue()
279
  {
280 1506 tao
    assertTrue(serverList.getReplicationValue("dev"));
281 1292 tao
  }
282
283
  /**
284
   * Test the method getReplicationValue(result is false)
285
   */
286
  public void testGetReplicationValueFalse()
287
  {
288 1506 tao
    assertTrue(!serverList.getReplicationValue("epsilon"));
289 1292 tao
  }
290
291
  /**
292
   * Test the method getDataReplicationValue(result is true)
293
   */
294
  public void testGetDataReplicationValueTrue()
295
  {
296 1506 tao
    assertTrue(serverList.getDataReplicationValue("dev"));
297 1292 tao
  }
298
299
  /**
300
   * Test the method getDataReplicationValue(result is false)
301
   */
302
  public void testGetDataReplicationValueFalse()
303
  {
304 1506 tao
    assertTrue(!serverList.getDataReplicationValue("epsilon"));
305 1292 tao
  }
306
307
  /**
308
   * Test the method getHubValue(result is true)
309
   */
310
  public void testGetHubValueTrue()
311
  {
312 1506 tao
    assertTrue(serverList.getHubValue("dev"));
313 1292 tao
  }
314
315
  /**
316
   * Test the method getHubValue(result is false)
317
   */
318
  public void testGetHubValueFalse()
319
  {
320 1506 tao
    assertTrue(!serverList.getHubValue("epsilon"));
321 1292 tao
  }
322
323
324
}//ReplicationServerListTest