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 2008/10/12 21:00:24 UTC
svn commit: r703862 - in /labs/vysper/src:
main/java/org/apache/vysper/xmpp/modules/roster/
test/java/org/apache/vysper/xmpp/modules/roster/
Author: berndf
Date: Sun Oct 12 12:00:23 2008
New Revision: 703862
URL: http://svn.apache.org/viewvc?rev=703862&view=rev
Log:
[vysper] add logic for presence subscription state change
Added:
labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutator.java
labs/vysper/src/test/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutatorTestCase.java
Modified:
labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java
Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java?rev=703862&r1=703861&r2=703862&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java Sun Oct 12 12:00:23 2008
@@ -66,9 +66,23 @@
return subscriptionType;
}
+ /**
+ * should be set using the RosterSubscriptionMutator
+ */
+ /*package*/ void setSubscriptionType(SubscriptionType subscriptionType) {
+ this.subscriptionType = subscriptionType;
+ }
+
public AskSubscriptionType getAskSubscriptionType() {
return askSubscriptionType;
}
+
+ /**
+ * should be set using the RosterSubscriptionMutator
+ */
+ /*package*/ void setAskSubscriptionType(AskSubscriptionType askSubscribe) {
+ this.askSubscriptionType = askSubscribe;
+ }
public boolean hasTo() {
return subscriptionType == TO || subscriptionType == BOTH;
Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutator.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutator.java?rev=703862&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutator.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutator.java Sun Oct 12 12:00:23 2008
@@ -0,0 +1,173 @@
+/***********************************************************************
+ * 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.modules.roster;
+
+import static org.apache.vysper.xmpp.modules.roster.RosterSubscriptionMutator.RosterSubscriptionMutatorResult.*;
+import static org.apache.vysper.xmpp.modules.roster.SubscriptionType.*;
+import static org.apache.vysper.xmpp.modules.roster.AskSubscriptionType.*;
+
+/**
+ */
+public class RosterSubscriptionMutator {
+
+ enum RosterSubscriptionMutatorResult {
+ OK,
+ ILLEGAL_ARGUMENT,
+ ALREADY_SET,
+ FAILED
+ }
+
+ /**
+ * adds a subscription request to the roster item
+ */
+ public RosterSubscriptionMutatorResult add(RosterItem item, AskSubscriptionType addAskSubscriptionType) {
+ synchronized (item) {
+ return addWorker(item, addAskSubscriptionType);
+ }
+ }
+
+ /**
+ * adds a subscription to the roster item
+ */
+ public RosterSubscriptionMutatorResult add(RosterItem item, SubscriptionType addSubscriptionType) {
+ synchronized (item) {
+ return addWorker(item, addSubscriptionType);
+ }
+ }
+
+ /**
+ * removes a subscription from the roster item
+ */
+ public RosterSubscriptionMutatorResult remove(RosterItem item, SubscriptionType removeSubscriptionType) {
+ synchronized (item) {
+ return removeWorker(item, removeSubscriptionType);
+ }
+ }
+
+ protected RosterSubscriptionMutatorResult addWorker(RosterItem item, SubscriptionType addSubscriptionType) {
+ switch (addSubscriptionType) {
+ case NONE:
+ case BOTH:
+ case REMOVE:
+ return RosterSubscriptionMutatorResult.ILLEGAL_ARGUMENT;
+ case FROM:
+ return addFrom(item);
+ case TO:
+ return addTo(item);
+ default:
+ throw new IllegalArgumentException("unhandled SubscriptionType " + addSubscriptionType.value());
+ }
+ }
+
+ protected RosterSubscriptionMutatorResult addWorker(RosterItem item, AskSubscriptionType addAskSubscriptionType) {
+ switch (addAskSubscriptionType) {
+ case NOT_SET:
+ return RosterSubscriptionMutatorResult.ILLEGAL_ARGUMENT;
+ case ASK_SUBSCRIBE:
+ return addAskSubscribe(item);
+ case ASK_SUBSCRIBED:
+ return addAskSubscribed(item);
+ default:
+ throw new IllegalArgumentException("unhandled SubscriptionType " + addAskSubscriptionType.value());
+ }
+ }
+
+ protected RosterSubscriptionMutatorResult addAskSubscribe(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ AskSubscriptionType typeAsk = item.getAskSubscriptionType();
+ if (type.includesTo()) return ALREADY_SET;
+ if (typeAsk == ASK_SUBSCRIBE) return ALREADY_SET;
+ // IGNORE this, overwrite! if (typeAsk == ASK_SUBSCRIBED) return ALREADY_SET;
+ item.setAskSubscriptionType(ASK_SUBSCRIBE);
+ return OK;
+ }
+
+ protected RosterSubscriptionMutatorResult addAskSubscribed(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ AskSubscriptionType typeAsk = item.getAskSubscriptionType();
+ if (type.includesFrom()) return ALREADY_SET;
+ if (typeAsk == ASK_SUBSCRIBE) return FAILED; // TODO think about return value
+ if (typeAsk == ASK_SUBSCRIBED) return ALREADY_SET;
+ item.setAskSubscriptionType(ASK_SUBSCRIBED);
+ return OK;
+ }
+
+ protected RosterSubscriptionMutatorResult addTo(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ if (type.includesTo()) return ALREADY_SET;
+ if (type == NONE) {
+ type = TO;
+ } else if (type == FROM) {
+ type = BOTH;
+ }
+ item.setSubscriptionType(type);
+ if (item.getAskSubscriptionType() == ASK_SUBSCRIBE) item.setAskSubscriptionType(NOT_SET);
+ return OK;
+ }
+
+ protected RosterSubscriptionMutatorResult addFrom(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ if (type.includesFrom()) return ALREADY_SET;
+ if (type == NONE) {
+ type = FROM;
+ } else if (type == FROM) {
+ type = BOTH;
+ }
+ item.setSubscriptionType(type);
+ if (item.getAskSubscriptionType() == ASK_SUBSCRIBED) item.setAskSubscriptionType(NOT_SET);
+ return OK;
+ }
+
+ protected RosterSubscriptionMutatorResult removeWorker(RosterItem item, SubscriptionType removeSubscriptionType) {
+ switch (removeSubscriptionType) {
+ case NONE:
+ case BOTH:
+ case REMOVE:
+ return RosterSubscriptionMutatorResult.ILLEGAL_ARGUMENT;
+ case FROM:
+ return removeTo(item);
+ case TO:
+ return removeFrom(item);
+ default:
+ throw new IllegalArgumentException("unhandled SubscriptionType " + removeSubscriptionType.value());
+ }
+ }
+
+ protected RosterSubscriptionMutatorResult removeTo(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ if (!type.includesTo()) return ALREADY_SET;
+ if (type == BOTH) {
+ type = FROM;
+ } else if (type == TO) {
+ type = NONE;
+ }
+ item.setSubscriptionType(type);
+ return OK;
+ }
+
+ protected RosterSubscriptionMutatorResult removeFrom(RosterItem item) {
+ SubscriptionType type = item.getSubscriptionType();
+ if (!type.includesFrom()) return ALREADY_SET;
+ if (type == BOTH) {
+ type = TO;
+ } else if (type == FROM) {
+ type = NONE;
+ }
+ item.setSubscriptionType(type);
+ return OK;
+ }
+}
Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutatorTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutatorTestCase.java?rev=703862&view=auto
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutatorTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/modules/roster/RosterSubscriptionMutatorTestCase.java Sun Oct 12 12:00:23 2008
@@ -0,0 +1,136 @@
+/***********************************************************************
+ * 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.modules.roster;
+
+import junit.framework.TestCase;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.addressing.EntityFormatException;
+import static org.apache.vysper.xmpp.modules.roster.AskSubscriptionType.*;
+import static org.apache.vysper.xmpp.modules.roster.SubscriptionType.*;
+import static org.apache.vysper.xmpp.modules.roster.RosterSubscriptionMutator.RosterSubscriptionMutatorResult.*;
+
+/**
+ */
+public class RosterSubscriptionMutatorTestCase extends TestCase {
+
+ @Override
+ public void setUp() {
+ // Add your code here
+ }
+
+ public void testAddSubscriptionRequest() {
+ checkAdd(NONE, NOT_SET, NOT_SET, ILLEGAL_ARGUMENT, null, null);
+
+ // most simple cases
+ checkAdd(NONE, NOT_SET, ASK_SUBSCRIBE, OK, NONE, ASK_SUBSCRIBE);
+ checkAdd(NONE, NOT_SET, ASK_SUBSCRIBED, OK, NONE, ASK_SUBSCRIBED);
+
+ // one existing subscription,
+ checkAdd(TO, NOT_SET, ASK_SUBSCRIBED, OK, TO, ASK_SUBSCRIBED);
+ checkAdd(FROM, NOT_SET, ASK_SUBSCRIBE, OK, FROM, ASK_SUBSCRIBE);
+
+ // status already set
+ checkAdd(TO, ASK_SUBSCRIBED, ASK_SUBSCRIBED, ALREADY_SET, null, null);
+ checkAdd(FROM, ASK_SUBSCRIBE, ASK_SUBSCRIBE, ALREADY_SET, null, null);
+
+ // BOTH + pending is kind of illegal state. well anyway...
+ checkAdd(BOTH, ASK_SUBSCRIBED, ASK_SUBSCRIBED, ALREADY_SET, null, null);
+ checkAdd(BOTH, ASK_SUBSCRIBE, ASK_SUBSCRIBE, ALREADY_SET, null, null);
+ checkAdd(BOTH, NOT_SET, ASK_SUBSCRIBED, ALREADY_SET, null, null);
+ checkAdd(BOTH, NOT_SET, ASK_SUBSCRIBE, ALREADY_SET, null, null);
+
+ // special cases for conflicting SUBSCRIBE/SUBSCRIBED stati
+ checkAdd(NONE, ASK_SUBSCRIBED, ASK_SUBSCRIBE, OK, NONE, ASK_SUBSCRIBE);
+ checkAdd(NONE, ASK_SUBSCRIBE, ASK_SUBSCRIBED, FAILED, null, null);
+
+ }
+
+ public void testAddSubscription() {
+ checkAdd(NONE, NOT_SET, NONE, ILLEGAL_ARGUMENT, null, null);
+ checkAdd(BOTH, NOT_SET, NONE, ILLEGAL_ARGUMENT, null, null);
+ checkAdd(REMOVE, NOT_SET, NONE, ILLEGAL_ARGUMENT, null, null);
+
+ checkAdd(TO, NOT_SET, TO, ALREADY_SET, null, null);
+ checkAdd(FROM, NOT_SET, FROM, ALREADY_SET, null, null);
+
+ checkAdd(NONE, NOT_SET, FROM, OK, FROM, NOT_SET);
+ checkAdd(NONE, ASK_SUBSCRIBED, FROM, OK, FROM, NOT_SET);
+ checkAdd(NONE, ASK_SUBSCRIBE, FROM, OK, FROM, ASK_SUBSCRIBE);
+
+ checkAdd(NONE, NOT_SET, TO, OK, TO, NOT_SET);
+ checkAdd(NONE, ASK_SUBSCRIBE, TO, OK, TO, NOT_SET);
+ checkAdd(NONE, ASK_SUBSCRIBED, TO, OK, TO, ASK_SUBSCRIBED);
+
+ checkAdd(TO, NOT_SET, FROM, OK, BOTH, NOT_SET);
+ checkAdd(TO, ASK_SUBSCRIBED, FROM, OK, BOTH, NOT_SET);
+ checkAdd(TO, ASK_SUBSCRIBE, FROM, OK, BOTH, ASK_SUBSCRIBE);
+
+ }
+
+ public void testRemoveSubscription() {
+ // TODO Add your code here
+ }
+
+ private void checkAdd(SubscriptionType initialSubscriptionType, AskSubscriptionType initialAskSubscriptionType,
+ SubscriptionType parameterSubscriptionType,
+ RosterSubscriptionMutator.RosterSubscriptionMutatorResult expectedResult,
+ SubscriptionType expectedSubscriptionType, AskSubscriptionType expectedAskSubscriptionType) {
+
+ RosterItem item = prepareItem(initialSubscriptionType, initialAskSubscriptionType);
+
+ // add parameterSubscriptionType
+ RosterSubscriptionMutator.RosterSubscriptionMutatorResult subscriptionMutatorResult = new RosterSubscriptionMutator().add(item, parameterSubscriptionType);
+
+ checkResult(initialSubscriptionType, initialAskSubscriptionType, expectedResult, expectedSubscriptionType, expectedAskSubscriptionType, item, subscriptionMutatorResult);
+ }
+
+ private void checkAdd(SubscriptionType initialSubscriptionType, AskSubscriptionType initialAskSubscriptionType,
+ AskSubscriptionType parameterAskSubscriptionType,
+ RosterSubscriptionMutator.RosterSubscriptionMutatorResult expectedResult,
+ SubscriptionType expectedSubscriptionType, AskSubscriptionType expectedAskSubscriptionType) {
+
+ RosterItem item = prepareItem(initialSubscriptionType, initialAskSubscriptionType);
+
+ // add parameterSubscriptionType
+ RosterSubscriptionMutator.RosterSubscriptionMutatorResult subscriptionMutatorResult = new RosterSubscriptionMutator().add(item, parameterAskSubscriptionType);
+
+ checkResult(initialSubscriptionType, initialAskSubscriptionType, expectedResult, expectedSubscriptionType, expectedAskSubscriptionType, item, subscriptionMutatorResult);
+ }
+
+ private void checkResult(SubscriptionType initialSubscriptionType, AskSubscriptionType initialAskSubscriptionType, RosterSubscriptionMutator.RosterSubscriptionMutatorResult expectedResult, SubscriptionType expectedSubscriptionType, AskSubscriptionType expectedAskSubscriptionType, RosterItem item, RosterSubscriptionMutator.RosterSubscriptionMutatorResult subscriptionMutatorResult) {
+ assertEquals(expectedResult, subscriptionMutatorResult);
+
+ if (expectedSubscriptionType == null && expectedAskSubscriptionType == null) {
+ assertEquals(initialSubscriptionType, item.getSubscriptionType());
+ assertEquals(initialAskSubscriptionType, item.getAskSubscriptionType());
+ } else {
+ assertEquals(expectedSubscriptionType, item.getSubscriptionType());
+ assertEquals(expectedAskSubscriptionType, item.getAskSubscriptionType());
+ }
+ }
+
+ private RosterItem prepareItem(SubscriptionType initialSubscriptionType, AskSubscriptionType initialAskSubscriptionType) {
+ EntityImpl jid = null;
+ try {
+ jid = EntityImpl.parse("test@test.org");
+ } catch (EntityFormatException e) {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ RosterItem item = new RosterItem(jid, "group", initialSubscriptionType, initialAskSubscriptionType);
+ return item;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org