Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *    Purpose: To test the ReplicationServerList class by JUnit
6
 *    Authors: Jing Tao
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-08-05 17:50:14 -0700 (Tue, 05 Aug 2008) $'
10
 * '$Revision: 4213 $'
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
import edu.ucsb.nceas.metacat.service.PropertyService;
31
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
32
//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
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39

    
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
    private static String metacatReplicationURL;
51
	 /* Initialize properties*/
52
	  static
53
	  {
54
		  try
55
		  {
56
			 metacatReplicationURL=
57
                     PropertyService.getProperty("junitreplicationurl");
58
			  PropertyService.getInstance("build/tests");
59
			  MetaCatUtil.pathsForIndexing 
60
			         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
61
		  }
62
		  catch(Exception e)
63
		  {
64
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
65
		  }
66
	  }
67

    
68
     private ReplicationServerList serverList = null;
69
     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
70
 
71
  /**
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
      log.debug("Error in ReplicationServerList() to get" +
125
                        " DBConnection pool"+e.getMessage());
126
    }//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
      log.debug("Error in ReplicationServerListTest.suite: "+
186
                                e.getMessage());
187
    }//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
    assertTrue(1 == 1);
204
  }
205

    
206
   
207
  /**
208
   * Test the a empty server list is empty
209
   */
210
  public void testEmptyServerList()
211
  {
212
    
213
    assertTrue(serverList.isEmpty());
214
  }
215
  
216
  /**
217
   * Test the a non-empty server list is non-empty
218
   */
219
  public void testNonEmptyServerList()
220
  {
221
    
222
    assertTrue(!serverList.isEmpty());
223
  }
224
  
225
  /**
226
   * Test the size() method
227
   */
228
  public void testSize()
229
  {
230
    int size = serverList.size();
231
    assertTrue(size ==2);
232
  }
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
    assertTrue(serverName.equals("dev"));
242
  }
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
    assertTrue(serverName.equals("epsilon"));
252
  }
253
   
254
  
255
  /**
256
   * Test the a given server is not in the server list
257
   */
258
  public void testServerIsNotInList()
259
  {
260
    assertTrue(!serverList.isGivenServerInList("localhost"));
261
  }
262
  
263
  /**
264
   * Test the a given server is in the server list
265
   */
266
  public void testServerIsInList()
267
  {
268
    assertTrue(serverList.isGivenServerInList("dev"));
269
  }
270
  
271
  /**
272
   * Test the method getLastCheckedDate
273
   */
274
  public void testGetLastCheckedDate()
275
  {
276
    String lastCheckedDate = serverList.getLastCheckedDate("dev");
277
    assertTrue(lastCheckedDate.equals("0001-01-01 BC"));
278
  }
279
  
280
  /**
281
   * Test the method getReplicationValue(resulst is true)
282
   */
283
  public void testGetReplicationValueTrue()
284
  {
285
    assertTrue(serverList.getReplicationValue("dev"));
286
  }
287
  
288
  /**
289
   * Test the method getReplicationValue(result is false)
290
   */
291
  public void testGetReplicationValueFalse()
292
  {
293
    assertTrue(!serverList.getReplicationValue("epsilon"));
294
  }
295
  
296
  /**
297
   * Test the method getDataReplicationValue(result is true)
298
   */
299
  public void testGetDataReplicationValueTrue()
300
  {
301
    assertTrue(serverList.getDataReplicationValue("dev"));
302
  }
303
  
304
  /**
305
   * Test the method getDataReplicationValue(result is false)
306
   */
307
  public void testGetDataReplicationValueFalse()
308
  {
309
    assertTrue(!serverList.getDataReplicationValue("epsilon"));
310
  }
311

    
312
  /**
313
   * Test the method getHubValue(result is true)
314
   */
315
  public void testGetHubValueTrue()
316
  {
317
    assertTrue(serverList.getHubValue("dev"));
318
  }
319
  
320
  /**
321
   * Test the method getHubValue(result is false)
322
   */
323
  public void testGetHubValueFalse()
324
  {
325
    assertTrue(!serverList.getHubValue("epsilon"));
326
  }  
327

    
328
   
329
}//ReplicationServerListTest
(15-15/19)