01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.client;
17:
18: import java.io.IOException;
19: import java.net.URI;
20: import java.rmi.RemoteException;
21:
22: import com.agical.rmock.extension.junit.RMockTestCase;
23:
24: public class StickToLastServerConnectionFactoryStrategyTest extends
25: RMockTestCase {
26:
27: private StickToLastServerConnectionFactoryStrategy factoryStrategy;
28: private ConnectionFactory connectionFactory;
29: private URI[] locations;
30:
31: @Override
32: protected void setUp() throws Exception {
33: connectionFactory = (ConnectionFactory) mock(ConnectionFactory.class);
34: ConnectionManager.setFactory(connectionFactory);
35:
36: factoryStrategy = new StickToLastServerConnectionFactoryStrategy();
37:
38: URI uri1 = new URI("ejbd://localhost:4201");
39: URI uri2 = new URI("ejbd://localhost:4202");
40: locations = new URI[] { uri1, uri2 };
41: }
42:
43: public void testThrowsRemoteExceptionIfCannotConnectToAllURIs()
44: throws Exception {
45: connectionFactory.getConnection(locations[0]);
46: modify().throwException(new IOException());
47:
48: connectionFactory.getConnection(locations[1]);
49: modify().throwException(new IOException());
50:
51: startVerification();
52:
53: try {
54: factoryStrategy.connect(locations, null);
55: fail();
56: } catch (RemoteException e) {
57: }
58: }
59:
60: public void testReturnFirstSuccessfulConnection() throws Exception {
61: connectionFactory.getConnection(locations[0]);
62: modify().throwException(new IOException());
63:
64: Connection expectedConnection = connectionFactory
65: .getConnection(locations[1]);
66:
67: startVerification();
68:
69: Connection actualConnection = factoryStrategy.connect(
70: locations, null);
71: assertSame(expectedConnection, actualConnection);
72: }
73:
74: public void testReConnectToLastServerFirst() throws Exception {
75: connectionFactory.getConnection(locations[0]);
76: modify().throwException(new IOException());
77:
78: connectionFactory.getConnection(locations[1]);
79: connectionFactory.getConnection(locations[1]);
80:
81: startVerification();
82:
83: factoryStrategy.connect(locations, null);
84: factoryStrategy.connect(locations, null);
85: }
86:
87: }
|