You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ms...@apache.org on 2010/05/23 08:23:16 UTC
svn commit: r947367 - in
/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src:
main/java/org/apache/cxf/dosgi/discovery/zookeeper/
test/java/org/apache/cxf/dosgi/discovery/zookeeper/
Author: mschaaf
Date: Sun May 23 06:23:15 2010
New Revision: 947367
URL: http://svn.apache.org/viewvc?rev=947367&view=rev
Log:
- Added many new test cases to the zookeeper discovery
- Removed some of the old incative testcases from the old implementation
- did some minor fixes and code cleanup
- added a mechanism for the detection of the owm EndpointListener independent of the corresponding bundle
- changed the InterfaceDataMonitorListenerImpl so that it always notifies an EndpointListener with the *first* matching scope
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerImplTest.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizerTest.java
Removed:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveredServiceNotificationImplTest.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactory.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizer.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactoryTest.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactory.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactory.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactory.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactory.java Sun May 23 06:23:15 2010
@@ -21,7 +21,6 @@ package org.apache.cxf.dosgi.discovery.z
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
-import java.util.UUID;
import java.util.logging.Logger;
import org.osgi.framework.Bundle;
@@ -34,6 +33,8 @@ import org.osgi.service.remoteserviceadm
public class EndpointListenerFactory implements ServiceFactory {
+ public static final String DISCOVERY_ZOOKEEPER_ID = "org.apache.cxf.dosgi.discovery.zookeeper";
+
private Logger LOG = Logger.getLogger(EndpointListenerFactory.class.getName());
private BundleContext bctx;
private ZooKeeperDiscovery discovery;
@@ -52,7 +53,6 @@ public class EndpointListenerFactory imp
listeners.add(epl);
return epl;
}
-
}
public void ungetService(Bundle b, ServiceRegistration sr, Object s) {
@@ -63,9 +63,7 @@ public class EndpointListenerFactory imp
epl.close();
listeners.remove(epl);
}
-
}
-
}
public synchronized void start() {
@@ -75,22 +73,11 @@ public class EndpointListenerFactory imp
private void updateServiceRegistration() {
Properties props = new Properties();
- props.put(EndpointListener.ENDPOINT_LISTENER_SCOPE, "(&(" + Constants.OBJECTCLASS + "=*)("+RemoteConstants.ENDPOINT_FRAMEWORK_UUID+"="+getUUID(bctx)+"))");
+ props.put(EndpointListener.ENDPOINT_LISTENER_SCOPE, "(&(" + Constants.OBJECTCLASS + "=*)("+RemoteConstants.ENDPOINT_FRAMEWORK_UUID+"="+Util.getUUID(bctx)+"))");
+ props.put(DISCOVERY_ZOOKEEPER_ID, "true");
serviceRegistartion.setProperties(props);
}
- // copied from the DSW OSGiUtils class
- public static String getUUID(BundleContext bc) {
- synchronized ("org.osgi.framework.uuid") {
- String uuid = bc.getProperty("org.osgi.framework.uuid");
- if (uuid == null) {
- uuid = UUID.randomUUID().toString();
- System.setProperty("org.osgi.framework.uuid", uuid);
- }
- return uuid;
- }
- }
-
public synchronized void stop() {
if (serviceRegistartion != null)
serviceRegistartion.unregister();
@@ -100,4 +87,11 @@ public class EndpointListenerFactory imp
}
}
+ /**
+ * only for the test case !
+ */
+ protected List<EndpointListenerImpl> getListeners(){
+ return listeners;
+ }
+
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizer.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizer.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizer.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizer.java Sun May 23 06:23:15 2010
@@ -72,6 +72,11 @@ public class EndpointListenerTrackerCust
LOG.finest("modifiedService: property: " + key + " => " + sref.getProperty(key));
}
+ if("true".equals(sref.getProperty(EndpointListenerFactory.DISCOVERY_ZOOKEEPER_ID))){
+ LOG.finest("found my own endpointListener ... skipping it");
+ return;
+ }
+
String[] scopes = Util.getScopes(sref);
LOG.info("trying to discover services for scopes[" + scopes.length + "]: ");
@@ -108,8 +113,7 @@ public class EndpointListenerTrackerCust
interest.im = null;
}
- InterfaceMonitor dm = new InterfaceMonitor(zooKeeperDiscovery.getZookeeper(),
- objClass, interest, scope, bctx);
+ InterfaceMonitor dm = createInterfaceMonitor(scope, objClass, interest);
dm.start();
interest.im = dm;
@@ -129,6 +133,7 @@ public class EndpointListenerTrackerCust
}
}
+
private String getObjectClass(String scope) {
Matcher m = OBJECTCLASS_PATTERN.matcher(scope);
if (m.matches())
@@ -158,7 +163,27 @@ public class EndpointListenerTrackerCust
-// public void discoveredEndpont(EndpointDescription epd) {
-// LOG.info("Endpoint Discovered: " + epd.getProperties());
-// }
+ /**
+ * Only for test case !
+ * */
+ protected Map<String, Interest> getInterestingScopes() {
+ return interestingScopes;
+ }
+
+ /**
+ * Only for test case !
+ * */
+ protected Map<ServiceReference, List<String>> getHandledEndpointlisteners() {
+ return handledEndpointlisteners;
+ }
+
+
+ /**
+ * Only for test case !
+ * */
+ protected InterfaceMonitor createInterfaceMonitor(String scope, String objClass, Interest interest) {
+ InterfaceMonitor dm = new InterfaceMonitor(zooKeeperDiscovery.getZookeeper(),
+ objClass, interest, scope, bctx);
+ return dm;
+ }
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java Sun May 23 06:23:15 2010
@@ -36,6 +36,7 @@ import org.jdom.Element;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.EndpointListener;
@@ -49,6 +50,7 @@ public class InterfaceDataMonitorListene
final EndpointListenerTrackerCustomizer.Interest discoveredServiceTracker;
final String scope;
final boolean recursive;
+ final InterfaceMonitor parent;
private final BundleContext bctx;
@@ -57,7 +59,8 @@ public class InterfaceDataMonitorListene
public InterfaceDataMonitorListenerImpl(ZooKeeper zk, String intf,
EndpointListenerTrackerCustomizer.Interest dst, String scope,
- BundleContext bc) {
+ BundleContext bc, InterfaceMonitor interfaceMonitor) {
+ parent = interfaceMonitor;
zookeeper = zk;
znode = Util.getZooKeeperPath(intf);
if (intf == null || "".equals(intf))
@@ -68,9 +71,8 @@ public class InterfaceDataMonitorListene
discoveredServiceTracker = dst;
bctx = bc;
this.scope = scope;
-
-
- LOG.fine("InterfaceDataMonitorListenerImpl is recursive: "+recursive);
+
+ LOG.fine("InterfaceDataMonitorListenerImpl is recursive: " + recursive);
}
public synchronized void change() {
@@ -80,52 +82,101 @@ public class InterfaceDataMonitorListene
processChildren(znode, newNodes, prevNodes);
+ LOG.fine("processChildren done nodes that are missing now and need to be removed: "
+ + prevNodes.values());
+
for (Map<String, Object> props : prevNodes.values()) {
// whatever's left in prevNodes now has been removed from Discovery
EndpointDescription epd = new EndpointDescription(props);
+ //notifyListeners(epd, true);
+
for (ServiceReference sref : discoveredServiceTracker.relatedServiceListeners) {
if (bctx.getService(sref) instanceof EndpointListener) {
EndpointListener epl = (EndpointListener)bctx.getService(sref);
- LOG.info("calling EndpointListener endpointRemoved: " + epl + "from bundle "
- + sref.getBundle().getSymbolicName());
- epl.endpointRemoved(epd, scope);
+
+ // return the >first< matching scope of the listener
+ // TODO: this code also exists for the endpoint adding in the processChild() method ->
+ // refactor !
+ String[] scopes = Util.getScopes(sref);
+ for (final String currentScope : scopes) {
+ LOG.fine("matching " + epd + " against " + currentScope);
+ Filter f = null;
+ try {
+ f = FrameworkUtil.createFilter(currentScope);
+
+ Dictionary d = new Properties();
+ Set<Map.Entry<String, Object>> entries = props.entrySet();
+ for (Map.Entry<String, Object> entry : entries) {
+ d.put(entry.getKey(), entry.getValue());
+ }
+
+ if (f.match(d)) {
+ LOG.fine("MATCHED " + epd + "against " + currentScope);
+ LOG.info("calling EndpointListener endpointRemoved: " + epl + "from bundle "
+ + sref.getBundle().getSymbolicName() + " for endpoint: " + epd);
+
+ epl.endpointRemoved(epd, currentScope);
+ break;
+ }
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+ }
+ }
}
}
- }
+
+ }
nodes = newNodes;
}
- private void processChildren(String znode, Map<String, Map<String, Object>> newNodes,
- Map<String, Map<String, Object>> prevNodes) {
+ /**
+ * iterates through all child nodes of the given node and tries to find endpoints. If the recursive flag
+ * is set it also traverses into the child nodes.
+ *
+ * @return true if an endpoint was found and if the node therefore needs to be monitored for changes
+ */
+ private boolean processChildren(String znode, Map<String, Map<String, Object>> newNodes,
+ Map<String, Map<String, Object>> prevNodes) {
List<String> children;
try {
LOG.info("Processing the children of " + znode);
children = zookeeper.getChildren(znode, false);
+ boolean foundANode = false;
for (String child : children) {
Map<String, Object> p = processChild(znode, child, prevNodes.get(child));
if (p != null) {
+ LOG.fine("found new node " + znode + "/[" + child + "] ( []->child ) props: "
+ + p.values());
newNodes.put(child, p);
prevNodes.remove(child);
+ foundANode = true;
}
if (recursive) {
String newNode = znode + '/' + child;
- processChildren(newNode, newNodes, prevNodes);
+ if (processChildren(newNode, newNodes, prevNodes))
+ zookeeper.getChildren(newNode, parent);
}
}
+ return foundANode;
} catch (KeeperException e) {
LOG.log(Level.SEVERE, "Problem processing Zookeeper node: " + e.getMessage(), e);
} catch (InterruptedException e) {
LOG.log(Level.SEVERE, "Problem processing Zookeeper node: " + e.getMessage(), e);
}
-
+ return false;
}
+ /**
+ * Scan the node data for Endpoint information and publish it to the related service listeners
+ *
+ * @return the properties of the endpoint found in the node or null if no endpoint was found
+ */
private Map<String, Object> processChild(String znode, String child, Map<String, Object> prevVal) {
String node = znode + '/' + child;
@@ -133,7 +184,6 @@ public class InterfaceDataMonitorListene
try {
Stat s = zookeeper.exists(node, false);
if (s.getDataLength() <= 0) {
- //LOG.info(node + " does not contain any discovery data");
return null;
}
byte[] data = zookeeper.getData(node, false, null);
@@ -152,45 +202,10 @@ public class InterfaceDataMonitorListene
if (prevVal == null) {
// This guy is new
+ notifyListeners(epd, false);
- for (ServiceReference sref : discoveredServiceTracker.relatedServiceListeners) {
- if (bctx.getService(sref) instanceof EndpointListener) {
- EndpointListener epl = (EndpointListener)bctx.getService(sref);
-
- // return the >first< matching scope of the listener
- String[] scopes = Util.getScopes(sref);
- for (String currentScope : scopes) {
- LOG.fine("matching " + epd + " against "+currentScope);
- Filter f = FrameworkUtil.createFilter(currentScope);
-
- Dictionary d = new Properties();
- Map<String, Object> props = epd.getProperties();
- Set<Map.Entry<String, Object>> entries = props.entrySet();
- for (Map.Entry<String, Object> entry : entries) {
- d.put(entry.getKey(), entry.getValue());
- }
-
- if(f.match(d)){
- LOG.fine("MATCHED " + epd + "against "+currentScope);
- LOG.info("calling EndpointListener; " + epl + " from bundle "
- + sref.getBundle().getSymbolicName() + " based on scope ["+currentScope+"]");
- epl.endpointAdded(epd, currentScope);
- break;
- }
- }
- }
- }
} else if (!prevVal.equals(epd.getProperties())) {
- // There's been a modification
- // ServiceEndpointDescriptionImpl sed = new
- // ServiceEndpointDescriptionImpl(Collections.singletonList(interFace), m);
- // DiscoveredServiceNotification dsn = new
- // DiscoveredServiceNotificationImpl(Collections.emptyList(),
- // Collections.singleton(interFace), DiscoveredServiceNotification.MODIFIED, sed);
- // discoveredServiceTracker.serviceChanged(dsn);
-
// TODO
-
}
return epd.getProperties();
@@ -206,4 +221,51 @@ public class InterfaceDataMonitorListene
// TODO Auto-generated method stub
}
+
+ private void notifyListeners(EndpointDescription epd, boolean isRemoval) {
+
+ System.out.println("**************** notifyListeners("+epd+" , "+isRemoval+")");
+
+ for (ServiceReference sref : discoveredServiceTracker.relatedServiceListeners) {
+ if (bctx.getService(sref) instanceof EndpointListener) {
+ final EndpointListener epl = (EndpointListener)bctx.getService(sref);
+
+ String[] scopes = Util.getScopes(sref);
+ for (final String currentScope : scopes) {
+ Filter f;
+ try {
+ f = FrameworkUtil.createFilter(currentScope);
+
+ Dictionary d = new Properties();
+ Map<String, Object> props = epd.getProperties();
+ Set<Map.Entry<String, Object>> entries = props.entrySet();
+ for (Map.Entry<String, Object> entry : entries) {
+ d.put(entry.getKey(), entry.getValue());
+ }
+
+ LOG.fine("matching " + epd + " against " + currentScope);
+
+ if (f.match(d)) {
+ LOG.fine("MATCHED " + epd + "against " + currentScope);
+
+ LOG.info("scheduling EndpointListener call for listener ; " + epl
+ + " from bundle " + sref.getBundle().getSymbolicName()
+ + " based on scope [" + currentScope + "]");
+
+ if (isRemoval)
+ epl.endpointRemoved(epd, currentScope);
+ else
+ epl.endpointAdded(epd, currentScope);
+
+ break;
+ }
+ } catch (InvalidSyntaxException e) {
+ LOG.warning("skipping scope [" + currentScope
+ + "] of endpoint listener from bundle "+sref.getBundle().getSymbolicName()+" becaue it is invalid: " + e.getMessage());
+ }
+ }
+
+ }
+ }
+ }
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java Sun May 23 06:23:15 2010
@@ -41,7 +41,7 @@ public class InterfaceMonitor implements
public InterfaceMonitor(ZooKeeper zk, String intf, EndpointListenerTrackerCustomizer.Interest zkd, String scope, BundleContext bctx) {
LOG.fine("Creating new InterfaceMonitor for scope ["+scope+"] and objectClass ["+intf+"] ");
- listener = new InterfaceDataMonitorListenerImpl(zk, intf, zkd,scope,bctx);
+ listener = createInterfaceDataMonitorListener(zk, intf, zkd, scope, bctx);
zookeeper = zk;
znode = Util.getZooKeeperPath(intf);
}
@@ -51,7 +51,7 @@ public class InterfaceMonitor implements
}
private void process() {
- LOG.finest("Kicking off a zookeeper.exists() on node: " + znode);
+ LOG.finest("registering a zookeeper.exists(" + znode+") callback");
zookeeper.exists(znode, this, this, null);
}
@@ -61,7 +61,8 @@ public class InterfaceMonitor implements
}
public void processResult(int rc, String path, Object ctx, Stat stat) {
- LOG.finer("ZooKeeper callback on node: " + znode + " code: " + rc);
+
+ LOG.finer("ZooKeeper callback on node: " + znode + " code: " + rc );
switch (rc) {
case Code.Ok:
@@ -91,8 +92,10 @@ public class InterfaceMonitor implements
try {
if (zookeeper.exists(znode, false) != null) {
- listener.change();
zookeeper.getChildren(znode, this);
+ listener.change();
+ }else{
+ LOG.fine(znode+" doesn't exist -> not processing any changes");
}
} catch (Exception ke) {
LOG.log(Level.SEVERE, "Error getting ZooKeeper data.", ke);
@@ -107,4 +110,14 @@ public class InterfaceMonitor implements
// TODO !!!
closed = true;
}
+
+ /**
+ * Only for thest case
+ * @return
+ * */
+ protected InterfaceDataMonitorListenerImpl createInterfaceDataMonitorListener(ZooKeeper zk, String intf,
+ EndpointListenerTrackerCustomizer.Interest zkd,
+ String scope, BundleContext bctx) {
+ return new InterfaceDataMonitorListenerImpl(zk, intf, zkd,scope,bctx,this);
+ }
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java Sun May 23 06:23:15 2010
@@ -22,7 +22,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.UUID;
+import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointListener;
@@ -81,10 +83,22 @@ public class Util {
String[] scopes = Util.getStringPlusProperty(sref.getProperty(EndpointListener.ENDPOINT_LISTENER_SCOPE));
ArrayList<String> normalizedScopes = new ArrayList<String>(scopes.length);
for (String scope : scopes) {
- if(scope!=null || "".equals(scope))
+ if(scope!=null && !"".equals(scope))
normalizedScopes.add(scope);
}
return normalizedScopes.toArray(new String[normalizedScopes.size()]);
}
+
+ // copied from the DSW OSGiUtils class
+ public static String getUUID(BundleContext bc) {
+ synchronized ("org.osgi.framework.uuid") {
+ String uuid = bc.getProperty("org.osgi.framework.uuid");
+ if (uuid == null) {
+ uuid = UUID.randomUUID().toString();
+ System.setProperty("org.osgi.framework.uuid", uuid);
+ }
+ return uuid;
+ }
+ }
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactoryTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactoryTest.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactoryTest.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerFactoryTest.java Sun May 23 06:23:15 2010
@@ -18,6 +18,7 @@
*/
package org.apache.cxf.dosgi.discovery.zookeeper;
+import java.util.List;
import java.util.Properties;
import junit.framework.TestCase;
@@ -84,4 +85,68 @@ public class EndpointListenerFactoryTest
}
+ public void testServiceFactory(){
+ IMocksControl c = EasyMock.createNiceControl();
+
+ BundleContext ctx = c.createMock(BundleContext.class);
+ ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);
+ ServiceRegistration sreg = c.createMock(ServiceRegistration.class);
+
+ EndpointListenerFactory eplf = new EndpointListenerFactory(zkd, ctx);
+
+ EasyMock.expect(
+ ctx.registerService(EasyMock.eq(EndpointListener.class.getName()), EasyMock.eq(eplf),
+ (Properties)EasyMock.anyObject())).andReturn(sreg).once();
+
+
+ sreg.setProperties((Properties)EasyMock.anyObject());
+ EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+
+ public Object answer() throws Throwable {
+ Properties p = (Properties)EasyMock.getCurrentArguments()[0];
+ assertNotNull(p);
+ String scope = (String)p.get(EndpointListener.ENDPOINT_LISTENER_SCOPE);
+ assertNotNull(scope);
+ assertEquals("(&(" + Constants.OBJECTCLASS + "=*)(" + RemoteConstants.ENDPOINT_FRAMEWORK_UUID
+ + "=myUUID))", scope);
+ return null;
+ }
+ }).once();
+
+
+ EasyMock.expect(ctx.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn("myUUID")
+ .anyTimes();
+
+
+
+ EndpointListenerImpl eli = c.createMock(EndpointListenerImpl.class);
+ eli.close();
+ EasyMock.expectLastCall().once();
+
+ c.replay();
+ eplf.start();
+
+
+ Object service = eplf.getService(null, null);
+ assertNotNull(service);
+ assertTrue(service instanceof EndpointListener);
+
+ List<EndpointListenerImpl> listeners = eplf.getListeners();
+ assertEquals(1, listeners.size());
+ assertEquals(service, listeners.get(0));
+
+ eplf.ungetService(null, null, service);
+ listeners = eplf.getListeners();
+ assertEquals(0, listeners.size());
+
+ eplf.ungetService(null, null, eli); // no call to close
+ listeners.add(eli);
+ eplf.ungetService(null, null, eli); // call to close
+ listeners = eplf.getListeners();
+ assertEquals(0, listeners.size());
+
+
+ c.verify();
+ }
+
}
Added: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerImplTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerImplTest.java?rev=947367&view=auto
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerImplTest.java (added)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerImplTest.java Sun May 23 06:23:15 2010
@@ -0,0 +1,133 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.cxf.dosgi.discovery.zookeeper;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.remoteserviceadmin.EndpointDescription;
+import org.osgi.service.remoteserviceadmin.RemoteConstants;
+
+public class EndpointListenerImplTest extends TestCase {
+
+ public void testEndpointRemovalAdding() throws KeeperException, InterruptedException {
+
+ IMocksControl c = EasyMock.createNiceControl();
+
+ BundleContext ctx = c.createMock(BundleContext.class);
+ ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);
+
+ ZooKeeper zk = c.createMock(ZooKeeper.class);
+
+ EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();
+
+
+ String path = "/osgi/service_registry/myClass/google.de#80##test";
+ EasyMock.expect(
+ zk.create(EasyMock.eq(path),
+ (byte[])EasyMock.anyObject(), EasyMock.eq(Ids.OPEN_ACL_UNSAFE), EasyMock
+ .eq(CreateMode.EPHEMERAL))).andReturn("").once();
+
+ zk.delete(EasyMock.eq("/osgi/service_registry/myClass/google.de#80##test"), EasyMock.eq(-1));
+ EasyMock.expectLastCall().once();
+
+ c.replay();
+
+ EndpointListenerImpl eli = new EndpointListenerImpl(zkd, ctx);
+
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put(Constants.OBJECTCLASS, new String[] {
+ "myClass"
+ });
+ props.put(RemoteConstants.ENDPOINT_ID, "http://google.de:80/test");
+ props.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "myConfig");
+
+ EndpointDescription ed = new EndpointDescription(props);
+
+ eli.endpointAdded(ed, null);
+ eli.endpointAdded(ed, null); // should do nothing
+
+ eli.endpointRemoved(ed, null);
+ eli.endpointRemoved(ed, null); // should do nothing
+
+ c.verify();
+
+ }
+
+
+ public void testClose() throws KeeperException, InterruptedException{
+
+
+ IMocksControl c = EasyMock.createNiceControl();
+
+ BundleContext ctx = c.createMock(BundleContext.class);
+ ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);
+
+ ZooKeeper zk = c.createMock(ZooKeeper.class);
+
+ EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();
+
+
+ String path = "/osgi/service_registry/myClass/google.de#80##test";
+ EasyMock.expect(
+ zk.create(EasyMock.eq(path),
+ (byte[])EasyMock.anyObject(), EasyMock.eq(Ids.OPEN_ACL_UNSAFE), EasyMock
+ .eq(CreateMode.EPHEMERAL))).andReturn("").once();
+
+ zk.delete(EasyMock.eq("/osgi/service_registry/myClass/google.de#80##test"), EasyMock.eq(-1));
+ EasyMock.expectLastCall().once();
+
+ c.replay();
+
+ EndpointListenerImpl eli = new EndpointListenerImpl(zkd, ctx);
+
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put(Constants.OBJECTCLASS, new String[] {
+ "myClass"
+ });
+ props.put(RemoteConstants.ENDPOINT_ID, "http://google.de:80/test");
+ props.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "myConfig");
+
+ EndpointDescription ed = new EndpointDescription(props);
+
+ eli.endpointAdded(ed, null);
+
+ eli.close(); // should result in zk.delete(...)
+
+ c.verify();
+
+ }
+
+
+ public void testGetKey() throws Exception {
+ assertEquals("somehost#9090##org#example#TestEndpoint",
+ EndpointListenerImpl.getKey("http://somehost:9090/org/example/TestEndpoint"));
+ }
+
+}
Added: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizerTest.java?rev=947367&view=auto
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizerTest.java (added)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/EndpointListenerTrackerCustomizerTest.java Sun May 23 06:23:15 2010
@@ -0,0 +1,152 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.cxf.dosgi.discovery.zookeeper;
+
+import java.util.ArrayList;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.apache.cxf.dosgi.discovery.zookeeper.EndpointListenerTrackerCustomizer.Interest;
+import org.easymock.IAnswer;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.remoteserviceadmin.EndpointListener;
+
+public class EndpointListenerTrackerCustomizerTest extends TestCase{
+
+ public void testEndpointListenerTrackerCustomizer(){
+
+ IMocksControl c = EasyMock.createNiceControl();
+
+ BundleContext ctx = c.createMock(BundleContext.class);
+ ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);
+
+ ServiceReference sref = c.createMock(ServiceReference.class);
+ ServiceReference sref2 = c.createMock(ServiceReference.class);
+
+ final Properties p = new Properties();
+
+
+ EasyMock.expect(sref.getPropertyKeys()).andAnswer(new IAnswer<String[]>() {
+ public String[] answer() throws Throwable {
+ return p.keySet().toArray(new String[p.keySet().size()]);
+ }
+ }).anyTimes();
+
+ EasyMock.expect(sref.getProperty((String)EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+ public Object answer() throws Throwable {
+ String key = (String)(EasyMock.getCurrentArguments()[0]);
+ return p.getProperty(key);
+ }
+ }).anyTimes();
+
+ EasyMock.expect(sref2.getPropertyKeys()).andAnswer(new IAnswer<String[]>() {
+ public String[] answer() throws Throwable {
+ return p.keySet().toArray(new String[p.keySet().size()]);
+ }
+ }).anyTimes();
+
+ EasyMock.expect(sref2.getProperty((String)EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+ public Object answer() throws Throwable {
+ String key = (String)(EasyMock.getCurrentArguments()[0]);
+ return p.getProperty(key);
+ }
+ }).anyTimes();
+
+
+ final ArrayList<IMocksControl> controls = new ArrayList<IMocksControl>();
+
+ EndpointListenerTrackerCustomizer eltc = new EndpointListenerTrackerCustomizer(zkd,ctx){
+ protected InterfaceMonitor createInterfaceMonitor(String scope, String objClass, Interest interest){
+ IMocksControl lc = EasyMock.createNiceControl();
+ InterfaceMonitor im = lc.createMock(InterfaceMonitor.class);
+ im.start();
+ EasyMock.expectLastCall().once();
+ im.close();
+ EasyMock.expectLastCall().once();
+ lc.replay();
+ controls.add(lc);
+ return im;
+ }
+ };
+
+ c.replay();
+
+ eltc.addingService(sref); // sref has no scope -> nothing should happen
+
+ assertEquals(0, eltc.getHandledEndpointlisteners().size());
+ assertEquals(0, eltc.getInterestingScopes().size());
+
+ p.put(EndpointListener.ENDPOINT_LISTENER_SCOPE, "(objectClass=mine)");
+
+ eltc.addingService(sref);
+
+ assertEquals(1, eltc.getHandledEndpointlisteners().size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref).size());
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref).get(0));
+ assertEquals(1, eltc.getInterestingScopes().size());
+
+
+ eltc.addingService(sref);
+
+ assertEquals(1, eltc.getHandledEndpointlisteners().size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref).size());
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref).get(0));
+ assertEquals(1, eltc.getInterestingScopes().size());
+
+ eltc.addingService(sref2);
+
+ assertEquals(2, eltc.getHandledEndpointlisteners().size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref).size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref2).size());
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref).get(0));
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref2).get(0));
+ assertEquals(1, eltc.getInterestingScopes().size());
+
+
+ eltc.removedService(sref, null);
+
+ assertEquals(1, eltc.getHandledEndpointlisteners().size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref2).size());
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref2).get(0));
+ assertEquals(1, eltc.getInterestingScopes().size());
+
+ eltc.removedService(sref, null);
+
+ assertEquals(1, eltc.getHandledEndpointlisteners().size());
+ assertEquals(1, eltc.getHandledEndpointlisteners().get(sref2).size());
+ assertEquals("(objectClass=mine)", eltc.getHandledEndpointlisteners().get(sref2).get(0));
+ assertEquals(1, eltc.getInterestingScopes().size());
+
+
+ eltc.removedService(sref2, null);
+
+ assertEquals(0, eltc.getHandledEndpointlisteners().size());
+ assertEquals(0, eltc.getInterestingScopes().size());
+
+ c.verify();
+ for (IMocksControl control : controls) {
+ control.verify();
+ }
+ }
+
+}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java Sun May 23 06:23:15 2010
@@ -18,196 +18,74 @@
*/
package org.apache.cxf.dosgi.discovery.zookeeper;
+import java.util.Collection;
+import java.util.Collections;
+
import junit.framework.TestCase;
+import org.apache.cxf.dosgi.discovery.zookeeper.EndpointListenerTrackerCustomizer.Interest;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.Watcher.Event.EventType;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
+import org.apache.zookeeper.data.Stat;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+import org.osgi.framework.BundleContext;
+
public class InterfaceMonitorTest extends TestCase {
-
- public void testDUMMY(){
- assertTrue(true);
+
+ public void testInterfaceMonitor() throws KeeperException, InterruptedException {
+
+ IMocksControl c = EasyMock.createNiceControl();
+
+ BundleContext ctx = c.createMock(BundleContext.class);
+ ZooKeeperDiscovery zkd = c.createMock(ZooKeeperDiscovery.class);
+
+ ZooKeeper zk = c.createMock(ZooKeeper.class);
+ EasyMock.expect(zk.getState()).andReturn(ZooKeeper.States.CONNECTED).anyTimes();
+
+ EasyMock.expect(zkd.getZookeeper()).andReturn(zk).anyTimes();
+
+ EndpointListenerTrackerCustomizer.Interest interest = new EndpointListenerTrackerCustomizer.Interest();
+
+ String scope = "(myProp=test)";
+ String interf = "es.schaaf.test";
+ String node = Util.getZooKeeperPath(interf);
+
+ final InterfaceDataMonitorListenerImpl idmli = c.createMock(InterfaceDataMonitorListenerImpl.class);
+
+ InterfaceMonitor im = new InterfaceMonitor(zk, interf, interest, scope, ctx) {
+ @Override
+ protected InterfaceDataMonitorListenerImpl createInterfaceDataMonitorListener(ZooKeeper zk,
+ String intf,
+ Interest zkd,
+ String scope,
+ BundleContext bctx) {
+ return idmli;
+ }
+ };
+
+ idmli.change();
+ EasyMock.expectLastCall().once();
+
+ zk.exists(EasyMock.eq(node), EasyMock.eq(im), EasyMock.eq(im), EasyMock.anyObject());
+ EasyMock.expectLastCall().once();
+
+ EasyMock.expect(zk.exists(EasyMock.eq(node), EasyMock.eq(false))).andReturn(new Stat()).anyTimes();
+
+ EasyMock.expect(zk.getChildren(EasyMock.eq(node), EasyMock.eq(im))).andReturn(Collections.EMPTY_LIST)
+ .once();
+
+ c.replay();
+
+ im.start();
+
+ // simulate a zk callback
+ WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, node);
+ im.process(we);
+
+ c.verify();
}
-
-// public void testCreateListener() {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// DiscoveredServiceTracker dst = EasyMock.createMock(DiscoveredServiceTracker.class);
-//
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), dst);
-// InterfaceDataMonitorListenerImpl listener = (InterfaceDataMonitorListenerImpl) dm.listener;
-// assertSame(zk, listener.zookeeper);
-// assertEquals(Util.getZooKeeperPath(String.class.getName()), listener.znode);
-// assertEquals(String.class.getName(), listener.interFace);
-// assertSame(dst, listener.discoveredServiceTracker);
-// }
-//
-// @SuppressWarnings("unchecked")
-// public void testInterfaceMonitor() throws Exception {
-// Properties s1Props = new Properties();
-// s1Props.put("a", "b");
-// ByteArrayOutputStream s1Bytes = new ByteArrayOutputStream();
-// s1Props.store(s1Bytes, "");
-//
-// Properties s2Props = new Properties();
-// s2Props.put("d", "e");
-// ByteArrayOutputStream s2Bytes = new ByteArrayOutputStream();
-// s2Props.store(s2Bytes, "");
-//
-// ZooKeeper zk = EasyMock.createNiceMock(ZooKeeper.class);
-// zk.exists(Util.getZooKeeperPath(String.class.getName()), false);
-// EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
-// EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), false))
-// .andReturn(Arrays.asList("a#90#r", "b#90#r")).anyTimes();
-// EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/a#90#r", false, null))
-// .andReturn(s1Bytes.toByteArray()).anyTimes();
-// EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/b#90#r", false, null))
-// .andReturn(s2Bytes.toByteArray()).anyTimes();
-// EasyMock.replay(zk);
-//
-// final List<DiscoveredServiceNotification> notifications = new ArrayList<DiscoveredServiceNotification>();
-// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() {
-// public void serviceChanged(DiscoveredServiceNotification dsn) {
-// notifications.add(dsn);
-// }
-// };
-//
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), dst);
-// assertEquals("Precondition failed", 0, notifications.size());
-// dm.processResult(Code.Ok, null, null, null);
-// assertEquals(2, notifications.size());
-//
-// boolean s1Found = false, s2Found = false;
-// for (DiscoveredServiceNotification dsn : notifications) {
-// assertEquals(DiscoveredServiceNotification.AVAILABLE, dsn.getType());
-// assertEquals(Collections.emptyList(), dsn.getFilters());
-// assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces());
-// ServiceEndpointDescription sed = dsn.getServiceEndpointDescription();
-// assertEquals(Collections.singleton(String.class.getName()), sed.getProvidedInterfaces());
-// Map<?, ?> m = sed.getProperties();
-// if (m.entrySet().containsAll(s1Props.entrySet())) {
-// s1Found = true;
-// }
-//
-// if (m.entrySet().containsAll(s2Props.entrySet())) {
-// s2Found = true;
-// }
-// }
-// assertTrue(s1Found);
-// assertTrue(s2Found);
-//
-// // Second time around, with same data
-// notifications.clear();
-// dm.processResult(Code.Ok, null, null, null);
-// assertEquals("No changes, so should not get any new notifications", 0, notifications.size());
-//
-// // Third time around, removal
-// EasyMock.reset(zk);
-// zk.exists(Util.getZooKeeperPath(String.class.getName()), false);
-// EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
-// EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), false))
-// .andReturn(Arrays.asList("a#90#r")).anyTimes();
-// EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/a#90#r", false, null))
-// .andReturn(s1Bytes.toByteArray()).anyTimes();
-// EasyMock.replay(zk);
-// notifications.clear();
-// dm.processResult(Code.Ok, null, null, null);
-// DiscoveredServiceNotification dsn = notifications.iterator().next();
-// assertEquals(1, notifications.size());
-// assertEquals(DiscoveredServiceNotification.UNAVAILABLE, dsn.getType());
-// assertEquals(Collections.emptyList(), dsn.getFilters());
-// assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces());
-// ServiceEndpointDescription sed = dsn.getServiceEndpointDescription();
-// assertEquals(Collections.singleton(String.class.getName()), sed.getProvidedInterfaces());
-// assertTrue(sed.getProperties().entrySet().containsAll(s2Props.entrySet()));
-// }
-//
-// public void testInterfaceMonitorNoExist() throws Exception {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// EasyMock.expect(zk.exists(Util.getZooKeeperPath(String.class.getName()), false))
-// .andReturn(null);
-// EasyMock.replay(zk);
-//
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
-// dm.processResult(Code.NoNode, null, null, null);
-//
-// EasyMock.verify(zk);
-// }
-//
-// public void testProcess() {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// zk.exists(
-// EasyMock.eq(Util.getZooKeeperPath(String.class.getName())),
-// (Watcher) EasyMock.anyObject(),
-// (StatCallback) EasyMock.anyObject(),
-// EasyMock.isNull());
-// EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
-// public Object answer() throws Throwable {
-// assertEquals(EasyMock.getCurrentArguments()[1],
-// EasyMock.getCurrentArguments()[2]);
-// return null;
-// }
-// });
-// EasyMock.replay(zk);
-//
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
-// dm.process();
-//
-// EasyMock.verify(zk);
-// }
-//
-// public void testProcessWatchedEvent() throws Exception {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
-//
-// zk.exists(
-// Util.getZooKeeperPath(String.class.getName()),
-// false);
-// EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
-// EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), dm)).andReturn(null);
-// EasyMock.replay(zk);
-//
-// DataMonitorListener listener = EasyMock.createMock(DataMonitorListener.class);
-// listener.change();
-// EasyMock.replay(listener);
-// dm.listener = listener;
-//
-// dm.process((WatchedEvent) null);
-//
-// EasyMock.verify(zk);
-// EasyMock.verify(listener);
-// }
-//
-// public void testProcessWatchedEventNoExist() throws Exception {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
-//
-// zk.exists(
-// Util.getZooKeeperPath(String.class.getName()),
-// false);
-// EasyMock.expectLastCall().andReturn(null);
-// EasyMock.replay(zk);
-//
-// DataMonitorListener listener = EasyMock.createMock(DataMonitorListener.class);
-// EasyMock.replay(listener);
-// dm.listener = listener;
-//
-// dm.process((WatchedEvent) null);
-//
-// EasyMock.verify(zk);
-// EasyMock.verify(listener);
-// }
-//
-// public void testInterfaceMonitorDefault() {
-// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-// EasyMock.replay(zk);
-//
-// InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
-// EasyMock.verify(zk);
-//
-// EasyMock.reset(zk);
-// zk.exists(Util.getZooKeeperPath(String.class.getName()), dm, dm, null);
-// EasyMock.expectLastCall();
-// EasyMock.replay(zk);
-// // This should trigger a call to zookeeper.exists() as defined above
-// dm.processResult(12345, null, null, null);
-//
-// EasyMock.verify(zk);
-// }
}
Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java?rev=947367&r1=947366&r2=947367&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java Sun May 23 06:23:15 2010
@@ -26,6 +26,10 @@ import java.util.List;
import junit.framework.TestCase;
import org.apache.cxf.dosgi.discovery.zookeeper.Util;
+import org.easymock.classextension.EasyMock;
+import org.easymock.classextension.IMocksControl;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.remoteserviceadmin.EndpointListener;
public class UtilTest extends TestCase {
@@ -45,6 +49,9 @@ public class UtilTest extends TestCase {
list.add("2");
list.add("3");
assertEquals(list, Util.getMultiValueProperty(list));
+
+ assertEquals(Collections.emptySet(), Util.getMultiValueProperty(null));
+
}
public void testGetZooKeeperPath() {
@@ -55,4 +62,46 @@ public class UtilTest extends TestCase {
assertEquals(Util.PATH_PREFIX,Util.getZooKeeperPath(null));
assertEquals(Util.PATH_PREFIX,Util.getZooKeeperPath(""));
}
+
+
+ public void testGetStringPlusProperty() {
+ Object in = "MyString";
+ String[] out = Util.getStringPlusProperty(in);
+ assertEquals(1, out.length);
+ assertEquals("MyString", out[0]);
+
+
+ in = new String[]{"MyString"};
+ out = Util.getStringPlusProperty(in);
+ assertEquals(1, out.length);
+ assertEquals("MyString", out[0]);
+
+ in = new ArrayList<String>();
+ ((List<String>)in).add("MyString");
+ out = Util.getStringPlusProperty(in);
+ assertEquals(1, out.length);
+ assertEquals("MyString", out[0]);
+
+ in = new Object();
+ out = Util.getStringPlusProperty(in);
+ assertEquals(0, out.length);
+ }
+
+ public void testGetScopes(){
+ IMocksControl c = EasyMock.createNiceControl();
+
+ String[] scopes = new String[]{"myScope=test",""};
+
+ ServiceReference sref = c.createMock(ServiceReference.class);
+ EasyMock.expect(sref.getProperty(EasyMock.eq(EndpointListener.ENDPOINT_LISTENER_SCOPE))).andReturn(scopes).anyTimes();
+
+ c.replay();
+
+ String[] ret = Util.getScopes(sref);
+
+ c.verify();
+ assertEquals(1, ret.length);
+ assertEquals(scopes[0], ret[0]);
+
+ }
}