Author: scottbw
Date: Sat Feb  8 17:11:24 2014
New Revision: 1566052

URL: http://svn.apache.org/r1566052
Log:
Added an SPI and default implementation for shared context information 
(participants and shared data for collaborative widgets)

Added:
    
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/SharedContextService.java
    
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultParticipantImpl.java
    
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedContextService.java
    
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedDataImpl.java
    
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/SharedContext.java
    wookie/trunk/wookie-services/wookie-spi/src/test/
    wookie/trunk/wookie-services/wookie-spi/src/test/java/
    wookie/trunk/wookie-services/wookie-spi/src/test/java/org/
    wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/
    wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/
    
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/
    
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/
    
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/DefaultSharedContextServiceTest.java

Added: 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/SharedContextService.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/SharedContextService.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/SharedContextService.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/SharedContextService.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,144 @@
+/*
+ *  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.wookie.services;
+
+import java.util.ServiceLoader;
+
+import org.apache.wookie.beans.IParticipant;
+import org.apache.wookie.beans.ISharedData;
+import org.apache.wookie.services.impl.DefaultSharedContextService;
+
+public interface SharedContextService {
+
+       /**
+        * get the shared data belonging to the shared context
+        * @return an array of SharedData instances, or null if no shared data 
exists
+        */
+       public abstract ISharedData[] getSharedData(String apiKey, String 
widgetId, String contextId);
+
+       /**
+        * Find a specific shared data object for a given session and object key
+        * @param instance the widget instance
+        * @param key the key of the shared data object, i.e. the tuple key not 
the shared data key
+        * @return a SharedData object, or null if no matches are found
+        */
+       public abstract ISharedData getSharedData(String apiKey, String 
widgetId, String contextId, String key);
+
+       /**
+        * Remove a single Shared Data Object
+        * @param name the name (key) of the data object
+        * @return true if a shared data object was located and deleted, false 
if no match was found
+        */
+       public abstract boolean removeSharedData(String apiKey, String 
widgetId, String contextId, String name);
+
+       /**
+        * Update a single Shared Data object
+        * @param name the name (key) of the data object
+        * @param value the value to set, or null to clear the entry
+        * @param append set to true to append the value to the current value
+        * @return true if the value was updated, false if a new object was 
created
+        */
+       public abstract boolean updateSharedData(String apiKey, String 
widgetId, String contextId, String name, String value,
+                       boolean append);
+
+       /**
+        * get the participants belonging to this shared context
+        * @return an arry of Participant instances, or null if there are no 
participants
+        */
+       public abstract IParticipant[] getParticipants(String apiKey, String 
widgetId, String contextId);
+
+       /**
+        * Add a participant to a shared context
+        * @param participantId the id property of the participant to add
+        * @param participantDisplayName the display name property of the 
participant to add
+        * @param participantThumbnailUrl the thumbnail url property of the 
participant to add
+        * @return true if the participant was successfully added, otherwise 
false
+        */
+       public abstract boolean addParticipant(String apiKey, String widgetId, 
String contextId, String participantId,
+                       String participantDisplayName, String 
participantThumbnailUrl);
+
+       /**
+        * Add a participant to a shared context
+        * @param participantId the id property of the participant to add
+        * @param participantDisplayName the display name property of the 
participant to add
+        * @param participantThumbnailUrl the thumbnail url property of the 
participant to add
+        * @return true if the participant was successfully added, otherwise 
false
+        */
+       public abstract boolean addParticipant(String apiKey, String widgetId, 
String contextId, String participantId,
+                       String participantDisplayName, String 
participantThumbnailUrl,
+                       String role);
+
+       /**
+        * Remove a participant from the shared context
+        * @param participant
+        */
+       public abstract void removeParticipant(String apiKey, String widgetId, 
String contextId, IParticipant participant);
+
+       /**
+        * Removes a participant from a widget instance
+        * @param participantId the id property of the participant
+        * @return true if the participant is successfully removed, otherwise 
false
+        */
+       public abstract boolean removeParticipant(String apiKey, String 
widgetId, String contextId, String participantId);
+
+       /**
+        * Get a participant by ID from this shared context
+        * @param participantId
+        * @return the participant, or null if there is no participant with the 
given id in the context
+        */
+       public abstract IParticipant getParticipant(String apiKey, String 
widgetId, String contextId, String participantId);
+
+       /**
+        * Get the participant associated with the widget instance as the viewer
+        * @param widgetInstance
+        * @return the IParticipant representing the viewer, or null if no 
match is found
+        */
+       public abstract IParticipant getViewer(String apiKey, String widgetId, 
String contextId, String viewerId);
+
+       /**
+        * Get the participant designated as the host of the shared context. 
Note that
+        * if there are multiple hosts, only the first is returned.
+        * @return a participant designated the host, or null if no participant 
is host
+        */
+       public abstract IParticipant getHost(String apiKey, String widgetId, 
String contextId);
+
+       public abstract IParticipant[] getHosts(String apiKey, String widgetId, 
String contextId);
+       
+       public static class Factory {
+               
+               private static SharedContextService provider;
+               
+           public static SharedContextService getInstance() {
+               //
+               // Use the service loader to load an implementation if one is 
available
+               //
+               if (provider == null){
+                       ServiceLoader<SharedContextService> ldr = 
ServiceLoader.load(SharedContextService.class);
+                       for (SharedContextService service : ldr) {
+                               // We are only expecting one
+                               provider = service;
+                       }
+               }
+               //
+               // If no service provider is found, use the default
+               //
+               if (provider == null){
+                       provider = new DefaultSharedContextService();
+               }
+               
+               return provider;
+           }
+       }
+
+}
\ No newline at end of file

Added: 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultParticipantImpl.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultParticipantImpl.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultParticipantImpl.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultParticipantImpl.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,93 @@
+/*
+ *  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.wookie.services.impl;
+
+import org.apache.wookie.beans.IParticipant;
+
+public class DefaultParticipantImpl implements IParticipant{
+
+       private String participantId;
+       private String participantName;
+       private String participantThumbnailUrl;
+       private String role;
+       
+       
+       public DefaultParticipantImpl(String id, String name, String src, 
String role){
+               this.participantId = id;
+               this.participantName = name;
+               this.participantThumbnailUrl = src;
+               this.role = role;
+       }
+       
+       /**
+        * @return the participantId
+        */
+       public String getParticipantId() {
+               return participantId;
+       }
+       /**
+        * @param participantId the participantId to set
+        */
+       public void setParticipantId(String participantId) {
+               this.participantId = participantId;
+       }
+       /**
+        * @return the participantName
+        */
+       public String getParticipantDisplayName() {
+               return participantName;
+       }
+       /**
+        * @param participantName the participantName to set
+        */
+       public void setParticipantDisplayName(String participantName) {
+               this.participantName = participantName;
+       }
+       /**
+        * @return the participantThumbnailUrl
+        */
+       public String getParticipantThumbnailUrl() {
+               return participantThumbnailUrl;
+       }
+       /**
+        * @param participantThumbnailUrl the participantThumbnailUrl to set
+        */
+       public void setParticipantThumbnailUrl(String participantThumbnailUrl) {
+               this.participantThumbnailUrl = participantThumbnailUrl;
+       }
+       /**
+        * @return the role
+        */
+       public String getRole() {
+               return role;
+       }
+       /**
+        * @param role the role to set
+        */
+       public void setRole(String role) {
+               this.role = role;
+       }
+       @Override
+       public Object getId() {
+               return null;
+       }
+       @Override
+       public String getSharedDataKey() {
+               return null;
+       }
+       @Override
+       public void setSharedDataKey(String sharedDataKey) {            
+       }
+
+}

Added: 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedContextService.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedContextService.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedContextService.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedContextService.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,214 @@
+/*
+ *  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.wookie.services.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashMap;
+
+import javax.swing.tree.DefaultMutableTreeNode;
+
+import org.apache.wookie.beans.IParticipant;
+import org.apache.wookie.beans.ISharedData;
+import org.apache.wookie.services.SharedContextService;
+
+public class DefaultSharedContextService implements SharedContextService {
+       
+       //
+       // Tree structure APIKEY->CONTEXT->WIDGET->SharedContext
+       //
+       
+       DefaultMutableTreeNode root;
+       
+       private DefaultMutableTreeNode getSharedContext(String apiKey, String 
contextId, String widgetId){
+               
+               DefaultMutableTreeNode apiKeyNode = null;
+               
+               Enumeration apiKeys = root.children();
+               while(apiKeys.hasMoreElements()){
+                       DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
apiKeys.nextElement();
+                       if (node.getUserObject().equals(apiKey)){
+                               apiKeyNode = node;
+                       }
+               }
+               if (apiKeyNode == null){
+                       apiKeyNode = new DefaultMutableTreeNode(apiKey); 
+                       root.add(apiKeyNode);
+               }
+               
+               //
+               // Context level
+               //
+               DefaultMutableTreeNode contextNode = null;
+               Enumeration contexts = apiKeyNode.children();
+               while(contexts.hasMoreElements()){
+                       DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
contexts.nextElement();
+                       if (node.getUserObject().equals(contextId)){
+                               contextNode = node;
+                       }
+               }
+               if (contextNode == null){
+                       contextNode = new DefaultMutableTreeNode(contextId);
+                       apiKeyNode.add(contextNode);
+               }
+               
+               //
+               // Widget level
+               //
+               DefaultMutableTreeNode widgetNode = null;
+               Enumeration widgets = contextNode.children();
+               while(widgets.hasMoreElements()){
+                       DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
widgets.nextElement();
+                       if (node.getUserObject().equals(widgetId)){
+                               widgetNode = node;
+                       } else {
+                               System.out.println(node.getUserObject());
+                       }
+               }
+               if (widgetNode == null){
+                       widgetNode = new DefaultMutableTreeNode(widgetId);
+                       contextNode.add(widgetNode);
+                       
+                       //
+                       // Add participants and shared data
+                       //
+                       widgetNode.add(new DefaultMutableTreeNode(new 
SharedContext()));
+               }
+               contextNode.add(widgetNode);
+               return widgetNode;
+       }
+       
+       private SharedContext getSharedContextFromTree(String apiKey, String 
contextId, String widgetId){
+               DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
getSharedContext(apiKey, contextId, widgetId).getFirstChild();
+               return (SharedContext) node.getUserObject();
+       }
+       
+       public DefaultSharedContextService(){
+               root = new DefaultMutableTreeNode();
+       }
+
+       @Override
+       public ISharedData[] getSharedData(String apiKey, String widgetId,
+                       String contextId){
+               Collection<ISharedData> sharedData = 
getSharedContextFromTree(apiKey,contextId, widgetId).getSharedData().values();
+               return sharedData.toArray(new ISharedData[sharedData.size()]);
+       }
+
+       @Override
+       public ISharedData getSharedData(String apiKey, String widgetId,
+                       String contextId, String key) {
+               HashMap<String, ISharedData> sharedData = 
getSharedContextFromTree(apiKey,contextId, widgetId).getSharedData();
+               return sharedData.get(key);
+       }
+
+       @Override
+       public boolean removeSharedData(String apiKey, String widgetId,
+                       String contextId, String key) {
+               HashMap<String, ISharedData> sharedData = 
getSharedContextFromTree(apiKey,contextId, widgetId).getSharedData();
+               if (!sharedData.containsKey(key)) return false;
+               sharedData.remove(key);
+               return true;
+       }
+
+       @Override
+       public boolean updateSharedData(String apiKey, String widgetId,
+                       String contextId, String key, String value, boolean 
append) {   
+               HashMap<String, ISharedData> sharedData = 
getSharedContextFromTree(apiKey,contextId, widgetId).getSharedData();
+               boolean existing = true;
+               if (!sharedData.containsKey(key)) existing = false;
+               sharedData.put(key, new DefaultSharedDataImpl(key,value));
+               return existing;
+       }
+
+       @Override
+       public IParticipant[] getParticipants(String apiKey, String widgetId,
+                       String contextId) {
+               Collection<IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants().values();
+               return participants.toArray(new 
IParticipant[participants.size()]);
+       }
+
+       @Override
+       public boolean addParticipant(String apiKey, String widgetId,
+                       String contextId, String participantId,
+                       String participantDisplayName, String 
participantThumbnailUrl) {
+               return addParticipant(apiKey, widgetId, contextId, 
participantId, participantDisplayName, participantThumbnailUrl, "VIEWER");
+       }
+
+       @Override
+       public boolean addParticipant(String apiKey, String widgetId,
+                       String contextId, String participantId,
+                       String participantDisplayName, String 
participantThumbnailUrl,
+                       String role) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               
+               if (participants.containsKey(participantId)) return false;
+               participants.put(participantId, new 
DefaultParticipantImpl(participantId,participantDisplayName, 
participantThumbnailUrl, role));
+               return true;
+       }
+
+       @Override
+       public void removeParticipant(String apiKey, String widgetId,
+                       String contextId, IParticipant participant) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               participants.remove(participant.getParticipantId());
+       }
+
+       @Override
+       public boolean removeParticipant(String apiKey, String widgetId,
+                       String contextId, String participantId) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               if (!participants.containsKey(participantId)) return false;
+               participants.remove(participantId);
+               return true;
+       }
+
+       @Override
+       public IParticipant getParticipant(String apiKey, String widgetId,
+                       String contextId, String participantId) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               return participants.get(participantId);
+       }
+
+       @Override
+       public IParticipant getViewer(String apiKey, String widgetId,
+                       String contextId, String viewerId) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               return participants.get(viewerId);
+       }
+
+       @Override
+       public IParticipant getHost(String apiKey, String widgetId, String 
contextId) {
+               IParticipant[] hosts = getHosts(apiKey, widgetId, contextId);
+               if (hosts != null && hosts.length > 0){
+                       return hosts[0];
+               } else {
+                       return null;
+               }
+       }
+
+       @Override
+       public IParticipant[] getHosts(String apiKey, String widgetId,
+                       String contextId) {
+               HashMap<String, IParticipant> participants = 
getSharedContextFromTree(apiKey,contextId, widgetId).getParticipants();
+               ArrayList<IParticipant> hosts = new ArrayList<IParticipant>();
+               for (IParticipant participant: participants.values()){
+                       if 
(participant.getRole().equals(IParticipant.HOST_ROLE)){
+                               hosts.add(participant);
+                       }
+               }
+               return hosts.toArray(new IParticipant[hosts.size()]);
+       }
+
+}

Added: 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedDataImpl.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedDataImpl.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedDataImpl.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/DefaultSharedDataImpl.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,61 @@
+/*
+ *  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.wookie.services.impl;
+
+import org.apache.wookie.beans.ISharedData;
+
+public class DefaultSharedDataImpl implements ISharedData {
+       
+       private String key;
+       private String value;
+       
+       public DefaultSharedDataImpl(String key, String value){
+               this.key = key;
+               this.value = value;
+       }
+
+       @Override
+       public Object getId() {
+               return null;
+       }
+
+       @Override
+       public String getDkey() {
+               return key;
+       }
+
+       @Override
+       public void setDkey(String key) {
+               this.key = key;
+       }
+
+       @Override
+       public String getDvalue() {
+               return value;
+       }
+
+       @Override
+       public void setDvalue(String value) {
+               this.value = value;
+       }
+
+       @Override
+       public String getSharedDataKey() {
+               return null;
+       }
+
+       @Override
+       public void setSharedDataKey(String sharedDataKey) {            
+       }
+}

Added: 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/SharedContext.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/SharedContext.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/SharedContext.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/main/java/org/apache/wookie/services/impl/SharedContext.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,46 @@
+/*
+ *  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.wookie.services.impl;
+
+import java.util.HashMap;
+
+import org.apache.wookie.beans.IParticipant;
+import org.apache.wookie.beans.ISharedData;
+
+public class SharedContext {
+
+       private HashMap<String, IParticipant> participants;
+       
+       private HashMap<String, ISharedData> sharedData;
+       
+       public SharedContext(){
+               participants = new HashMap<String, IParticipant>();
+               sharedData = new HashMap<String, ISharedData>();
+       }
+
+       /**
+        * @return the participants
+        */
+       public HashMap<String, IParticipant> getParticipants() {
+               return participants;
+       }
+
+       /**
+        * @return the sharedData
+        */
+       public HashMap<String, ISharedData> getSharedData() {
+               return sharedData;
+       }
+
+}

Added: 
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/DefaultSharedContextServiceTest.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/DefaultSharedContextServiceTest.java?rev=1566052&view=auto
==============================================================================
--- 
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/DefaultSharedContextServiceTest.java
 (added)
+++ 
wookie/trunk/wookie-services/wookie-spi/src/test/java/org/apache/wookie/services/impl/DefaultSharedContextServiceTest.java
 Sat Feb  8 17:11:24 2014
@@ -0,0 +1,55 @@
+/*
+ *  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.wookie.services.impl;
+
+import static org.junit.Assert.*;
+
+import org.apache.wookie.beans.IParticipant;
+import org.apache.wookie.services.SharedContextService;
+import org.junit.Test;
+
+public class DefaultSharedContextServiceTest {
+       
+       private static final String API_KEY = "TEST";
+       private static final String WIDGET_ID = "http://TEST_WIDGET";;
+       private static final String CONTEXT_ID = "TEST_CONTEXT";
+       
+       
+       @Test
+       public void addAndRemoveParticipant(){
+               
SharedContextService.Factory.getInstance().addParticipant(API_KEY, WIDGET_ID, 
CONTEXT_ID, "bob", "Bob", "http://test/img";);
+               
+               IParticipant result = 
SharedContextService.Factory.getInstance().getParticipant(API_KEY, WIDGET_ID, 
CONTEXT_ID, "bob");
+               
+               assertNotNull(result);
+               assertEquals("bob", result.getParticipantId());
+               
+               
SharedContextService.Factory.getInstance().removeParticipant(API_KEY, 
WIDGET_ID, CONTEXT_ID, "bob");
+               result = 
SharedContextService.Factory.getInstance().getParticipant(API_KEY, WIDGET_ID, 
CONTEXT_ID, "bob");
+               
+               assertNull(result);
+
+       }
+       
+       @Test
+       public void addDuplicateParticipant(){
+               boolean result = 
SharedContextService.Factory.getInstance().addParticipant(API_KEY, WIDGET_ID, 
CONTEXT_ID, "bob", "Bob", "http://test/img";);            
+               assertTrue(result);
+               result = 
SharedContextService.Factory.getInstance().addParticipant(API_KEY, WIDGET_ID, 
CONTEXT_ID, "bob", "Bob", "http://test/img";);            
+               assertFalse(result);
+               
SharedContextService.Factory.getInstance().removeParticipant(API_KEY, 
WIDGET_ID, CONTEXT_ID, "bob");
+       }
+
+
+}


Reply via email to