01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
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.Arrays;
24:
25: import org.testng.annotations.Test;
26:
27: import net.sf.hajdbc.Balancer;
28: import net.sf.hajdbc.MockDatabase;
29:
30: /**
31: * @author Paul Ferraro
32: */
33: @Test
34: @SuppressWarnings("nls")
35: public class TestSimpleBalancer extends AbstractTestBalancer {
36: @Override
37: protected Balancer<Void> createBalancer() {
38: return new SimpleBalancer<Void>();
39: }
40:
41: @Override
42: protected void next(Balancer<Void> balancer) {
43: balancer.add(new MockDatabase("0", 0));
44:
45: String[] results = new String[20];
46:
47: for (int i = 0; i < results.length; ++i) {
48: results[i] = balancer.next().getId();
49: }
50:
51: String[] expected = new String[results.length];
52:
53: Arrays.fill(expected, "0");
54:
55: assert Arrays.equals(results, expected) : Arrays
56: .asList(results);
57:
58: balancer.add(new MockDatabase("2", 2));
59:
60: for (int i = 0; i < results.length; ++i) {
61: results[i] = balancer.next().getId();
62: }
63:
64: Arrays.fill(expected, "2");
65:
66: assert Arrays.equals(results, expected) : Arrays
67: .asList(results);
68:
69: balancer.add(new MockDatabase("1", 1));
70:
71: for (int i = 0; i < results.length; ++i) {
72: results[i] = balancer.next().getId();
73: }
74:
75: assert Arrays.equals(results, expected) : Arrays
76: .asList(results);
77: }
78: }
|