[zb4osgi-changeset] [scm] ZigBee 4 OSGi repository change: r1095 - in /projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api: impl/ZigBeeDeviceImplTest.java impl/ZigBeeNodeImplTest.java test/ test/ZigBeeBaseDriverTestUnitBase.java

scm-notify at zb4osgi.aaloa.org scm-notify at zb4osgi.aaloa.org
Wed Nov 12 20:37:05 CET 2014


Author: stefano.lenzi
Date: Wed Nov 12 20:37:05 2014
New Revision: 1095

Log:
Created a first utility class for simplify the creation of TestUnit


Added:
    projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/
    projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/ZigBeeBaseDriverTestUnitBase.java   (with props)
Modified:
    projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeDeviceImplTest.java
    projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeNodeImplTest.java

Modified: projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeDeviceImplTest.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeDeviceImplTest.java	(original)
+++ projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeDeviceImplTest.java	Wed Nov 12 20:37:05 2014
@@ -1,5 +1,5 @@
 /*
-   Copyright 2008-2013 CNR-ISTI, http://isti.cnr.it
+   Copyright 2014-2014 CNR-ISTI, http://isti.cnr.it
    Institute of Information Science and Technologies
    of the Italian National Research Council
 
@@ -25,30 +25,20 @@
 
 import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.Iterator;
-
-import it.cnr.isti.primitvetypes.util.Integers;
+
 import it.cnr.isti.zigbee.api.Cluster;
+import it.cnr.isti.zigbee.api.ClusterFilter;
+import it.cnr.isti.zigbee.api.ClusterListner;
 import it.cnr.isti.zigbee.api.ZigBeeBasedriverException;
 import it.cnr.isti.zigbee.api.ZigBeeBasedriverTimeOutException;
 import it.cnr.isti.zigbee.api.ZigBeeDevice;
-import it.cnr.isti.zigbee.basedriver.Activator;
-import it.cnr.isti.zigbee.basedriver.configuration.ConfigurationService;
-import it.cnr.isti.zigbee.basedriver.discovery.DeviceBuilderThreadTest;
+import it.cnr.isti.zigbee.basedriver.api.test.ZigBeeBaseDriverTestUnitBase;
 import it.cnr.isti.zigbee.dongle.api.AFMessageListner;
-import it.cnr.isti.zigbee.dongle.api.DuplicateMacPolicy;
 import it.cnr.isti.zigbee.dongle.api.SimpleDriver;
 
 import org.easymock.IAnswer;
-import org.junit.Before;
-import org.junit.Test;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.itaca.ztool.api.ZToolPacket;
-import com.itaca.ztool.api.ZToolPacketStream;
+import org.junit.*;
+
 import com.itaca.ztool.api.af.AF_DATA_CONFIRM;
 import com.itaca.ztool.api.af.AF_DATA_REQUEST;
 import com.itaca.ztool.api.af.AF_INCOMING_MSG;
@@ -62,65 +52,12 @@
 
 
 /**
-*
-* @author <a href="mailto:stefano.lenzi at isti.cnr.it">Stefano "Kismet" Lenzi</a>
-* @version $LastChangedRevision$ ($LastChangedDate$ )
-*
-*/
-public class ZigBeeDeviceImplTest {
-
-    private ConfigurationService cs = null;
-    private BundleContext bc = null;
-
-    private BundleContext stub;
-
-    @Before
-    public void createActivatorStub() {
-        cs = createConfigurationServiceStub();
-        bc = createBundleContextStub();
-        Activator.setStubObjectes(cs, bc);
-    }
-
-    public BundleContext createBundleContextStub() {
-        stub = createMock(BundleContext.class);
-
-        try {
-            expect(stub.getServiceReferences(anyObject(String.class),anyObject(String.class)))
-            	.andReturn(null).anyTimes();
-            expect(stub.getProperty("org.aaloa.zb4osgi.zigbee.basedriver.timeout")).andReturn("10000").anyTimes();
-        } catch (InvalidSyntaxException ex) {
-        }
-        replay(stub);
-
-        return stub;
-    }
-
-    public ConfigurationService createConfigurationServiceStub() {
-        ConfigurationService stub = createMock(ConfigurationService.class);
-        int messageRetryCount = 10;
-        expect(stub.getFirstFreeEndPoint()).andReturn(new Integer(2))
-                .anyTimes();
-
-        expect(stub.getDeviceInspectionPeriod()).andReturn(new Long(250))
-                .anyTimes();
-
-        expect(stub.getMessageRetryCount()).andReturn(messageRetryCount)
-                .anyTimes();
-
-        expect(stub.getMessageRetryDelay()).andReturn(new Integer(100))
-                .anyTimes();
-
-        expect(stub.getPanId()).andReturn(new Short((short) 13531)).anyTimes();
-
-        expect(stub.getDiscoveryDuplicateMacPolicy())
-                .andReturn(DuplicateMacPolicy.IGNORE).times(1)
-                .andReturn(DuplicateMacPolicy.UPDATE).times(1)
-                .andReturn(DuplicateMacPolicy.REGISTER).times(1);
-
-        replay(stub);
-
-        return stub;
-    }
+ *
+ * @author <a href="mailto:stefano.lenzi at isti.cnr.it">Stefano "Kismet" Lenzi</a>
+ * @version $LastChangedRevision$ ($LastChangedDate$ )
+ *
+ */
+public class ZigBeeDeviceImplTest extends ZigBeeBaseDriverTestUnitBase {
 
     /**
      * This test verify if any pending {@link AFMessageConsumer} also known as {@link WaitForClusterResponse} is left 
@@ -221,7 +158,79 @@
 		assertEquals("Expecting no pending consumers", 0, device.getAFConsumers().size() );
 		
 	}
-    
+
+	
+    /**
+     * This verify that @
+     * @See <a href="http://zb4o.aaloa.org/redmine/issues/273">Redmine issue #273</a>
+     */
+	@Test
+	public void testRegisteringAFMessageListener() {
+		final HashSet<AFMessageListner> listeners = new HashSet<AFMessageListner>();
+		final ArrayList<ZigBeeDeviceImpl> network = new ArrayList<ZigBeeDeviceImpl>();
+		ZigBeeNodeImpl node = null;
+		ZigBeeDeviceImpl device = null;
+		try {
+			SimpleDriver drv = createMock(SimpleDriver.class);
+			ZDO_SIMPLE_DESC_RSP dsc = new ZDO_SIMPLE_DESC_RSP(new int[]{
+					0x9C, 0x40,										//16-bit Source Address
+					0x00,											//Status
+					0x9C, 0x40,										//16-bit Network Address
+					0x00,											//Length
+					0xC8,											//End Point Address
+					0x04, 0x01,										//Profile Id
+					0x02, 0x01,										//Device Id
+					0x00,											//Device Version
+					0x04,											//Input Cluster Count
+					0x00, 0x10, 0x10, 0x00, 0x00, 0x80, 0xF0, 0xFA, //Input Cluster List				
+					0x04,											//Output Cluster Count
+					0x00, 0x10, 0x10, 0x00, 0x00, 0x80, 0xF0, 0xFA, //Output Cluster List				
+			});		
+			AF_REGISTER_SRSP successRegister = new AF_REGISTER_SRSP(new int[]{0});
+			expect(drv.sendZDOSimpleDescriptionRequest(anyObject(ZDO_SIMPLE_DESC_REQ.class))).andReturn(dsc).anyTimes();
+			expect(drv.sendAFRegister(anyObject(AF_REGISTER.class))).andReturn(successRegister).anyTimes();
+			
+			expect(drv.addAFMessageListner(anyObject(AFMessageListner.class))).andAnswer(new IAnswer<Boolean>() {
+
+				public Boolean answer() throws Throwable {
+					return listeners.add( (AFMessageListner) getCurrentArguments()[0] );
+				}
+			}).once();
+			replay(drv);
+			node = new ZigBeeNodeImpl(40000, "00:00:00:00:00:AA", (short) 1);
+			device = new ZigBeeDeviceImpl(drv, node, (byte) 200);
+			device.addClusterListener(new ClusterListner() {
+				
+				public void setClusterFilter(ClusterFilter filter) {
+				}
+				
+				public void handleCluster(ZigBeeDevice device, Cluster cluster) {
+				}
+				
+				public ClusterFilter getClusterFilter() {
+					return null;
+				}
+			});
+			device.addClusterListener(new ClusterListner() {
+				
+				public void setClusterFilter(ClusterFilter filter) {
+				}
+				
+				public void handleCluster(ZigBeeDevice device, Cluster cluster) {
+				}
+				
+				public ClusterFilter getClusterFilter() {
+					return null;
+				}
+			});
+			verify(drv);
+			//response = device.invoke(new ClusterImpl(new byte[]{0x00,0x01,0x0a,0x20},(short) 0x100));
+		}catch(Exception ex){
+			System.out.println(ex.getMessage());
+		}
+		
+	}
+	
     
     /**
      * This test verify the dispatching of response from the network to OSGi service that proxying 

Modified: projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeNodeImplTest.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeNodeImplTest.java	(original)
+++ projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/impl/ZigBeeNodeImplTest.java	Wed Nov 12 20:37:05 2014
@@ -1,48 +1,36 @@
+/*
+   Copyright 2014-2014 CNR-ISTI, http://isti.cnr.it
+   Institute of Information Science and Technologies
+   of the Italian National Research Council
+
+
+   See the NOTICE file distributed with this work for additional
+   information regarding copyright ownership
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ */
 package it.cnr.isti.zigbee.basedriver.api.impl;
 
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.*;
 import static org.junit.Assert.*;
-import it.cnr.isti.zigbee.basedriver.Activator;
-import it.cnr.isti.zigbee.basedriver.configuration.ConfigurationService;
-import it.cnr.isti.zigbee.dongle.api.DuplicateMacPolicy;
+import it.cnr.isti.zigbee.basedriver.api.test.ZigBeeBaseDriverTestUnitBase;
 
-import org.junit.Before;
-import org.junit.Test;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
+import org.junit.*;
+
 
 import com.itaca.ztool.api.ZToolAddress64;
 
-public class ZigBeeNodeImplTest {
-
-    private ConfigurationService cs = null;
-    private BundleContext bc = null;
-
-    private BundleContext stub;
-
-    @Before
-    public void createActivatorStub() {
-        cs = createConfigurationServiceStub();
-        bc = createBundleContextStub();
-        Activator.setStubObjectes(cs, bc);
-    }
-
-    public BundleContext createBundleContextStub() {
-        stub = createMock(BundleContext.class);
-        return stub;
-    }
-
-    public ConfigurationService createConfigurationServiceStub() {
-        ConfigurationService stub = createMock(ConfigurationService.class);
-        expect(stub.getPanId()).andReturn(new Short((short) 13531)).anyTimes();
-
-        replay(stub);
-
-        return stub;
-    }
+public class ZigBeeNodeImplTest extends ZigBeeBaseDriverTestUnitBase {
     
     /**
      * This test verify that any negative value assigned as network address is translated 

Added: projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/ZigBeeBaseDriverTestUnitBase.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/ZigBeeBaseDriverTestUnitBase.java	(added)
+++ projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/ZigBeeBaseDriverTestUnitBase.java	Wed Nov 12 20:37:05 2014
@@ -0,0 +1,90 @@
+/*
+   Copyright 2014-2014 CNR-ISTI, http://isti.cnr.it
+   Institute of Information Science and Technologies
+   of the Italian National Research Council
+
+
+   See the NOTICE file distributed with this work for additional
+   information regarding copyright ownership
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ */
+package it.cnr.isti.zigbee.basedriver.api.test;
+
+import static org.easymock.EasyMock.*;
+
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.easymock.*;
+import org.junit.*;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+
+import it.cnr.isti.zigbee.basedriver.Activator;
+import it.cnr.isti.zigbee.basedriver.configuration.ConfigurationService;
+
+/**
+*
+* @author <a href="mailto:stefano.lenzi at isti.cnr.it">Stefano "Kismet" Lenzi</a>
+* @version $LastChangedRevision$ ($LastChangedDate$ )
+*
+*/
+public class ZigBeeBaseDriverTestUnitBase {
+
+
+    private ConfigurationService cs = null;
+    private BundleContext bc = null;
+
+    private BundleContext stub;
+    private Properties contextProperties = (Properties) System.getProperties().clone();
+
+    public BundleContext createBundleContextStub() {
+        stub = createMock(BundleContext.class);
+
+        try {
+            expect(stub.getServiceReferences(anyObject(String.class),anyObject(String.class)))
+            	.andReturn(null).anyTimes();
+            expect(stub.getProperty(anyObject(String.class))).andAnswer(new IAnswer<String>() {
+
+				public String answer() throws Throwable {
+					return contextProperties.getProperty((String) getCurrentArguments()[0]);
+				}
+				
+			}).anyTimes();
+        } catch (InvalidSyntaxException ex) {
+        }
+        replay(stub);
+
+        return stub;
+    }	
+	
+	protected ZigBeeBaseDriverTestUnitBase(){
+        bc = createBundleContextStub();
+		cs = new ConfigurationService();
+        Activator.setStubObjectes(cs, bc);
+	}
+	
+	protected void setBundleContextProperty(String name, String value) {
+		this.contextProperties.setProperty(name, value);
+	}
+	
+	protected void setBundleContextProperties(Properties update) {
+		Enumeration<Object> keys = update.keys();
+		while (keys.hasMoreElements()) {
+			String key = (String) keys.nextElement();
+			contextProperties.setProperty(key, update.getProperty(key));
+		}
+	}
+	
+}

Propchange: projects/zb4osgi/trunk/zigbee.basedriver/src/test/java/it/cnr/isti/zigbee/basedriver/api/test/ZigBeeBaseDriverTestUnitBase.java
------------------------------------------------------------------------------
    svn:executable = *




More information about the Commit mailing list