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