[Asterisk-java-cvs] CVS: asterisk-java/src/test/net/sf/asterisk/fastagi/impl AGIReaderImplTest.java,
Brought to you by:
srt
Update of /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/fastagi/impl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21021/src/test/net/sf/asterisk/fastagi/impl Added Files: AGIReaderImplTest.java AGIChannelImplTest.java RequestBuilderImplTest.java AGIWriterImplTest.java ReplyBuilderImplTest.java Log Message: Refactored private implementation classes into impl subpackage --- NEW FILE: AGIReaderImplTest.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.fastagi.impl; import junit.framework.TestCase; import net.sf.asterisk.fastagi.AGIHangupException; import net.sf.asterisk.fastagi.AGIReader; import net.sf.asterisk.fastagi.AGIRequest; import net.sf.asterisk.fastagi.reply.AGIReply; import net.sf.asterisk.io.SocketConnectionFacade; import org.easymock.MockControl; public class AGIReaderImplTest extends TestCase { private AGIReader agiReader; private MockControl socketMC; private SocketConnectionFacade socket; protected void setUp() throws Exception { super.setUp(); this.socketMC = MockControl.createControl(SocketConnectionFacade.class); this.socket = (SocketConnectionFacade) socketMC.getMock(); this.agiReader = new AGIReaderImpl(socket); } public void testReadRequest() throws Exception { AGIRequest request; socket.readLine(); socketMC.setReturnValue("agi_network: yes"); socket.readLine(); socketMC.setReturnValue("agi_network_script: myscript.agi"); socket.readLine(); socketMC.setReturnValue("agi_request: agi://host/myscript.agi"); socket.readLine(); socketMC.setReturnValue("agi_channel: SIP/1234-d715"); socket.readLine(); socketMC.setReturnValue(""); socketMC.replay(); request = agiReader.readRequest(); assertEquals("incorrect script", "myscript.agi", request.getScript()); assertEquals("incorrect requestURL", "agi://host/myscript.agi", request .getRequestURL()); assertEquals("incorrect channel", "SIP/1234-d715", request.getChannel()); socketMC.verify(); } public void testReadReply() throws Exception { AGIReply reply; socket.readLine(); socketMC.setReturnValue("200 result=49 endpos=2240"); socketMC.replay(); reply = agiReader.readReply(); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 49, reply.getResultCode()); socketMC.verify(); } public void testReadReplyInvalidOrUnknownCommand() throws Exception { AGIReply reply; socket.readLine(); socketMC.setReturnValue("510 Invalid or unknown command"); socketMC.replay(); reply = agiReader.readReply(); assertEquals("Incorrect status", AGIReply.SC_INVALID_OR_UNKNOWN_COMMAND, reply.getStatus()); socketMC.verify(); } public void testReadReplyInvalidCommandSyntax() throws Exception { AGIReply reply; socket.readLine(); socketMC .setReturnValue("520-Invalid command syntax. Proper usage follows:"); socket.readLine(); socketMC.setReturnValue(" Usage: DATABASE DEL <family> <key>"); socket.readLine(); socketMC .setReturnValue(" Deletes an entry in the Asterisk database for a"); socket.readLine(); socketMC.setReturnValue(" given family and key."); socket.readLine(); socketMC.setReturnValue(" Returns 1 if succesful, 0 otherwise"); socket.readLine(); socketMC.setReturnValue("520 End of proper usage."); socketMC.replay(); reply = agiReader.readReply(); assertEquals("Incorrect status", AGIReply.SC_INVALID_COMMAND_SYNTAX, reply.getStatus()); assertEquals("Incorrect synopsis", "DATABASE DEL <family> <key>", reply .getSynopsis()); socketMC.verify(); } public void testReadReplyWhenHungUp() throws Exception { socket.readLine(); socketMC.setReturnValue(null); socketMC.replay(); try { agiReader.readReply(); fail("Must throw AGIHangupException"); } catch (AGIHangupException e) { } socketMC.verify(); } } --- NEW FILE: AGIChannelImplTest.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.fastagi.impl; import junit.framework.TestCase; import net.sf.asterisk.fastagi.AGIChannel; import net.sf.asterisk.fastagi.AGIReader; import net.sf.asterisk.fastagi.AGIWriter; import net.sf.asterisk.fastagi.command.NoopCommand; import net.sf.asterisk.fastagi.reply.AGIReply; import org.easymock.MockControl; public class AGIChannelImplTest extends TestCase { private MockControl agiWriterMC; private AGIWriter agiWriter; private MockControl agiReaderMC; private AGIReader agiReader; private AGIChannel agiChannel; protected void setUp() throws Exception { super.setUp(); this.agiWriterMC = MockControl.createControl(AGIWriter.class); this.agiWriter = (AGIWriter) agiWriterMC.getMock(); this.agiReaderMC = MockControl.createControl(AGIReader.class); this.agiReader = (AGIReader) agiReaderMC.getMock(); this.agiChannel = new AGIChannelImpl(agiWriter, agiReader); } public void testSendCommand() throws Exception { AGIReply reply; NoopCommand command; reply = new AGIReply(); reply.setStatus(AGIReply.SC_SUCCESS); reply.setResult("0"); command = new NoopCommand(); agiWriter.sendCommand(command); agiReader.readReply(); agiReaderMC.setReturnValue(reply); agiWriterMC.replay(); agiReaderMC.replay(); assertEquals(reply, agiChannel.sendCommand(command)); agiWriterMC.verify(); agiReaderMC.verify(); } } --- NEW FILE: RequestBuilderImplTest.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.fastagi.impl; import java.util.ArrayList; import java.util.Collection; import junit.framework.TestCase; import net.sf.asterisk.fastagi.AGIRequest; import net.sf.asterisk.fastagi.RequestBuilder; public class RequestBuilderImplTest extends TestCase { private RequestBuilder requestBuilder; protected void setUp() { this.requestBuilder = new RequestBuilderImpl(); } public void testBuildRequest() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_network: yes"); lines.add("agi_network_script: myscript.agi"); lines.add("agi_request: agi://host/myscript.agi"); lines.add("agi_channel: SIP/1234-d715"); lines.add("agi_language: en"); lines.add("agi_type: SIP"); lines.add("agi_uniqueid: 1110023416.6"); lines.add("agi_callerid: John Doe<1234>"); lines.add("agi_dnid: 8870"); lines.add("agi_rdnis: unknown"); lines.add("agi_context: local"); lines.add("agi_extension: 8870"); lines.add("agi_priority: 1"); lines.add("agi_enhanced: 0.0"); lines.add("agi_accountcode: "); request = requestBuilder.buildRequest(lines); assertEquals("incorrect script", "myscript.agi", request.getScript()); assertEquals("incorrect requestURL", "agi://host/myscript.agi", request.getRequestURL()); assertEquals("incorrect channel", "SIP/1234-d715", request.getChannel()); assertEquals("incorrect uniqueId", "SIP/1234-d715", request.getChannel()); assertEquals("incorrect type", "SIP", request.getType()); assertEquals("incorrect uniqueId", "1110023416.6", request.getUniqueId()); assertEquals("incorrect language", "en", request.getLanguage()); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertEquals("incorrect callerIdName", "John Doe", request.getCallerIdName()); assertEquals("incorrect dnid", "8870", request.getDnid()); assertEquals("incorrect rdnis", "unknown", request.getRdnis()); assertEquals("incorrect context", "local", request.getContext()); assertEquals("incorrect extension", "8870", request.getExtension()); assertEquals("incorrect priority", new Integer(1), request.getPriority()); assertEquals("incorrect enhanced", Boolean.FALSE, request.getEnhanced()); assertNull("incorrect accountCode must not be set", request.getAccountCode()); } public void testBuildRequestWithoutCallerIdName() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: 1234"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertNull("callerIdName must not be set", request.getCallerIdName()); } public void testBuildRequestWithoutCallerIdNameButBracket() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: <1234>"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertNull("callerIdName must not be set", request.getCallerIdName()); } public void testBuildRequestWithoutCallerIdNameButBracketAndQuotesAndSpace() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: \"\" <1234>"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertNull("callerIdName must not be set", request.getCallerIdName()); } public void testBuildRequestWithQuotedCallerIdName() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: \"John Doe\"<1234>"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertEquals("incorrect callerIdName", "John Doe", request.getCallerIdName()); } public void testBuildRequestWithQuotedCallerIdNameAndSpace() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: \"John Doe\" <1234>"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect callerId", "1234", request.getCallerId()); assertEquals("incorrect callerIdName", "John Doe", request.getCallerIdName()); } public void testBuildRequestWithoutCallerId() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("agi_callerid: "); request = requestBuilder.buildRequest(lines); assertNull("callerId must not be set", request.getCallerId()); assertNull("callerIdName must not be set", request.getCallerIdName()); } public void testBuildRequestWithNullEnvironment() { try { requestBuilder.buildRequest(null); fail("No IllegalArgumentException thrown."); } catch (IllegalArgumentException e) { } } public void testBuildRequestWithUnusualInput() { Collection lines; AGIRequest request; lines = new ArrayList(); lines.add("var without agi prefix: a value"); lines.add("agi_without_colon another value"); lines.add("agi_without_space_after_colon:"); lines.add("agi_channel: SIP/1234-a892"); request = requestBuilder.buildRequest(lines); assertEquals("incorrect channel", "SIP/1234-a892", request.getChannel()); } } --- NEW FILE: AGIWriterImplTest.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.fastagi.impl; import junit.framework.TestCase; import net.sf.asterisk.fastagi.AGIWriter; import net.sf.asterisk.fastagi.command.StreamFileCommand; import net.sf.asterisk.io.SocketConnectionFacade; import org.easymock.MockControl; public class AGIWriterImplTest extends TestCase { private AGIWriter agiWriter; private MockControl socketMC; private SocketConnectionFacade socket; protected void setUp() throws Exception { super.setUp(); this.socketMC = MockControl.createControl(SocketConnectionFacade.class); this.socket = (SocketConnectionFacade) socketMC.getMock(); this.agiWriter = new AGIWriterImpl(socket); } public void testSendCommand() throws Exception { StreamFileCommand command; command = new StreamFileCommand("welcome"); socket.write("STREAM FILE \"welcome\" \"\"\n"); socket.flush(); socketMC.replay(); agiWriter.sendCommand(command); socketMC.verify(); } } --- NEW FILE: ReplyBuilderImplTest.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.fastagi.impl; import java.util.ArrayList; import java.util.List; import junit.framework.TestCase; import net.sf.asterisk.fastagi.reply.AGIReply; public class ReplyBuilderImplTest extends TestCase { private ReplyBuilderImpl replyBuilder; private List lines; protected void setUp() { this.replyBuilder = new ReplyBuilderImpl(); this.lines = new ArrayList(); } public void testBuildReply() { AGIReply reply; lines.add("200 result=49"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 49, reply.getResultCode()); assertEquals("Incorrect result as character", '1', reply .getResultCodeAsChar()); assertEquals("Incorrect result when get via getAttribute()", "49", reply.getAttribute("result")); } public void testBuildReplyWithAdditionalAttribute() { AGIReply reply; lines.add("200 result=49 endpos=2240"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 49, reply.getResultCode()); assertEquals("Incorrect result as character", '1', reply .getResultCodeAsChar()); assertEquals("Incorrect result when get via getAttribute()", "49", reply.getAttribute("result")); assertEquals("Incorrect endpos attribute", "2240", reply .getAttribute("endpos")); } public void testBuildReplyWithMultipleAdditionalAttribute() { AGIReply reply; lines.add("200 result=49 startpos=1234 endpos=2240"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 49, reply.getResultCode()); assertEquals("Incorrect result as character", '1', reply .getResultCodeAsChar()); assertEquals("Incorrect result when get via getAttribute()", "49", reply.getAttribute("result")); assertEquals("Incorrect startpos attribute", "1234", reply .getAttribute("startpos")); assertEquals("Incorrect endpos attribute", "2240", reply .getAttribute("endpos")); } public void testBuildReplyWithParenthesis() { AGIReply reply; lines.add("200 result=1 ((hello)(world))"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 1, reply.getResultCode()); assertEquals("Incorrect parenthesis", "(hello)(world)", reply .getExtra()); } public void testBuildReplyWithAdditionalAttributeAndParenthesis() { AGIReply reply; lines.add("200 result=1 ((hello)(world)) endpos=2240"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_SUCCESS, reply.getStatus()); assertEquals("Incorrect result", 1, reply.getResultCode()); assertEquals("Incorrect parenthesis", "(hello)(world)", reply .getExtra()); assertEquals("Incorrect endpos attribute", "2240", reply .getAttribute("endpos")); } public void testBuildReplyInvalidOrUnknownCommand() { AGIReply reply; lines.add("510 Invalid or unknown command"); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_INVALID_OR_UNKNOWN_COMMAND, reply.getStatus()); } public void testBuildReplyInvalidCommandSyntax() { AGIReply reply; lines.add("520-Invalid command syntax. Proper usage follows:"); lines.add(" Usage: DATABASE DEL <family> <key>"); lines.add(" Deletes an entry in the Asterisk database for a"); lines.add(" given family and key."); lines.add(" Returns 1 if succesful, 0 otherwise"); lines.add("520 End of proper usage."); reply = replyBuilder.buildReply(lines); assertEquals("Incorrect status", AGIReply.SC_INVALID_COMMAND_SYNTAX, reply.getStatus()); assertEquals("Incorrect synopsis", "DATABASE DEL <family> <key>", reply .getSynopsis()); assertEquals( "Incorrect usage", "Deletes an entry in the Asterisk database for a given family and key. Returns 1 if succesful, 0 otherwise", reply.getUsage()); } public void testBuildReplyNoLines() { AGIReply reply; reply = replyBuilder.buildReply(lines); assertNull(reply); } } |