http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/DiscoveryDriverTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/DiscoveryDriverTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/DiscoveryDriverTest.java new file mode 100644 index 0000000..b0b187a --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/DiscoveryDriverTest.java @@ -0,0 +1,135 @@ +/** + * 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.aries.rsa.discovery.zookeeper; + +import junit.framework.TestCase; + +public class DiscoveryDriverTest extends TestCase { + + public void testDUMMY() { + assertTrue(true); + } + +// public void testDiscoveryDriver() throws Exception { +// BundleContext bc = getDefaultBundleContext(); +// Dictionary<String, String> props = getDefaultProps(); +// +// final StringBuilder sb = new StringBuilder(); +// DiscoveryDriver dd = new DiscoveryDriver(bc, props) { +// @Override +// ZooKeeper createZooKeeper() throws IOException { +// sb.append(zkHost + ":" + zkPort); +// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); +// EasyMock.replay(zk); +// return zk; +// } +// }; +// EasyMock.verify(bc); +// assertEquals("somehost:1910", sb.toString()); +// +// EasyMock.verify(dd.zooKeeper); +// EasyMock.reset(dd.zooKeeper); +// dd.zooKeeper.close(); +// EasyMock.expectLastCall(); +// EasyMock.replay(dd.zooKeeper); +// +// ServiceTracker st1 = EasyMock.createMock(ServiceTracker.class); +// st1.close(); +// EasyMock.expectLastCall(); +// EasyMock.replay(st1); +// ServiceTracker st2 = EasyMock.createMock(ServiceTracker.class); +// st2.close(); +// EasyMock.expectLastCall(); +// EasyMock.replay(st2); +// +// dd.lookupTracker = st1; +// dd.publicationTracker = st2; +// +// dd.destroy(); +// } +// +// private void expectServiceTrackerCalls(BundleContext bc, String objectClass) +// throws InvalidSyntaxException { +// Filter filter = EasyMock.createNiceMock(Filter.class); +// EasyMock.replay(filter); +// +// EasyMock.expect(bc.createFilter("(objectClass=" + objectClass + ")")) +// .andReturn(filter).anyTimes(); +// bc.addServiceListener((ServiceListener) EasyMock.anyObject(), +// EasyMock.eq("(objectClass=" + objectClass + ")")); +// EasyMock.expectLastCall().anyTimes(); +// EasyMock.expect(bc.getServiceReferences(objectClass, null)) +// .andReturn(new ServiceReference [0]).anyTimes(); +// } +// +// public void testProcessEvent() throws Exception { +// DiscoveryDriver db = new DiscoveryDriver(getDefaultBundleContext(), getDefaultProps()) { +// @Override +// ZooKeeper createZooKeeper() throws IOException { +// return null; +// } +// }; +// +// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(null, null); +// List<InterfaceMonitor> l1 = new ArrayList<InterfaceMonitor>(); +// InterfaceMonitor dm1a = EasyMock.createMock(InterfaceMonitor.class); +// dm1a.process(); +// EasyMock.expectLastCall(); +// EasyMock.replay(dm1a); +// InterfaceMonitor dm1b = EasyMock.createMock(InterfaceMonitor.class); +// dm1b.process(); +// EasyMock.expectLastCall(); +// EasyMock.replay(dm1b); +// l1.add(dm1a); +// l1.add(dm1b); +// +// List<InterfaceMonitor> l2 = new ArrayList<InterfaceMonitor>(); +// InterfaceMonitor dm2 = EasyMock.createMock(InterfaceMonitor.class); +// dm2.process(); +// EasyMock.expectLastCall(); +// EasyMock.replay(dm2); +// l2.add(dm2); +// +// fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l1); +// fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l2); +// +// db.finderCustomizer = fc; +// db.process(null); +// +// EasyMock.verify(dm1a); +// EasyMock.verify(dm1b); +// EasyMock.verify(dm2); +// } +// +// private BundleContext getDefaultBundleContext() throws InvalidSyntaxException { +// BundleContext bc = EasyMock.createMock(BundleContext.class); +// expectServiceTrackerCalls(bc, ServicePublication.class.getName()); +// expectServiceTrackerCalls(bc, DiscoveredServiceTracker.class.getName()); +// EasyMock.replay(bc); +// return bc; +// } +// +// private Dictionary<String, String> getDefaultProps() { +// Dictionary<String, String> props = new Hashtable<String, String>(); +// props.put("zookeeper.host", "somehost"); +// props.put("zookeeper.port", "1910"); +// props.put("zookeeper.timeout", "1500"); +// return props; +// } +}
http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/FindInZooKeeperCustomizerTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/FindInZooKeeperCustomizerTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/FindInZooKeeperCustomizerTest.java new file mode 100644 index 0000000..a0a828a --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/FindInZooKeeperCustomizerTest.java @@ -0,0 +1,301 @@ +/** + * 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.aries.rsa.discovery.zookeeper; + +import junit.framework.TestCase; + +public class FindInZooKeeperCustomizerTest extends TestCase { + + public void testDUMMY() { + assertTrue(true); + } + +// public void testAddingServiceInterface() { +// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() { +// public void serviceChanged(DiscoveredServiceNotification dsn) {} +// }; +// +// ServiceReference sr = EasyMock.createMock(ServiceReference.class); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(Collections.singleton(String.class.getName())); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(null); +// EasyMock.replay(sr); +// +// DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() { +// public void serviceChanged(DiscoveredServiceNotification dsn) {} +// }; +// +// ServiceReference sr2 = EasyMock.createMock(ServiceReference.class); +// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(Arrays.asList(Integer.class.getName(), Comparable.class.getName())); +// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(null); +// EasyMock.replay(sr2); +// +// BundleContext bc = EasyMock.createMock(BundleContext.class); +// EasyMock.expect(bc.getService(sr)).andReturn(dst); +// EasyMock.expect(bc.getService(sr2)).andReturn(dst2); +// EasyMock.replay(bc); +// +// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); +// zkExpectExists(zk, String.class.getName()); +// zkExpectExists(zk, Integer.class.getName()); +// zkExpectExists(zk, Comparable.class.getName()); +// EasyMock.expectLastCall(); +// EasyMock.replay(zk); +// +// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(bc, zk); +// +// // --------------------------------------------------------------- +// // Test the addingService APIs +// // --------------------------------------------------------------- +// +// assertEquals("Precondition failed", 0, fc.watchers.size()); +// fc.addingService(sr); +// assertEquals(1, fc.watchers.size()); +// +// DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next(); +// assertSame(dst, key); +// List<InterfaceMonitor> dmList = fc.watchers.get(key); +// assertEquals(1, dmList.size()); +// InterfaceMonitor dm = dmList.iterator().next(); +// assertNotNull(dm.listener); +// assertSame(zk, dm.zookeeper); +// assertEquals(Utils.getZooKeeperPath(String.class.getName()), dm.znode); +// +// assertEquals("Precondition failed", 1, fc.watchers.size()); +// fc.addingService(sr2); +// assertEquals(2, fc.watchers.size()); +// +// assertTrue(fc.watchers.containsKey(dst)); +// assertTrue(fc.watchers.containsKey(dst2)); +// assertEquals(dmList, fc.watchers.get(dst)); +// List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2); +// assertEquals(2, dmList2.size()); +// +// Set<String> actual = new HashSet<String>(); +// for (InterfaceMonitor im : dmList2) { +// actual.add(im.znode); +// } +// Set<String> expected = new HashSet<String>(Arrays.asList( +// Utils.getZooKeeperPath(Integer.class.getName()), +// Utils.getZooKeeperPath(Comparable.class.getName()))); +// assertEquals(expected, actual); +// +// EasyMock.verify(zk); +// +// // --------------------------------------------------------------- +// // Test the modifiedService APIs +// // --------------------------------------------------------------- +// EasyMock.reset(zk); +// zkExpectExists(zk, List.class.getName()); +// EasyMock.replay(zk); +// +// EasyMock.reset(sr); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(Collections.singleton(List.class.getName())); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(null); +// EasyMock.replay(sr); +// +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// fc.modifiedService(sr, dst); +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// +// assertTrue(fc.watchers.containsKey(dst)); +// assertTrue(fc.watchers.containsKey(dst2)); +// assertEquals(dmList2, fc.watchers.get(dst2)); +// List<InterfaceMonitor> dmList3 = fc.watchers.get(dst); +// assertEquals(1, dmList3.size()); +// assertEquals(Utils.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode); +// +// EasyMock.verify(zk); +// +// // --------------------------------------------------------------- +// // Test the removedService APIs +// // --------------------------------------------------------------- +// EasyMock.reset(zk); +// EasyMock.replay(zk); +// +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// fc.removedService(sr2, dst2); +// assertEquals("Precondition failed", 1, fc.watchers.size()); +// +// assertEquals(dmList3, fc.watchers.get(dst)); +// assertNull(fc.watchers.get(dst2)); +// +// EasyMock.verify(zk); +// } +// +// public void testAddingServiceFilter() { +// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() { +// public void serviceChanged(DiscoveredServiceNotification dsn) {} +// }; +// +// ServiceReference sr = EasyMock.createMock(ServiceReference.class); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(null); +// Set<String> stringFilter = Collections.singleton("(objectClass=java.lang.String)"); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(stringFilter); +// EasyMock.replay(sr); +// +// DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() { +// public void serviceChanged(DiscoveredServiceNotification dsn) {} +// }; +// +// ServiceReference sr2 = EasyMock.createMock(ServiceReference.class); +// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(null); +// List<String> combinedFilter = +// Arrays.asList("(objectClass=java.lang.Integer)", "(objectClass=java.lang.Comparable)"); +// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(combinedFilter); +// EasyMock.replay(sr2); +// +// BundleContext bc = EasyMock.createMock(BundleContext.class); +// EasyMock.expect(bc.getService(sr)).andReturn(dst); +// EasyMock.expect(bc.getService(sr2)).andReturn(dst2); +// EasyMock.replay(bc); +// +// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); +// zkExpectExists(zk, String.class.getName()); +// zkExpectExists(zk, Integer.class.getName()); +// zkExpectExists(zk, Comparable.class.getName()); +// EasyMock.expectLastCall(); +// EasyMock.replay(zk); +// +// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(bc, zk); +// +// // --------------------------------------------------------------- +// // Test the addingService APIs +// // --------------------------------------------------------------- +// +// assertEquals("Precondition failed", 0, fc.watchers.size()); +// fc.addingService(sr); +// assertEquals(1, fc.watchers.size()); +// +// DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next(); +// assertSame(dst, key); +// List<InterfaceMonitor> dmList = fc.watchers.get(key); +// assertEquals(1, dmList.size()); +// InterfaceMonitor dm = dmList.iterator().next(); +// assertNotNull(dm.listener); +// assertSame(zk, dm.zookeeper); +// assertEquals(Utils.getZooKeeperPath(String.class.getName()), dm.znode); +// +// assertEquals("Precondition failed", 1, fc.watchers.size()); +// fc.addingService(sr2); +// assertEquals(2, fc.watchers.size()); +// +// assertTrue(fc.watchers.containsKey(dst)); +// assertTrue(fc.watchers.containsKey(dst2)); +// assertEquals(dmList, fc.watchers.get(dst)); +// List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2); +// assertEquals(2, dmList2.size()); +// Set<String> actual = new HashSet<String>(); +// for (InterfaceMonitor im : dmList2) { +// actual.add(im.znode); +// } +// Set<String> expected = new HashSet<String>(Arrays.asList( +// Utils.getZooKeeperPath(Integer.class.getName()), +// Utils.getZooKeeperPath(Comparable.class.getName()))); +// assertEquals(expected, actual); +// +// EasyMock.verify(zk); +// +// // --------------------------------------------------------------- +// // Test the modifiedService APIs +// // --------------------------------------------------------------- +// EasyMock.reset(zk); +// zkExpectExists(zk, List.class.getName()); +// EasyMock.replay(zk); +// +// EasyMock.reset(sr); +// Set<String> listFilter = Collections.singleton("(objectClass=java.util.List)"); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) +// .andReturn(null); +// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) +// .andReturn(listFilter); +// EasyMock.replay(sr); +// +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// fc.modifiedService(sr, dst); +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// +// assertTrue(fc.watchers.containsKey(dst)); +// assertTrue(fc.watchers.containsKey(dst2)); +// assertEquals(dmList2, fc.watchers.get(dst2)); +// List<InterfaceMonitor> dmList3 = fc.watchers.get(dst); +// assertEquals(1, dmList3.size()); +// assertEquals(Utils.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode); +// +// EasyMock.verify(zk); +// +// // --------------------------------------------------------------- +// // Test the removedService APIs +// // --------------------------------------------------------------- +// EasyMock.reset(zk); +// EasyMock.replay(zk); +// +// assertEquals("Precondition failed", 2, fc.watchers.size()); +// fc.removedService(sr2, dst2); +// assertEquals("Precondition failed", 1, fc.watchers.size()); +// +// assertEquals(dmList3, fc.watchers.get(dst)); +// assertNull(fc.watchers.get(dst2)); +// +// EasyMock.verify(zk); +// } +// +// public void testGetInterfacesFromFilter() { +// testGetInterfacesFromFilter("objectClass=org.apache_2.Some$FunnyClass", +// "org.apache_2.Some$FunnyClass"); +// testGetInterfacesFromFilter("(&(a=b)(objectClass = org.acme.Q)", +// "org.acme.Q"); +// testGetInterfacesFromFilter("(&(objectClassIdentifier=b)(objectClass = org.acme.Q)", +// "org.acme.Q"); +// testGetInterfacesFromFilter("(|(OBJECTCLASS= X )(objectclass = Y)", +// "X", "Y"); +// testGetInterfacesFromFilter(new String[] {"(objectClass=X)", "(objectClass=Y)"}, +// "X", "Y"); +// } +// +// private void testGetInterfacesFromFilter(String filter, String ... interfaces) { +// testGetInterfacesFromFilter(new String[] {filter}, interfaces); +// } +// +// private void testGetInterfacesFromFilter(String[] filters, String ... interfaces) { +// FindInZooKeeperCustomizer.getInterfacesFromFilter(Arrays.asList(filters)); +// } +// +// private void zkExpectExists(ZooKeeper zk, String className) { +// zk.exists(EasyMock.eq(Utils.getZooKeeperPath(className)), +// (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; +// } +// }); +// } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/InterfaceDataMonitorListenerImplTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/InterfaceDataMonitorListenerImplTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/InterfaceDataMonitorListenerImplTest.java new file mode 100644 index 0000000..bac4f78 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/InterfaceDataMonitorListenerImplTest.java @@ -0,0 +1,183 @@ +/** + * 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.aries.rsa.discovery.zookeeper; + +import junit.framework.TestCase; + +public class InterfaceDataMonitorListenerImplTest extends TestCase { + + public void testDUMMY() { + assertTrue(true); + } + +// public void testChange() throws Exception { +// final List<DiscoveredServiceNotification> dsnCallbacks = new ArrayList<DiscoveredServiceNotification>(); +// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() { +// public void serviceChanged(DiscoveredServiceNotification dsn) { +// dsnCallbacks.add(dsn); +// } +// }; +// +// //---------------------------------------------------------------- +// // Test DiscoveredServiceNotification.AVAILABLE +// //---------------------------------------------------------------- +// Properties initial = new Properties(); +// initial.put("a", "b"); +// initial.put(ServicePublication.ENDPOINT_LOCATION, "http://somehost:12345/some/context"); +// ByteArrayOutputStream propBytes = new ByteArrayOutputStream(); +// initial.store(propBytes, ""); +// +// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Arrays.asList("x#y#z")); +// EasyMock.expect(zk.getData(Utils.getZooKeeperPath(String.class.getName()) + "/x#y#z", false, null)) +// .andReturn(propBytes.toByteArray()); +// EasyMock.replay(zk); +// +// InterfaceDataMonitorListenerImpl dml = new InterfaceDataMonitorListenerImpl(zk, String.class.getName(), dst); +// +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals(1, dsnCallbacks.size()); +// DiscoveredServiceNotification dsn = dsnCallbacks.iterator().next(); +// assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces()); +// assertEquals(DiscoveredServiceNotification.AVAILABLE, dsn.getType()); +// assertEquals(0, dsn.getFilters().size()); +// ServiceEndpointDescription sed = dsn.getServiceEndpointDescription(); +// assertEquals(Collections.singleton(String.class.getName()), sed.getProvidedInterfaces()); +// +// Properties expected = new Properties(); +// expected.load(new ByteArrayInputStream(propBytes.toByteArray())); +// expected.put("service.exported.configs", "org.apache.cxf.ws"); +// expected.put("org.apache.cxf.ws.address", "http://somehost:12345/some/context"); +// +// assertEquals(expected, sed.getProperties()); +// EasyMock.verify(zk); +// +// // Again with the same data +// EasyMock.reset(zk); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Arrays.asList("x#y#z")); +// EasyMock.expect(zk.getData(Utils.getZooKeeperPath(String.class.getName()) + "/x#y#z", false, null)) +// .andReturn(propBytes.toByteArray()); +// EasyMock.replay(zk); +// +// dsnCallbacks.clear(); +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals(0, dsnCallbacks.size()); +// +// EasyMock.verify(zk); +// //---------------------------------------------------------------- +// // Test DiscoveredServiceNotification.MODIFIED +// //---------------------------------------------------------------- +// Properties modified = new Properties(); +// modified.put("c", "d"); +// modified.put(ServicePublication.ENDPOINT_LOCATION, "http://somehost:999/some/context"); +// modified.put("service.exported.configs", "org.apache.cxf.rs"); +// ByteArrayOutputStream modBytes = new ByteArrayOutputStream(); +// modified.store(modBytes, ""); +// +// EasyMock.reset(zk); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Arrays.asList("x#y#z")); +// EasyMock.expect(zk.getData(Utils.getZooKeeperPath(String.class.getName()) + "/x#y#z", false, null)) +// .andReturn(modBytes.toByteArray()); +// EasyMock.replay(zk); +// +// dsnCallbacks.clear(); +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals(1, dsnCallbacks.size()); +// DiscoveredServiceNotification dsn2 = dsnCallbacks.iterator().next(); +// assertEquals(Collections.singleton(String.class.getName()), dsn2.getInterfaces()); +// assertEquals(DiscoveredServiceNotification.MODIFIED, dsn2.getType()); +// assertEquals(0, dsn2.getFilters().size()); +// ServiceEndpointDescription sed2 = dsn2.getServiceEndpointDescription(); +// assertEquals(Collections.singleton(String.class.getName()), sed2.getProvidedInterfaces()); +// assertEquals(modified, sed2.getProperties()); +// +// EasyMock.verify(zk); +// +// //---------------------------------------------------------------- +// // Test DiscoveredServiceNotification.MODIFIED2 +// //---------------------------------------------------------------- +// Properties modified2 = new Properties(); +// modified2.put("c", "d2"); +// modified2.put(ServicePublication.ENDPOINT_LOCATION, "http://somehost:112/some/context"); +// modified2.put("service.exported.configs", "org.apache.cxf.ws"); +// modified2.put("org.apache.cxf.ws.address", "http://somewhereelse/123"); +// ByteArrayOutputStream modBytes2 = new ByteArrayOutputStream(); +// modified2.store(modBytes2, ""); +// +// EasyMock.reset(zk); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Arrays.asList("x#y#z")); +// EasyMock.expect(zk.getData(Utils.getZooKeeperPath(String.class.getName()) + "/x#y#z", false, null)) +// .andReturn(modBytes2.toByteArray()); +// EasyMock.replay(zk); +// +// dsnCallbacks.clear(); +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals(1, dsnCallbacks.size()); +// DiscoveredServiceNotification dsn3 = dsnCallbacks.iterator().next(); +// assertEquals(Collections.singleton(String.class.getName()), dsn3.getInterfaces()); +// assertEquals(DiscoveredServiceNotification.MODIFIED, dsn3.getType()); +// assertEquals(0, dsn3.getFilters().size()); +// ServiceEndpointDescription sed3 = dsn3.getServiceEndpointDescription(); +// assertEquals(Collections.singleton(String.class.getName()), sed3.getProvidedInterfaces()); +// assertEquals(modified2, sed3.getProperties()); +// +// EasyMock.verify(zk); +// //---------------------------------------------------------------- +// // Test DiscoveredServiceNotification.UNAVAILABLE +// //---------------------------------------------------------------- +// EasyMock.reset(zk); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Collections.<String>emptyList()); +// EasyMock.replay(zk); +// +// dsnCallbacks.clear(); +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals(1, dsnCallbacks.size()); +// DiscoveredServiceNotification dsn4 = dsnCallbacks.iterator().next(); +// assertEquals(Collections.singleton(String.class.getName()), dsn4.getInterfaces()); +// assertEquals(DiscoveredServiceNotification.UNAVAILABLE, dsn4.getType()); +// assertEquals(0, dsn4.getFilters().size()); +// ServiceEndpointDescription sed4 = dsn4.getServiceEndpointDescription(); +// assertEquals(Collections.singleton(String.class.getName()), sed4.getProvidedInterfaces()); +// assertEquals(modified2, sed4.getProperties()); +// +// EasyMock.verify(zk); +// +// // Try the same again... +// EasyMock.reset(zk); +// EasyMock.expect(zk.getChildren(Utils.getZooKeeperPath(String.class.getName()), false)) +// .andReturn(Collections.<String>emptyList()); +// EasyMock.replay(zk); +// +// dsnCallbacks.clear(); +// assertEquals("Precondition failed", 0, dsnCallbacks.size()); +// dml.change(); +// assertEquals("Should not receive a callback again...", 0, dsnCallbacks.size()); +// EasyMock.verify(zk); +// } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/ZookeeperDiscoveryTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/ZookeeperDiscoveryTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/ZookeeperDiscoveryTest.java new file mode 100644 index 0000000..8e4af45 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/ZookeeperDiscoveryTest.java @@ -0,0 +1,56 @@ +package org.apache.aries.rsa.discovery.zookeeper; + +import java.util.Dictionary; +import java.util.Hashtable; + +import org.apache.aries.rsa.discovery.zookeeper.ZooKeeperDiscovery; +import org.apache.zookeeper.ZooKeeper; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.Assert; +import org.junit.Test; +import org.osgi.framework.BundleContext; +import org.osgi.service.cm.ConfigurationException; + + +public class ZookeeperDiscoveryTest { + + @Test + public void testDefaults() throws ConfigurationException { + IMocksControl c = EasyMock.createControl(); + BundleContext bctx = c.createMock(BundleContext.class); + ZooKeeperDiscovery zkd = new ZooKeeperDiscovery(bctx) { + @Override + protected ZooKeeper createZooKeeper(String host, String port, int timeout) { + Assert.assertEquals("localhost", host); + Assert.assertEquals("2181", port); + Assert.assertEquals(3000, timeout); + return null; + } + }; + + Dictionary<String, Object> configuration = new Hashtable<String, Object>(); + zkd.updated(configuration); + } + + @Test + public void testConfig() throws ConfigurationException { + IMocksControl c = EasyMock.createControl(); + BundleContext bctx = c.createMock(BundleContext.class); + ZooKeeperDiscovery zkd = new ZooKeeperDiscovery(bctx) { + @Override + protected ZooKeeper createZooKeeper(String host, String port, int timeout) { + Assert.assertEquals("myhost", host); + Assert.assertEquals("1", port); + Assert.assertEquals(1000, timeout); + return null; + } + }; + + Dictionary<String, Object> configuration = new Hashtable<String, Object>(); + configuration.put("zookeeper.host", "myhost"); + configuration.put("zookeeper.port", "1"); + configuration.put("zookeeper.timeout", "1000"); + zkd.updated(configuration); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java new file mode 100644 index 0000000..0cbe7ca --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java @@ -0,0 +1,102 @@ +/** + * 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.aries.rsa.discovery.zookeeper.publish; + +import java.util.Dictionary; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.aries.rsa.discovery.zookeeper.publish.PublishingEndpointListener; +import org.apache.aries.rsa.discovery.zookeeper.publish.PublishingEndpointListenerFactory; +import org.apache.zookeeper.ZooKeeper; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.remoteserviceadmin.EndpointListener; + +public class PublishingEndpointListenerFactoryTest extends TestCase { + + @SuppressWarnings("unchecked") + public void testScope() { + IMocksControl c = EasyMock.createNiceControl(); + + BundleContext ctx = c.createMock(BundleContext.class); + ZooKeeper zk = c.createMock(ZooKeeper.class); + @SuppressWarnings("rawtypes") + ServiceRegistration sreg = c.createMock(ServiceRegistration.class); + + PublishingEndpointListenerFactory eplf = new PublishingEndpointListenerFactory(zk, ctx); + + EasyMock.expect(ctx.registerService(EasyMock.eq(EndpointListener.class.getName()), EasyMock.eq(eplf), + (Dictionary<String, String>)EasyMock.anyObject())).andReturn(sreg).once(); + + EasyMock.expect(ctx.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn("myUUID").anyTimes(); + + c.replay(); + eplf.start(); + c.verify(); + + } + + @SuppressWarnings("unchecked") + public void testServiceFactory() { + IMocksControl c = EasyMock.createNiceControl(); + + BundleContext ctx = c.createMock(BundleContext.class); + ZooKeeper zk = c.createMock(ZooKeeper.class); + @SuppressWarnings("rawtypes") + ServiceRegistration sreg = c.createMock(ServiceRegistration.class); + + PublishingEndpointListenerFactory eplf = new PublishingEndpointListenerFactory(zk, ctx); + + EasyMock.expect(ctx.registerService(EasyMock.eq(EndpointListener.class.getName()), EasyMock.eq(eplf), + (Dictionary<String, String>)EasyMock.anyObject())).andReturn(sreg).once(); + + EasyMock.expect(ctx.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn("myUUID").anyTimes(); + + PublishingEndpointListener eli = c.createMock(PublishingEndpointListener.class); + eli.close(); + EasyMock.expectLastCall().once(); + + c.replay(); + eplf.start(); + + PublishingEndpointListener service = eplf.getService(null, null); + assertNotNull(service); + assertTrue(service instanceof EndpointListener); + + List<PublishingEndpointListener> 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(); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java new file mode 100644 index 0000000..6e6dfb9 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java @@ -0,0 +1,209 @@ +/** + * 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.aries.rsa.discovery.zookeeper.publish; + +import static org.easymock.EasyMock.expect; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.aries.rsa.discovery.endpoint.EndpointDescriptionParser; +import org.apache.aries.rsa.discovery.endpoint.PropertiesMapper; +import org.apache.aries.rsa.discovery.zookeeper.publish.DiscoveryPlugin; +import org.apache.aries.rsa.discovery.zookeeper.publish.PublishingEndpointListener; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.easymock.EasyMock; +import org.easymock.IAnswer; +import org.easymock.IMocksControl; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.Filter; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.ServiceReference; +import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.RemoteConstants; +import org.osgi.xmlns.rsa.v1_0.EndpointDescriptionType; +import org.osgi.xmlns.rsa.v1_0.PropertyType; + +import junit.framework.TestCase; + +public class PublishingEndpointListenerTest extends TestCase { + + private static final String ENDPOINT_PATH = "/osgi/service_registry/myClass/google.de#80##test#sub"; + + public void testEndpointRemovalAdding() throws KeeperException, InterruptedException { + IMocksControl c = EasyMock.createNiceControl(); + + BundleContext ctx = c.createMock(BundleContext.class); + ZooKeeper zk = c.createMock(ZooKeeper.class); + + String path = ENDPOINT_PATH; + expectCreated(zk, path); + expectDeleted(zk, path); + + c.replay(); + + PublishingEndpointListener eli = new PublishingEndpointListener(zk, ctx); + EndpointDescription endpoint = createEndpoint(); + eli.endpointAdded(endpoint, null); + eli.endpointAdded(endpoint, null); // should do nothing + eli.endpointRemoved(endpoint, null); + eli.endpointRemoved(endpoint, null); // should do nothing + + c.verify(); + } + + public void testDiscoveryPlugin() throws Exception { + BundleContext ctx = EasyMock.createMock(BundleContext.class); + stubCreateFilter(ctx); + ctx.addServiceListener(EasyMock.isA(ServiceListener.class), + EasyMock.eq("(objectClass=" + DiscoveryPlugin.class.getName() + ")")); + + ServiceReference<DiscoveryPlugin> sr1 = createAppendPlugin(ctx); + ServiceReference<DiscoveryPlugin> sr2 = createPropertyPlugin(ctx); + + EasyMock.expect(ctx.getServiceReferences(DiscoveryPlugin.class.getName(), null)) + .andReturn(new ServiceReference[]{sr1, sr2}).anyTimes(); + EasyMock.replay(ctx); + + EndpointDescription endpoint = createEndpoint(); + + Map<String, Object> expectedProps = new HashMap<String, Object>(endpoint.getProperties()); + expectedProps.put("endpoint.id", "http://google.de:80/test/sub/appended"); + expectedProps.put("foo", "bar"); + expectedProps.put("service.imported", "true"); + + final ZooKeeper zk = EasyMock.createNiceMock(ZooKeeper.class); + String expectedFullPath = "/osgi/service_registry/org/foo/myClass/some.machine#9876##test"; + + List<PropertyType> props2 = new PropertiesMapper().fromProps(expectedProps); + EndpointDescriptionType epd = new EndpointDescriptionType(); + epd.getProperty().addAll(props2); + byte[] data = new EndpointDescriptionParser().getData(epd); + expectCreated(zk, expectedFullPath, EasyMock.aryEq(data)); + EasyMock.replay(zk); + + PublishingEndpointListener eli = new PublishingEndpointListener(zk, ctx); + + List<EndpointDescription> endpoints = getEndpoints(eli); + assertEquals("Precondition", 0, endpoints.size()); + eli.endpointAdded(endpoint, null); + assertEquals(1, endpoints.size()); + + //TODO enable + //EasyMock.verify(zk); + } + + + + public void testClose() throws KeeperException, InterruptedException { + IMocksControl c = EasyMock.createNiceControl(); + BundleContext ctx = c.createMock(BundleContext.class); + ZooKeeper zk = c.createMock(ZooKeeper.class); + expectCreated(zk, ENDPOINT_PATH); + expectDeleted(zk, ENDPOINT_PATH); + + c.replay(); + + PublishingEndpointListener eli = new PublishingEndpointListener(zk, ctx); + EndpointDescription endpoint = createEndpoint(); + eli.endpointAdded(endpoint, null); + eli.close(); // should result in zk.delete(...) + + c.verify(); + } + + @SuppressWarnings("unchecked") + private ServiceReference<DiscoveryPlugin> createAppendPlugin(BundleContext ctx) { + DiscoveryPlugin plugin1 = new DiscoveryPlugin() { + public String process(Map<String, Object> mutableProperties, String endpointKey) { + String eid = (String) mutableProperties.get("endpoint.id"); + mutableProperties.put("endpoint.id", eid + "/appended"); + return endpointKey; + } + }; + ServiceReference<DiscoveryPlugin> sr1 = EasyMock.createMock(ServiceReference.class); + EasyMock.expect(ctx.getService(sr1)).andReturn(plugin1).anyTimes(); + return sr1; + } + + @SuppressWarnings("unchecked") + private ServiceReference<DiscoveryPlugin> createPropertyPlugin(BundleContext ctx) { + DiscoveryPlugin plugin2 = new DiscoveryPlugin() { + public String process(Map<String, Object> mutableProperties, String endpointKey) { + mutableProperties.put("foo", "bar"); + return endpointKey.replaceAll("localhost", "some.machine"); + } + }; + ServiceReference<DiscoveryPlugin> sr2 = EasyMock.createMock(ServiceReference.class); + EasyMock.expect(ctx.getService(sr2)).andReturn(plugin2).anyTimes(); + return sr2; + } + + @SuppressWarnings("unchecked") + private List<EndpointDescription> getEndpoints(PublishingEndpointListener eli) throws Exception { + Field field = eli.getClass().getDeclaredField("endpoints"); + field.setAccessible(true); + return (List<EndpointDescription>) field.get(eli); + } + + private void stubCreateFilter(BundleContext ctx) throws InvalidSyntaxException { + EasyMock.expect(ctx.createFilter(EasyMock.isA(String.class))).andAnswer(new IAnswer<Filter>() { + public Filter answer() throws Throwable { + return FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0]); + } + }).anyTimes(); + } + + private void expectCreated(ZooKeeper zk, String path, byte[] dataMatcher) throws KeeperException, InterruptedException { + expect(zk.create(EasyMock.eq(path), + dataMatcher, + EasyMock.eq(Ids.OPEN_ACL_UNSAFE), + EasyMock.eq(CreateMode.EPHEMERAL))) + .andReturn(""); + } + + private void expectCreated(ZooKeeper zk, String path) throws KeeperException, InterruptedException { + expect(zk.create(EasyMock.eq(path), + (byte[])EasyMock.anyObject(), + EasyMock.eq(Ids.OPEN_ACL_UNSAFE), + EasyMock.eq(CreateMode.EPHEMERAL))) + .andReturn(""); + } + + private void expectDeleted(ZooKeeper zk, String path) throws InterruptedException, KeeperException { + zk.delete(EasyMock.eq(path), EasyMock.eq(-1)); + EasyMock.expectLastCall().once(); + } + + private EndpointDescription createEndpoint() { + 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/sub"); + props.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "myConfig"); + return new EndpointDescription(props); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/server/ZookeeperStarterTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/server/ZookeeperStarterTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/server/ZookeeperStarterTest.java new file mode 100644 index 0000000..7d58fe3 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/server/ZookeeperStarterTest.java @@ -0,0 +1,82 @@ +/** + * 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.aries.rsa.discovery.zookeeper.server; + +import java.io.File; +import java.util.Dictionary; +import java.util.Hashtable; + +import junit.framework.TestCase; + +import org.apache.aries.rsa.discovery.zookeeper.server.ZookeeperStarter; +import org.apache.aries.rsa.discovery.zookeeper.server.ZookeeperStarter.MyZooKeeperServerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.easymock.classextension.EasyMock; +import org.easymock.classextension.IMocksControl; +import org.osgi.framework.BundleContext; + +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +public class ZookeeperStarterTest extends TestCase { + + public void testUpdateConfig() throws Exception { + final File tempDir = new File("target"); + IMocksControl control = EasyMock.createControl(); + BundleContext bc = control.createMock(BundleContext.class); + expect(bc.getDataFile("")).andReturn(tempDir); + final MyZooKeeperServerMain mockServer = control.createMock(MyZooKeeperServerMain.class); + control.replay(); + + ZookeeperStarter starter = new ZookeeperStarter(bc) { + @Override + protected void startFromConfig(QuorumPeerConfig config) { + assertEquals(1234, config.getClientPortAddress().getPort()); + assertTrue(config.getDataDir().contains(tempDir + File.separator + "zkdata")); + assertEquals(2000, config.getTickTime()); + assertEquals(10, config.getInitLimit()); + assertEquals(5, config.getSyncLimit()); + this.main = mockServer; + } + }; + Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put("clientPort", "1234"); + starter.updated(props); + assertNotNull(starter.main); + + control.verify(); + } + + public void testRemoveConfiguration() throws Exception { + BundleContext bc = EasyMock.createMock(BundleContext.class); + MyZooKeeperServerMain zkServer = EasyMock.createMock(MyZooKeeperServerMain.class); + zkServer.shutdown(); + EasyMock.expectLastCall(); + + replay(zkServer); + + ZookeeperStarter starter = new ZookeeperStarter(bc); + starter.main = zkServer; + starter.updated(null); + + verify(zkServer); + assertNull("main should be null", starter.main); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorManagerTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorManagerTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorManagerTest.java new file mode 100644 index 0000000..49c9dad --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorManagerTest.java @@ -0,0 +1,113 @@ +/** + * 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.aries.rsa.discovery.zookeeper.subscribe; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.aries.rsa.discovery.zookeeper.subscribe.InterfaceMonitorManager; +import org.apache.zookeeper.ZooKeeper; +import org.easymock.EasyMock; +import org.easymock.IAnswer; +import org.easymock.IMocksControl; +import org.junit.Test; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.remoteserviceadmin.EndpointListener; + +public class InterfaceMonitorManagerTest { + + @Test + public void testEndpointListenerTrackerCustomizer() { + IMocksControl c = EasyMock.createNiceControl(); + BundleContext ctx = c.createMock(BundleContext.class); + ServiceReference<EndpointListener> sref = createService(c); + ServiceReference<EndpointListener> sref2 = createService(c); + ZooKeeper zk = c.createMock(ZooKeeper.class); + InterfaceMonitorManager eltc = new InterfaceMonitorManager(ctx, zk); + + c.replay(); + + // sref has no scope -> nothing should happen + assertEquals(0, eltc.getEndpointListenerScopes().size()); + assertEquals(0, eltc.getInterests().size()); + + eltc.addInterest(sref, "(objectClass=mine)", "mine"); + assertScopeIncludes(sref, eltc); + assertEquals(1, eltc.getEndpointListenerScopes().size()); + assertEquals(1, eltc.getInterests().size()); + + eltc.addInterest(sref, "(objectClass=mine)", "mine"); + assertScopeIncludes(sref, eltc); + assertEquals(1, eltc.getEndpointListenerScopes().size()); + assertEquals(1, eltc.getInterests().size()); + + eltc.addInterest(sref2, "(objectClass=mine)", "mine"); + assertScopeIncludes(sref, eltc); + assertScopeIncludes(sref2, eltc); + assertEquals(2, eltc.getEndpointListenerScopes().size()); + assertEquals(1, eltc.getInterests().size()); + + eltc.removeInterest(sref); + assertScopeIncludes(sref2, eltc); + assertEquals(1, eltc.getEndpointListenerScopes().size()); + assertEquals(1, eltc.getInterests().size()); + + eltc.removeInterest(sref); + assertScopeIncludes(sref2, eltc); + assertEquals(1, eltc.getEndpointListenerScopes().size()); + assertEquals(1, eltc.getInterests().size()); + + eltc.removeInterest(sref2); + assertEquals(0, eltc.getEndpointListenerScopes().size()); + assertEquals(0, eltc.getInterests().size()); + + c.verify(); + } + + @SuppressWarnings("unchecked") + private ServiceReference<EndpointListener> createService(IMocksControl c) { + final Map<String, ?> p = new HashMap<String, Object>(); + ServiceReference<EndpointListener> sref = c.createMock(ServiceReference.class); + EasyMock.expect(sref.getPropertyKeys()).andAnswer(new IAnswer<String[]>() { + public String[] answer() throws Throwable { + return p.keySet().toArray(new String[p.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.get(key); + } + }).anyTimes(); + return sref; + } + + private void assertScopeIncludes(ServiceReference<EndpointListener> sref, InterfaceMonitorManager eltc) { + List<String> srefScope = eltc.getEndpointListenerScopes().get(sref); + assertEquals(1, srefScope.size()); + assertEquals("(objectClass=mine)", srefScope.get(0)); + + } + +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorTest.java new file mode 100644 index 0000000..3a54399 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/subscribe/InterfaceMonitorTest.java @@ -0,0 +1,68 @@ +/** + * 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.aries.rsa.discovery.zookeeper.subscribe; + +import java.util.Collections; + +import junit.framework.TestCase; + +import org.apache.aries.rsa.discovery.zookeeper.subscribe.InterfaceMonitor; +import org.apache.aries.rsa.discovery.zookeeper.util.Utils; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.osgi.service.remoteserviceadmin.EndpointListener; + +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; + +public class InterfaceMonitorTest extends TestCase { + + public void testInterfaceMonitor() throws KeeperException, InterruptedException { + IMocksControl c = EasyMock.createControl(); + + ZooKeeper zk = c.createMock(ZooKeeper.class); + expect(zk.getState()).andReturn(ZooKeeper.States.CONNECTED).anyTimes(); + + String scope = "(myProp=test)"; + String interf = "es.schaaf.test"; + String node = Utils.getZooKeeperPath(interf); + + EndpointListener endpointListener = c.createMock(EndpointListener.class); + InterfaceMonitor im = new InterfaceMonitor(zk, interf, endpointListener, scope); + zk.exists(eq(node), eq(im), eq(im), EasyMock.anyObject()); + EasyMock.expectLastCall().once(); + + expect(zk.exists(eq(node), eq(false))).andReturn(new Stat()).anyTimes(); + expect(zk.getChildren(eq(node), eq(false))).andReturn(Collections.<String> emptyList()).once(); + expect(zk.getChildren(eq(node), eq(im))).andReturn(Collections.<String> emptyList()).once(); + + c.replay(); + im.start(); + // simulate a zk callback + WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, node); + im.process(we); + c.verify(); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/util/UtilsTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/util/UtilsTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/util/UtilsTest.java new file mode 100644 index 0000000..4d41fb0 --- /dev/null +++ b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/util/UtilsTest.java @@ -0,0 +1,37 @@ +/** + * 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.aries.rsa.discovery.zookeeper.util; + +import org.apache.aries.rsa.discovery.zookeeper.util.Utils; + +import junit.framework.TestCase; + +public class UtilsTest extends TestCase { + + public void testGetZooKeeperPath() { + assertEquals(Utils.PATH_PREFIX + '/' + "org/example/Test", + Utils.getZooKeeperPath("org.example.Test")); + + // used for the recursive discovery + assertEquals(Utils.PATH_PREFIX, Utils.getZooKeeperPath(null)); + assertEquals(Utils.PATH_PREFIX, Utils.getZooKeeperPath("")); + } + + +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java b/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java deleted file mode 100644 index 84470c2..0000000 --- a/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * 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 DiscoveryDriverTest extends TestCase { - - public void testDUMMY() { - assertTrue(true); - } - -// public void testDiscoveryDriver() throws Exception { -// BundleContext bc = getDefaultBundleContext(); -// Dictionary<String, String> props = getDefaultProps(); -// -// final StringBuilder sb = new StringBuilder(); -// DiscoveryDriver dd = new DiscoveryDriver(bc, props) { -// @Override -// ZooKeeper createZooKeeper() throws IOException { -// sb.append(zkHost + ":" + zkPort); -// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); -// EasyMock.replay(zk); -// return zk; -// } -// }; -// EasyMock.verify(bc); -// assertEquals("somehost:1910", sb.toString()); -// -// EasyMock.verify(dd.zooKeeper); -// EasyMock.reset(dd.zooKeeper); -// dd.zooKeeper.close(); -// EasyMock.expectLastCall(); -// EasyMock.replay(dd.zooKeeper); -// -// ServiceTracker st1 = EasyMock.createMock(ServiceTracker.class); -// st1.close(); -// EasyMock.expectLastCall(); -// EasyMock.replay(st1); -// ServiceTracker st2 = EasyMock.createMock(ServiceTracker.class); -// st2.close(); -// EasyMock.expectLastCall(); -// EasyMock.replay(st2); -// -// dd.lookupTracker = st1; -// dd.publicationTracker = st2; -// -// dd.destroy(); -// } -// -// private void expectServiceTrackerCalls(BundleContext bc, String objectClass) -// throws InvalidSyntaxException { -// Filter filter = EasyMock.createNiceMock(Filter.class); -// EasyMock.replay(filter); -// -// EasyMock.expect(bc.createFilter("(objectClass=" + objectClass + ")")) -// .andReturn(filter).anyTimes(); -// bc.addServiceListener((ServiceListener) EasyMock.anyObject(), -// EasyMock.eq("(objectClass=" + objectClass + ")")); -// EasyMock.expectLastCall().anyTimes(); -// EasyMock.expect(bc.getServiceReferences(objectClass, null)) -// .andReturn(new ServiceReference [0]).anyTimes(); -// } -// -// public void testProcessEvent() throws Exception { -// DiscoveryDriver db = new DiscoveryDriver(getDefaultBundleContext(), getDefaultProps()) { -// @Override -// ZooKeeper createZooKeeper() throws IOException { -// return null; -// } -// }; -// -// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(null, null); -// List<InterfaceMonitor> l1 = new ArrayList<InterfaceMonitor>(); -// InterfaceMonitor dm1a = EasyMock.createMock(InterfaceMonitor.class); -// dm1a.process(); -// EasyMock.expectLastCall(); -// EasyMock.replay(dm1a); -// InterfaceMonitor dm1b = EasyMock.createMock(InterfaceMonitor.class); -// dm1b.process(); -// EasyMock.expectLastCall(); -// EasyMock.replay(dm1b); -// l1.add(dm1a); -// l1.add(dm1b); -// -// List<InterfaceMonitor> l2 = new ArrayList<InterfaceMonitor>(); -// InterfaceMonitor dm2 = EasyMock.createMock(InterfaceMonitor.class); -// dm2.process(); -// EasyMock.expectLastCall(); -// EasyMock.replay(dm2); -// l2.add(dm2); -// -// fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l1); -// fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l2); -// -// db.finderCustomizer = fc; -// db.process(null); -// -// EasyMock.verify(dm1a); -// EasyMock.verify(dm1b); -// EasyMock.verify(dm2); -// } -// -// private BundleContext getDefaultBundleContext() throws InvalidSyntaxException { -// BundleContext bc = EasyMock.createMock(BundleContext.class); -// expectServiceTrackerCalls(bc, ServicePublication.class.getName()); -// expectServiceTrackerCalls(bc, DiscoveredServiceTracker.class.getName()); -// EasyMock.replay(bc); -// return bc; -// } -// -// private Dictionary<String, String> getDefaultProps() { -// Dictionary<String, String> props = new Hashtable<String, String>(); -// props.put("zookeeper.host", "somehost"); -// props.put("zookeeper.port", "1910"); -// props.put("zookeeper.timeout", "1500"); -// return props; -// } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/69bb901e/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java b/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java deleted file mode 100644 index cb2180b..0000000 --- a/discovery/zookeeper/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java +++ /dev/null @@ -1,301 +0,0 @@ -/** - * 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 FindInZooKeeperCustomizerTest extends TestCase { - - public void testDUMMY() { - assertTrue(true); - } - -// public void testAddingServiceInterface() { -// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() { -// public void serviceChanged(DiscoveredServiceNotification dsn) {} -// }; -// -// ServiceReference sr = EasyMock.createMock(ServiceReference.class); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(Collections.singleton(String.class.getName())); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(null); -// EasyMock.replay(sr); -// -// DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() { -// public void serviceChanged(DiscoveredServiceNotification dsn) {} -// }; -// -// ServiceReference sr2 = EasyMock.createMock(ServiceReference.class); -// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(Arrays.asList(Integer.class.getName(), Comparable.class.getName())); -// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(null); -// EasyMock.replay(sr2); -// -// BundleContext bc = EasyMock.createMock(BundleContext.class); -// EasyMock.expect(bc.getService(sr)).andReturn(dst); -// EasyMock.expect(bc.getService(sr2)).andReturn(dst2); -// EasyMock.replay(bc); -// -// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); -// zkExpectExists(zk, String.class.getName()); -// zkExpectExists(zk, Integer.class.getName()); -// zkExpectExists(zk, Comparable.class.getName()); -// EasyMock.expectLastCall(); -// EasyMock.replay(zk); -// -// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(bc, zk); -// -// // --------------------------------------------------------------- -// // Test the addingService APIs -// // --------------------------------------------------------------- -// -// assertEquals("Precondition failed", 0, fc.watchers.size()); -// fc.addingService(sr); -// assertEquals(1, fc.watchers.size()); -// -// DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next(); -// assertSame(dst, key); -// List<InterfaceMonitor> dmList = fc.watchers.get(key); -// assertEquals(1, dmList.size()); -// InterfaceMonitor dm = dmList.iterator().next(); -// assertNotNull(dm.listener); -// assertSame(zk, dm.zookeeper); -// assertEquals(Utils.getZooKeeperPath(String.class.getName()), dm.znode); -// -// assertEquals("Precondition failed", 1, fc.watchers.size()); -// fc.addingService(sr2); -// assertEquals(2, fc.watchers.size()); -// -// assertTrue(fc.watchers.containsKey(dst)); -// assertTrue(fc.watchers.containsKey(dst2)); -// assertEquals(dmList, fc.watchers.get(dst)); -// List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2); -// assertEquals(2, dmList2.size()); -// -// Set<String> actual = new HashSet<String>(); -// for (InterfaceMonitor im : dmList2) { -// actual.add(im.znode); -// } -// Set<String> expected = new HashSet<String>(Arrays.asList( -// Utils.getZooKeeperPath(Integer.class.getName()), -// Utils.getZooKeeperPath(Comparable.class.getName()))); -// assertEquals(expected, actual); -// -// EasyMock.verify(zk); -// -// // --------------------------------------------------------------- -// // Test the modifiedService APIs -// // --------------------------------------------------------------- -// EasyMock.reset(zk); -// zkExpectExists(zk, List.class.getName()); -// EasyMock.replay(zk); -// -// EasyMock.reset(sr); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(Collections.singleton(List.class.getName())); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(null); -// EasyMock.replay(sr); -// -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// fc.modifiedService(sr, dst); -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// -// assertTrue(fc.watchers.containsKey(dst)); -// assertTrue(fc.watchers.containsKey(dst2)); -// assertEquals(dmList2, fc.watchers.get(dst2)); -// List<InterfaceMonitor> dmList3 = fc.watchers.get(dst); -// assertEquals(1, dmList3.size()); -// assertEquals(Utils.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode); -// -// EasyMock.verify(zk); -// -// // --------------------------------------------------------------- -// // Test the removedService APIs -// // --------------------------------------------------------------- -// EasyMock.reset(zk); -// EasyMock.replay(zk); -// -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// fc.removedService(sr2, dst2); -// assertEquals("Precondition failed", 1, fc.watchers.size()); -// -// assertEquals(dmList3, fc.watchers.get(dst)); -// assertNull(fc.watchers.get(dst2)); -// -// EasyMock.verify(zk); -// } -// -// public void testAddingServiceFilter() { -// DiscoveredServiceTracker dst = new DiscoveredServiceTracker() { -// public void serviceChanged(DiscoveredServiceNotification dsn) {} -// }; -// -// ServiceReference sr = EasyMock.createMock(ServiceReference.class); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(null); -// Set<String> stringFilter = Collections.singleton("(objectClass=java.lang.String)"); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(stringFilter); -// EasyMock.replay(sr); -// -// DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() { -// public void serviceChanged(DiscoveredServiceNotification dsn) {} -// }; -// -// ServiceReference sr2 = EasyMock.createMock(ServiceReference.class); -// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(null); -// List<String> combinedFilter = -// Arrays.asList("(objectClass=java.lang.Integer)", "(objectClass=java.lang.Comparable)"); -// EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(combinedFilter); -// EasyMock.replay(sr2); -// -// BundleContext bc = EasyMock.createMock(BundleContext.class); -// EasyMock.expect(bc.getService(sr)).andReturn(dst); -// EasyMock.expect(bc.getService(sr2)).andReturn(dst2); -// EasyMock.replay(bc); -// -// ZooKeeper zk = EasyMock.createMock(ZooKeeper.class); -// zkExpectExists(zk, String.class.getName()); -// zkExpectExists(zk, Integer.class.getName()); -// zkExpectExists(zk, Comparable.class.getName()); -// EasyMock.expectLastCall(); -// EasyMock.replay(zk); -// -// FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(bc, zk); -// -// // --------------------------------------------------------------- -// // Test the addingService APIs -// // --------------------------------------------------------------- -// -// assertEquals("Precondition failed", 0, fc.watchers.size()); -// fc.addingService(sr); -// assertEquals(1, fc.watchers.size()); -// -// DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next(); -// assertSame(dst, key); -// List<InterfaceMonitor> dmList = fc.watchers.get(key); -// assertEquals(1, dmList.size()); -// InterfaceMonitor dm = dmList.iterator().next(); -// assertNotNull(dm.listener); -// assertSame(zk, dm.zookeeper); -// assertEquals(Utils.getZooKeeperPath(String.class.getName()), dm.znode); -// -// assertEquals("Precondition failed", 1, fc.watchers.size()); -// fc.addingService(sr2); -// assertEquals(2, fc.watchers.size()); -// -// assertTrue(fc.watchers.containsKey(dst)); -// assertTrue(fc.watchers.containsKey(dst2)); -// assertEquals(dmList, fc.watchers.get(dst)); -// List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2); -// assertEquals(2, dmList2.size()); -// Set<String> actual = new HashSet<String>(); -// for (InterfaceMonitor im : dmList2) { -// actual.add(im.znode); -// } -// Set<String> expected = new HashSet<String>(Arrays.asList( -// Utils.getZooKeeperPath(Integer.class.getName()), -// Utils.getZooKeeperPath(Comparable.class.getName()))); -// assertEquals(expected, actual); -// -// EasyMock.verify(zk); -// -// // --------------------------------------------------------------- -// // Test the modifiedService APIs -// // --------------------------------------------------------------- -// EasyMock.reset(zk); -// zkExpectExists(zk, List.class.getName()); -// EasyMock.replay(zk); -// -// EasyMock.reset(sr); -// Set<String> listFilter = Collections.singleton("(objectClass=java.util.List)"); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)) -// .andReturn(null); -// EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)) -// .andReturn(listFilter); -// EasyMock.replay(sr); -// -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// fc.modifiedService(sr, dst); -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// -// assertTrue(fc.watchers.containsKey(dst)); -// assertTrue(fc.watchers.containsKey(dst2)); -// assertEquals(dmList2, fc.watchers.get(dst2)); -// List<InterfaceMonitor> dmList3 = fc.watchers.get(dst); -// assertEquals(1, dmList3.size()); -// assertEquals(Utils.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode); -// -// EasyMock.verify(zk); -// -// // --------------------------------------------------------------- -// // Test the removedService APIs -// // --------------------------------------------------------------- -// EasyMock.reset(zk); -// EasyMock.replay(zk); -// -// assertEquals("Precondition failed", 2, fc.watchers.size()); -// fc.removedService(sr2, dst2); -// assertEquals("Precondition failed", 1, fc.watchers.size()); -// -// assertEquals(dmList3, fc.watchers.get(dst)); -// assertNull(fc.watchers.get(dst2)); -// -// EasyMock.verify(zk); -// } -// -// public void testGetInterfacesFromFilter() { -// testGetInterfacesFromFilter("objectClass=org.apache_2.Some$FunnyClass", -// "org.apache_2.Some$FunnyClass"); -// testGetInterfacesFromFilter("(&(a=b)(objectClass = org.acme.Q)", -// "org.acme.Q"); -// testGetInterfacesFromFilter("(&(objectClassIdentifier=b)(objectClass = org.acme.Q)", -// "org.acme.Q"); -// testGetInterfacesFromFilter("(|(OBJECTCLASS= X )(objectclass = Y)", -// "X", "Y"); -// testGetInterfacesFromFilter(new String[] {"(objectClass=X)", "(objectClass=Y)"}, -// "X", "Y"); -// } -// -// private void testGetInterfacesFromFilter(String filter, String ... interfaces) { -// testGetInterfacesFromFilter(new String[] {filter}, interfaces); -// } -// -// private void testGetInterfacesFromFilter(String[] filters, String ... interfaces) { -// FindInZooKeeperCustomizer.getInterfacesFromFilter(Arrays.asList(filters)); -// } -// -// private void zkExpectExists(ZooKeeper zk, String className) { -// zk.exists(EasyMock.eq(Utils.getZooKeeperPath(className)), -// (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; -// } -// }); -// } -}
