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: jones $'
9
 *     '$Date: 2006-11-10 11:30:36 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3080 $'
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.morpho.framework.*;
31
import junit.framework.Test;
32
import junit.framework.TestCase;
33
import junit.framework.TestResult;
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
import java.util.*;
41

    
42

    
43
/**
44
 * A JUnit test for testing Step class processing
45
 */
46
public class ReplicationServerListTest extends TestCase
47
{
48
  private static String metacatReplicationURL=
49
                                   MetaCatUtil.getOption("junitreplicationurl");
50
  private ReplicationServerList serverList = null;
51
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
52
  
53
  /**
54
   * Constructor to build the test
55
   *
56
   * @param name the name of the test method
57
   */
58
  public ReplicationServerListTest(String name)
59
  {
60
    super(name);
61
  }
62

    
63
  /**
64
   * Constructor to build the test
65
   *
66
   * @param name the name of the test method
67
   * @param list the ReplicationServerList will be passed int
68
   */
69
  public ReplicationServerListTest(String name, ReplicationServerList list)
70
  {
71
    super(name);
72
    serverList=list;
73
  }
74
 
75
  /**
76
   * Establish a testing framework by initializing appropriate objects
77
   */
78
  public void setUp()
79
 {
80
   
81
 }
82

    
83
  /**
84
   * Release any objects after tests are complete
85
   */
86
  public void tearDown()
87
  {
88
    //DBConnectionPool will be release
89
    DBConnectionPool.release();
90
  }
91

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

    
178

    
179
  /**
180
   * Run an initial test that always passes to check that the test
181
   * harness is working.
182
   */
183
  public void initialize()
184
  {
185
    assertTrue(1 == 1);
186
  }
187

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

    
294
  /**
295
   * Test the method getHubValue(result is true)
296
   */
297
  public void testGetHubValueTrue()
298
  {
299
    assertTrue(serverList.getHubValue("dev"));
300
  }
301
  
302
  /**
303
   * Test the method getHubValue(result is false)
304
   */
305
  public void testGetHubValueFalse()
306
  {
307
    assertTrue(!serverList.getHubValue("epsilon"));
308
  }  
309

    
310
   
311
}//ReplicationServerListTest
(10-10/12)