001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.balancer;
022:
023: import java.util.NoSuchElementException;
024: import java.util.Set;
025:
026: import net.sf.hajdbc.Balancer;
027: import net.sf.hajdbc.Database;
028: import net.sf.hajdbc.MockDatabase;
029:
030: import org.testng.annotations.AfterMethod;
031: import org.testng.annotations.DataProvider;
032: import org.testng.annotations.Test;
033:
034: /**
035: * @author Paul Ferraro
036: */
037: @SuppressWarnings("nls")
038: public abstract class AbstractTestBalancer implements Balancer<Void> {
039: private Balancer<Void> balancer = createBalancer();
040:
041: protected abstract Balancer<Void> createBalancer();
042:
043: @AfterMethod
044: void tearDown() {
045: this .balancer.clear();
046: }
047:
048: @DataProvider(name="database")
049: Object[][] databaseProvider() {
050: return new Object[][] { new Object[] { new MockDatabase("1", 1) } };
051: }
052:
053: @Override
054: @Test(dataProvider="database")
055: public boolean add(Database<Void> database) {
056: boolean added = this .balancer.add(database);
057:
058: assert added;
059:
060: added = this .balancer.add(database);
061:
062: assert !added;
063:
064: return added;
065: }
066:
067: @Override
068: @Test(dataProvider="database")
069: public void afterInvocation(Database<Void> database) {
070: this .balancer.add(database);
071:
072: this .balancer.beforeInvocation(database);
073: }
074:
075: @Override
076: @Test(dataProvider="database")
077: public void beforeInvocation(Database<Void> database) {
078: this .balancer.add(database);
079:
080: this .balancer.beforeInvocation(database);
081: }
082:
083: @Override
084: @Test
085: public Set<Database<Void>> all() {
086: Set<Database<Void>> databaseList = this .balancer.all();
087:
088: assert databaseList.isEmpty() : databaseList.size();
089:
090: Database<Void> database1 = new MockDatabase("db1", 1);
091: this .balancer.add(database1);
092:
093: databaseList = this .balancer.all();
094:
095: assert databaseList.size() == 1 : databaseList.size();
096: assert databaseList.contains(database1);
097:
098: Database<Void> database2 = new MockDatabase("db2", 1);
099: this .balancer.add(database2);
100:
101: databaseList = this .balancer.all();
102:
103: assert databaseList.size() == 2 : databaseList.size();
104: assert databaseList.contains(database1)
105: && databaseList.contains(database2);
106:
107: this .balancer.remove(database1);
108:
109: databaseList = this .balancer.all();
110:
111: assert databaseList.size() == 1 : databaseList.size();
112: assert databaseList.contains(database2);
113:
114: this .balancer.remove(database2);
115:
116: databaseList = this .balancer.all();
117:
118: assert databaseList.isEmpty() : databaseList.size();
119:
120: return databaseList;
121: }
122:
123: @Override
124: @Test
125: public Database<Void> next() {
126: try {
127: Database<Void> database = this .balancer.next();
128:
129: assert false : database.getId();
130: } catch (NoSuchElementException e) {
131: assert true;
132: }
133:
134: this .next(this .balancer);
135:
136: return null;
137: }
138:
139: protected abstract void next(Balancer<Void> balancer);
140:
141: @Override
142: @Test(dataProvider="database")
143: public boolean remove(Database<Void> database) {
144: boolean removed = this .balancer.remove(database);
145:
146: assert !removed;
147:
148: this .balancer.add(database);
149:
150: Database<Void> database2 = new MockDatabase("2", 1);
151:
152: this .balancer.add(database2);
153:
154: removed = this .balancer.remove(database2);
155:
156: assert removed;
157:
158: removed = this .balancer.remove(database2);
159:
160: assert !removed;
161:
162: removed = this .balancer.remove(database);
163:
164: assert removed;
165:
166: removed = this .balancer.remove(database);
167:
168: assert !removed;
169:
170: return removed;
171: }
172:
173: @Override
174: @Test
175: public void clear() {
176: int size = this .balancer.all().size();
177:
178: assert size == 0 : size;
179:
180: this .balancer.clear();
181:
182: assert size == 0 : size;
183:
184: this .balancer.add(new MockDatabase("1"));
185: this .balancer.add(new MockDatabase("2"));
186:
187: size = this .balancer.all().size();
188:
189: assert size == 2 : size;
190:
191: this .balancer.clear();
192:
193: size = this .balancer.all().size();
194:
195: assert size == 0 : size;
196: }
197: }
|