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-08-24 14:42:25 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5035 $'
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.database.*;
32
import edu.ucsb.nceas.metacat.replication.*;
33
import edu.ucsb.nceas.metacat.properties.PropertyService;
34
import edu.ucsb.nceas.metacat.util.MetacatUtil;
35
import junit.framework.Test;
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

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

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

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

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

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

    
192

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

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

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

    
324
   
325
}//ReplicationServerListTest
(17-17/22)