[Asterisk-java-cvs] CVS: asterisk-java/src/test/net/sf/asterisk/manager ResponseBuilderImplTest.java
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-05 13:34:35
|
Update of /cvsroot/asterisk-java/asterisk-java/src/test/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20712/src/test/net/sf/asterisk/manager Added Files: ResponseBuilderImplTest.java ActionBuilderImplTest.java EventBuilderImplTest.java Removed Files: ResponseBuilderTest.java EventBuilderTest.java ActionBuilderTest.java Log Message: Refactored (Action|Event|Response)Builder into interfaces --- NEW FILE: ResponseBuilderImplTest.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.util.HashMap; import java.util.Map; import net.sf.asterisk.manager.response.ChallengeResponse; import net.sf.asterisk.manager.response.ExtensionStateResponse; import net.sf.asterisk.manager.response.MailboxCountResponse; import net.sf.asterisk.manager.response.MailboxStatusResponse; import net.sf.asterisk.manager.response.ManagerError; import net.sf.asterisk.manager.response.ManagerResponse; import junit.framework.TestCase; public class ResponseBuilderImplTest extends TestCase { private ResponseBuilder responseBuilder; private Map attributes; public void setUp() { this.responseBuilder = new ResponseBuilderImpl(); this.attributes = new HashMap(); } public void testBuildResponse() { ManagerResponse response; attributes.put("response", "Success"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", ManagerResponse.class, response.getClass()); assertEquals("Response not set correctly", "Success", response.getResponse()); } public void testBuildError() { ManagerResponse response; attributes.put("response", "Error"); attributes.put("message", "Missing action in request"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", ManagerError.class, response.getClass()); assertEquals("Message not set correctly", "Missing action in request", response.getMessage()); } public void testBuildErrorWithActionId() { ManagerResponse response; attributes.put("response", "Error"); attributes.put("actionid", "1234"); attributes.put("message", "Missing action in request"); response = responseBuilder.buildResponse(attributes); assertEquals("ActionId not set correctly", "1234", response.getActionId()); } public void testBuildChallengeResponse() { ManagerResponse response; attributes.put("response", "Success"); attributes.put("challenge", "131494410"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", ChallengeResponse.class, response.getClass()); assertEquals("Challenge not set correctly", "131494410", ((ChallengeResponse) response).getChallenge()); } public void testBuildMailboxStatusResponse() { ManagerResponse response; attributes.put("response", "Success"); attributes.put("message", "Mailbox Status"); attributes.put("mailbox", "123"); attributes.put("waiting", "1"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", MailboxStatusResponse.class, response.getClass()); MailboxStatusResponse mailboxStatusResponse = (MailboxStatusResponse) response; assertEquals("Mailbox not set correctly", "123", mailboxStatusResponse.getMailbox()); assertEquals("Waiting not set correctly", Boolean.TRUE, mailboxStatusResponse.getWaiting()); } public void testBuildMailboxStatusResponseWithNoWaiting() { ManagerResponse response; attributes.put("response", "Success"); attributes.put("message", "Mailbox Status"); attributes.put("mailbox", "123,user2"); attributes.put("waiting", "0"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", MailboxStatusResponse.class, response.getClass()); MailboxStatusResponse mailboxStatusResponse = (MailboxStatusResponse) response; assertEquals("Mailbox not set correctly", "123,user2", mailboxStatusResponse.getMailbox()); assertEquals("Waiting not set correctly", Boolean.FALSE, mailboxStatusResponse.getWaiting()); } public void testBuildMailboxCountResponse() { ManagerResponse response; attributes.put("response", "Success"); attributes.put("message", "Mailbox Message Count"); attributes.put("mailbox", "123@myctx"); attributes.put("newmessages", "2"); attributes.put("oldmessages", "5"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", MailboxCountResponse.class, response.getClass()); MailboxCountResponse mailboxCountResponse = (MailboxCountResponse) response; assertEquals("Mailbox not set correctly", "123@myctx", mailboxCountResponse.getMailbox()); assertEquals("New messages not set correctly", new Integer(2), mailboxCountResponse.getNewMessages()); assertEquals("Old messages set correctly", new Integer(5), mailboxCountResponse.getOldMessages()); } public void testBuildExtensionStateResponse() { ManagerResponse response; attributes.put("response", "Success"); attributes.put("message", "Extension Status"); attributes.put("exten", "1"); attributes.put("context", "default"); attributes.put("hint", ""); attributes.put("status", "-1"); response = responseBuilder.buildResponse(attributes); assertEquals("Response of wrong type", ExtensionStateResponse.class, response.getClass()); ExtensionStateResponse extensionStateResponse = (ExtensionStateResponse) response; assertEquals("Exten not set correctly", "1", extensionStateResponse.getExten()); assertEquals("Context not set correctly", "default", extensionStateResponse.getContext()); assertEquals("Hint not set correctly", "", extensionStateResponse.getHint()); assertEquals("Status not set correctly", new Integer(-1), extensionStateResponse.getStatus()); } } --- NEW FILE: ActionBuilderImplTest.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.ManagerAction; public class ActionBuilderImplTest extends TestCase { private ActionBuilder actionBuilder; public void setUp() { this.actionBuilder = new ActionBuilderImpl(); } public void testBuildAction() { MyAction myAction; String actual; myAction = new MyAction(); myAction.setFirstProperty("first value"); myAction.setSecondProperty(new Integer(2)); myAction.setNonPublicProperty("private"); actual = actionBuilder.buildAction(myAction); assertTrue("Action name missing", actual.indexOf("action: My\r\n") >= 0); assertTrue("First property missing", actual.indexOf("firstproperty: first value\r\n") >= 0); assertTrue("Second property missing", actual.indexOf("secondproperty: 2\r\n") >= 0); assertTrue("Missing trailing CRNL CRNL", actual.endsWith("\r\n\r\n")); assertEquals("Incorrect length", 61, actual.length()); } public void testBuildActionWithNullValue() { MyAction myAction; String actual; myAction = new MyAction(); myAction.setFirstProperty("first value"); actual = actionBuilder.buildAction(myAction); assertTrue("Action name missing", actual.indexOf("action: My\r\n") >= 0); assertTrue("First property missing", actual.indexOf("firstproperty: first value\r\n") >= 0); assertTrue("Missing trailing CRNL CRNL", actual.endsWith("\r\n\r\n")); assertEquals("Incorrect length", 42, actual.length()); } class MyAction extends ManagerAction { private static final long serialVersionUID = 3257568425345102641L; private String firstProperty; private Integer secondProperty; private String nonPublicProperty; public String getAction() { return "My"; } public String getFirstProperty() { return firstProperty; } public void setFirstProperty(String firstProperty) { this.firstProperty = firstProperty; } public Integer getSecondProperty() { return secondProperty; } public void setSecondProperty(Integer secondProperty) { this.secondProperty = secondProperty; } protected String getNonPublicProperty() { return nonPublicProperty; } protected void setNonPublicProperty(String privateProperty) { this.nonPublicProperty = privateProperty; } public String get() { return "This method must not be considered a getter"; } public String getIndexedProperty(int i) { return "This method must not be considered a getter relevant for building the action"; } } } --- NEW FILE: EventBuilderImplTest.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.util.HashMap; import java.util.Map; import junit.framework.TestCase; import net.sf.asterisk.manager.event.CdrEvent; import net.sf.asterisk.manager.event.ChannelEvent; import net.sf.asterisk.manager.event.ManagerEvent; import net.sf.asterisk.manager.event.NewChannelEvent; import net.sf.asterisk.manager.event.NewExtenEvent; import net.sf.asterisk.manager.event.ResponseEvent; import net.sf.asterisk.manager.event.ShutdownEvent; import net.sf.asterisk.manager.event.StatusCompleteEvent; /** * @author srt * @version $Id: EventBuilderImplTest.java,v 1.1 2005/03/05 13:34:23 srt Exp $ */ public class EventBuilderImplTest extends TestCase { private EventBuilder eventBuilder; public void setUp() { this.eventBuilder = new EventBuilderImpl(); } public void testRegisterEvent() { eventBuilder.registerEventClass(NewChannelEvent.class); } public void testRegisterEventWithAbstractEvent() { try { eventBuilder.registerEventClass(ChannelEvent.class); fail("registerEvent() must not accept abstract classes"); } catch (IllegalArgumentException ex) { } } public void testRegisterEventWithWrongClass() { try { eventBuilder.registerEventClass(String.class); fail("registerEvent() must only accept subclasses of ManagerEvent"); } catch (IllegalArgumentException ex) { } } /* * public void testGetSetters() { Map setters; EventBuilderImpl eventBuilder = * getEventBuilder(); * * setters = eventBuilder.getSetters(NewChannelEvent.class); * * assertTrue("Setter not found", setters.containsKey("callerid")); } */ public void testBuildEventWithMixedCaseSetter() { Map properties = new HashMap(); String callerid = "1234"; NewChannelEvent event; properties.put("event", "Newchannel"); properties.put("callerid", callerid); event = (NewChannelEvent) eventBuilder.buildEvent(this, properties); assertNotNull(event); assertEquals("Returned event is of wrong type", NewChannelEvent.class, event.getClass()); assertEquals("String property not set correctly", callerid, event.getCallerId()); assertEquals("Source not set correctly", this, event.getSource()); } public void testBuildEventWithIntegerProperty() { Map properties = new HashMap(); String channel = "SIP/1234"; Integer priority = new Integer(1); NewExtenEvent event; properties.put("event", "newexten"); properties.put("channel", channel); properties.put("priority", priority.toString()); event = (NewExtenEvent) eventBuilder.buildEvent(this, properties); assertNotNull(event); assertEquals("Returned event is of wrong type", NewExtenEvent.class, event.getClass()); assertEquals("String property not set correctly", channel, event.getChannel()); assertEquals("Integer property not set correctly", priority, event.getPriority()); } public void testBuildEventWithBooleanProperty() { Map properties = new HashMap(); ShutdownEvent event; eventBuilder.registerEventClass(ShutdownEvent.class); properties.put("event", "shutdown"); properties.put("restart", "True"); event = (ShutdownEvent) eventBuilder.buildEvent(this, properties); assertNotNull(event); assertEquals("Returned event is of wrong type", ShutdownEvent.class, event.getClass()); assertEquals("Boolean property not set correctly", Boolean.TRUE, event.getRestart()); } public void testBuildEventWithUnregisteredEvent() { Map properties = new HashMap(); ManagerEvent event; properties.put("event", "Nonexisting"); event = eventBuilder.buildEvent(this, properties); assertNull(event); } public void testBuildEventWithEmptyAttributes() { Map properties = new HashMap(); ManagerEvent event; event = eventBuilder.buildEvent(this, properties); assertNull(event); } public void testBuildEventWithResponseEvent() { Map properties = new HashMap(); ManagerEvent event; properties.put("event", "StatusComplete"); properties.put("actionid", "1234-origId"); event = eventBuilder.buildEvent(this, properties); assertNotNull(event); assertEquals("Returned event is of wrong type", StatusCompleteEvent.class, event.getClass()); assertEquals("ActionId not set correctly", "origId", ((ResponseEvent) event).getActionId()); } public void testBuildEventWithSourceProperty() { Map properties = new HashMap(); ManagerEvent event; properties.put("event", "Cdr"); properties.put("source", "source value"); event = eventBuilder.buildEvent(this, properties); assertNotNull(event); assertEquals("Src property not set correctly", "source value", ((CdrEvent) event).getSrc()); } } --- ResponseBuilderTest.java DELETED --- --- EventBuilderTest.java DELETED --- --- ActionBuilderTest.java DELETED --- |