KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > AbstractTestBalancer


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.balancer;
22
23 import java.util.List JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 import net.sf.hajdbc.Balancer;
27 import net.sf.hajdbc.Database;
28 import net.sf.hajdbc.MockDatabase;
29
30 import org.testng.annotations.AfterMethod;
31 import org.testng.annotations.Test;
32
33 /**
34  * @author Paul Ferraro
35  * @since 1.0
36  */

37 public abstract class AbstractTestBalancer
38 {
39     private Balancer balancer = createBalancer();
40     
41     protected abstract Balancer createBalancer();
42
43     @AfterMethod
44     protected void tearDown()
45     {
46         for (Database database: this.balancer.list())
47         {
48             this.balancer.remove(database);
49         }
50     }
51     
52     /**
53      * Test method for {@link Balancer#add(Database)}
54      */

55     @Test
56     public void testAdd()
57     {
58         Database database = new MockDatabase("1", 1);
59         
60         boolean added = this.balancer.add(database);
61         
62         assert added;
63
64         added = this.balancer.add(database);
65
66         assert !added;
67     }
68
69     /**
70      * Test method for {@link Balancer#beforeOperation(Database)}
71      */

72     @Test
73     public void testBeforeOperation()
74     {
75         Database database = new MockDatabase("db1", 1);
76
77         this.balancer.add(database);
78         
79         this.balancer.beforeOperation(database);
80     }
81
82     /**
83      * Test method for {@link Balancer#afterOperation(Database)}
84      */

85     @Test
86     public void testAfterOperation()
87     {
88         Database database = new MockDatabase("db1", 1);
89
90         this.balancer.add(database);
91         
92         this.balancer.afterOperation(database);
93     }
94     
95     /**
96      * Test method for {@link Balancer#remove(Database)}
97      */

98     @Test
99     public void testRemove()
100     {
101         Database database = new MockDatabase("1", 1);
102         
103         boolean removed = this.balancer.remove(database);
104
105         assert !removed;
106         
107         this.balancer.add(database);
108
109         removed = this.balancer.remove(database);
110
111         assert removed;
112         
113         removed = this.balancer.remove(database);
114
115         assert !removed;
116     }
117
118     /**
119      * Test method for {@link Balancer#contains(Database)}
120      */

121     @Test
122     public void testGetDatabases()
123     {
124         List JavaDoc<Database> databases = this.balancer.list();
125         
126         assert databases.isEmpty() : databases.size();
127         
128         Database database1 = new MockDatabase("db1", 1);
129         this.balancer.add(database1);
130         
131         databases = this.balancer.list();
132         
133         assert databases.size() == 1 : databases.size();
134         assert databases.contains(database1);
135         
136         Database database2 = new MockDatabase("db2", 1);
137         this.balancer.add(database2);
138
139         databases = this.balancer.list();
140
141         assert databases.size() == 2 : databases.size();
142         assert databases.contains(database1) && databases.contains(database2);
143
144         this.balancer.remove(database1);
145
146         databases = this.balancer.list();
147         
148         assert databases.size() == 1 : databases.size();
149         assert databases.contains(database2);
150         
151         this.balancer.remove(database2);
152         
153         databases = this.balancer.list();
154         
155         assert databases.isEmpty() : databases.size();
156     }
157
158     /**
159      * Test method for {@link Balancer#contains(Database)}
160      */

161     @Test
162     public void testContains()
163     {
164         Database database1 = new MockDatabase("db1", 1);
165         Database database2 = new MockDatabase("db2", 1);
166
167         this.balancer.add(database1);
168         
169         assert this.balancer.contains(database1);
170         assert !this.balancer.contains(database2);
171     }
172
173     /**
174      * Test method for {@link Balancer#first()}
175      */

176     @Test
177     public void testFirst()
178     {
179         try
180         {
181             this.balancer.first();
182             
183             assert false;
184         }
185         catch (NoSuchElementException JavaDoc e)
186         {
187             assert true;
188         }
189         
190         Database database = new MockDatabase("0", 0);
191         
192         this.balancer.add(database);
193         
194         Database first = this.balancer.first();
195
196         assert database.equals(first) : database;
197     }
198
199     /**
200      * Test method for {@link Balancer#next()}
201      */

202     @Test
203     public void testNext()
204     {
205         try
206         {
207             Database database = this.balancer.next();
208             
209             assert false : database.getId();
210         }
211         catch (NoSuchElementException JavaDoc e)
212         {
213             assert true;
214         }
215         
216         testNext(this.balancer);
217     }
218     
219     protected abstract void testNext(Balancer balancer);
220 }
221
Popular Tags