Author: [email protected]
Date: Tue Jan 31 16:14:04 2012
New Revision: 2044

Log:
- Added tests for AMDATU-532;
- added test for testing overwrite of tenant.pid property.

Modified:
   
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/MultiTenantTest.java
   
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/mock/MyDependentServiceImpl.java

Modified: 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/MultiTenantTest.java
==============================================================================
--- 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/MultiTenantTest.java
 (original)
+++ 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/MultiTenantTest.java
 Tue Jan 31 16:14:04 2012
@@ -23,6 +23,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.ops4j.pax.exam.CoreOptions.cleanCaches;
 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
@@ -47,6 +48,7 @@
 import org.amdatu.core.itest.tests.tenant.mock.MyDependentService;
 import org.amdatu.core.itest.tests.tenant.mock.MyDependentServiceImpl;
 import org.amdatu.core.itest.tests.tenant.mock.MyTenancyActivator;
+import org.amdatu.tenant.Tenant;
 import org.amdatu.tenant.TenantConstants;
 import org.amdatu.tenant.adapter.MultiTenantBundleActivator;
 import org.apache.felix.dm.DependencyManager;
@@ -70,7 +72,7 @@
 
 /**
  * Integration test cases for multi-tenancy.
- *
+ * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 @RunWith(JUnit4TestRunner.class)
@@ -128,15 +130,16 @@
 
     /**
      * Helper method to count the number of service registrations for a given 
service-class.
-     *
+     * 
      * @param context the bundle context to use;
-     * @param service the service to count.
+     * @param service the service to count;
+     * @param filter the optional filter to search for, can be 
<code>null</code>.
      * @return a service registration count, >= 0.
      */
-    private static int countAllServices(BundleContext context, Class<?> 
service) {
+    private static int countAllServices(BundleContext context, Class<?> 
service, String filter) {
         ServiceReference[] serviceReferences = null;
         try {
-            serviceReferences = 
context.getAllServiceReferences(service.getName(), null);
+            serviceReferences = 
context.getAllServiceReferences(service.getName(), filter);
         }
         catch (InvalidSyntaxException exception) {
             throw new RuntimeException("null-filter is incorrect?!");
@@ -145,8 +148,19 @@
     }
 
     /**
+     * Helper method to count the number of service registrations for a given 
service-class.
+     * 
+     * @param context the bundle context to use;
+     * @param service the service to count.
+     * @return a service registration count, >= 0.
+     */
+    private static int countAllServices(BundleContext context, Class<?> 
service) {
+        return countAllServices(context, service, null /* filter */);
+    }
+
+    /**
      * Creates the PAX-exam configuration.
-     *
+     * 
      * @return a PAX-exam configuration, never <code>null</code>.
      */
     @org.ops4j.pax.exam.junit.Configuration
@@ -174,12 +188,12 @@
             junitBundles(),
             CoreBundles.provisionAll(),
             provision(mtBundle),
-            systemTimeout(30000));
+            systemTimeout(300000));
     }
 
     /**
      * Sets up an individual test case.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Before
@@ -203,7 +217,7 @@
 
     /**
      * Tears down an individual test case.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @After
@@ -222,7 +236,7 @@
 
     /**
      * Tests that when a multi-tenant aware service is registered, multiple 
service registrations are made, one for each tenant.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Test
@@ -230,10 +244,10 @@
         String tenantId = generateTenantId();
 
         Properties properties = createTenantConfiguration(tenantId);
-        updateTenantConfig(properties);
+        addTenantConfig(properties);
 
         // We've got a number of explicit tenants + one framework (= default) 
tenant
-        int expectedServiceCount = m_configurations.size() + 1;
+        int expectedServiceCount = getConfigurationCount();
 
         // A non-MT aware bundle should be able to see *all* service 
registrations...
         assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
@@ -241,13 +255,13 @@
 
     /**
      * Tests that when a multi-tenant aware service is registered, multiple 
service registrations are made, one for each tenant.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Test
     public void 
testMultiTenantAwareServiceCausesMultipleServiceRegistrations() throws 
Exception {
         // We've got a number of explicit tenants + one framework (= default) 
tenant
-        int expectedServiceCount = m_configurations.size() + 1;
+        int expectedServiceCount = getConfigurationCount();
 
         // A non-MT aware bundle should be able to see *all* service 
registrations...
         assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
@@ -255,7 +269,7 @@
 
     /**
      * Tests that when a multi-tenant aware service is registered, each tenant 
only sees its own services, not the services of other tenants.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Test
@@ -265,18 +279,18 @@
         List<String> msg = m_eventHandler.getMessages();
 
         Properties properties = createTenantConfiguration(tenantId);
-        updateTenantConfig(properties);
+        addTenantConfig(properties);
 
         msg = m_eventHandler.getMessages();
         assertNotNull(msg);
-
-        assertStringFound(msg, "START{" + TenantConstants.PID_KEY + ";" + 
tenantId + "}{" + TenantConstants.NAME_KEY
-            + ";Tenant " + tenantId + "}");
+        assertEquals(2, msg.size());
+        assertStringFound(msg, "START");
+        assertStringFound(msg, "START{" + TenantConstants.PID_KEY + ";" + 
tenantId + "}");
     }
 
     /**
      * Tests that when a non-multi-tenant service (= this test case) tries to 
obtain a multi-tenant aware service, this would fail.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Test
@@ -284,7 +298,7 @@
         String tenantId = generateTenantId();
 
         Properties properties = createTenantConfiguration(tenantId);
-        updateTenantConfig(properties);
+        addTenantConfig(properties);
 
         ServiceReference[] serviceRefs =
             
m_bundleContext.getServiceReferences(MyDependentService.class.getName(),
@@ -294,16 +308,16 @@
 
     /**
      * Tests that when a non-multi-tenant service (= this test case) tries to 
obtain a multi-tenant aware service, this would fail.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
-    @Ignore
+    @Ignore("Cannot work until PAX-EXAM allows us to exclude classes from its 
test-probes. See PAXEXAM-345.")
     @Test
     public void 
testObtainPlatformServiceInstanceSucceedsForNonMultiTenantService() throws 
Exception {
         String tenantId = generateTenantId();
 
         Properties properties = createTenantConfiguration(tenantId);
-        updateTenantConfig(properties);
+        addTenantConfig(properties);
 
         ServiceReference[] serviceRef =
             
m_bundleContext.getServiceReferences(MyDependentService.class.getName(),
@@ -313,23 +327,24 @@
 
     /**
      * Tests that when a multi-tenant aware service is registered, multiple 
service registrations are made, one for each tenant.
-     *
+     * 
      * @throws Exception not part of this test case.
      */
     @Test
     public void 
testRemovingMultiTenantAwareServiceCausesServiceDeregistrations() throws 
Exception {
         String tenantId = generateTenantId();
-        List<String> msg = m_eventHandler.getMessages();
 
         Properties properties = createTenantConfiguration(tenantId);
-        Configuration config = updateTenantConfig(properties);
+        Configuration config = addTenantConfig(properties);
 
         // We've got a number of explicit tenants + one framework (= default) 
tenant
-        int expectedServiceCount = m_configurations.size() + 1;
+        int expectedServiceCount = getConfigurationCount();
 
         // A non-MT aware bundle should be able to see *all* service 
registrations...
         assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
 
+        List<String> msg = m_eventHandler.getMessages();
+
         // Remove configuration; should cause service de-registration for 
tenant...
         removeTenantConfig(config);
 
@@ -338,8 +353,88 @@
 
         msg = m_eventHandler.getMessages();
 
-        assertStringFound(msg, "STOP{" + TenantConstants.PID_KEY + ";" + 
tenantId + "}{" + TenantConstants.NAME_KEY
-            + ";Tenant " + tenantId + "}");
+        assertEquals(2, msg.size());
+        assertStringFound(msg, "STOP");
+        assertStringFound(msg, "STOP{" + TenantConstants.PID_KEY + ";" + 
tenantId + "}");
+    }
+
+    /**
+     * Tests that it is impossible to update the PID of a tenant once it is 
registered.
+     * 
+     * @throws Exception not part of this test case.
+     */
+    @Test
+    public void testUpdateTenantPIDIsImpossibleForExistingTenants() throws 
Exception {
+        String tenantId = generateTenantId();
+
+        Properties properties = createTenantConfiguration(tenantId);
+        Configuration config = addTenantConfig(properties);
+
+        // We've got a number of explicit tenants + one framework (= default) 
tenant
+        int expectedServiceCount = getConfigurationCount();
+
+        // A non-MT aware bundle should be able to see *all* service 
registrations...
+        assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
+
+        properties.setProperty(TenantConstants.PID_KEY, "foo-bar-key");
+
+        // Update the configuration; should log an exception, but not cause 
this method to fail...
+        updateTenantConfig(config, properties);
+
+        // Check that we can still find back our existing services...
+        assertEquals(1, countAllServices(m_bundleContext, Tenant.class, "(" + 
TenantConstants.PID_KEY
+            + "=" + tenantId + ")"));
+        // Check that the "old" tenant is really gone...
+        assertEquals(0, countAllServices(m_bundleContext, Tenant.class, "(" + 
TenantConstants.PID_KEY
+            + "=foo-bar-key)"));
+    }
+
+    /**
+     * Tests that when a multi-tenant aware service is updated only its 
properties are updated.
+     * 
+     * @throws Exception not part of this test case.
+     */
+    @Test
+    public void testUpdateMultiTenantAwareServiceCausesServiceUpdates() throws 
Exception {
+        String tenantId = generateTenantId();
+        List<String> msg = m_eventHandler.getMessages();
+
+        Properties properties = createTenantConfiguration(tenantId);
+        String oldTenantName = (String) 
properties.get(TenantConstants.NAME_KEY);
+        Configuration config = addTenantConfig(properties);
+
+        // We've got a number of explicit tenants + one framework (= default) 
tenant
+        int expectedServiceCount = getConfigurationCount();
+
+        // A non-MT aware bundle should be able to see *all* service 
registrations...
+        assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
+
+        // Verify the tenant can be found by its current name...
+        assertEquals(1, countAllServices(m_bundleContext, Tenant.class, "(" + 
TenantConstants.NAME_KEY
+            + "=" + oldTenantName + ")"));
+
+        msg = m_eventHandler.getMessages();
+
+        String newTenantName = "My other tenant";
+        properties.setProperty(TenantConstants.NAME_KEY, newTenantName);
+
+        // Update the configuration...
+        updateTenantConfig(config, properties);
+
+        // A non-MT aware bundle should be able to see *all* service 
registrations...
+        assertEquals(expectedServiceCount, countAllServices(m_bundleContext, 
MyDependentService.class));
+
+        msg = m_eventHandler.getMessages();
+
+        // Verify that no start/stops of tenants occurred...
+        assertTrue(msg.isEmpty());
+
+        // Check that we can find back our newly named tenant...
+        assertEquals(1, countAllServices(m_bundleContext, Tenant.class, "(" + 
TenantConstants.NAME_KEY
+            + "=" + newTenantName + ")"));
+        // Check that the "old" tenant is really gone...
+        assertEquals(0, countAllServices(m_bundleContext, Tenant.class, "(" + 
TenantConstants.NAME_KEY
+            + "=" + oldTenantName + ")"));
     }
 
     /**
@@ -361,6 +456,15 @@
     }
 
     /**
+     * Returns the number of configurations.
+     * 
+     * @return the configuration count + 1 for the _PLATFORM tenant service.
+     */
+    private int getConfigurationCount() {
+        return m_configurations.size() + 1;
+    }
+
+    /**
      * @param properties
      * @throws Exception
      */
@@ -373,10 +477,20 @@
      * @param properties
      * @throws Exception
      */
-    private Configuration updateTenantConfig(Properties properties) throws 
Exception {
+    private Configuration addTenantConfig(Properties properties) throws 
Exception {
         Configuration config = 
m_testContext.updateFactoryConfig(CoreConfigs.TENANT.getPid(), properties);
         m_configurations.add(config);
         m_testContext.waitForSystemToSettle();
         return config;
     }
+
+    /**
+     * @param properties
+     * @throws Exception
+     */
+    private Configuration updateTenantConfig(Configuration config, Properties 
properties) throws Exception {
+        config.update(properties);
+        m_testContext.waitForSystemToSettle();
+        return config;
+    }
 }

Modified: 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/mock/MyDependentServiceImpl.java
==============================================================================
--- 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/mock/MyDependentServiceImpl.java
     (original)
+++ 
trunk/amdatu-core/itest/tests/src/test/java/org/amdatu/core/itest/tests/tenant/mock/MyDependentServiceImpl.java
     Tue Jan 31 16:14:04 2012
@@ -67,8 +67,6 @@
         if (serviceRef != null) {
             sb.append("{").append(TenantConstants.PID_KEY).append(";")
                 
.append(serviceRef.getProperty(TenantConstants.PID_KEY)).append("}");
-            sb.append("{").append(TenantConstants.NAME_KEY).append(";")
-                
.append(serviceRef.getProperty(TenantConstants.NAME_KEY)).append("}");
         }
         return sb.toString();
     }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to