You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by be...@apache.org on 2007/05/31 00:07:44 UTC
svn commit: r542950 [4/4] - in /labs/vysper: ./ lib/ src/ src/main/
src/main/config/ src/main/java/ src/main/java/org/
src/main/java/org/apache/ src/main/java/org/apache/vysper/
src/main/java/org/apache/vysper/xmpp/ src/main/java/org/apache/vysper/xmpp...
Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLFragment.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLFragment.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLFragment.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLFragment.java Wed May 30 15:07:38 2007
@@ -0,0 +1,23 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.xmlfragment;
+
+/**
+ */
+public interface XMLFragment {
+}
Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLText.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLText.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLText.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLText.java Wed May 30 15:07:38 2007
@@ -0,0 +1,48 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.xmlfragment;
+
+/**
+ * immutable text section (as it can appear between two xml elements
+ */
+public class XMLText implements XMLFragment {
+ private String text;
+
+ public XMLText(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ final XMLText xmlText = (XMLText) o;
+
+ if (text != null ? !text.equals(xmlText.text) : xmlText.text != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (text != null ? text.hashCode() : 0);
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,161 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.applicationdomains.base.handler;
+
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.protocol.ResponseStanzaContainer;
+import org.apache.vysper.xmpp.protocol.SessionStateHolder;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.stanza.IQStanza;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.stanza.XMPPCoreStanzaVerifier;
+import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
+import junit.framework.TestCase;
+
+/**
+ */
+public class IQHandlerTestCase extends TestCase {
+ private TestSessionContext sessionContext;
+
+ private SessionStateHolder sessionStateHolder = new SessionStateHolder();
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ sessionContext = new TestSessionContext(sessionStateHolder);
+ }
+
+ public void testMissingToInServerCall() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_SERVER);
+ stanzaBuilder.addAttribute("type", "get");
+ // missing stanzaBuilder.addAttribute("to", "test@example.com");
+ stanzaBuilder.addAttribute("id", "anyway");
+ stanzaBuilder.startInnerElement("inner").endInnerElement();
+
+ TestSessionContext sessionContext = this.sessionContext;
+ sessionContext.setServerToServer();
+
+ TestIQHandler iqHandler = new TestIQHandler();
+ ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanzaBuilder.getFinalStanza(), sessionContext, null);
+ Stanza responseStanza = responseStanzaContainer.getResponseStanza();
+ XMLElementVerifier verifier = responseStanza.getVerifier();
+ assertTrue("error", verifier.nameEquals("error"));
+ }
+
+ public void testMissingID() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("type", "get");
+ assertIQError(stanzaBuilder.getFinalStanza());
+ }
+
+ public void testDoNotRespondToErrorWithError() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("type", "error");
+ Stanza stanza = stanzaBuilder.getFinalStanza(); // this stanza has no ID
+
+ IQHandler iqHandler = new IQHandler();
+ ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanza, sessionContext, null);
+ Stanza responseStanza = responseStanzaContainer.getResponseStanza();
+ XMLElementVerifier verifier = responseStanza.getVerifier();
+ assertTrue("error", verifier.nameEquals("error")); // response is _not_ IQ stanza
+ }
+
+ private void assertIQError(Stanza stanza) {
+ TestIQHandler iqHandler = new TestIQHandler();
+ ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanza, sessionContext, null);
+ Stanza responseStanza = responseStanzaContainer.getResponseStanza();
+ XMLElementVerifier verifier = responseStanza.getVerifier();
+ assertTrue("iq", verifier.nameEquals("iq"));
+ assertTrue("error type", verifier.attributeEquals("type", IQStanzaType.ERROR.value()));
+ assertTrue("iq-error", verifier.subElementPresent("error"));
+ }
+
+ public void testMissingType() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("id", "1");
+ // missing: stanzaBuilder.addAttribute("type", "get");
+ assertIQError(stanzaBuilder.getFinalStanza());
+ }
+
+ public void testUnsupportedType() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("id", "1");
+ stanzaBuilder.addAttribute("type", "bogus");
+ assertIQError(stanzaBuilder.getFinalStanza());
+ }
+
+ public void testGetAndSetSubelements() {
+ // get and set must have exactly one subelement
+
+ String type = "get";
+ assertAnySub(type); // test with zero
+ assertNotTwoSubs(type); // test with 2
+
+ type = "set";
+ assertAnySub(type);
+ assertNotTwoSubs(type);
+ }
+
+ public void testResultSubelements() {
+ // result must have zero or one subelements
+ String type = "result";
+ assertNotTwoSubs(type); // test with two
+ }
+
+ private void assertNotTwoSubs(String type) {
+ StanzaBuilder stanzaTwoSubs = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaTwoSubs.addAttribute("id", "1");
+ stanzaTwoSubs.addAttribute("type", type);
+ stanzaTwoSubs.startInnerElement("firstSub").endInnerElement();
+ stanzaTwoSubs.startInnerElement("secondSub").endInnerElement();
+ assertIQError(stanzaTwoSubs.getFinalStanza());
+ }
+
+ private void assertAnySub(String type) {
+ StanzaBuilder stanzaNoSub = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaNoSub.addAttribute("id", "1");
+ stanzaNoSub.addAttribute("type", type);
+ assertIQError(stanzaNoSub.getFinalStanza());
+ }
+
+ public void testGet() {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("id", "1");
+ stanzaBuilder.addAttribute("type", "get");
+ stanzaBuilder.startInnerElement("getRequest").endInnerElement();
+
+ TestIQHandler iqHandler = new TestIQHandler();
+ ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanzaBuilder.getFinalStanza(), sessionContext, null);
+ IQStanza incomingStanza = iqHandler.getIncomingStanza();
+
+ XMPPCoreStanzaVerifier verifier = incomingStanza.getCoreVerifier();
+ assertTrue("iq", verifier.nameEquals("iq"));
+ assertTrue("iq-id", verifier.attributeEquals("id", "1"));
+ assertTrue("iq-type-get", verifier.attributeEquals("type", "get"));
+
+ // response is "result"
+ Stanza responseStanza = responseStanzaContainer.getResponseStanza();
+ XMLElementVerifier responseVerifier = responseStanza.getVerifier();
+ assertTrue("iq", responseVerifier.nameEquals("iq"));
+ assertTrue("iq-id", responseVerifier.attributeEquals("id", "1"));
+ assertTrue("iq-type-result", responseVerifier.attributeEquals("type", "result"));
+ }
+
+}
+
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/TestIQHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/TestIQHandler.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/TestIQHandler.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/TestIQHandler.java Wed May 30 15:07:38 2007
@@ -0,0 +1,62 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.applicationdomains.base.handler;
+
+import org.apache.vysper.xmpp.server.SessionContext;
+import org.apache.vysper.xmpp.stanza.IQStanza;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+
+public class TestIQHandler extends IQHandler {
+
+ String name = null;
+
+ private IQStanza incomingStanza;
+ private String namespaceURI;
+
+ public TestIQHandler() {
+ // empty
+ }
+
+ public TestIQHandler(String name, String namespaceURI) {
+ this.name = name;
+ this.namespaceURI = namespaceURI;
+ }
+
+ public boolean verify(Stanza stanza) {
+ if (!super.verify(stanza)) return false;
+ if (name == null) return true;
+ return stanza.getVerifier().onlySubelementEquals(name, namespaceURI);
+ }
+
+ protected Stanza executeIQLogic(IQStanza stanza, SessionContext sessionContext) {
+ incomingStanza = stanza;
+
+ StanzaBuilder responseBuilder = new StanzaBuilder("iq", stanza.getNamespace());
+ if (stanza.getID() != null) responseBuilder.addAttribute("id", stanza.getID());
+
+ responseBuilder.addAttribute("type", IQStanzaType.RESULT.value());
+
+ return responseBuilder.getFinalStanza();
+ }
+
+ public IQStanza getIncomingStanza() {
+ return incomingStanza;
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/PlainToTLSTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/PlainToTLSTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/PlainToTLSTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/PlainToTLSTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,56 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.parser;
+
+import junit.framework.TestCase;
+import org.bouncycastle.crypto.tls.TlsInputStream;
+import org.bouncycastle.crypto.tls.TlsOuputStream;
+import org.bouncycastle.crypto.tls.TlsProtocolHandler;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ */
+public class PlainToTLSTestCase extends TestCase {
+
+ protected void setUp() throws Exception {
+
+ }
+
+ public void testTLS() throws IOException {
+ OutputStream unencOutSend = new ByteArrayOutputStream();
+
+
+ InputStream unencIn = new ByteArrayInputStream("".getBytes());
+
+ TlsProtocolHandler protocolHandler = new TlsProtocolHandler(unencIn, unencOutSend);
+ //protocolHandler.connect(new AlwaysValidVerifyer());
+
+ TlsInputStream encIn = protocolHandler.getTlsInputStream();
+ TlsOuputStream unencOut = protocolHandler.getTlsOuputStream();
+
+ unencOutSend.write("Hello TLS world".getBytes());
+
+ int i = encIn.available();
+
+ unencIn.close();
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/StringStreamParserTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/StringStreamParserTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/StringStreamParserTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/parser/StringStreamParserTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,181 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.parser;
+
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.xmlfragment.Attribute;
+import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
+import org.apache.vysper.xmpp.xmlfragment.XMLText;
+import junit.framework.TestCase;
+
+import java.util.List;
+
+/* TODO use this test to test ALL StreamParser implementations */
+public class StringStreamParserTestCase extends TestCase {
+
+ private Stanza getFirstStanzaFromXML(String xml) throws ParsingException {
+ StringStreamParser stringStreamParser = new StringStreamParser(xml);
+ return stringStreamParser.getNextStanza();
+ }
+
+ public void testSimple() throws ParsingException {
+ Stanza stanza = getFirstStanzaFromXML("<test>leeklf</test>");
+ assertEquals("stanza simple text", "test", stanza.getName());
+ assertNotNull("empty attr", stanza.getAttributes());
+ assertEquals("zero attr", 0, stanza.getAttributes().size());
+
+ stanza = getFirstStanzaFromXML("<test2/><next>...");
+ assertEquals("stanza simple text2", "test2", stanza.getName());
+ }
+
+ public void testSequenceOfTwo() throws ParsingException {
+ assertSequenceOfTwo("<test_uno>payload_1</test_uno><test_due>payload_2</test_due>");
+ assertSequenceOfTwo("<test_uno><inner><inner_x></inner_x>payload_1</inner></test_uno><test_due><inner/>payload_2</test_due>");
+ }
+
+ public void testSequenceOfTwo_RestrictedXML() throws ParsingException {
+ // restricted XML must be ignored (RFC3920 section 11.1)
+ assertSequenceOfTwo("<test_uno></test_uno>NOT ALLOWED TEXT BETWEEN STANZAS<test_due></test_due>");
+ assertSequenceOfTwo("<test_uno></test_uno><!--NOT ALLOWED COMMENT BETWEEN STANZAS --><test_due></test_due>");
+ }
+
+ private void assertSequenceOfTwo(String xml) throws ParsingException {
+ StringStreamParser stringStreamParser = new StringStreamParser(xml);
+ Stanza stanza = stringStreamParser.getNextStanza();
+ assertEquals("stanza 1", "test_uno", stanza.getName());
+ stanza = stringStreamParser.getNextStanza();
+ assertEquals("stanza 2", "test_due", stanza.getName());
+ stanza = stringStreamParser.getNextStanza();
+ assertNull("stanza 3 not existing", stanza);
+ }
+
+ public void testBalancedElements() throws ParsingException {
+ getFirstStanzaFromXML("<test>leeklf</test>");
+
+ getFirstStanzaFromXML("<test>leeklf<inner></inner></test>");
+
+ getFirstStanzaFromXML("<test>leeklf<inner/></test>");
+
+ try {
+ getFirstStanzaFromXML("<test>leeklf<inner></test>");
+ fail("must raise exception");
+ } catch (Exception e) {
+ // fall thru
+ }
+
+ Stanza stanza = getFirstStanzaFromXML("<test>leeklf<test></test></test>");
+ assertEquals("inners", 2, stanza.getInnerFragments().size());
+ assertEquals("inner w/same name", "test", ((XMLElement) stanza.getInnerFragments().get(1)).getName());
+ }
+
+ public void testAttributes() throws ParsingException {
+ StringStreamParser stringStreamParser = new StringStreamParser("<testAttr at1=\"av1\" at2=\"av2\" />");
+ Stanza stanza = stringStreamParser.getNextStanza();
+ assertEquals("attributes length", 2, stanza.getAttributes().size());
+ assertEquals("stanza name", "testAttr", stanza.getName());
+ List<Attribute> attributes = stanza.getAttributes();
+
+ // inner attribues are immutable
+ int size = attributes.size();
+ try {
+ attributes.add(new Attribute("not", "insertable"));
+ fail("attributes should be immutable");
+ } catch (java.lang.UnsupportedOperationException e) {
+ // succeeded
+ }
+ assertEquals("nothing inserted", size, attributes.size());
+
+ assertEquals("a1", "at1", attributes.get(0).getName());
+ assertEquals("a1", "av1", attributes.get(0).getValue());
+ assertEquals("a2", "at2", attributes.get(1).getName());
+ assertEquals("a2", "av2", attributes.get(1).getValue());
+ try {
+ attributes.add(new Attribute("unmodName", "unmodValue"));
+ fail("could modify mutual attribute list");
+ } catch (UnsupportedOperationException e) {
+ // fall through
+ }
+ }
+
+ public void testNestedFragments() throws ParsingException {
+ StringStreamParser stringStreamParser = new StringStreamParser("<test_uno><inner><inner_x></inner_x>payload_1</inner>payload_2</test_uno>");
+ Stanza stanza = stringStreamParser.getNextStanza();
+
+ List<XMLFragment> innerFragments = stanza.getInnerFragments();
+
+ // inner frags are immutable
+ int size = innerFragments.size();
+ try {
+ innerFragments.add(new XMLText("not insertable"));
+ fail("fragments should be immutable");
+ } catch (java.lang.UnsupportedOperationException e) {
+ // succeeded
+ }
+ assertEquals("nothing inserted", size, innerFragments.size());
+
+ XMLFragment xmlFragment = innerFragments.get(0);
+ assertInnerXMLElement(xmlFragment, "inner", 2);
+
+ XMLFragment xmlFragmentDeep = ((XMLElement)xmlFragment).getInnerFragments().get(0);
+ assertInnerXMLElement(xmlFragmentDeep, "inner_x", 0);
+
+ xmlFragment = innerFragments.get(1);
+ assertInnerTextElement(xmlFragment, "payload_2");
+
+ }
+
+ private void assertInnerTextElement(XMLFragment xmlFragment, String text) {
+ assertTrue(xmlFragment instanceof XMLText);
+ XMLText xmlText = ((XMLText) xmlFragment);
+ assertEquals("text", text, xmlText.getText());
+ }
+
+ private void assertInnerXMLElement(XMLFragment xmlFragment, String elementName, int numberOfSubelements) {
+ assertTrue(xmlFragment instanceof XMLElement);
+ XMLElement xmlElement = ((XMLElement) xmlFragment);
+ assertEquals("elementName", elementName, xmlElement.getName());
+ assertEquals("subelements", numberOfSubelements, xmlElement.getInnerFragments().size());
+ }
+
+
+ public void testStartStanza() throws ParsingException {
+ StringStreamParser stringStreamParser =
+ new StringStreamParser(
+ "<stream:stream\n" +
+ " to='example.com'\n" +
+ " xmlns='jabber:client'\n" +
+ " xmlns:stream='http://etherx.jabber.org/streams'\n" +
+ " version='1.0' />");
+ Stanza stanza = stringStreamParser.getNextStanza();
+ String name = stanza.getName();
+ assertEquals("name", "stream", name);
+ }
+
+ public void testXMLHeader() throws ParsingException {
+ String xml =
+ "<?xml version='1.0'?><stream:stream from='example.com' id='someid' " +
+ " xmlns='jabber:client'" +
+ " xmlns:stream='http://etherx.jabber.org/streams' version='1.0' />";
+
+ StringStreamParser stringStreamParser = new StringStreamParser(xml);
+ Stanza stanza = stringStreamParser.getNextStanza();
+ assertEquals("stream start", "stream", stanza.getName());
+ }
+
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandler.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandler.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandler.java Wed May 30 15:07:38 2007
@@ -0,0 +1,70 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.server.SessionContext;
+import org.apache.vysper.xmpp.stanza.Stanza;
+
+public class CallTestStanzaHandler implements StanzaHandler {
+ private boolean handlerCalled = false;
+ private String name;
+ private String namespaceURI = null;
+ private ProtocolException exception = null;
+ private boolean verifyCalled = false;
+
+ public CallTestStanzaHandler(String name, String namespaceURI) {
+ this.name = name;
+ this.namespaceURI = namespaceURI;
+ }
+
+ public CallTestStanzaHandler(String name) {
+ this(name, null);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean verify(Stanza stanza) {
+ verifyCalled = true;
+ if (name != null && !name.equals(stanza.getName())) return false;
+ if (namespaceURI != null && !namespaceURI.equals(stanza.getNamespace())) return false;
+ return true;
+ }
+
+ public boolean isVerifyCalled() {
+ boolean isVerifyCalled = verifyCalled;
+ verifyCalled = false; // reset for next time.
+ return isVerifyCalled;
+ }
+
+ public void setProtocolException(ProtocolException exception) {
+ this.exception = exception;
+ }
+
+ public ResponseStanzaContainer execute(Stanza stanza, SessionContext sessionContext, SessionStateHolder sessionStateHolder) throws ProtocolException {
+ if (stanza == null || !stanza.getName().equals(getName()) || sessionContext == null) throw new RuntimeException("test failed");
+ handlerCalled = true;
+ if (exception != null) throw exception;
+ return null;
+ }
+
+ public void assertHandlerCalled() {
+ if (!handlerCalled) throw new RuntimeException("handler not called");
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandlerResponse.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandlerResponse.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandlerResponse.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/CallTestStanzaHandlerResponse.java Wed May 30 15:07:38 2007
@@ -0,0 +1,52 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.server.SessionContext;
+
+/**
+ */
+public class CallTestStanzaHandlerResponse extends CallTestStanzaHandler implements ResponseStanzaContainer {
+ private Stanza response;
+
+ public CallTestStanzaHandlerResponse(String name) {
+ super(name);
+ }
+
+ public ResponseStanzaContainer execute(Stanza stanza, SessionContext sessionContext, SessionStateHolder sessionStateHolder) throws ProtocolException {
+ super.execute(stanza, sessionContext, null);
+ return new ResponseStanzaContainerImpl(getResponseStanza());
+ }
+
+ public void setResponseStanza(Stanza response) {
+ this.response = response;
+ }
+
+ public Stanza getResponseStanza() {
+ return response;
+ }
+
+ public boolean hasResponse() {
+ return response != null;
+ }
+
+ public boolean hasNoResponse() {
+ return !hasResponse();
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolInitiationTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolInitiationTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolInitiationTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolInitiationTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,210 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.BaseStreamStanzaDictionary;
+import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionState;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.server.XMPPVersion;
+import org.apache.vysper.xmpp.server.response.ServerResponses;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
+import org.apache.vysper.xmpp.delivery.StanzaRelayImpl;
+import junit.framework.TestCase;
+
+/**
+ * test session initiation bevahior
+ */
+public class ProtocolInitiationTestCase extends TestCase {
+ private ProtocolWorker protocolWorker;
+ private DefaultServerRuntimeContext serverRuntimeContext;
+ private TestSessionContext sessionContext;
+
+ private static Entity serverEnitity = new EntityImpl(null, "vysper-server.org", null);
+ private EntityImpl testFrom = new EntityImpl("testuser", "vysper.org", null);
+ private SessionStateHolder sessionStateHolder;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ protocolWorker = new ProtocolWorker();
+ serverRuntimeContext = new DefaultServerRuntimeContext(serverEnitity, new StanzaRelayImpl());
+ serverRuntimeContext.addDictionary(new BaseStreamStanzaDictionary());
+ sessionStateHolder = new SessionStateHolder();
+ sessionContext = new TestSessionContext(serverRuntimeContext, sessionStateHolder);
+ }
+
+ public void testProcessClientCanonicalStreamOpeningResponse() {
+
+ sessionContext.setXMLLang("fr");
+ openClientSession();
+
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ XMLElementVerifier responseVerifier = recordedResponse.getVerifier();
+ assertTrue(responseVerifier.nameEquals("stream"));
+
+ assertTrue(responseVerifier.attributeEquals("xml:lang", "fr"));
+
+ assertTrue("initiated => started", sessionContext.getState() == SessionState.STARTED);
+
+ }
+
+ public void testProcessClientStreamOpeningResponse_XMLLang_fr() {
+
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ // french in, french returned
+ checkLanguage("fr");
+ }
+
+ public void testProcessClientStreamOpeningResponse_XMLLang_null() {
+
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ // no lang in, no lang returned
+ checkLanguage(null);
+ }
+
+ public void testProcessClientStreamOpeningResponse_XMLLang_enUS() {
+
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ // US-english in, US-english returned
+ checkLanguage("en_US");
+ }
+
+ private void checkLanguage(String xmlLang) {
+ Stanza stanza;
+ Stanza recordedResponse;
+ XMLElementVerifier responseVerifier;
+ stanza = new ServerResponses().getStreamOpener(true, testFrom, xmlLang, XMPPVersion.VERSION_1_0, null);
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ recordedResponse = sessionContext.getRecordedResponse();
+ responseVerifier = recordedResponse.getVerifier();
+ if (xmlLang == null) {
+ assertFalse(responseVerifier.attributePresent("xml:lang"));
+ } else {
+ assertTrue(responseVerifier.attributeEquals("xml:lang", xmlLang));
+ }
+ }
+
+ private void openClientSession() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+ Stanza stanza = new ServerResponses().getStreamOpener(true,
+ testFrom,
+ sessionContext.getXMLLang(),
+ XMPPVersion.VERSION_1_0, null);
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+ }
+
+ public void testProcessClientStreamOpeningResponse_Version_1_0() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ XMPPVersion versionSent = XMPPVersion.VERSION_1_0;
+ Stanza response = getVersionResponse(versionSent);
+
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertTrue(responseVerifier.attributeEquals("version", XMPPVersion.VERSION_1_0.toString()));
+ assertTrue(responseVerifier.attributePresent("id"));
+ assertFalse("no error", responseVerifier.subElementPresent("error"));
+ }
+
+ public void testProcessClientStreamOpeningResponse_NoVersion() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ XMPPVersion versionSent = null;
+ Stanza response = getVersionResponse(versionSent);
+
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertFalse(responseVerifier.attributePresent("version"));
+ assertFalse("no error", responseVerifier.subElementPresent("error"));
+ }
+
+ public void testProcessClientStreamOpeningResponse_Version_1_1() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ XMPPVersion versionSent = new XMPPVersion(1, 1);
+ Stanza response = getVersionResponse(versionSent);
+
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertTrue(responseVerifier.attributeEquals("version", XMPPVersion.VERSION_1_0.toString()));
+ assertFalse("no error", responseVerifier.subElementPresent("error"));
+ }
+
+ private class IllegalXMPPVersion extends XMPPVersion {
+ private String versionString;
+
+ public IllegalXMPPVersion(String version) {
+ versionString = version;
+ }
+
+ public String toString() {
+ return versionString;
+ }
+ }
+
+ public void testProcessClientStreamOpeningResponse_IllegalVersion() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ XMPPVersion versionSent = new IllegalXMPPVersion("IllV1.0");
+ Stanza response = getVersionResponse(versionSent);
+
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertTrue(responseVerifier.nameEquals("error"));
+ assertTrue("error", responseVerifier.subElementPresent("unsupported-version"));
+ }
+
+ public void testProcessClientStreamOpeningResponse_Version_2_0() {
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ XMPPVersion versionSent = new XMPPVersion(2, 0);
+ Stanza response = getVersionResponse(versionSent);
+
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertTrue(responseVerifier.nameEquals("error"));
+ assertTrue("error", responseVerifier.subElementPresent("unsupported-version"));
+ }
+
+ private Stanza getVersionResponse(XMPPVersion versionSent) {
+ Stanza stanza = new ServerResponses().getStreamOpener(true, testFrom, null, versionSent, null);
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ return sessionContext.getRecordedResponse();
+ }
+
+ public void testProcessClientStreamOpeningResponse_MissingMainNamespace() {
+ // we do not supply "http://etherx.jabber.org/streams"
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("stream", NamespaceURIs.HTTP_ETHERX_JABBER_ORG_STREAMS)
+ .addNamespaceAttribute(NamespaceURIs.JABBER_CLIENT)
+ .addAttribute("xml:lang", "en_UK")
+ .addAttribute("version", XMPPVersion.VERSION_1_0.toString());
+ protocolWorker.processStanza(sessionContext, stanzaBuilder.getFinalStanza(), sessionStateHolder);
+
+ Stanza response = sessionContext.getRecordedResponse();
+ XMLElementVerifier responseVerifier = response.getVerifier();
+ assertTrue(responseVerifier.nameEquals("error"));
+ assertTrue("error", responseVerifier.subElementPresent("invalid-namespace"));
+
+ }
+
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerAquireTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerAquireTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerAquireTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerAquireTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,95 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.delivery.StanzaRelayImpl;
+import org.apache.vysper.xmpp.parser.StreamParser;
+import org.apache.vysper.xmpp.parser.StringStreamParser;
+import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import junit.framework.TestCase;
+
+/**
+ * test basic behavior of ProtocolWorker.aquireStanza()
+ */
+public class ProtocolWorkerAquireTestCase extends TestCase {
+ private ProtocolWorker protocolWorker;
+ private TestSessionContext sessionContext;
+ private SessionStateHolder sessionStateHolder;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ protocolWorker = new ProtocolWorker();
+ sessionStateHolder = new SessionStateHolder();
+ sessionContext = new TestSessionContext(new DefaultServerRuntimeContext(new EntityImpl(null, "test", null), new StanzaRelayImpl()), sessionStateHolder);
+ }
+
+ public void testAquireSimpleStanza() {
+
+ StreamParser streamParser = new StringStreamParser("<t:ProtocolHandlerTestStanzaHandler xmlns:t='testNSURI' ></t:ProtocolHandlerTestStanzaHandler>");
+ Stanza stanza = protocolWorker.aquireStanza(sessionContext, streamParser);
+ assertNotNull(stanza);
+
+ Stanza expectedStanza = new StanzaBuilder("ProtocolHandlerTestStanzaHandler", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+ assertEquals("stanza full match", expectedStanza, stanza);
+
+ }
+
+ public void testAquireXMLNotWellformedStanza() {
+
+ StreamParser streamParser = new StringStreamParser("<ProtocolHandlerTestStanzaHandler><inner_not_closed></ProtocolHandlerTestStanzaHandler>");
+ Stanza stanza = protocolWorker.aquireStanza(sessionContext, streamParser);
+ assertNull(stanza);
+
+ assertErrorResponse();
+
+ }
+
+ public void testAquireXMLIncomplete() {
+
+ StreamParser streamParser = new StringStreamParser("<ProtocolHandlerTestStanzaHandler></Pro");
+ Stanza stanza = protocolWorker.aquireStanza(sessionContext, streamParser);
+ assertNull(stanza);
+
+ assertErrorResponse();
+
+ }
+
+ private void assertErrorResponse() {
+ assertTrue("session closed", sessionContext.isClosed());
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ assertEquals("error reported", "error", recordedResponse.getName());
+ assertTrue("bad format", recordedResponse.getVerifier().subElementPresent("bad-format"));
+ }
+
+ public void testAquireNoNextStanza() {
+
+ StreamParser streamParser = new StringStreamParser("<ProtocolHandlerTestStanzaHandler ></ProtocolHandlerTestStanzaHandler>");
+ Stanza stanza = protocolWorker.aquireStanza(sessionContext, streamParser);
+ assertNotNull(stanza);
+
+ stanza = protocolWorker.aquireStanza(sessionContext, streamParser);
+ assertNull("no next", stanza);
+ assertFalse("not closed", sessionContext.isClosed());
+ }
+
+}
\ No newline at end of file
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,151 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionState;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
+import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+import org.apache.vysper.xmpp.delivery.StanzaRelayImpl;
+import junit.framework.TestCase;
+
+/**
+ * test basic behavior of ProtocolWorker.processStanza()
+ */
+public class ProtocolWorkerProcessTestCase extends TestCase {
+ private ProtocolWorker protocolWorker;
+ private NamespaceHandlerDictionary namespaceHandlerDictionary;
+ private DefaultServerRuntimeContext serverRuntimeContext;
+ private TestSessionContext sessionContext;
+
+ private static Entity serverEnitity = new EntityImpl(null, "test", null);
+ private SessionStateHolder sessionStateHolder;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ protocolWorker = new ProtocolWorker();
+ namespaceHandlerDictionary = new NamespaceHandlerDictionary("testNSURI");
+ serverRuntimeContext = new DefaultServerRuntimeContext(serverEnitity, new StanzaRelayImpl());
+ serverRuntimeContext.addDictionary(namespaceHandlerDictionary);
+ sessionStateHolder = new SessionStateHolder();
+ sessionContext = new TestSessionContext(serverRuntimeContext, sessionStateHolder);
+ }
+
+ public void testProcessUnknownStanza() {
+
+ sessionContext.setSessionState(SessionState.AUTHENTICATED);
+
+ Stanza stanza = new StanzaBuilder("ProtocolWorkerProcessTestCase", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ XMLElementVerifier verifier = recordedResponse.getVerifier();
+ assertTrue("error", verifier.nameEquals("error"));
+ assertTrue("unsupported stanza type", verifier.subElementPresent(StreamErrorCondition.UNSUPPORTED_STANZA_TYPE.value()));
+ }
+
+ public void testProcessStanzaNoResponse() {
+
+ sessionContext.setSessionState(SessionState.AUTHENTICATED);
+
+ CallTestStanzaHandler stanzaHandler = new CallTestStanzaHandler("ProtocolWorkerProcessTestCase");
+ namespaceHandlerDictionary.register(stanzaHandler);
+
+ Stanza stanza = new StanzaBuilder("ProtocolWorkerProcessTestCase", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ stanzaHandler.assertHandlerCalled();
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ assertNull("no emmitter, no response", recordedResponse);
+ }
+
+ public void testProcessStanzaWithResponse() {
+
+ sessionContext.setSessionState(SessionState.AUTHENTICATED);
+
+ CallTestStanzaHandlerResponse stanzaHandler = new CallTestStanzaHandlerResponse("ProtocolWorkerProcessTestCase");
+ namespaceHandlerDictionary.register(stanzaHandler);
+
+ Stanza stanza = new StanzaBuilder("ProtocolWorkerProcessTestCase", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+ Stanza responseStanza = new StanzaBuilder("response").getFinalStanza();
+
+ stanzaHandler.setResponseStanza(responseStanza);
+
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ stanzaHandler.assertHandlerCalled();
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ assertEquals("response handled", responseStanza, recordedResponse);
+
+ stanzaHandler.setResponseStanza(null);
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+ assertNull("handler emmitted null as response", stanzaHandler.getResponseStanza());
+
+ }
+
+ public void testHandlerThrowProtocolException() {
+
+ sessionContext.setSessionState(SessionState.AUTHENTICATED);
+
+ CallTestStanzaHandler stanzaHandler = new CallTestStanzaHandler("ProtocolWorkerProcessTestCase");
+ namespaceHandlerDictionary.register(stanzaHandler);
+ stanzaHandler.setProtocolException(new ProtocolException("forced error"));
+
+ Stanza stanza = new StanzaBuilder("ProtocolWorkerProcessTestCase", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ assertEquals("bad format", "error", recordedResponse.getName());
+ assertTrue("closed", sessionContext.isClosed());
+ }
+
+ public void testProcessWrongStartStanza() {
+
+ sessionContext.setSessionState(SessionState.INITIATED);
+
+ CallTestStanzaHandler stanzaHandler = new CallTestStanzaHandler("ProtocolWorkerProcessTestCase");
+ namespaceHandlerDictionary.register(stanzaHandler);
+
+ Stanza stanza = new StanzaBuilder("ProtocolWorkerProcessTestCase", "testNSURI").addAttribute("xmlns:t", "testNSURI").getFinalStanza();
+
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ try {
+ stanzaHandler.assertHandlerCalled();
+ fail("handler called");
+ } catch (Exception e) {
+ // not called, OK
+ }
+ Stanza recordedResponse = sessionContext.getRecordedResponse();
+ assertEquals("open stream", "stream", recordedResponse.getName());
+ XMLElementVerifier xmlElementVerifier = recordedResponse.getVerifier();
+ assertTrue("error embedded", xmlElementVerifier.subElementPresent("error"));
+ XMLElement error = (XMLElement) recordedResponse.getInnerFragments().get(0);
+ assertEquals("bad format", "error", error.getName());
+ assertTrue("closed", sessionContext.isClosed());
+ }
+
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerStateAwarenessTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerStateAwarenessTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerStateAwarenessTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerStateAwarenessTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,103 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.delivery.StanzaRelayImpl;
+import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.server.SessionState;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.BaseStreamStanzaDictionary;
+import org.apache.vysper.xmpp.applicationdomains.base.StanzaErrorCondition;
+import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
+import junit.framework.TestCase;
+
+/**
+ * test basic behavior of ProtocolWorker.aquireStanza()
+ */
+public class ProtocolWorkerStateAwarenessTestCase extends TestCase {
+ private ProtocolWorker protocolWorker;
+ private TestSessionContext sessionContext;
+ private SessionStateHolder sessionStateHolder;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ protocolWorker = new ProtocolWorker();
+ sessionStateHolder = new SessionStateHolder();
+ Entity serverEnitity = new EntityImpl(null, "vysper-server.org", null);
+ DefaultServerRuntimeContext serverRuntimeContext = new DefaultServerRuntimeContext(serverEnitity, new StanzaRelayImpl());
+ serverRuntimeContext.addDictionary(new BaseStreamStanzaDictionary());
+ sessionStateHolder = new SessionStateHolder();
+ sessionContext = new TestSessionContext(serverRuntimeContext, sessionStateHolder);
+ }
+
+ public void testNotAcceptRegularStanzaBeyondAuthenticatedState_IQ() throws Exception {
+
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("id", "1");
+ stanzaBuilder.addAttribute("type", "get");
+ Stanza stanza = stanzaBuilder.getFinalStanza();
+
+ assertNotAuthorized(stanza);
+ }
+
+ public void testNotAcceptRegularStanzaBeyondAuthenticatedState_Presence() throws Exception {
+
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("presence", NamespaceURIs.JABBER_CLIENT);
+ // TODO? add more presence specifics
+ Stanza stanza = stanzaBuilder.getFinalStanza();
+
+ assertNotAuthorized(stanza);
+ }
+
+ public void testNotAcceptRegularStanzaBeyondAuthenticatedState_Message() throws Exception {
+
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("message", NamespaceURIs.JABBER_CLIENT);
+ // TODO? add more message specifics
+ Stanza stanza = stanzaBuilder.getFinalStanza();
+
+ assertNotAuthorized(stanza);
+ }
+
+ private void assertNotAuthorized(Stanza stanza) throws Exception {
+ SessionState[] allStates = SessionState.values();
+ for (SessionState state : allStates) {
+ if (state == SessionState.AUTHENTICATED) continue; // skip allowed state
+
+ setUp();
+
+ sessionStateHolder.setState(state);
+ protocolWorker.processStanza(sessionContext, stanza, sessionStateHolder);
+
+ Stanza response = sessionContext.getRecordedResponse();
+ XMLElementVerifier xmlElementVerifier = response.getVerifier();
+
+ // RFC3920/4.3: response must be "not-authorized"
+ assertTrue("error stanza", xmlElementVerifier.nameEquals("error"));
+ assertTrue("error stanza not-authorized", xmlElementVerifier.subElementPresent(StanzaErrorCondition.NOT_AUTHORIZED.value()));
+ assertTrue("writer had been closed", sessionContext.isClosed());
+ assertEquals("session closed", SessionState.CLOSED, sessionContext.getState());
+ }
+ }
+
+
+}
\ No newline at end of file
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookupTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookupTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookupTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookupTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,159 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.protocol;
+
+import junit.framework.TestCase;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.xmlfragment.Attribute;
+import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.BaseStreamStanzaDictionary;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.IQHandler;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.PresenceHandler;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.MessageHandler;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.TestIQHandler;
+
+import java.util.ArrayList;
+
+/**
+ */
+public class StanzaHandlerLookupTestCase extends TestCase {
+
+ public void testDictionaryHierarchy() {
+ NamespaceHandlerDictionary upperNamespaceHandlerDictionary = new NamespaceHandlerDictionary("testNSURI1");
+ CallTestStanzaHandler upperStanzaHandler = new CallTestStanzaHandler("testDictionaryHierarchy", "testNSURI1");
+ upperNamespaceHandlerDictionary.register(upperStanzaHandler);
+
+ NamespaceHandlerDictionary lowerNamespaceHandlerDictionary = new NamespaceHandlerDictionary("testNSURI2");
+ CallTestStanzaHandler lowerStanzaHandler = new CallTestStanzaHandler("testDictionaryHierarchy", "testNSURI2");
+ lowerNamespaceHandlerDictionary.register(lowerStanzaHandler);
+
+
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(upperNamespaceHandlerDictionary);
+ stanzaHandlerLookup.addDictionary(lowerNamespaceHandlerDictionary);
+
+ Stanza nonExistingStanza = new Stanza("testDictionaryHierarchyNotExist", "testNSURI", new ArrayList<Attribute>(), new ArrayList<XMLFragment>());
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(nonExistingStanza);
+ assertNull("handler not found", handler);
+
+ Stanza existingStanzaNS1 = new Stanza("testDictionaryHierarchy", "testNSURI1", new ArrayList<Attribute>(), new ArrayList<XMLFragment>());
+ handler = stanzaHandlerLookup.getHandler(existingStanzaNS1);
+ assertNotNull("handler found in dict1", handler);
+ assertTrue("verify got called", ((CallTestStanzaHandler)handler).isVerifyCalled());
+ assertNotSame("lower not found", lowerStanzaHandler, handler);
+ assertSame("upper found", upperStanzaHandler, handler);
+
+ Stanza existingStanzaNS2 = new Stanza("testDictionaryHierarchy", "testNSURI2", new ArrayList<Attribute>(), new ArrayList<XMLFragment>());
+ handler = stanzaHandlerLookup.getHandler(existingStanzaNS2);
+ assertTrue("verify got called", ((CallTestStanzaHandler)handler).isVerifyCalled());
+ assertNotNull("handler found in dict2", handler);
+ assertSame("lower found", lowerStanzaHandler, handler);
+ assertNotSame("upper not found", upperStanzaHandler, handler);
+
+ }
+
+ public void testLookupCoreHandlerClientNS() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT).getFinalStanza();
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+
+ assertNotNull("handler found", handler);
+ assertTrue("iq handler found", handler instanceof IQHandler);
+
+ }
+
+ public void testLookupCoreHandlerServerNS() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_SERVER).getFinalStanza();
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+
+ assertNotNull("handler found", handler);
+ assertTrue("iq handler found", handler instanceof IQHandler);
+
+ }
+
+ public void testLookupCoreHandlerWrongNamespace() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ Stanza stanza = new StanzaBuilder("iq", "arbitraryNamespace").getFinalStanza();
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+
+ assertNull("iq handler with arbitrary namespace not found", handler);
+ }
+
+ public void testLookupPresenceHandler() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ Stanza stanza = new StanzaBuilder("presence", NamespaceURIs.JABBER_CLIENT).getFinalStanza();
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+
+ assertNotNull("handler found", handler);
+ assertTrue("iq handler found", handler instanceof PresenceHandler);
+ }
+
+ public void testLookupMessageHandler() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ Stanza stanza = new StanzaBuilder("message", NamespaceURIs.JABBER_CLIENT).getFinalStanza();
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+
+ assertNotNull("handler found", handler);
+ assertTrue("iq handler found", handler instanceof MessageHandler);
+ }
+
+ public void testLookupSpecializedIQHandler() {
+ StanzaHandlerLookup stanzaHandlerLookup = new StanzaHandlerLookup();
+ stanzaHandlerLookup.addDictionary(new BaseStreamStanzaDictionary());
+
+ NamespaceHandlerDictionary testDictionary = new NamespaceHandlerDictionary("test:namespace:OK");
+ testDictionary.register(new TestIQHandler("testOK", "test:namespace:OK"));
+ stanzaHandlerLookup.addDictionary(testDictionary);
+
+ Stanza stanza = buildStanza("testOK", "test:namespace:FAIL");
+ StanzaHandler handler = stanzaHandlerLookup.getHandler(stanza);
+ assertNull("handler not found for NS", handler);
+
+ stanza = buildStanza("testFAIL", "test:namespace:OK");
+ handler = stanzaHandlerLookup.getHandler(stanza);
+ assertNull("handler not found for name", handler);
+
+ stanza = buildStanza("testOK", "test:namespace:OK");
+ handler = stanzaHandlerLookup.getHandler(stanza);
+ assertNotNull("handler found", handler);
+ assertTrue("test handler", TestIQHandler.class.equals(handler.getClass()));
+
+
+ }
+
+ private Stanza buildStanza(String name, String namespaceURI) {
+ StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
+ stanzaBuilder.addAttribute("id", "1");
+ stanzaBuilder.addAttribute("type", "get");
+ stanzaBuilder.startInnerElement(name, namespaceURI).endInnerElement();
+ Stanza stanza = stanzaBuilder.getFinalStanza();
+ return stanza;
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/SessionStateTest.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/SessionStateTest.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/SessionStateTest.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/SessionStateTest.java Wed May 30 15:07:38 2007
@@ -0,0 +1,36 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.server;
+
+import junit.framework.TestCase;
+
+/**
+ */
+public class SessionStateTest extends TestCase {
+
+ public void testCompare() {
+ assertEquals("diff between first and last state", -6, SessionState.INITIATED.compareTo(SessionState.CLOSED));
+
+ assertEquals("diff to next state 1", -1, SessionState.INITIATED.compareTo(SessionState.STARTED));
+ assertEquals("diff to next state 2", -1, SessionState.STARTED.compareTo(SessionState.ENCRYPTION_STARTED));
+ assertEquals("diff to next state 3", -1, SessionState.ENCRYPTION_STARTED.compareTo(SessionState.ENCRYPTED));
+ assertEquals("diff to next state 4", -1, SessionState.ENCRYPTED.compareTo(SessionState.AUTHENTICATED));
+ assertEquals("diff to next state 5", -1, SessionState.AUTHENTICATED.compareTo(SessionState.ENDED));
+ assertEquals("diff to next state 6", -1, SessionState.ENDED.compareTo(SessionState.CLOSED));
+ }
+
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java Wed May 30 15:07:38 2007
@@ -0,0 +1,65 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.server;
+
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.writer.StanzaWriter;
+import org.apache.vysper.xmpp.delivery.StanzaRelayImpl;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.protocol.SessionStateHolder;
+
+/**
+ * makes response available for testing
+ */
+public class TestSessionContext extends DefaultSessionContext implements StanzaWriter {
+
+ private Stanza recordedResponse = null;
+ private boolean closed = false;
+
+ public TestSessionContext(SessionStateHolder sessionStateHolder) {
+ this(new DefaultServerRuntimeContext(new EntityImpl(null, "test", null), new StanzaRelayImpl()), sessionStateHolder);
+ }
+
+ public TestSessionContext(ServerRuntimeContext serverRuntimeContext, SessionStateHolder sessionStateHolder) {
+ super(serverRuntimeContext, sessionStateHolder);
+ sessionId = serverRuntimeContext.getNextSessionId();
+ xmlLang = "de";
+ stanzaWriter = this;
+ }
+
+ public Stanza getRecordedResponse() {
+ return recordedResponse;
+ }
+
+ public void close() {
+ stanzaWriter = null;
+ closed = true;
+ }
+
+ public boolean isClosed() {
+ return closed; // checks if close had been called
+ }
+
+ public void write(Stanza stanza) {
+ recordedResponse = stanza;
+ }
+
+ public void setSessionState(SessionState sessionState) {
+ this.sessionStateHolder.setState(sessionState);
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/XMPPVersionTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/XMPPVersionTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/XMPPVersionTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/XMPPVersionTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,79 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.server;
+
+import junit.framework.TestCase;
+
+/**
+ */
+public class XMPPVersionTestCase extends TestCase {
+
+ public void testParsedVersionOK() {
+ assertVersionOK("0.0", "0.0");
+ assertVersionOK("0.1", "0.1");
+ assertVersionOK("1.0", "1.0");
+ assertVersionOK("2.0", "2.0");
+ assertVersionOK("2.1", "2.1");
+ assertVersionOK("2.11", "2.11");
+ assertVersionOK("2.1", "2.01");
+ assertVersionOK("2.1", "02.01");
+ assertVersionOK("22.1", "022.1");
+ }
+
+ public void testParsedVersionNotOK() {
+ assertVersionNotOK("-1.0");
+ assertVersionNotOK("2.-0");
+ assertVersionNotOK("2.01A");
+ assertVersionNotOK("02A.01");
+ }
+
+ private void assertVersionNotOK(String version) {
+ try {
+ XMPPVersion xmppVersion = new XMPPVersion(version);
+ fail("test failed");
+ } catch (Exception e) {
+ // test succeeded
+ }
+ }
+
+ private void assertVersionOK(String versionExpected, String versionInput) {
+ XMPPVersion xmppVersion = new XMPPVersion(versionInput);
+ assertEquals(versionExpected, xmppVersion.toString());
+ }
+
+ public void testCommonDenominator() {
+ assertFirstIsCommonDenominator("1.0", "1.1");
+ assertFirstIsCommonDenominator("1.1", "1.11");
+ assertFirstIsCommonDenominator("1.11", "11.1");
+ assertFirstIsCommonDenominator("11.11", "11.111");
+ assertFirstIsCommonDenominator("11.001", "11.010");
+ assertFirstIsCommonDenominator("11.001", "11.011");
+ assertFirstIsCommonDenominator("10.001", "10.011");
+ assertFirstIsCommonDenominator("010.001", "10.0011");
+
+ }
+
+ private void assertFirstIsCommonDenominator(String v1, String v2) {
+ XMPPVersion version1 = new XMPPVersion(v1);
+ XMPPVersion version2 = new XMPPVersion(v2);
+
+ XMPPVersion.getCommonDenomitator(version2, version1);
+
+ }
+
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/response/ServerResponsesTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/response/ServerResponsesTestCase.java?view=auto&rev=542950
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/response/ServerResponsesTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/response/ServerResponsesTestCase.java Wed May 30 15:07:38 2007
@@ -0,0 +1,43 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation. *
+ * All rights reserved. *
+ * ------------------------------------------------------------------- *
+ * 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 org.apache.vysper.xmpp.server.response;
+
+import org.apache.vysper.xmpp.parser.ParsingException;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.parser.StringStreamParser;
+import junit.framework.TestCase;
+
+/**
+ */
+public class ServerResponsesTestCase extends TestCase {
+
+ public void testFeaturesForAuthentication() throws ParsingException {
+ StringStreamParser parser = new StringStreamParser(
+ "<features>" +
+ "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" +
+ "<mechanism>DIGEST-MD5</mechanism>" +
+ "<mechanism>PLAIN</mechanism>" +
+ "<mechanism>KERBEROS_V4</mechanism>" +
+ "</mechanisms>" +
+ "</features>");
+
+ Stanza stanza = parser.getNextStanza();
+
+ assertEquals("stanzas are identical", stanza, new ServerResponses().getFeaturesForAuthentication());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org