[Asterisk-java-cvs] CVS: asterisk-java/src/test/net/sf/asterisk/manager ChannelStateEnumTest.java,NO
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-13 11:28:00
|
Update of /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20829/src/test/net/sf/asterisk/manager Modified Files: DefaultManagerConnectionTest.java Added Files: ChannelStateEnumTest.java Log Message: Removed dependcy on commons-lang --- NEW FILE: ChannelStateEnumTest.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import junit.framework.TestCase; public class ChannelStateEnumTest extends TestCase { public void testGetEnum() { assertEquals(ChannelStateEnum.BUSY, ChannelStateEnum.getEnum("Busy")); } public void testToString() { assertEquals("Busy", ChannelStateEnum.BUSY.toString()); } } Index: DefaultManagerConnectionTest.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/manager/DefaultManagerConnectionTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -p -r1.7 -r1.8 --- DefaultManagerConnectionTest.java 9 Mar 2005 15:50:28 -0000 1.7 +++ DefaultManagerConnectionTest.java 13 Mar 2005 11:26:49 -0000 1.8 @@ -17,12 +17,15 @@ package net.sf.asterisk.manager; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import junit.framework.TestCase; import net.sf.asterisk.io.SocketConnectionFacade; import net.sf.asterisk.manager.action.StatusAction; import net.sf.asterisk.manager.event.ConnectEvent; import net.sf.asterisk.manager.event.DisconnectEvent; +import net.sf.asterisk.manager.event.ManagerEvent; import net.sf.asterisk.manager.response.ManagerResponse; import org.easymock.MockControl; @@ -50,7 +53,8 @@ public class DefaultManagerConnectionTes socketMC = MockControl.createControl(SocketConnectionFacade.class); mockSocket = (SocketConnectionFacade) socketMC.getMock(); - dmc = new MockedDefaultManagerConnection(mockReader, mockWriter, mockSocket); + dmc = new MockedDefaultManagerConnection(mockReader, mockWriter, + mockSocket); // make tests faster that expect a TimeoutException dmc.setSleepTime(5); @@ -62,15 +66,18 @@ public class DefaultManagerConnectionTes public void testDefaultConstructor() { - assertEquals("Invalid default hostname", "localhost", dmc.getAsteriskServer().getHostname()); - assertEquals("Invalid default port", 5038, dmc.getAsteriskServer().getPort()); + assertEquals("Invalid default hostname", "localhost", dmc + .getAsteriskServer().getHostname()); + assertEquals("Invalid default port", 5038, dmc.getAsteriskServer() + .getPort()); } public void testFullConstructor() { dmc = new MockedDefaultManagerConnection("host", 1234, "u", "p"); - assertEquals("Invalid hostname", "host", dmc.getAsteriskServer().getHostname()); + assertEquals("Invalid hostname", "host", dmc.getAsteriskServer() + .getHostname()); assertEquals("Invalid port", 1234, dmc.getAsteriskServer().getPort()); assertEquals("Invalid username", "u", dmc.getUsername()); assertEquals("Invalid password", "p", dmc.getPassword()); @@ -87,11 +94,14 @@ public class DefaultManagerConnectionTes managerReader.registerEventClass(MyUserEvent.class); managerReaderMC.replay(); - dmc = new MockedDefaultManagerConnection(managerReader, mockWriter, mockSocket); + dmc = new MockedDefaultManagerConnection(managerReader, mockWriter, + mockSocket); dmc.registerUserEventClass(MyUserEvent.class); - assertEquals("unexpected call to createSocket", 0, dmc.createSocketCalls); - assertEquals("unexpected call to createWriter", 0, dmc.createWriterCalls); + assertEquals("unexpected call to createSocket", 0, + dmc.createSocketCalls); + assertEquals("unexpected call to createWriter", 0, + dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); managerReaderMC.verify(); @@ -99,17 +109,13 @@ public class DefaultManagerConnectionTes public void testLogin() throws Exception { - MockControl managerEventHandlerMC; - ManagerEventHandler managerEventHandler; + MockedManagerEventHandler managerEventHandler; ConnectEvent connectEvent; - managerEventHandlerMC = MockControl.createControl(ManagerEventHandler.class); - managerEventHandler = (ManagerEventHandler) managerEventHandlerMC.getMock(); + managerEventHandler = new MockedManagerEventHandler(); connectEvent = new ConnectEvent(asteriskServer); connectEvent.setProtocolIdentifier("Asterisk Call Manager/1.0"); - managerEventHandler.handleEvent(connectEvent); - managerEventHandlerMC.replay(); socketMC.replay(); @@ -122,12 +128,17 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("login action not sent 1 time", 1, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 1 time", 1, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); - assertEquals("setSocket() not called 1 time", 1, mockReader.setSocketCalls); - // Some time for the reader thread to be started. Otherwise run() might not yet have been + assertEquals("setSocket() not called 1 time", 1, + mockReader.setSocketCalls); + // Some time for the reader thread to be started. Otherwise run() might + // not yet have been // called. Thread.sleep(10); assertEquals("run() not called 1 time", 1, mockReader.runCalls); @@ -135,8 +146,13 @@ public class DefaultManagerConnectionTes assertTrue("keepAlive not set", dmc.getKeepAlive()); + assertEquals("must have handled exactly one event", 1, + managerEventHandler.eventsHandled.size()); + + assertTrue("eventHandled must be a ConnectEvent", + managerEventHandler.eventsHandled.get(0) instanceof ConnectEvent); + socketMC.verify(); - managerEventHandlerMC.verify(); } public void testLoginIncorrectKey() throws Exception @@ -166,12 +182,17 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("login action not sent 1 time", 1, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 1 time", 1, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); - assertEquals("setSocket() not called 1 time", 1, mockReader.setSocketCalls); - // Some time for the reader thread to be started. Otherwise run() might not yet have been + assertEquals("setSocket() not called 1 time", 1, + mockReader.setSocketCalls); + // Some time for the reader thread to be started. Otherwise run() might + // not yet have been // called. Thread.sleep(10); assertEquals("run() not called 1 time", 1, mockReader.runCalls); @@ -198,11 +219,15 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("unexpected challenge action sent", 0, mockWriter.challengeActionsSent); - assertEquals("unexpected login action sent", 0, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("unexpected challenge action sent", 0, + mockWriter.challengeActionsSent); + assertEquals("unexpected login action sent", 0, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); - assertEquals("unexpected call to setSocket()", 0, mockReader.setSocketCalls); + assertEquals("unexpected call to setSocket()", 0, + mockReader.setSocketCalls); assertEquals("unexpected call to run()", 0, mockReader.runCalls); assertEquals("unexpected call to die()", 0, mockReader.dieCalls); @@ -224,19 +249,25 @@ public class DefaultManagerConnectionTes } catch (TimeoutException e) { - assertEquals("Timeout waiting for protocol identifier", e.getMessage()); + assertEquals("Timeout waiting for protocol identifier", e + .getMessage()); } assertEquals("createSocket not called 1 time", 1, dmc.createSocketCalls); assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("unexpected challenge action sent", 0, mockWriter.challengeActionsSent); - assertEquals("unexpected login action sent", 0, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("unexpected challenge action sent", 0, + mockWriter.challengeActionsSent); + assertEquals("unexpected login action sent", 0, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); - assertEquals("setSocket() not called 1 time", 1, mockReader.setSocketCalls); - // Some time for the reader thread to be started. Otherwise run() might not yet have been + assertEquals("setSocket() not called 1 time", 1, + mockReader.setSocketCalls); + // Some time for the reader thread to be started. Otherwise run() might + // not yet have been // called. Thread.sleep(10); assertEquals("run() not called 1 time", 1, mockReader.runCalls); @@ -259,19 +290,25 @@ public class DefaultManagerConnectionTes } catch (TimeoutException e) { - assertEquals("Timeout waiting for response to Challenge", e.getMessage()); + assertEquals("Timeout waiting for response to Challenge", e + .getMessage()); } assertEquals("createSocket not called 1 time", 1, dmc.createSocketCalls); assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("unexpected login action sent", 0, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("unexpected login action sent", 0, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); - assertEquals("setSocket() not called 1 time", 1, mockReader.setSocketCalls); - // Some time for the reader thread to be started. Otherwise run() might not yet have been + assertEquals("setSocket() not called 1 time", 1, + mockReader.setSocketCalls); + // Some time for the reader thread to be started. Otherwise run() might + // not yet have been // called. Thread.sleep(10); assertEquals("run() not called 1 time", 1, mockReader.runCalls); @@ -290,7 +327,8 @@ public class DefaultManagerConnectionTes dmc.logoff(); - assertEquals("logoff action not sent 1 time", 1, mockWriter.logoffActionsSent); + assertEquals("logoff action not sent 1 time", 1, + mockWriter.logoffActionsSent); socketMC.verify(); } @@ -300,7 +338,8 @@ public class DefaultManagerConnectionTes dmc.logoff(); - assertEquals("unexpected logoff action sent", 0, mockWriter.logoffActionsSent); + assertEquals("unexpected logoff action sent", 0, + mockWriter.logoffActionsSent); socketMC.verify(); } @@ -346,10 +385,12 @@ public class DefaultManagerConnectionTes dmc.connect(); response = dmc.sendAction(statusAction); - assertEquals("incorrect actionId in response", "123", response.getActionId()); + assertEquals("incorrect actionId in response", "123", response + .getActionId()); assertEquals("incorrect response", "Success", response.getResponse()); - assertEquals("other actions not sent 1 time", 1, mockWriter.otherActionsSent); + assertEquals("other actions not sent 1 time", 1, + mockWriter.otherActionsSent); } public void testSendActionTimeout() throws Exception @@ -372,7 +413,8 @@ public class DefaultManagerConnectionTes { } - assertEquals("other actions not sent 1 time", 1, mockWriter.otherActionsSent); + assertEquals("other actions not sent 1 time", 1, + mockWriter.otherActionsSent); } public void testDispatchResponseUnexpectedResponse() @@ -437,9 +479,12 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("login action not sent 1 time", 1, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 1 time", 1, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); assertTrue("keepAlive not enabled", dmc.getKeepAlive()); @@ -467,9 +512,12 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("login action not sent 1 time", 1, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 1 time", 1, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); assertTrue("keepAlive not enabled", dmc.getKeepAlive()); @@ -495,16 +543,20 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 1 time", 1, mockWriter.challengeActionsSent); - assertEquals("login action not sent 1 time", 1, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 1 time", 1, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 1 time", 1, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); assertFalse("keepAlive not disabled", dmc.getKeepAlive()); socketMC.verify(); } - public void testReconnectWithKeepAliveAfterAuthenticationFailure() throws Exception + public void testReconnectWithKeepAliveAfterAuthenticationFailure() + throws Exception { DisconnectEvent disconnectEvent; @@ -517,7 +569,8 @@ public class DefaultManagerConnectionTes // fake successful login dmc.setKeepAlive(true); - // to prevent an infinite loop we will be able to log in after two unsuccessful attempts + // to prevent an infinite loop we will be able to log in after two + // unsuccessful attempts // even if the password is not correct. dmc.setKeepAliveAfterAuthenticationFailure(true); dmc.setUsername("username"); @@ -528,16 +581,36 @@ public class DefaultManagerConnectionTes assertEquals("createWriter not called 1 time", 1, dmc.createWriterCalls); assertEquals("createReader not called 1 time", 1, dmc.createReaderCalls); - assertEquals("challenge action not sent 3 time", 3, mockWriter.challengeActionsSent); - assertEquals("login action not sent 3 time", 3, mockWriter.loginActionsSent); - assertEquals("unexpected other actions sent", 0, mockWriter.otherActionsSent); + assertEquals("challenge action not sent 3 time", 3, + mockWriter.challengeActionsSent); + assertEquals("login action not sent 3 time", 3, + mockWriter.loginActionsSent); + assertEquals("unexpected other actions sent", 0, + mockWriter.otherActionsSent); assertTrue("keepAlive not enabled", dmc.getKeepAlive()); socketMC.verify(); } - private class MockedDefaultManagerConnection extends DefaultManagerConnection + private class MockedManagerEventHandler implements ManagerEventHandler + { + List eventsHandled; + + public MockedManagerEventHandler() + { + this.eventsHandled = new ArrayList(); + } + + public void handleEvent(ManagerEvent event) + { + eventsHandled.add(event); + } + } + + private class MockedDefaultManagerConnection + extends + DefaultManagerConnection { ManagerReader mockReader; ManagerWriter mockWriter; @@ -549,8 +622,8 @@ public class DefaultManagerConnectionTes public int createWriterCalls = 0; public int createSocketCalls = 0; - public MockedDefaultManagerConnection(ManagerReader mockReader, ManagerWriter mockWriter, - SocketConnectionFacade mockSocket) + public MockedDefaultManagerConnection(ManagerReader mockReader, + ManagerWriter mockWriter, SocketConnectionFacade mockSocket) { super(); this.mockReader = mockReader; @@ -558,12 +631,14 @@ public class DefaultManagerConnectionTes this.mockSocket = mockSocket; } - public MockedDefaultManagerConnection(String hostname, int port, String username, String password) + public MockedDefaultManagerConnection(String hostname, int port, + String username, String password) { super(hostname, port, username, password); } - public void setThrowIOExceptionOnFirstSocketCreate(boolean throwIOExceptionOnSocketCreate) + public void setThrowIOExceptionOnFirstSocketCreate( + boolean throwIOExceptionOnSocketCreate) { this.throwIOExceptionOnFirstSocketCreate = throwIOExceptionOnSocketCreate; } |