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: 2009-02-23 11:29:07 -0800 (Mon, 23 Feb 2009) $'
10
 * '$Revision: 4816 $'
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.MCTestCase;
30
import edu.ucsb.nceas.metacat.*;
31
import edu.ucsb.nceas.metacat.service.PropertyService;
32
import edu.ucsb.nceas.metacat.util.MetacatUtil;
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37

    
38
import java.io.*;
39
import java.net.*;
40

    
41
/**
42
 * A JUnit test for testing Step class processing
43
 */
44
public class ReplicationServerListTest extends MCTestCase
45
{
46
    private static String metacatReplicationURL;
47
	 /* Initialize properties*/
48
	  static
49
	  {
50
		  try
51
		  {
52
			 PropertyService.getInstance();
53
			 metacatReplicationURL=
54
                     PropertyService.getProperty("junitreplicationurl");
55
		  }
56
		  catch(Exception e)
57
		  {
58
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
59
		  }
60
	  }
61

    
62
     private ReplicationServerList serverList = null;
63
     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
64
 
65
  /**
66
   * Constructor to build the test
67
   *
68
   * @param name the name of the test method
69
   */
70
  public ReplicationServerListTest(String name)
71
  {
72
    super(name);
73
  }
74

    
75
  /**
76
   * Constructor to build the test
77
   *
78
   * @param name the name of the test method
79
   * @param list the ReplicationServerList will be passed int
80
   */
81
  public ReplicationServerListTest(String name, ReplicationServerList list)
82
  {
83
    super(name);
84
    serverList=list;
85
  }
86
 
87
  /**
88
   * Establish a testing framework by initializing appropriate objects
89
   */
90
  public void setUp()
91
 {
92
   
93
 }
94

    
95
  /**
96
   * Release any objects after tests are complete
97
   */
98
  public void tearDown()
99
  {
100
    //DBConnectionPool will be release
101
    DBConnectionPool.release();
102
  }
103

    
104
  /**
105
   * Create a suite of tests to be run together
106
   */
107
  public static Test suite()
108
  {
109
     //Get DBConnection pool, this is only for junit test.
110
    //Because DBConnection is singleton class. So there is only one DBConnection
111
    //pool in the program
112
    try
113
    {
114
      DBConnectionPool pool = DBConnectionPool.getInstance();
115
    }//try
116
    catch (Exception e)
117
    {
118
      log.debug("Error in ReplicationServerList() to get" +
119
                        " DBConnection pool"+e.getMessage());
120
    }//catch
121
    
122
    TestSuite suite = new TestSuite();
123
    suite.addTest(new ReplicationServerListTest("initialize"));
124
    
125
    try
126
    {
127
  
128
    //Add two server into xml_replication table
129
    URL dev = new URL(metacatReplicationURL+"?action=servercontrol&server=dev"+
130
                        "&subaction=add&replicate=1&datareplicate=1&hub=1");
131
    URL epsilon = new URL(metacatReplicationURL+"?action=servercontrol"+
132
             "&server=epsilon&subaction=add&replicate=0&datareplicate=1&hub=0");
133
    InputStream input = dev.openStream();
134
    input.close();
135
    input = epsilon.openStream();
136
    input.close();
137
    
138
    //create a new server list
139
    ReplicationServerList list = new ReplicationServerList();
140
    
141
    //Doing test test cases
142
    suite.addTest(new ReplicationServerListTest("testSize", list));
143
    
144
    suite.addTest(new ReplicationServerListTest("testServerAt0", list));
145
    suite.addTest(new ReplicationServerListTest("testServerAt1", list));
146
    
147
    suite.addTest(new ReplicationServerListTest
148
                                              ("testNonEmptyServerList", list));
149
    
150
    suite.addTest(new ReplicationServerListTest("testServerIsNotInList", list));
151
    suite.addTest(new ReplicationServerListTest("testServerIsInList", list));
152
    
153
    suite.addTest(new ReplicationServerListTest
154
                                              ("testGetLastCheckedDate", list));
155
    
156
    suite.addTest(new ReplicationServerListTest
157
                                        ("testGetReplicationValueFalse", list));
158
    
159
    suite.addTest(new ReplicationServerListTest
160
                                         ("testGetReplicationValueTrue", list));
161
    suite.addTest(new ReplicationServerListTest
162
                                    ("testGetDataReplicationValueFalse", list));
163
    
164
    suite.addTest(new ReplicationServerListTest("testGetHubValueTrue", list));
165
    suite.addTest(new ReplicationServerListTest("testGetHubValueFalse", list));
166
    
167
    //Delete this two server
168
    URL deleteDev = new URL(metacatReplicationURL+"?action=servercontrol" +
169
                                        "&server=dev&subaction=delete");
170
    URL deleteEpsilon = new URL(metacatReplicationURL+"?action=servercontrol" +
171
                                        "&server=epsilon&subaction=delete");
172
    input = deleteDev.openStream();
173
    input.close();
174
    input = deleteEpsilon.openStream();
175
    input.close();                                   
176
    }//try
177
    catch (Exception e)
178
    {
179
      log.debug("Error in ReplicationServerListTest.suite: "+
180
                                e.getMessage());
181
    }//catch
182
    
183
 
184
    
185
                              
186
    return suite;
187
  }
188
  
189

    
190

    
191
  /**
192
   * Run an initial test that always passes to check that the test
193
   * harness is working.
194
   */
195
  public void initialize()
196
  {
197
    assertTrue(1 == 1);
198
  }
199

    
200
   
201
  /**
202
   * Test the a empty server list is empty
203
   */
204
  public void testEmptyServerList()
205
  {
206
    
207
    assertTrue(serverList.isEmpty());
208
  }
209
  
210
  /**
211
   * Test the a non-empty server list is non-empty
212
   */
213
  public void testNonEmptyServerList()
214
  {
215
    
216
    assertTrue(!serverList.isEmpty());
217
  }
218
  
219
  /**
220
   * Test the size() method
221
   */
222
  public void testSize()
223
  {
224
    int size = serverList.size();
225
    assertTrue(size ==2);
226
  }
227
  
228
  /**
229
   * Test the method serverAt(0)
230
   */
231
  public void testServerAt0()
232
  {
233
    ReplicationServer server = serverList.serverAt(0);
234
    String serverName = server.getServerName();
235
    assertTrue(serverName.equals("dev"));
236
  }
237
  
238
  /**
239
   * Test the method serverAt(1)
240
   */
241
  public void testServerAt1()
242
  {
243
    ReplicationServer server = serverList.serverAt(1);
244
    String serverName = server.getServerName();
245
    assertTrue(serverName.equals("epsilon"));
246
  }
247
   
248
  
249
  /**
250
   * Test the a given server is not in the server list
251
   */
252
  public void testServerIsNotInList()
253
  {
254
    assertTrue(!serverList.isGivenServerInList("localhost"));
255
  }
256
  
257
  /**
258
   * Test the a given server is in the server list
259
   */
260
  public void testServerIsInList()
261
  {
262
    assertTrue(serverList.isGivenServerInList("dev"));
263
  }
264
  
265
  /**
266
   * Test the method getLastCheckedDate
267
   */
268
  public void testGetLastCheckedDate()
269
  {
270
    String lastCheckedDate = serverList.getLastCheckedDate("dev");
271
    assertTrue(lastCheckedDate.equals("0001-01-01 BC"));
272
  }
273
  
274
  /**
275
   * Test the method getReplicationValue(resulst is true)
276
   */
277
  public void testGetReplicationValueTrue()
278
  {
279
    assertTrue(serverList.getReplicationValue("dev"));
280
  }
281
  
282
  /**
283
   * Test the method getReplicationValue(result is false)
284
   */
285
  public void testGetReplicationValueFalse()
286
  {
287
    assertTrue(!serverList.getReplicationValue("epsilon"));
288
  }
289
  
290
  /**
291
   * Test the method getDataReplicationValue(result is true)
292
   */
293
  public void testGetDataReplicationValueTrue()
294
  {
295
    assertTrue(serverList.getDataReplicationValue("dev"));
296
  }
297
  
298
  /**
299
   * Test the method getDataReplicationValue(result is false)
300
   */
301
  public void testGetDataReplicationValueFalse()
302
  {
303
    assertTrue(!serverList.getDataReplicationValue("epsilon"));
304
  }
305

    
306
  /**
307
   * Test the method getHubValue(result is true)
308
   */
309
  public void testGetHubValueTrue()
310
  {
311
    assertTrue(serverList.getHubValue("dev"));
312
  }
313
  
314
  /**
315
   * Test the method getHubValue(result is false)
316
   */
317
  public void testGetHubValueFalse()
318
  {
319
    assertTrue(!serverList.getHubValue("epsilon"));
320
  }  
321

    
322
   
323
}//ReplicationServerListTest
(15-15/20)