[Asterisk-java-cvs] CVS: asterisk-java/src/test/net/sf/asterisk/manager ManagerWriterMock.java,NONE,
Brought to you by:
srt
Update of /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3989/src/test/net/sf/asterisk/manager Modified Files: ManagerReaderImplTest.java Added Files: ManagerWriterMock.java ManagerReaderMock.java DefaultManagerConnectionTest.java Log Message: Added unit tests for DefaultManagerConnection --- NEW FILE: ManagerWriterMock.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 java.io.IOException; import net.sf.asterisk.manager.action.ChallengeAction; import net.sf.asterisk.manager.action.LoginAction; import net.sf.asterisk.manager.action.ManagerAction; import net.sf.asterisk.manager.event.ConnectEvent; import net.sf.asterisk.manager.io.SocketConnectionFacade; import net.sf.asterisk.manager.response.ChallengeResponse; import net.sf.asterisk.manager.response.ManagerResponse; public class ManagerWriterMock implements ManagerWriter { private static final String CHALLENGE = "12345"; private Dispatcher dispatcher; private AsteriskServer asteriskServer; private SocketConnectionFacade socket; private String expectedKey; private String expectedUsername; public int challengeActionsSent = 0; public int loginActionsSent = 0; public int otherActionsSent = 0; public ManagerWriterMock() { } public void setDispatcher(Dispatcher dispatcher) { this.dispatcher = dispatcher; } public void setAsteriskServer(AsteriskServer asteriskServer) { this.asteriskServer = asteriskServer; } public void setExpectedKey(String key) { this.expectedKey = key; } public void setExpectedUsername(String username) { this.expectedUsername = username; } public void setSocket(SocketConnectionFacade socket) { this.socket = socket; ConnectEvent connectEvent; connectEvent = new ConnectEvent(asteriskServer); connectEvent.setProtocolIdentifier("Asterisk Call Manager/1.0"); dispatcher.dispatchEvent(connectEvent); } public void sendAction(ManagerAction action) throws IOException { if (action instanceof ChallengeAction) { ChallengeResponse challengeResponse; ChallengeAction challengeAction = (ChallengeAction) action; String authType = challengeAction.getAuthType(); if (!authType.equals("MD5")) { throw new RuntimeException("Expected authType 'MD5' got '" + authType + "'"); } challengeResponse = new ChallengeResponse(); challengeResponse.setActionId(action.getActionId()); challengeResponse.setChallenge(CHALLENGE); dispatcher.dispatchResponse(challengeResponse); challengeActionsSent++; } else if (action instanceof LoginAction) { ManagerResponse loginResponse; LoginAction loginAction = (LoginAction) action; String username = loginAction.getUsername(); String key = loginAction.getKey(); String authType = loginAction.getAuthType(); if (!authType.equals("MD5")) { throw new RuntimeException("Expected authType 'MD5' got '" + authType + "'"); } if (!username.equals(expectedUsername)) { throw new RuntimeException("Expected username '" + expectedUsername + "' got '" + username + "'"); } if (!key.equals(expectedKey)) { throw new RuntimeException("Expected key '" + expectedKey + "' got '" + key + "'"); } loginResponse = new ManagerResponse(); loginResponse.setActionId(action.getActionId()); loginResponse.setResponse("Success"); dispatcher.dispatchResponse(loginResponse); loginActionsSent++; } else { ManagerResponse response; response = new ManagerResponse(); response.setActionId(action.getActionId()); response.setResponse("Success"); dispatcher.dispatchResponse(response); otherActionsSent++; } } } --- NEW FILE: ManagerReaderMock.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 net.sf.asterisk.manager.io.SocketConnectionFacade; public class ManagerReaderMock implements ManagerReader { public int setSocketCalls = 0; public int dieCalls = 0; public int runCalls = 0; public ManagerReaderMock() { } public void setSocket(SocketConnectionFacade socket) { setSocketCalls++; } public void die() { dieCalls++; } public void run() { runCalls++; } } --- NEW FILE: DefaultManagerConnectionTest.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; import net.sf.asterisk.manager.action.StatusAction; import net.sf.asterisk.manager.event.ConnectEvent; import net.sf.asterisk.manager.io.SocketConnectionFacade; import net.sf.asterisk.manager.response.ManagerResponse; import org.easymock.MockControl; public class DefaultManagerConnectionTest extends TestCase { protected MockControl socketMC; protected SocketConnectionFacade mockSocket; protected ManagerWriterMock mockWriter; protected ManagerReaderMock mockReader; protected MockedDefaultManagerConnection dmc; protected AsteriskServer asteriskServer; protected void setUp() throws Exception { asteriskServer = new AsteriskServer("localhost", 5038); mockWriter = new ManagerWriterMock(); mockReader = new ManagerReaderMock(); socketMC = MockControl.createControl(SocketConnectionFacade.class); mockSocket = (SocketConnectionFacade) socketMC.getMock(); dmc = new MockedDefaultManagerConnection(mockReader, mockWriter, mockSocket); mockWriter.setDispatcher(dmc); mockWriter.setAsteriskServer(asteriskServer); } public void testConstructor() { assertEquals("Invalid default hostname", "localhost", dmc.getAsteriskServer().getHostname()); assertEquals("Invalid default port", 5038, dmc.getAsteriskServer().getPort()); } public void testLogin() throws Exception { MockControl managerEventHandlerMC; ManagerEventHandler managerEventHandler; ConnectEvent connectEvent; managerEventHandlerMC = MockControl.createControl(ManagerEventHandler.class); managerEventHandler = (ManagerEventHandler) managerEventHandlerMC.getMock(); connectEvent = new ConnectEvent(asteriskServer); connectEvent.setProtocolIdentifier("Asterisk Call Manager/1.0"); managerEventHandler.handleEvent(connectEvent); managerEventHandlerMC.replay(); socketMC.replay(); mockWriter.setExpectedUsername("username"); // md5 sum of 12345password mockWriter.setExpectedKey("40b1b887502902a8ce61a16e44630f7c"); dmc.setUsername("username"); dmc.setPassword("password"); dmc.addEventHandler(managerEventHandler); dmc.login(); 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("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); assertEquals("run() not called 1 time", 1, mockReader.runCalls); assertEquals("unexpected call to die()", 0, mockReader.dieCalls); socketMC.verify(); managerEventHandlerMC.verify(); } public void testSendActionWhenNotConnected() throws Exception { StatusAction statusAction; statusAction = new StatusAction(); try { dmc.sendAction(statusAction); fail("No IllegalStateException thrown"); } catch (IllegalStateException e) { } } public void testSendAction() throws Exception { StatusAction statusAction; ManagerResponse response; statusAction = new StatusAction(); statusAction.setActionId("123"); // fake connect dmc.connect(); response = dmc.sendAction(statusAction); assertEquals("incorrect actionId in response", "123", response.getActionId()); assertEquals("incorrect response", "Success", response.getResponse()); assertEquals("other actions not sent 1 time", 1, mockWriter.otherActionsSent); } private class MockedDefaultManagerConnection extends DefaultManagerConnection { ManagerReader mockReader; ManagerWriter mockWriter; SocketConnectionFacade mockSocket; public int createReaderCalls = 0; public int createWriterCalls = 0; public int createSocketCalls = 0; public MockedDefaultManagerConnection(ManagerReader mockReader, ManagerWriter mockWriter, SocketConnectionFacade mockSocket) { super(); this.mockReader = mockReader; this.mockWriter = mockWriter; this.mockSocket = mockSocket; } protected ManagerReader createReader(Dispatcher d, AsteriskServer s) { createReaderCalls++; return mockReader; } protected ManagerWriter createWriter() { createWriterCalls++; return mockWriter; } protected SocketConnectionFacade createSocket() { createSocketCalls++; return mockSocket; } } } Index: ManagerReaderImplTest.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/manager/ManagerReaderImplTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -p -r1.1 -r1.2 --- ManagerReaderImplTest.java 2 Mar 2005 23:15:50 -0000 1.1 +++ ManagerReaderImplTest.java 4 Mar 2005 22:21:04 -0000 1.2 @@ -42,7 +42,7 @@ public class ManagerReaderImplTest exten private AsteriskServer asteriskServer; private ManagerReader managerReader; - public void setUp() + protected void setUp() { now = new Date(); DateUtil.overrideCurrentDate(now); @@ -54,6 +54,11 @@ public class ManagerReaderImplTest exten socketConnectionFacadeMC = MockControl.createControl(SocketConnectionFacade.class); socketConnectionFacade = (SocketConnectionFacade) socketConnectionFacadeMC.getMock(); } + + protected void tearDown() + { + DateUtil.overrideCurrentDate(null); + } public void testRunWithoutSocket() throws Exception { |