celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject [1/2] celix git commit: CELIX-313: fix tests and mocks for service_reference; service_registration; service_registry; and service_tracker
Date Tue, 01 Dec 2015 18:13:28 GMT
Repository: celix
Updated Branches:
  refs/heads/develop 7a6d31f42 -> 8a0ee2603


http://git-wip-us.apache.org/repos/asf/celix/blob/8a0ee260/framework/private/test/service_registry_test.cpp
----------------------------------------------------------------------
diff --git a/framework/private/test/service_registry_test.cpp b/framework/private/test/service_registry_test.cpp
index 34dab24..9fce76f 100644
--- a/framework/private/test/service_registry_test.cpp
+++ b/framework/private/test/service_registry_test.cpp
@@ -25,6 +25,7 @@
  */
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "CppUTest/TestHarness.h"
 #include "CppUTest/TestHarness_c.h"
@@ -45,10 +46,49 @@ void serviceRegistryTest_serviceChanged(framework_pt framework, service_event_ty
 			->withPointerParameters("registration", registration)
 			->withPointerParameters("oldprops", oldprops);
 }
+
+static char* my_strdup(const char* s) {
+	if (s == NULL) {
+		return NULL;
+	}
+
+	size_t len = strlen(s);
+
+	char *d = (char*) calloc(len + 1, sizeof(char));
+
+	if (d == NULL) {
+		return NULL;
+	}
+
+	strncpy(d, s, len);
+	return d;
+}
+
+static int registry_callback_t_isEqual(const void* object1, const void* object2)
+{
+	registry_callback_t callback1 = *(registry_callback_t*) object1;
+	registry_callback_t callback2 = *(registry_callback_t*) object2;
+	return 	callback1.getUsingBundles == callback2.getUsingBundles &&
+       		callback1.handle == callback2.handle &&
+       		callback1.modified == callback2.modified &&
+       		callback1.unregister == callback2.unregister;
+}
+
+static char * registry_callback_t_toString(const void* object)
+{
+	char buff[512];
+	registry_callback_t callback = *(registry_callback_t*) object;
+	snprintf(buff, 512, "< getUsingBudles: %p, handle: %p, modified: %p, unregister: %p >", callback.getUsingBundles, callback.handle, callback.modified, callback.unregister);
+
+	return my_strdup(buff);
+}
 }
 
 int main(int argc, char** argv) {
-	return RUN_ALL_TESTS(argc, argv);
+	mock_c()->installComparator("registry_callback_t", registry_callback_t_isEqual, registry_callback_t_toString);
+	int ret = RUN_ALL_TESTS(argc, argv);
+	mock_c()->removeAllComparators();
+	return ret;
 }
 
 TEST_GROUP(service_registry) {
@@ -79,10 +119,9 @@ TEST(service_registry, create) {
 }
 
 TEST(service_registry, getRegisteredServices) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
-	registry->serviceReferences = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadRwlock_create(&registry->lock, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 	array_list_pt registrations = NULL;
 	arrayList_create(&registrations);
 	service_registration_pt reg = (service_registration_pt) 0x10;
@@ -90,137 +129,131 @@ TEST(service_registry, getRegisteredServices) {
 	bundle_pt bundle = (bundle_pt) 0x20;
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);
 
+	hash_map_pt usages = hashMap_create(NULL, NULL, NULL, NULL);
 	service_reference_pt ref = (service_reference_pt) 0x30;
-
-	array_list_pt refs = NULL;
-	arrayList_create(&refs);
+	hashMap_put(usages, reg, ref);
+	hashMap_put(registry->serviceReferences, bundle, usages);
 
 	mock()
 		.expectOneCall("serviceRegistration_isValid")
 		.withParameter("registration", reg)
 		.andReturnValue(true);
 	mock()
-		.expectOneCall("serviceRegistration_getBundle")
-		.withParameter("registration", reg)
-		.withOutputParameterReturning("bundle", &bundle, sizeof(bundle))
-		.andReturnValue(CELIX_SUCCESS)
-		.ignoreOtherParameters();
-	mock()
-		.expectOneCall("serviceReference_create")
-		.withParameter("bundle", bundle)
-		.withParameter("registration", reg)
-		.withOutputParameterReturning("reference", &ref, sizeof(ref))
-		.andReturnValue(CELIX_SUCCESS)
-		.ignoreOtherParameters();
-	/*mock()
-		.expectOneCall("serviceRegistration_getServiceReferences")
-		.withParameter("registration", reg)
-		.withOutputParameterReturning("references", &refs, sizeof(refs))
-		.andReturnValue(CELIX_SUCCESS);*/
+		.expectOneCall("serviceReference_retain")
+		.withParameter("ref", ref);
 
 	array_list_pt services = NULL;
 	serviceRegistry_getRegisteredServices(registry, bundle, &services);
 	LONGS_EQUAL(1, arrayList_size(services));
 	POINTERS_EQUAL(ref, arrayList_get(services, 0));
 
-	free(registry);
+	arrayList_destroy(services);
+	arrayList_destroy(registrations);
+	hashMap_remove(registry->serviceRegistrations, bundle);
+	serviceRegistry_destroy(registry);
+	hashMap_destroy(usages, false, false);
 }
 
 TEST(service_registry, getServicesInUse) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	celixThreadRwlock_create(&registry->lock, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
-	array_list_pt usages = NULL;
-	arrayList_create(&usages);
+	hash_map_pt usages = hashMap_create(NULL, NULL, NULL, NULL);
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_reference_pt ref = (service_reference_pt) 0x20;
-	usage_count_pt usage = (usage_count_pt) malloc(sizeof(*usage));
-	usage->reference = ref;
-	arrayList_add(usages, usage);
-	hashMap_put(registry->inUseMap, bundle, usages);
+	service_registration_pt reg = (service_registration_pt) 0x30;
+	hashMap_put(usages, reg, ref);
+	hashMap_put(registry->serviceReferences, bundle, usages);
 
 	array_list_pt inUse = NULL;
 	serviceRegistry_getServicesInUse(registry, bundle, &inUse);
 	LONGS_EQUAL(1, arrayList_size(inUse));
 	POINTERS_EQUAL(ref, arrayList_get(inUse, 0));
 
-	free(registry);
+	arrayList_destroy(inUse);
+	serviceRegistry_destroy(registry);
+	hashMap_destroy(usages, false, false);
 }
 
 TEST(service_registry, registerServiceNoProps) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
-	registry->serviceChanged = NULL;
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
-	std::string serviceName = "service";
+	char * serviceName = my_strdup("service");
 	void *service = (void *) 0x20;
 	service_registration_pt reg = (service_registration_pt) 0x30;
 
 	mock()
 		.expectOneCall("serviceRegistration_create")
-		.withParameter("registry", registry)
+		.withParameterOfType("registry_callback_t", "callback", &registry->callback)
 		.withParameter("bundle", bundle)
-		.withParameter("serviceName", (char *) serviceName.c_str())
+		.withParameter("serviceName", serviceName)
 		.withParameter("serviceId", 2)
 		.withParameter("serviceObject", service)
 		.withParameter("dictionary", (void *) NULL)
 		.andReturnValue(reg);
 
+	mock().expectOneCall("serviceRegistryTest_serviceChanged")
+			.withParameter("framework", framework)
+			.withParameter("eventType", OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED)
+			.withParameter("registration", reg)
+			.withParameter("oldprops", (void*)NULL);
+
 	service_registration_pt registration = NULL;
-	serviceRegistry_registerService(registry, bundle, (char *) serviceName.c_str(), service, NULL, &registration);
+	serviceRegistry_registerService(registry, bundle, serviceName, service, NULL, &registration);
 	POINTERS_EQUAL(reg, registration);
 
-	free(registry);
+	array_list_pt destroy_this = (array_list_pt) hashMap_remove(registry->serviceRegistrations, bundle);
+	arrayList_destroy(destroy_this);
+	serviceRegistry_destroy(registry);
+	free(serviceName);
 }
 
 TEST(service_registry, registerServiceFactoryNoProps) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
-	registry->serviceChanged = NULL;
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
-	std::string serviceName = "service";
-	service_factory_pt factory = (service_factory_pt) 0x20;
-	service_registration_pt reg = (service_registration_pt) 0x30;
+	char * serviceName = my_strdup("service");
+	service_factory_pt factory = (service_factory_pt) malloc(sizeof(*factory));
+	factory->factory = (void*) 0x20;
+	service_registration_pt reg = (service_registration_pt) 0x40;
 
 	mock()
 		.expectOneCall("serviceRegistration_createServiceFactory")
-		.withParameter("registry", registry)
+		.withParameterOfType("registry_callback_t", "callback", &registry->callback)
 		.withParameter("bundle", bundle)
-		.withParameter("serviceName", (char *) serviceName.c_str())
+		.withParameter("serviceName", serviceName)
 		.withParameter("serviceId", 2)
 		.withParameter("serviceObject", factory)
 		.withParameter("dictionary", (void *) NULL)
 		.andReturnValue(reg);
 
+	mock().expectOneCall("serviceRegistryTest_serviceChanged")
+		.withParameter("framework", framework)
+		.withParameter("eventType", OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED)
+		.withParameter("registration", reg)
+		.withParameter("oldprops", (void*)NULL);
+
 	service_registration_pt registration = NULL;
-	serviceRegistry_registerServiceFactory(registry, bundle, (char *) serviceName.c_str(), factory, NULL, &registration);
+	serviceRegistry_registerServiceFactory(registry, bundle, serviceName, factory, NULL, &registration);
 	POINTERS_EQUAL(reg, registration);
 
-	free(registry);
+	array_list_pt destroy_this = (array_list_pt) hashMap_remove(registry->serviceRegistrations, bundle);
+	arrayList_destroy(destroy_this);
+	serviceRegistry_destroy(registry);
+	free(serviceName);
+	free(factory);
 }
 
 TEST(service_registry, unregisterService) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
-	registry->serviceChanged = NULL;
-
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
 	array_list_pt registrations = NULL;
@@ -228,17 +261,8 @@ TEST(service_registry, unregisterService) {
 	arrayList_add(registrations, registration);
 
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);
-
 	properties_pt properties = (properties_pt) 0x30;
 
-	array_list_pt references = NULL;
-	arrayList_create(&references);
-	service_reference_pt reference = (service_reference_pt) 0x40;
-	arrayList_add(references, reference);
-
-	framework_pt framework = (framework_pt) 0x50;
-	registry->framework = framework;
-
 	mock()
 		.expectOneCall("serviceRegistration_getProperties")
 		.withParameter("registration", registration)
@@ -249,38 +273,33 @@ TEST(service_registry, unregisterService) {
 		.withParameter("properties", properties)
 		.withParameter("key", "objectClass")
 		.andReturnValue("test");
+
 	mock()
-		.expectOneCall("serviceRegistration_getServiceReferences")
+		.expectOneCall("serviceRegistryTest_serviceChanged")
+		.withParameter("framework", framework)
+		.withParameter("eventType", OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING)
 		.withParameter("registration", registration)
-		.withOutputParameterReturning("references", &references, sizeof(references))
-		.andReturnValue(CELIX_SUCCESS);
-	mock()
-		.expectOneCall("serviceReference_invalidate")
-		.withParameter("reference", reference)
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("oldprops", (void*) NULL);
 
-	mock()
+		mock()
 		.expectOneCall("serviceRegistration_invalidate")
-		.withParameter("registration", registration)
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("registration", registration);
 
 	mock()
-		.expectOneCall("serviceRegistration_destroy")
+		.expectOneCall("serviceRegistration_release")
 		.withParameter("registration", registration);
 
+
+
 	serviceRegistry_unregisterService(registry, bundle, registration);
 
-	free(registry);
+	serviceRegistry_destroy(registry);
 }
 
-TEST(service_registry, unregisterServices) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+/*TEST(service_registry, unregisterServices) {
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework, NULL, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
@@ -298,21 +317,16 @@ TEST(service_registry, unregisterServices) {
 		.expectOneCall("serviceRegistration_unregister")
 		.withParameter("registration", registration)
 		.andReturnValue(CELIX_SUCCESS);
-	serviceRegistry_unregisterServices(registry, bundle);
+	serviceRegistry_unregisterService(registry, bundle, );
 	LONGS_EQUAL(0, hashMap_size(registry->serviceRegistrations));
 
 	free(registry);
-}
+}*/
 
-TEST(service_registry, getServiceReferencesForRegistration){
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	celixThreadMutex_create(&registry->referencesMapMutex, NULL);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+/*TEST(service_registry, getServiceReferencesForRegistration){
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
@@ -329,31 +343,30 @@ TEST(service_registry, getServiceReferencesForRegistration){
 	arrayList_add(references, reference);
 
 	serviceRegistry_getServiceReferencesForRegistration(registry, registration, &references);
-}
+}*/
 
 TEST(service_registry, getServiceReferences) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	celixThreadMutex_create(&registry->referencesMapMutex, NULL);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
 	array_list_pt registrations = NULL;
 	arrayList_create(&registrations);
 	arrayList_add(registrations, registration);
-	registry->serviceReferences = hashMap_create(NULL, NULL, NULL, NULL);
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);
 
 	properties_pt properties = (properties_pt) 0x30;
-	array_list_pt references = NULL;
-	arrayList_create(&references);
-	service_reference_pt reference = (service_reference_pt) 0x40;
-	arrayList_add(references, reference);
+
+	hash_map_pt usages = hashMap_create(NULL, NULL, NULL, NULL);
+	service_reference_pt reference = (service_reference_pt) 0x30;
+	hashMap_put(usages, registration, reference);
+	hashMap_put(registry->serviceReferences, bundle, usages);
+
+	mock()
+		.expectOneCall("serviceRegistration_retain")
+		.withParameter("registration", registration);
 
 	mock()
 		.expectOneCall("serviceRegistration_getProperties")
@@ -370,42 +383,46 @@ TEST(service_registry, getServiceReferences) {
 		.expectOneCall("serviceRegistration_isValid")
 		.withParameter("registration", registration)
 		.andReturnValue(true);
+
 	mock()
+		.expectOneCall("serviceReference_retain")
+		.withParameter("ref", reference);
+	/*mock()
 		.expectOneCall("serviceRegistration_getBundle")
 		.withParameter("registration", registration)
 		.withOutputParameterReturning("bundle", &bundle, sizeof(bundle))
 		.andReturnValue(CELIX_SUCCESS)
 		.ignoreOtherParameters();
+
 	mock()
 		.expectOneCall("serviceReference_create")
-		.withParameter("bundle", bundle)
+		.withParameter("referenceOwner", bundle)
 		.withParameter("registration", registration)
 		.withOutputParameterReturning("reference", &reference, sizeof(reference))
-		.andReturnValue(CELIX_SUCCESS)
-		.ignoreOtherParameters();
-	/*mock()
-		.expectOneCall("serviceRegistration_getServiceReferences")
-		.withParameter("registration", registration)
-		.withOutputParameterReturning("references", &references, sizeof(references))
-		.andReturnValue(CELIX_SUCCESS);*/
+		.ignoreOtherParameters();*/
+
+
+	mock()
+		.expectOneCall("serviceRegistration_release")
+		.withParameter("registration", registration);
 
 	array_list_pt actual  = NULL;
 
-	serviceRegistry_getServiceReferences(registry, NULL, "test", NULL, &actual);
+	serviceRegistry_getServiceReferences(registry, bundle, "test", NULL, &actual);
 	LONGS_EQUAL(1, arrayList_size(actual));
 	POINTERS_EQUAL(reference, arrayList_get(actual, 0));
 
-	free(registry);
+	hashMap_destroy(usages, false, false);
+	arrayList_destroy(actual);
+	arrayList_destroy(registrations);
+	hashMap_remove(registry->serviceRegistrations, bundle);
+	serviceRegistry_destroy(registry);
 }
 
 TEST(service_registry, getService) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
@@ -415,13 +432,9 @@ TEST(service_registry, getService) {
 
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);
 
-	properties_pt properties = (properties_pt) 0x30;
-	array_list_pt references = NULL;
-	arrayList_create(&references);
 	service_reference_pt reference = (service_reference_pt) 0x40;
-	arrayList_add(references, reference);
 
-	module_pt module = (module_pt) 0x50;
+	hashMap_put(registry->deletedServiceReferences, reference, (void*) false);
 
 	void * service = (void *) 0x60;
 
@@ -434,37 +447,43 @@ TEST(service_registry, getService) {
 		.expectOneCall("serviceRegistration_isValid")
 		.withParameter("registration", registration)
 		.andReturnValue(true);
-	/*mock()
-		.expectOneCall("bundle_getCurrentModule")
-		.withParameter("bundle", bundle)
-		.withOutputParameterReturning("module", &module, sizeof(module))
-		.andReturnValue(CELIX_SUCCESS);*/
+
+	int count = 1;
+	mock()
+		.expectOneCall("serviceReference_increaseUsage")
+		.withParameter("reference", reference)
+		.withOutputParameterReturning("updatedCount", &count, sizeof(count));
+
 	mock()
 		.expectOneCall("serviceRegistration_getService")
 		.withParameter("registration", registration)
 		.withParameter("bundle", bundle)
 		.withOutputParameterReturning("service", &service, sizeof(service))
 		.andReturnValue(true);
+
 	mock()
-		.expectOneCall("serviceRegistration_isValid")
-		.withParameter("registration", registration)
-		.andReturnValue(true);
+		.expectOneCall("serviceReference_setService")
+		.withParameter("ref", reference)
+		.withParameter("service", service);
+
+	mock()
+		.expectOneCall("serviceReference_getService")
+		.withParameter("reference", reference)
+		.withOutputParameterReturning("service", &service, sizeof(service));
 
 	void *actual = NULL;
 	serviceRegistry_getService(registry, bundle, reference, &actual);
 	POINTERS_EQUAL(service, actual);
 
-	free(registry);
+	hashMap_remove(registry->serviceRegistrations, bundle);
+	arrayList_destroy(registrations);
+	serviceRegistry_destroy(registry);
 }
 
 TEST(service_registry, ungetService) {
-	service_registry_pt registry = (service_registry_pt) calloc(1,sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
@@ -474,53 +493,67 @@ TEST(service_registry, ungetService) {
 
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);
 
-	properties_pt properties = (properties_pt) 0x30;
-	array_list_pt references = NULL;
-	arrayList_create(&references);
 	service_reference_pt reference = (service_reference_pt) 0x40;
-	arrayList_add(references, reference);
 
 	array_list_pt usages = NULL;
 	arrayList_create(&usages);
-	usage_count_pt usage = (usage_count_pt) calloc(1,sizeof(*usage));
-	usage->reference = reference;
-	arrayList_add(usages, usage);
-	hashMap_put(registry->inUseMap, bundle, usages);
+	hashMap_put(registry->serviceReferences, bundle, reference);
+	hashMap_put(registry->deletedServiceReferences, reference, (void*) false);
+	void * service = (void*) 0x50;
 
+	int count = 0;
 	mock()
-		.expectOneCall("serviceReference_getServiceRegistration")
+		.expectOneCall("serviceReference_decreaseUsage")
+		.withParameter("ref", reference)
+		.withOutputParameterReturning("updatedCount", &count, sizeof(count));
+
+	mock()
+		.expectOneCall("serviceReference_getService")
 		.withParameter("reference", reference)
-		.withOutputParameterReturning("registration", &registration, sizeof(registration))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("service", &service, sizeof(service));
 
-	bool out = true;
 	mock()
-			.expectOneCall("serviceReference_equals")
-			.withParameter("reference", reference)
-			.withParameter("compareTo", reference)
-			.withOutputParameterReturning("equal", &out, sizeof(out))
-			.andReturnValue(CELIX_SUCCESS);
+		.expectOneCall("serviceReference_getServiceRegistration")
+		.withParameter("reference", reference)
+		.withOutputParameterReturning("registration", &registration, sizeof(registration));
 
 	mock()
+		.expectOneCall("serviceRegistration_ungetService")
+		.withParameter("registration", registration)
+		.withParameter("bundle", bundle)
+		.withOutputParameterReturning("service", &service, sizeof(service));
+
+/*	mock()
 		.expectOneCall("serviceRegistration_isValid")
 		.withParameter("registration", registration)
-		.andReturnValue(true);
+		.andReturnValue(true);*/
 
 	bool result = false;
-	serviceRegistry_ungetService(registry, bundle, reference, &result);
-	LONGS_EQUAL(1, result);
+	celix_status_t status;
+	status = serviceRegistry_ungetService(registry, bundle, reference, &result);
+	LONGS_EQUAL(CELIX_SUCCESS, status)
+	LONGS_EQUAL(true, result);
 
-	free(registry);
+	hashMap_remove(registry->deletedServiceReferences, reference);
+	hashMap_put(registry->deletedServiceReferences, reference, (void*) true);
+
+	mock()
+		.expectOneCall("framework_log");
+
+	status = serviceRegistry_ungetService(registry, bundle, reference, &result);
+	LONGS_EQUAL(CELIX_BUNDLE_EXCEPTION, status);
+
+
+	arrayList_destroy(registrations);
+	hashMap_remove(registry->serviceRegistrations, bundle);
+	serviceRegistry_destroy(registry);
+	arrayList_destroy(usages);
 }
 
-TEST(service_registry, ungetServivces) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+/*TEST(service_registry, ungetServivces) {
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
@@ -585,17 +618,12 @@ TEST(service_registry, ungetServivces) {
 	serviceRegistry_ungetServices(registry, bundle);
 
 	free(registry);
-}
-
-TEST(service_registry, getUsingBundles) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-    celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	registry->currentServiceId = 1l;
-	registry->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
+}*/
 
+/*TEST(service_registry, getUsingBundles) {
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
 	array_list_pt registrations = NULL;
@@ -615,7 +643,7 @@ TEST(service_registry, getUsingBundles) {
 	usage_count_pt usage = (usage_count_pt) malloc(sizeof(*usage));
 	usage->reference = reference;
 	arrayList_add(usages, usage);
-	hashMap_put(registry->inUseMap, bundle, usages);
+	hashMap_put(registry->serviceReferences, bundle, usages);
 
 	bool out = true;
 	mock()
@@ -630,27 +658,22 @@ TEST(service_registry, getUsingBundles) {
 	POINTERS_EQUAL(bundle, arrayList_get(actual, 0));
 
 	free(registry);
-}
+}*/
 
-TEST(service_registry, createServiceReference) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-    celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-	celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	celixThreadMutex_create(&registry->referencesMapMutex, NULL);
-	registry->currentServiceId = 1l;
-	registry->serviceReferences = hashMap_create(NULL, NULL, NULL, NULL);
+/*TEST(service_registry, createServiceReference) {
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 
 	bundle_pt bundle = (bundle_pt) 0x10;
-	service_registration_pt registration = (service_registration_pt) 0x20;
+	service_registration_pt registration = (service_registration_pt) 0x20;*/
 	/*array_list_pt registrations = NULL;
 	arrayList_create(&registrations);
 	arrayList_add(registrations, registration);
 
 	hashMap_put(registry->serviceRegistrations, bundle, registrations);*/
 
-	array_list_pt references = NULL;
+/*	array_list_pt references = NULL;
 	arrayList_create(&references);
 	service_reference_pt reference = (service_reference_pt) 0x40;
 	arrayList_add(references, reference);
@@ -674,55 +697,48 @@ TEST(service_registry, createServiceReference) {
 	POINTERS_EQUAL(reference, actual);
 
 	free(registry);
-}
+}*/
 
 TEST(service_registry, getListenerHooks) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	registry->listenerHooks = NULL;
-	arrayList_create(&registry->listenerHooks);
-	celixThreadMutexAttr_create(&registry->mutexAttr);
-	celixThreadMutexAttr_settype(&registry->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE);
-	celixThreadMutex_create(&registry->mutex, &registry->mutexAttr);
-	celixThreadMutex_create(&registry->referencesMapMutex, NULL);
-	registry->serviceReferences = hashMap_create(NULL, NULL, NULL, NULL);
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
 	bundle_pt bundle = (bundle_pt) 0x10;
 	service_registration_pt registration = (service_registration_pt) 0x20;
 	arrayList_add(registry->listenerHooks, registration);
 
-	array_list_pt references = NULL;
-	arrayList_create(&references);
-	service_reference_pt reference = (service_reference_pt) 0x40;
-	arrayList_add(references, reference);
+	hash_map_pt usages = hashMap_create(NULL, NULL, NULL, NULL);
+	service_reference_pt reference = (service_reference_pt) 0x30;
+	hashMap_put(usages, registration, reference);
+	hashMap_put(registry->serviceReferences, bundle, usages);
 
 	mock()
-		.expectOneCall("serviceRegistration_getBundle")
-		.withParameter("registration", registration)
-		.withOutputParameterReturning("bundle", &bundle, sizeof(bundle))
-		.andReturnValue(CELIX_SUCCESS)
-		.ignoreOtherParameters();
+		.expectOneCall("serviceRegistration_retain")
+		.withParameter("registration", registration);
 	mock()
-		.expectOneCall("serviceReference_create")
-		.withParameter("bundle", bundle)
-		.withParameter("registration", registration)
-		.withOutputParameterReturning("reference", &reference, sizeof(reference))
-		.andReturnValue(CELIX_SUCCESS)
-		.ignoreOtherParameters();
+		.expectOneCall("serviceReference_retain")
+		.withParameter("ref", reference);
+	mock()
+		.expectOneCall("serviceRegistration_release")
+		.withParameter("registration", registration);
 
 	array_list_pt hooks = NULL;
-	celix_status_t status = serviceRegistry_getListenerHooks(registry, NULL, &hooks);
+	serviceRegistry_getListenerHooks(registry, bundle, &hooks);
 	LONGS_EQUAL(1, arrayList_size(hooks));
 	POINTERS_EQUAL(reference, arrayList_get(hooks, 0));
 
-	free(registry);
+	hashMap_destroy(usages, false, false);
+	arrayList_destroy(hooks);
+	arrayList_remove(registry->listenerHooks, 0);
+	serviceRegistry_destroy(registry);
 }
 
 TEST(service_registry, servicePropertiesModified) {
-	service_registry_pt registry = (service_registry_pt) malloc(sizeof(*registry));
-	service_registration_pt registration = (service_registration_pt) 0x10;
-	properties_pt properties = (properties_pt) 0x20;
-	//test function, defined at top of this test file
-	registry->serviceChanged = serviceRegistryTest_serviceChanged;
-	registry->framework = (framework_pt) 0x30;
+	service_registry_pt registry = NULL;
+	framework_pt framework = (framework_pt) 0x01;
+	serviceRegistry_create(framework,serviceRegistryTest_serviceChanged, &registry);
+	service_registration_pt registration = (service_registration_pt) 0x02;
+	properties_pt properties = (properties_pt) 0x03;
 
 	mock().expectOneCall("serviceRegistryTest_serviceChanged")
 		.withParameter("framework", registry->framework)
@@ -732,5 +748,5 @@ TEST(service_registry, servicePropertiesModified) {
 
 	serviceRegistry_servicePropertiesModified(registry, registration, properties);
 
-	free(registry);
+	serviceRegistry_destroy(registry);
 }

http://git-wip-us.apache.org/repos/asf/celix/blob/8a0ee260/framework/private/test/service_tracker_test.cpp
----------------------------------------------------------------------
diff --git a/framework/private/test/service_tracker_test.cpp b/framework/private/test/service_tracker_test.cpp
index ea06058..ca0981a 100644
--- a/framework/private/test/service_tracker_test.cpp
+++ b/framework/private/test/service_tracker_test.cpp
@@ -25,6 +25,7 @@
  */
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "CppUTest/TestHarness.h"
 #include "CppUTest/TestHarness_c.h"
@@ -44,6 +45,23 @@ int main(int argc, char** argv) {
 	return RUN_ALL_TESTS(argc, argv);
 }
 
+static char* my_strdup(const char* s) {
+	if (s == NULL) {
+		return NULL;
+	}
+
+	size_t len = strlen(s);
+
+	char *d = (char*) calloc(len + 1, sizeof(char));
+
+	if (d == NULL) {
+		return NULL;
+	}
+
+	strncpy(d, s, len);
+	return d;
+}
+
 TEST_GROUP(service_tracker) {
 	void setup(void) {
 	}
@@ -57,69 +75,65 @@ TEST_GROUP(service_tracker) {
 TEST(service_tracker, create) {
 	celix_status_t status;
 	service_tracker_pt tracker = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x123;
-	std::string service = "service";
-	status = serviceTracker_create(ctx, (char *) service.c_str(), NULL, &tracker);
+	bundle_context_pt context = (bundle_context_pt) 0x123;
+	char * service = my_strdup("service");
+	status = serviceTracker_create(context, service, NULL, &tracker);
 
 	LONGS_EQUAL(CELIX_SUCCESS, status);
-	POINTERS_EQUAL(ctx, tracker->context);
+	POINTERS_EQUAL(context, tracker->context);
 	POINTERS_EQUAL(NULL, tracker->customizer);
 	POINTERS_EQUAL(NULL, tracker->listener);
 	POINTERS_EQUAL(tracker, tracker->tracker);
 	STRCMP_EQUAL("(objectClass=service)", tracker->filter);
 
 	serviceTracker_destroy(tracker);
+	free(service);
 }
 
 TEST(service_tracker, createWithFilter) {
 	celix_status_t status;
 	service_tracker_pt tracker = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x123;
-	std::string filter = "(objectClass=test)";
-	status = serviceTracker_createWithFilter(ctx, (char *) filter.c_str(), NULL, &tracker);
+	bundle_context_pt context = (bundle_context_pt) 0x123;
+	char * filter = my_strdup("(objectClass=test)");
+	status = serviceTracker_createWithFilter(context, filter, NULL, &tracker);
 
 	LONGS_EQUAL(CELIX_SUCCESS, status);
-	POINTERS_EQUAL(ctx, tracker->context);
+	POINTERS_EQUAL(context, tracker->context);
 	POINTERS_EQUAL(NULL, tracker->customizer);
 	POINTERS_EQUAL(NULL, tracker->listener);
 	POINTERS_EQUAL(tracker, tracker->tracker);
 	STRCMP_EQUAL("(objectClass=test)", tracker->filter);
 
 	serviceTracker_destroy(tracker);
+	free(filter);
 }
 
 TEST(service_tracker, destroy) {
 	celix_status_t status;
 	service_tracker_pt tracker = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x123;
-	std::string filter = "(objectClass=test)";
-	status = serviceTracker_createWithFilter(ctx, (char *) filter.c_str(), NULL, &tracker);
+	bundle_context_pt context = (bundle_context_pt) 0x123;
+	char * filter = my_strdup("(objectClass=test)");
+	status = serviceTracker_createWithFilter(context, filter, NULL, &tracker);
 	LONGS_EQUAL(CELIX_SUCCESS, status);
 	service_listener_pt listener = (service_listener_pt) calloc(1, sizeof(serviceListener));
 	tracker->listener = listener;
 
 	mock()
 		.expectOneCall("bundleContext_removeServiceListener")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("listener", listener)
 		.andReturnValue(CELIX_SUCCESS);
 
 	status = serviceTracker_destroy(tracker);
 	LONGS_EQUAL(CELIX_SUCCESS, status);
+	free(filter);
 }
 
 TEST(service_tracker, open) {
-	// Without initial services and no customizer
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
-	std::string filter = "(objectClass=service)";
-	tracker->filter = (char *) filter.c_str();
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * filter = my_strdup("(objectClass=service)");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_createWithFilter(context, filter, NULL, &tracker);
 
 	array_list_pt refs = NULL;
 	arrayList_create(&refs);
@@ -127,53 +141,37 @@ TEST(service_tracker, open) {
 	mock().strictOrder();
 	mock()
 		.expectOneCall("bundleContext_getServiceReferences")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("serviceName", (char *) NULL)
 		.withParameter("filter", "(objectClass=service)")
-		.withOutputParameterReturning("service_references", &refs, sizeof(refs))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("service_references", &refs, sizeof(refs));
 	mock()
 		.expectOneCall("bundleContext_addServiceListener")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("filter", "(objectClass=service)")
-		.ignoreOtherParameters()
-		.andReturnValue(CELIX_SUCCESS);
+		.ignoreOtherParameters();
 	serviceTracker_open(tracker);
 	CHECK(tracker->listener != NULL);
 
 	// No services should be found
 	LONGS_EQUAL(0, arrayList_size(tracker->trackedServices));
 
-	arrayList_destroy(tracked);
+	mock().expectOneCall("bundleContext_removeServiceListener")
+			.withParameter("context", context)
+			.ignoreOtherParameters();
 
-	free(tracker->listener);
-	free(tracker);
+	serviceTracker_destroy(tracker);
+	free(filter);
 }
 
 TEST(service_tracker, open_withRefs) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
-	std::string filter = "(objectClass=service)";
-	tracker->filter = (char *) filter.c_str();
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
-//	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-//	entry->service = (void *) 0x31;
-//	service_reference_pt ref = (service_reference_pt) 0x51;
-//	entry->reference = ref;
-//	arrayList_add(tracked, entry);
-//	tracked_pt entry2 = (tracked_pt) malloc(sizeof(*entry));
-//	entry2->service = (void *) 0x32;
-//	service_reference_pt ref2 = (service_reference_pt) 0x52;
-//	entry2->reference = ref2;
-//	arrayList_add(tracked, entry2);
+	celix_status_t status;
+	service_tracker_pt tracker = NULL;
+	bundle_context_pt context = (bundle_context_pt) 0x123;
+	char * filter = my_strdup("(objectClass=test)");
+	status = serviceTracker_createWithFilter(context, filter, NULL, &tracker);
+	LONGS_EQUAL(CELIX_SUCCESS, status);
+	tracker->listener = NULL;
 
 	array_list_pt refs = NULL;
 	arrayList_create(&refs);
@@ -181,78 +179,70 @@ TEST(service_tracker, open_withRefs) {
 	arrayList_add(refs, ref);
 	void *src = (void *) 0x345;
 
-//	mock().strictOrder();
 	mock()
 		.expectOneCall("bundleContext_getServiceReferences")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("serviceName", (char *) NULL)
-		.withParameter("filter", "(objectClass=service)")
-		.withOutputParameterReturning("service_references", &refs, sizeof(refs))
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("filter", filter)
+		.withOutputParameterReturning("service_references", &refs, sizeof(refs));
 	mock()
 		.expectOneCall("bundleContext_addServiceListener")
-		.withParameter("context", ctx)
-		.withParameter("filter", "(objectClass=service)")
-		.ignoreOtherParameters()
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("context", context)
+		.withParameter("filter", filter)
+		.ignoreOtherParameters();
 	mock()
 		.expectOneCall("bundleContext_getService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
-		.withOutputParameterReturning("service_instance", &src, sizeof(src))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("service_instance", &src, sizeof(src));
 	serviceTracker_open(tracker);
+
 	CHECK(tracker->listener != NULL);
+	tracked_pt get_tracked = (tracked_pt) arrayList_get(tracker->trackedServices, 0);
+	POINTERS_EQUAL(src, get_tracked->service);
+	POINTERS_EQUAL(ref, get_tracked->reference);
+
 
 	// One service should be found
 	LONGS_EQUAL(1, arrayList_size(tracker->trackedServices));
 
-	free(arrayList_get(tracked, 0));
-	arrayList_destroy(tracked);
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.ignoreOtherParameters();
 
-	free(tracker->listener);
-	free(tracker);
+	serviceTracker_destroy(tracker);
 	free(ref);
+	free(get_tracked);
+	free(filter);
 }
 
 TEST(service_tracker, open_withRefsAndTracked) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
-	std::string filter = "(objectClass=service)";
-	tracker->filter = (char *) filter.c_str();
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-	entry->service = (void *) 0x31;
-	service_reference_pt ref = (service_reference_pt) 0x51;
+	service_reference_pt ref = (service_reference_pt) 0x02;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	array_list_pt refs = NULL;
 	arrayList_create(&refs);
 	arrayList_add(refs, ref);
 
-//	mock().strictOrder();
 	mock()
 		.expectOneCall("bundleContext_getServiceReferences")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("serviceName", (char *) NULL)
-		.withParameter("filter", "(objectClass=service)")
-		.withOutputParameterReturning("service_references", &refs, sizeof(refs))
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("filter", "(objectClass=service_name)")
+		.withOutputParameterReturning("service_references", &refs, sizeof(refs));
 	mock()
 		.expectOneCall("bundleContext_addServiceListener")
-		.withParameter("context", ctx)
-		.withParameter("filter", "(objectClass=service)")
-		.ignoreOtherParameters()
-		.andReturnValue(CELIX_SUCCESS);
+		.withParameter("context", context)
+		.withParameter("filter", "(objectClass=service_name)")
+		.ignoreOtherParameters();
 
 	bool equal = true;
 	mock()
@@ -260,7 +250,6 @@ TEST(service_tracker, open_withRefsAndTracked) {
 		.withParameter("reference", ref)
 		.withParameter("compareTo", ref)
 		.withOutputParameterReturning("equal", &equal, sizeof(equal))
-		//.ignoreOtherParameters()
 		.andReturnValue(CELIX_SUCCESS);
 
 	serviceTracker_open(tracker);
@@ -269,36 +258,35 @@ TEST(service_tracker, open_withRefsAndTracked) {
 	// One service should be found
 	LONGS_EQUAL(1, arrayList_size(tracker->trackedServices));
 
-	arrayList_destroy(tracked);
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.ignoreOtherParameters();
 
-	free(tracker->listener);
-	free(tracker);
+	serviceTracker_destroy(tracker);
 	free(entry);
+	free(service);
 }
 
 TEST(service_tracker, close) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x345;
-	tracker->context = ctx;
-	// new tracker->listener
-	service_listener_pt listener = (service_listener_pt) 0x42;
-	tracker->listener = (service_listener_pt) listener;
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
+	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-	entry->service = (void *) 0x31;
-	service_reference_pt ref = (service_reference_pt) 0x51;
+	service_reference_pt ref = (service_reference_pt) 0x02;
+
+	tracker->listener = listener;
+
+	entry->service = (void *) 0x03;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	mock()
 		.expectOneCall("bundleContext_removeServiceListener")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("listener", listener)
 		.andReturnValue(CELIX_SUCCESS);
 	bool equal = true;
@@ -311,161 +299,161 @@ TEST(service_tracker, close) {
 	bool result = true;
 	mock()
 		.expectOneCall("bundleContext_ungetService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
 		.withOutputParameterReturning("result", &result, sizeof(result))
 		.andReturnValue(CELIX_SUCCESS);
 
+	celix_status_t status;
+
+	status = serviceTracker_close(tracker);
+	LONGS_EQUAL(CELIX_SUCCESS, status);
+
 	mock()
-		.expectOneCall("bundleContext_ungetServiceReference")
-		.withParameter("context", ctx)
-		.withParameter("reference", ref)
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
 		.andReturnValue(CELIX_SUCCESS);
 
-	serviceTracker_close(tracker);
-
-	arrayList_destroy(tracked);
-	free(tracker);
+	serviceTracker_destroy(tracker);
+	free(service);
 }
 
 TEST(service_tracker, getServiceReference) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
-	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-	entry->service = (void *) 0x31;
-	service_reference_pt ref = (service_reference_pt) 0x51;
-	entry->reference = ref;
-	arrayList_add(tracked, entry);
-	tracked_pt entry2 = (tracked_pt) malloc(sizeof(*entry));
-	entry2->service = (void *) 0x32;
-	service_reference_pt ref2 = (service_reference_pt) 0x52;
-	entry2->reference = ref2;
-	arrayList_add(tracked, entry2);
-
-	service_reference_pt reference = serviceTracker_getServiceReference(tracker);
-	POINTERS_EQUAL(ref, reference);
-
-	arrayList_destroy(tracked);
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+	service_reference_pt reference = (service_reference_pt) 0x02;
+	service_reference_pt reference2 = (service_reference_pt) 0x03;
+	service_reference_pt get_reference;
+	tracked_pt tracked = (tracked_pt) malloc(sizeof(*tracked));
+	tracked_pt tracked2 = (tracked_pt) malloc(sizeof(*tracked2));
+
+	tracked->reference = reference;
+	tracked2->reference = reference2;
+	arrayList_add(tracker->trackedServices, tracked);
+	arrayList_add(tracker->trackedServices, tracked2);
+
+	get_reference = serviceTracker_getServiceReference(tracker);
+
+	//test for either of the references
+	if(get_reference != reference && get_reference != reference2){
+		FAIL("unknown reference");
+	}
 
-	free(tracker);
-	free(entry);
-	free(entry2);
+	serviceTracker_destroy(tracker);
+	free(service);
+	free(tracked);
+	free(tracked2);
 }
 
 TEST(service_tracker, getServiceReferenceNull) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 
 	service_reference_pt reference = serviceTracker_getServiceReference(tracker);
 	POINTERS_EQUAL(NULL, reference);
 
-	arrayList_destroy(tracked);
-	free(tracker);
+	serviceTracker_destroy(tracker);
+	free(service);
 }
 
 TEST(service_tracker, getServiceReferences) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
-	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-	entry->service = (void *) 0x31;
-	service_reference_pt ref = (service_reference_pt) 0x51;
-	entry->reference = ref;
-	arrayList_add(tracked, entry);
-	tracked_pt entry2 = (tracked_pt) malloc(sizeof(*entry));
-	entry2->service = (void *) 0x32;
-	service_reference_pt ref2 = (service_reference_pt) 0x52;
-	entry2->reference = ref2;
-	arrayList_add(tracked, entry2);
-
-	array_list_pt references = serviceTracker_getServiceReferences(tracker);
-	LONGS_EQUAL(2, arrayList_size(references));
-	POINTERS_EQUAL(ref, arrayList_get(references, 0));
-	POINTERS_EQUAL(ref2, arrayList_get(references, 1));
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+	service_reference_pt reference = (service_reference_pt) 0x02;
+	service_reference_pt reference2 = (service_reference_pt) 0x03;
+	service_reference_pt get_reference;
+	tracked_pt tracked = (tracked_pt) malloc(sizeof(*tracked));
+	tracked_pt tracked2 = (tracked_pt) malloc(sizeof(*tracked2));
+	array_list_pt get_references;
+
+	tracked->reference = reference;
+	tracked2->reference = reference2;
+	arrayList_add(tracker->trackedServices, tracked);
+	arrayList_add(tracker->trackedServices, tracked2);
+
+	get_references = serviceTracker_getServiceReferences(tracker);
+
+	//test for the references, in no specific order
+	get_reference = (service_reference_pt) arrayList_get(get_references, 0);
+	if(get_reference == reference){
+		get_reference = (service_reference_pt) arrayList_get(get_references, 1);
+		POINTERS_EQUAL(reference2, get_reference);
+	} else {
+		POINTERS_EQUAL(reference2, get_reference);
+		get_reference = (service_reference_pt) arrayList_get(get_references, 1);
+		POINTERS_EQUAL(reference, get_reference);
+	}
 
-	arrayList_destroy(references);
-	arrayList_destroy(tracked);
+	arrayList_destroy(get_references);
 
-	free(tracker);
-	free(entry);
-	free(entry2);
+	serviceTracker_destroy(tracker);
+	free(service);
+	free(tracked);
+	free(tracked2);
 }
 
 TEST(service_tracker, getService) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
-	entry->service = (void *) 0x31;
-	service_reference_pt ref = (service_reference_pt) 0x51;
+	service_reference_pt ref = (service_reference_pt) 0x02;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	void * actual_service = (void*) 0x32;
+	entry->service = actual_service;
+	arrayList_add(tracker->trackedServices, entry);
 	tracked_pt entry2 = (tracked_pt) malloc(sizeof(*entry));
-	entry2->service = (void *) 0x32;
 	service_reference_pt ref2 = (service_reference_pt) 0x52;
 	entry2->reference = ref2;
-	arrayList_add(tracked, entry2);
+	arrayList_add(tracker->trackedServices, entry2);
 
-	void *service = serviceTracker_getService(tracker);
-	POINTERS_EQUAL(0x31, service);
-
-	arrayList_destroy(tracked);
+	void *get_service = serviceTracker_getService(tracker);
+	POINTERS_EQUAL(actual_service, get_service);
 
+	serviceTracker_destroy(tracker);
 	free(entry);
 	free(entry2);
-	free(tracker);
+	free(service);
 }
 
 TEST(service_tracker, getServiceNull) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-
-	void *service = serviceTracker_getService(tracker);
-	POINTERS_EQUAL(NULL, service);
-
-	arrayList_destroy(tracked);
-	free(tracker);
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
+	void * get_service = serviceTracker_getService(tracker);
+	POINTERS_EQUAL(NULL, get_service);
+
+	serviceTracker_destroy(tracker);
+	free(service);
 }
 
 TEST(service_tracker, getServices) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 	tracked_pt entry2 = (tracked_pt) malloc(sizeof(*entry));
 	entry2->service = (void *) 0x32;
 	service_reference_pt ref2 = (service_reference_pt) 0x52;
 	entry2->reference = ref2;
-	arrayList_add(tracked, entry2);
+	arrayList_add(tracker->trackedServices, entry2);
 
 	array_list_pt services = serviceTracker_getServices(tracker);
 	LONGS_EQUAL(2, arrayList_size(services));
@@ -473,26 +461,24 @@ TEST(service_tracker, getServices) {
 	POINTERS_EQUAL(0x32, arrayList_get(services, 1));
 
 	arrayList_destroy(services);
-	arrayList_destroy(tracked);
 
+	serviceTracker_destroy(tracker);
 	free(entry);
 	free(entry2);
-	free(tracker);
+	free(service);
 }
 
 TEST(service_tracker, getServiceByReference) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	bool equal = true;
 	mock()
@@ -502,28 +488,25 @@ TEST(service_tracker, getServiceByReference) {
 		.withOutputParameterReturning("equal", &equal, sizeof(equal))
 		.andReturnValue(4);
 		//.ignoreOtherParameters();
-	void *service = serviceTracker_getServiceByReference(tracker, ref);
-	POINTERS_EQUAL(0x31, service);
-
-	arrayList_destroy(tracked);
+	void * get_service = serviceTracker_getServiceByReference(tracker, ref);
+	POINTERS_EQUAL(0x31, get_service);
 
-	free(tracker);
+	serviceTracker_destroy(tracker);
 	free(entry);
+	free(service);
 }
 
 TEST(service_tracker, getServiceByReferenceNull) {
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-	// add tracked to tracker->trackedServices
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	bool equal = false;
 	mock()
@@ -533,28 +516,23 @@ TEST(service_tracker, getServiceByReferenceNull) {
 		.ignoreOtherParameters()
 		.andReturnValue(CELIX_SUCCESS)
 		.withCallOrder(1);
-	void *service = serviceTracker_getServiceByReference(tracker, ref);
-	POINTERS_EQUAL(NULL, service);
+	void * get_service = serviceTracker_getServiceByReference(tracker, ref);
+	POINTERS_EQUAL(NULL, get_service);
 
-	arrayList_destroy(tracked);
-
-	free(tracker);
+	serviceTracker_destroy(tracker);
 	free(entry);
+	free(service);
 }
 
 TEST(service_tracker, serviceChangedRegistered) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
 
 	service_reference_pt ref = (service_reference_pt) 0x51;
 
@@ -565,41 +543,44 @@ TEST(service_tracker, serviceChangedRegistered) {
 	void *src = (void *) 0x345;
 	mock()
 		.expectOneCall("bundleContext_getService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
 		.withOutputParameterReturning("service_instance", &src, sizeof(src))
 		.andReturnValue(CELIX_SUCCESS);
 	serviceTracker_serviceChanged(listener, event);
 
-	free(arrayList_get(tracked, 0));
-	arrayList_destroy(tracked);
+	tracked_pt get_tracked = (tracked_pt) arrayList_get(tracker->trackedServices, 0);
+	POINTERS_EQUAL(src, get_tracked->service);
+	POINTERS_EQUAL(ref, get_tracked->reference);
+
+	//cleanup
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
 
+	serviceTracker_destroy(tracker);
+	free(get_tracked);
 	free(event);
-	free(tracker);
-	free(listener);
+	free(service);
 }
 
 TEST(service_tracker, serviceChangedModified) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_MODIFIED;
@@ -615,36 +596,33 @@ TEST(service_tracker, serviceChangedModified) {
 
 	serviceTracker_serviceChanged(listener, event);
 
-	free(arrayList_get(tracked, 0));
-	free(arrayList_get(tracked, 1));
-	arrayList_destroy(tracked);
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
 
+	serviceTracker_destroy(tracker);
+	free(entry);
 	free(event);
-	free(listener);
-	free(tracker);
+	free(service);
 }
 
 TEST(service_tracker, serviceChangedUnregistering) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	tracker->customizer = NULL;
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
+
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING;
@@ -660,46 +638,38 @@ TEST(service_tracker, serviceChangedUnregistering) {
 	bool result = true;
 	mock()
 		.expectOneCall("bundleContext_ungetService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
 		.withOutputParameterReturning("result", &result, sizeof(result))
 		.andReturnValue(CELIX_SUCCESS);
 
-	mock()
-		.expectOneCall("bundleContext_ungetServiceReference")
-		.withParameter("context", ctx)
-		.withParameter("reference", ref)
-		.andReturnValue(CELIX_SUCCESS);
-
 	serviceTracker_serviceChanged(listener, event);
 
-	arrayList_destroy(tracked);
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
 
-	free(listener);
+	serviceTracker_destroy(tracker);
 	free(event);
-	free(tracker);
+	free(service);
 }
 
 TEST(service_tracker, serviceChangedModifiedEndmatch) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_MODIFIED_ENDMATCH;
@@ -707,12 +677,17 @@ TEST(service_tracker, serviceChangedModifiedEndmatch) {
 
 	serviceTracker_serviceChanged(listener, event);
 
-	arrayList_destroy(tracked);
+	//cleanup
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
 
+	serviceTracker_destroy(tracker);
 	free(entry);
-	free(listener);
-	free(tracker);
 	free(event);
+	free(service);
 }
 
 extern "C" {
@@ -727,17 +702,13 @@ extern "C" {
 }
 
 TEST(service_tracker, serviceChangedRegisteredCustomizer) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
 	service_tracker_customizer_pt customizer = (service_tracker_customizer_pt) 0x20;
 	tracker->customizer = customizer;
 
@@ -772,12 +743,25 @@ TEST(service_tracker, serviceChangedRegisteredCustomizer) {
 		.andReturnValue(CELIX_SUCCESS);
 	serviceTracker_serviceChanged(listener, event);
 
-	free(arrayList_get(tracked, 0));
-	arrayList_destroy(tracked);
+	tracked_pt get_tracked = (tracked_pt) arrayList_get(tracker->trackedServices, 0);
+	POINTERS_EQUAL(0x45, get_tracked->service);
+	POINTERS_EQUAL(ref, get_tracked->reference);
 
+	//cleanup
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
+
+	mock()
+		.expectOneCall("serviceTrackerCustomizer_destroy")
+		.withParameter("customizer", customizer);
+
+	serviceTracker_destroy(tracker);
+	free(get_tracked);
 	free(event);
-	free(tracker);
-	free(listener);
+	free(service);
 }
 
 
@@ -788,11 +772,10 @@ extern "C" {
 }
 
 TEST(service_tracker, serviceChangedModifiedCustomizer) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
@@ -801,16 +784,11 @@ TEST(service_tracker, serviceChangedModifiedCustomizer) {
 	//adding_callback_pt adding_func = NULL;
 	//added_callback_pt added_func = NULL;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_MODIFIED;
@@ -845,7 +823,7 @@ TEST(service_tracker, serviceChangedModifiedCustomizer) {
 
 	mock()
 		.expectOneCall("bundleContext_getService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
 		.withOutputParameterReturning("service_instance", &entry->service, sizeof(entry->service));
 */
@@ -865,13 +843,21 @@ TEST(service_tracker, serviceChangedModifiedCustomizer) {
 
 	serviceTracker_serviceChanged(listener, event);
 
-	free(arrayList_get(tracked, 0));
-	free(arrayList_get(tracked, 1));
-	arrayList_destroy(tracked);
+	//cleanup
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
+
+	mock()
+		.expectOneCall("serviceTrackerCustomizer_destroy")
+		.withParameter("customizer", customizer);
 
+	serviceTracker_destroy(tracker);
+	free(entry);
 	free(event);
-	free(listener);
-	free(tracker);
+	free(service);
 }
 
 extern "C" {
@@ -881,27 +867,21 @@ extern "C" {
 }
 
 TEST(service_tracker, serviceChangedUnregisteringCustomizer) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
 	service_tracker_customizer_pt customizer = (service_tracker_customizer_pt) 0x20;
 	tracker->customizer = customizer;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING;
@@ -927,42 +907,47 @@ TEST(service_tracker, serviceChangedUnregisteringCustomizer) {
 		.withOutputParameterReturning("function", &function , sizeof(function))
 		.andReturnValue(CELIX_SUCCESS);
 
+	bool result = true;
 	mock()
-		.expectOneCall("bundleContext_ungetServiceReference")
-		.withParameter("context", ctx)
+		.expectOneCall("bundleContext_ungetService")
+		.withParameter("context", context)
 		.withParameter("reference", ref)
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("result", &result, sizeof(result));
+
 	serviceTracker_serviceChanged(listener, event);
 
-	arrayList_destroy(tracked);
+	//clean up
+	mock()
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
+		.andReturnValue(CELIX_SUCCESS);
+
+	mock()
+		.expectOneCall("serviceTrackerCustomizer_destroy")
+		.withParameter("customizer", customizer);
 
-	free(listener);
+	serviceTracker_destroy(tracker);
 	free(event);
-	free(tracker);
+	free(service);
 }
 
 TEST(service_tracker, serviceChangedUnregisteringCustomizerNoFunc) {
-	// With one initial service
-	// new tracker
-	service_tracker_pt tracker = (service_tracker_pt) malloc(sizeof(*tracker));
-	bundle_context_pt ctx = (bundle_context_pt) 0x10;
-	tracker->context = ctx;
+	bundle_context_pt context= (bundle_context_pt) 0x01;
+	char * service = my_strdup("service_name");
+	service_tracker_pt tracker = NULL;
+	serviceTracker_create(context, service, NULL, &tracker);
 	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));
 	tracker->listener = listener;
 	listener->handle = tracker;
 	service_tracker_customizer_pt customizer = (service_tracker_customizer_pt) 0x20;
 	tracker->customizer = customizer;
 
-	// new tracker->trackedServices
-	array_list_pt tracked = NULL;
-	arrayList_create(&tracked);
-	tracker->trackedServices = tracked;
-//	// add tracked to tracker->trackedServices
 	tracked_pt entry = (tracked_pt) malloc(sizeof(*entry));
 	entry->service = (void *) 0x31;
 	service_reference_pt ref = (service_reference_pt) 0x51;
 	entry->reference = ref;
-	arrayList_add(tracked, entry);
+	arrayList_add(tracker->trackedServices, entry);
 
 	service_event_pt event = (service_event_pt) malloc(sizeof(*event));
 	event->type = OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING;
@@ -973,41 +958,37 @@ TEST(service_tracker, serviceChangedUnregisteringCustomizerNoFunc) {
 		.expectOneCall("serviceReference_equals")
 		.withParameter("reference", ref)
 		.withParameter("compareTo", ref)
-		.withOutputParameterReturning("equal", &equals, sizeof(equals))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("equal", &equals, sizeof(equals));
 	void * handle = (void*) 0x60;
 	mock()
 		.expectOneCall("serviceTrackerCustomizer_getHandle")
 		.withParameter("customizer", customizer)
-		.withOutputParameterReturning("handle", &handle, sizeof(handle))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("handle", &handle, sizeof(handle));
 	void *function = NULL;
 	mock()
 		.expectOneCall("serviceTrackerCustomizer_getRemovedFunction")
 		.withParameter("customizer", customizer)
-		.withOutputParameterReturning("function", &function, sizeof(function))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("function", &function, sizeof(function));
 	bool result = true;
 	mock()
 		.expectOneCall("bundleContext_ungetService")
-		.withParameter("context", ctx)
+		.withParameter("context", context)
 		.withParameter("reference", ref)
-		.withOutputParameterReturning("result", &result, sizeof(result))
-		.andReturnValue(CELIX_SUCCESS);
+		.withOutputParameterReturning("result", &result, sizeof(result));
+
+	serviceTracker_serviceChanged(listener, event);
 
 	mock()
-		.expectOneCall("bundleContext_ungetServiceReference")
-		.withParameter("context", ctx)
-		.withParameter("reference", ref)
+		.expectOneCall("bundleContext_removeServiceListener")
+		.withParameter("context", context)
+		.withParameter("listener", listener)
 		.andReturnValue(CELIX_SUCCESS);
 
-	serviceTracker_serviceChanged(listener, event);
+	mock()
+		.expectOneCall("serviceTrackerCustomizer_destroy")
+		.withParameter("customizer", customizer);
 
-	arrayList_destroy(tracked);
-	free(listener);
-	free(tracker);
+	serviceTracker_destroy(tracker);
 	free(event);
+	free(service);
 }
-
-
-


Mime
View raw message