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 net.sf.hajdbc.Balancer;
24:
25: import org.easymock.EasyMock;
26: import org.testng.annotations.Test;
27:
28: /**
29: * @author Paul Ferraro
30: *
31: */
32: @Test
33: @SuppressWarnings({"unchecked","nls"})
34: public class TestBalancerClass {
35: /**
36: * Test for {@link BalancerClass#serialize(Balancer)}.
37: */
38: public void serialize() {
39: this .assertBalancer(new SimpleBalancer(), "simple");
40: this .assertBalancer(new RandomBalancer(), "random");
41: this .assertBalancer(new RoundRobinBalancer(), "round-robin");
42: this .assertBalancer(new LoadBalancer(), "load");
43:
44: try {
45: String balancer = BalancerClass.serialize(EasyMock
46: .createMock(Balancer.class));
47:
48: assert false : balancer;
49: } catch (IllegalArgumentException e) {
50: assert true;
51: }
52: }
53:
54: private void assertBalancer(Balancer balancer, String id) {
55: String name = BalancerClass.serialize(balancer);
56:
57: assert name.equals(id) : name;
58: }
59:
60: /**
61: * Test for {@link BalancerClass#deserialize(String)}.
62: */
63: public void deserialize() {
64: this .assertBalancer("simple", SimpleBalancer.class);
65: this .assertBalancer("random", RandomBalancer.class);
66: this .assertBalancer("round-robin", RoundRobinBalancer.class);
67: this .assertBalancer("load", LoadBalancer.class);
68:
69: try {
70: Balancer balancer = BalancerClass.deserialize("invalid");
71:
72: assert false : balancer.getClass().getName();
73: } catch (Exception e) {
74: assert true;
75: }
76: }
77:
78: private void assertBalancer(String id,
79: Class<? extends Balancer> balancerClass) {
80: try {
81: Balancer balancer = BalancerClass.deserialize("load");
82:
83: assert LoadBalancer.class.isInstance(balancer);
84: } catch (Exception e) {
85: assert false : e;
86: }
87: }
88: }
|