Author: davidb
Date: Tue Apr 14 14:59:21 2009
New Revision: 764812
URL: http://svn.apache.org/viewvc?rev=764812&view=rev
Log:
More classes and unit tests for the CXF-DOSGi Discovery implementation.
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
(with props)
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
(with props)
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
(with props)
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
(with props)
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
(with props)
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
(with props)
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImpl.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImplTest.java
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java?rev=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,75 @@
+/**
+ * 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 org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.discovery.DiscoveredServiceTracker;
+import org.osgi.service.discovery.ServicePublication;
+import org.osgi.util.tracker.ServiceTracker;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.osgi.context.BundleContextAware;
+
+public class DiscoveryBean implements BundleContextAware, InitializingBean,
DisposableBean, Watcher {
+ private BundleContext bundleContext;
+ private DiscoveryServiceImpl discoveryService;
+ private ZooKeeper zooKeeper;
+
+ private FindInZooKeeperCustomizer finderCustomizer;
+ private ServiceTracker lookupTracker;
+ private ServiceTracker publicationTracker;
+
+ public void setBundleContext(BundleContext bc) {
+ bundleContext = bc;
+ }
+
+ public void setDiscoveryServiceBean(DiscoveryServiceImpl discovery) {
+ discoveryService = discovery;
+ }
+
+ public void afterPropertiesSet() throws Exception {
+ String hostPort = discoveryService.getZooKeeperHost() + ":" +
+ discoveryService.getZooKeeperPort();
+ zooKeeper = new ZooKeeper(hostPort,
discoveryService.getZooKeeperTimeout(), this);
+
+ publicationTracker = new ServiceTracker(bundleContext,
ServicePublication.class.getName(),
+ new PublishToZooKeeperCustomizer(bundleContext, zooKeeper));
+ publicationTracker.open();
+
+ finderCustomizer = new FindInZooKeeperCustomizer(bundleContext,
zooKeeper);
+ lookupTracker = new ServiceTracker(bundleContext,
DiscoveredServiceTracker.class.getName(),
+ finderCustomizer);
+ lookupTracker.open();
+ }
+
+ public void destroy() throws Exception {
+ lookupTracker.close();
+ publicationTracker.close();
+ zooKeeper.close();
+ }
+
+ public void process(WatchedEvent event) {
+ System.out.println("*** [Spring] process (dropped): " + event);
+ // TODO do we need this? The zookeeper examples do this, but I'm
unsure why...
+ // finderCustomizer.process(event);
+ }
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImpl.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImpl.java?rev=764812&r1=764811&r2=764812&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImpl.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImpl.java
Tue Apr 14 14:59:21 2009
@@ -70,12 +70,10 @@
zooKeeperTimeout = timeout;
}
- @Override
public void setBundleContext(BundleContext bc) {
bundleContext = bc;
}
- @Override
public void afterPropertiesSet() throws Exception {
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put(Constants.SERVICE_PID, SERVICE_PID);
@@ -85,7 +83,6 @@
reg = bundleContext.registerService(Discovery.class.getName(), this,
props);
}
- @Override
public void destroy() throws Exception {
reg.unregister();
}
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java?rev=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,131 @@
+/**
+ * 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.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+public class PublishToZooKeeperCustomizer implements ServiceTrackerCustomizer {
+ private static final Logger LOG =
Logger.getLogger(PublishToZooKeeperCustomizer.class.getName());
+
+ private final BundleContext bundleContext;
+ private final ZooKeeper zookeeper;
+
+ PublishToZooKeeperCustomizer(BundleContext bc, ZooKeeper zk) {
+ bundleContext = bc;
+ zookeeper = zk;
+ }
+
+ public Object addingService(ServiceReference sr) {
+ try {
+ Object obj = bundleContext.getService(sr);
+ Collection<String> interfaces =
Util.getMultiValueProperty(sr.getProperty("service.interface"));
+ String endpoint =
sr.getProperty("osgi.remote.endpoint.location").toString();
+ String endpointKey = getKey(endpoint);
+
+ for (String name : interfaces) {
+ String path = Util.getZooKeeperPath(name);
+ String fullPath = path + '/' + endpointKey;
+ LOG.info("Creating ZooKeeper node: " + fullPath);
+
+ ensurePath(path);
+ Object x = zookeeper.create(fullPath, getData(sr),
+ Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
+ }
+ return obj;
+ } catch (Exception ex) {
+ LOG.log(Level.SEVERE, "Exception while processing a
ServicePublication.", ex);
+ return null;
+ }
+ }
+
+ public void modifiedService(ServiceReference sr, Object obj) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removedService(ServiceReference sr, Object obj) {
+ // TODO Auto-generated method stub
+
+ }
+
+ void ensurePath(String path) throws KeeperException, InterruptedException {
+ StringBuilder current = new StringBuilder();
+
+ String[] tree = path.split("/");
+ for (int i = 0; i < tree.length; i++) {
+ if (tree[i].length() == 0) {
+ continue;
+ }
+
+ current.append('/');
+ current.append(tree[i]);
+ if (zookeeper.exists(current.toString(), false) == null) {
+ zookeeper.create(current.toString(), new byte [0],
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ static byte[] getData(ServiceReference sr) throws IOException {
+ Properties p = new Properties();
+
+ Map<String, Object> serviceProps = (Map<String, Object>)
sr.getProperty("service.properties");
+ for (Map.Entry<String, Object> prop : serviceProps.entrySet()) {
+ p.setProperty(prop.getKey(), prop.getValue().toString());
+ }
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ p.store(baos, "");
+ return baos.toByteArray();
+ }
+
+ static String getKey(String endpoint) throws UnknownHostException,
URISyntaxException {
+ URI uri = new URI(endpoint);
+ if ("localhost".equals(uri.getHost()) ||
"127.0.0.1".equals(uri.getHost())) {
+ uri = new URI(uri.getScheme(), uri.getUserInfo(),
InetAddress.getLocalHost().getHostAddress(),
+ uri.getPort(), uri.getPath(), uri.getQuery(),
uri.getFragment());
+ }
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(uri.getHost());
+ sb.append("#");
+ sb.append(uri.getPort());
+ sb.append("#");
+ sb.append(uri.getPath().replace('/', '#'));
+ return sb.toString();
+ }
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
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=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,42 @@
+/**
+ * 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.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+public class Util {
+ static final String PATH_PREFIX = "/osgi/service_registry/";
+
+ @SuppressWarnings("unchecked")
+ static Collection<String> getMultiValueProperty(Object property) {
+ if (property instanceof Collection) {
+ return (Collection<String>) property;
+ } else if (property instanceof String []) {
+ return Arrays.asList((String []) property);
+ } else {
+ return Collections.singleton(property.toString());
+ }
+ }
+
+ static String getZooKeeperPath(String name) {
+ return PATH_PREFIX + name.replace('.', '/');
+ }
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java?rev=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,25 @@
+/**
+ * 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 junit.framework.TestCase;
+
+public class DiscoveryBeanTest extends TestCase {
+ public void testTodo() {}
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImplTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImplTest.java?rev=764812&r1=764811&r2=764812&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImplTest.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryServiceImplTest.java
Tue Apr 14 14:59:21 2009
@@ -60,7 +60,6 @@
andAnswer(new IAnswer<ServiceRegistration>(){
@SuppressWarnings("unchecked")
- @Override
public ServiceRegistration answer() throws Throwable {
Dictionary<String, Object> props =
(Dictionary<String, Object>)
EasyMock.getCurrentArguments()[2];
Added:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java?rev=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,164 @@
+/**
+ * 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.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.Writer;
+import java.net.InetAddress;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+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.apache.zookeeper.data.ACL;
+import org.easymock.IAnswer;
+import org.easymock.classextension.EasyMock;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+public class PublishToZooKeeperCustomizerTest extends TestCase {
+ public void testAddingService() throws Exception {
+ Hashtable<String, Object> srProps = new Hashtable<String, Object>();
+ srProps.put("osgi.remote.interfaces", "*");
+
+ final Properties expected = new Properties();
+ expected.put("osgi.remote.interfaces", "*");
+ ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
+ expected.store(expectedBytes, "");
+
+ ServiceReference sr = EasyMock.createMock(ServiceReference.class);
+
EasyMock.expect(sr.getProperty("service.interface")).andReturn(Arrays.asList("java.lang.String",
"org.example.interface.AnInterface"));
+
EasyMock.expect(sr.getProperty("osgi.remote.endpoint.location")).andReturn("http://somehost.someorg:80/abc/def");
+
EasyMock.expect(sr.getProperty("service.properties")).andReturn(srProps).anyTimes();
+ EasyMock.replay(sr);
+
+ BundleContext bc = EasyMock.createMock(BundleContext.class);
+ EasyMock.expect(bc.getService(sr)).andReturn("something");
+ EasyMock.replay(bc);
+
+ ZooKeeper zk = EasyMock.createNiceMock(ZooKeeper.class);
+ // Expect create calls for both interfaces
+ EasyMock.expect(zk.create(EasyMock.eq(Util.PATH_PREFIX +
"java/lang/String"),
+ (byte[]) EasyMock.aryEq(new byte[0]),
+ (List<ACL>) EasyMock.eq(Ids.OPEN_ACL_UNSAFE),
+ (CreateMode)
EasyMock.eq(CreateMode.PERSISTENT))).andReturn(null);
+ EasyMock.expect(zk.create(EasyMock.eq(Util.PATH_PREFIX +
"java/lang/String/somehost.someorg#80##abc#def"),
+ (byte[]) EasyMock.anyObject(),
+ (List<ACL>) EasyMock.eq(Ids.OPEN_ACL_UNSAFE),
+ (CreateMode) EasyMock.eq(CreateMode.EPHEMERAL))).andAnswer(new
IAnswer<String>() {
+ public String answer() throws Throwable {
+ byte [] b = (byte[]) EasyMock.getCurrentArguments()[1];
+ Properties actual = new Properties();
+ actual.load(new ByteArrayInputStream(b));
+ assertEquals(expected, actual);
+
+ return null;
+ }
+ });
+
+ EasyMock.expect(zk.create(EasyMock.eq(Util.PATH_PREFIX +
"org/example/interface/AnInterface"),
+ (byte[]) EasyMock.aryEq(new byte[0]),
+ (List<ACL>) EasyMock.eq(Ids.OPEN_ACL_UNSAFE),
+ (CreateMode)
EasyMock.eq(CreateMode.PERSISTENT))).andReturn(null);
+ EasyMock.expect(zk.create(EasyMock.eq(Util.PATH_PREFIX +
"org/example/interface/AnInterface/somehost.someorg#80##abc#def"),
+ (byte[]) EasyMock.anyObject(),
+ (List<ACL>) EasyMock.eq(Ids.OPEN_ACL_UNSAFE),
+ (CreateMode) EasyMock.eq(CreateMode.EPHEMERAL))).andAnswer(new
IAnswer<String>() {
+ public String answer() throws Throwable {
+ byte [] b = (byte[]) EasyMock.getCurrentArguments()[1];
+ Properties actual = new Properties();
+ actual.load(new ByteArrayInputStream(b));
+ assertEquals(expected, actual);
+
+ return null;
+ }
+ });
+ EasyMock.replay(zk);
+
+ PublishToZooKeeperCustomizer pc = new PublishToZooKeeperCustomizer(bc,
zk);
+ pc.addingService(sr);
+
+ EasyMock.verify(sr);
+ EasyMock.verify(bc);
+ EasyMock.verify(zk);
+ }
+
+ public void testEnsurePath() throws Exception {
+ ZooKeeper zk = EasyMock.createStrictMock(ZooKeeper.class);
+ EasyMock.expect(zk.exists((String) EasyMock.anyObject(),
EasyMock.anyBoolean())).andReturn(null);
+ expectCreateCall(zk, "/a");
+ EasyMock.expect(zk.exists((String) EasyMock.anyObject(),
EasyMock.anyBoolean())).andReturn(null);
+ expectCreateCall(zk, "/a/b");
+ EasyMock.expect(zk.exists((String) EasyMock.anyObject(),
EasyMock.anyBoolean())).andReturn(null);
+ expectCreateCall(zk, "/a/b/c");
+ EasyMock.expect(zk.exists((String) EasyMock.anyObject(),
EasyMock.anyBoolean())).andReturn(null);
+ expectCreateCall(zk, "/a/b/c/D");
+ EasyMock.replay(zk);
+
+ PublishToZooKeeperCustomizer pc = new
PublishToZooKeeperCustomizer(null, zk);
+ pc.ensurePath("/a/b/c/D");
+ EasyMock.verify(zk);
+ }
+
+ private static void expectCreateCall(ZooKeeper zk, String path) throws
Exception {
+ EasyMock.expect(zk.create(EasyMock.eq(path),
+ (byte[]) EasyMock.aryEq(new byte[0]),
+ (List<ACL>) EasyMock.eq(Ids.OPEN_ACL_UNSAFE),
+ (CreateMode)
EasyMock.eq(CreateMode.PERSISTENT))).andReturn(path);
+ }
+
+ public void testGetData() throws Exception {
+ Map<String, Object> expected = new HashMap<String, Object>();
+ expected.put("osgi.remote.interfaces", "*");
+ expected.put("osgi.remote.configuration.type", "pojo");
+ expected.put("osgi.remote.configuration.pojo.address",
"http://localhost:9090/ps");
+
+ ServiceReference sr = EasyMock.createMock(ServiceReference.class);
+
EasyMock.expect(sr.getProperty("service.properties")).andReturn(expected);
+ EasyMock.replay(sr);
+
+ byte[] data = PublishToZooKeeperCustomizer.getData(sr);
+
+ Properties actual = new Properties();
+ actual.load(new ByteArrayInputStream(data));
+
+ assertEquals(expected, actual);
+ EasyMock.verify(sr);
+ }
+
+ public void testGetKey() throws Exception {
+ assertEquals("somehost#9090##org#example#TestEndpoint",
+
PublishToZooKeeperCustomizer.getKey("http://somehost:9090/org/example/TestEndpoint"));
+ }
+
+ public void testGetKeyLocalhost() throws Exception {
+ String hostAddr = InetAddress.getLocalHost().getHostAddress();
+ assertEquals(hostAddr + "#8000##ps",
+ PublishToZooKeeperCustomizer.getKey("http://localhost:8000/ps"));
+ }
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
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=764812&view=auto
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
(added)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
Tue Apr 14 14:59:21 2009
@@ -0,0 +1,51 @@
+/**
+ * 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.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class UtilTest extends TestCase {
+ public void testMultiValuePropertyAsString() {
+ assertEquals(Collections.singleton("hi"),
+ Util.getMultiValueProperty("hi"));
+ }
+
+ public void testMultiValuePropertyAsArray() {
+ assertEquals(Arrays.asList("a", "b"),
+ Util.getMultiValueProperty(new String [] {"a", "b"}));
+ }
+
+ public void testMultiValuePropertyAsCollection() {
+ List<String> list = new ArrayList<String>();
+ list.add("1");
+ list.add("2");
+ list.add("3");
+ assertEquals(list, Util.getMultiValueProperty(list));
+ }
+
+ public void testGetZooKeeperPath() {
+ assertEquals(Util.PATH_PREFIX + "org/example/Test",
+ Util.getZooKeeperPath("org.example.Test"));
+ }
+}
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/UtilTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date