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: leinfelder $'
9
 *     '$Date: 2011-06-01 13:45:13 -0700 (Wed, 01 Jun 2011) $'
10
 * '$Revision: 6118 $'
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
import java.util.Date;
43

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

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

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

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

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

    
193

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

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

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

    
325
   
326
}//ReplicationServerListTest
(19-19/24)