Author: bdekruijff at gmail.com
Date: Tue Nov 16 23:51:21 2010
New Revision: 431

Log:
AMDATU-176 additional junit testing / some minor refactoring

Added:
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivator.java
   
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/
   
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
Removed:
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/osgi/Activator.java
Modified:
   trunk/amdatu-core/tenantstore-fs/pom.xml
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
   
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java

Modified: trunk/amdatu-core/tenantstore-fs/pom.xml
==============================================================================
--- trunk/amdatu-core/tenantstore-fs/pom.xml    (original)
+++ trunk/amdatu-core/tenantstore-fs/pom.xml    Tue Nov 16 23:51:21 2010
@@ -30,7 +30,7 @@
         <artifactId>maven-bundle-plugin</artifactId>
         <configuration>
           <instructions>
-            
<Bundle-Activator>org.amdatu.core.tenantstore.fs.osgi.Activator</Bundle-Activator>
+            
<Bundle-Activator>org.amdatu.core.tenantstore.fs.osgi.FSTenantStorageProviderActivator</Bundle-Activator>
             
<Bundle-SymbolicName>org.amdatu.core.tenantstore-fs</Bundle-SymbolicName>
           </instructions>
         </configuration>

Added: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivator.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivator.java
    Tue Nov 16 23:51:21 2010
@@ -0,0 +1,66 @@
+/*
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.core.tenantstore.fs.osgi;
+
+import java.io.File;
+
+import org.amdatu.core.tenant.TenantStorageException;
+import org.amdatu.core.tenant.TenantStorageProvider;
+import org.amdatu.core.tenantstore.fs.service.FSTenantStorageProvider;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ * This class represents the OSGi activator for the tenant service fs storage 
provider. By default it will use the
+ * bundle storage, but can be configured to use an alternative directory by 
setting the system property specified
+ * by <code>FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME</code>.
+ */
+public final class FSTenantStorageProviderActivator extends 
DependencyActivatorBase {
+
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws 
Exception {
+
+        File storageDirectory;
+        String dirProperty = 
System.getProperty(FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME);
+        if (dirProperty != null && !"".equals(dirProperty)) {
+            storageDirectory = new File(dirProperty);
+        }
+        else {
+            // Default bundle storage
+            storageDirectory = context.getDataFile("");
+        }
+
+        // Check accessibility
+        if (storageDirectory == null || !storageDirectory.exists() || 
!storageDirectory.canRead()
+            || !storageDirectory.canWrite()) {
+            throw new TenantStorageException("Unable to access storage 
directory:" + storageDirectory.getAbsolutePath());
+        }
+
+        manager.add(
+                createComponent()
+                    .setImplementation(new 
FSTenantStorageProvider(storageDirectory))
+                    .setInterface(TenantStorageProvider.class.getName(), null)
+                    
.add(createServiceDependency().setService(LogService.class).setRequired(false)));
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
+    }
+}

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  Tue Nov 16 23:51:21 2010
@@ -48,10 +48,6 @@
         m_fsSEntityIdList = new FSTenantIdList(new File(rootDirectory, 
ENTITYLIST_FILENAME));
     }
 
-    public FSTenantStorageProvider() throws TenantStorageException {
-        this(new File("" + 
System.getProperty(FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME)));
-    }
-
     public synchronized void store(final TenantEntity entity) throws 
TenantStorageException {
         FSTenantStore tenantStorageFile = getStorageFile(entity.getId());
         TenantEntity storedEntity = tenantStorageFile.addEntity(entity);

Added: 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
        Tue Nov 16 23:51:21 2010
@@ -0,0 +1,204 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.core.tenantstore.fs.osgi;
+
+import java.io.File;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Random;
+
+import junit.framework.Assert;
+
+import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.core.tenant.TenantStorageException;
+import org.amdatu.core.tenant.TenantStorageProvider;
+import org.amdatu.core.tenantstore.fs.service.FSTenantStorageProvider;
+import org.apache.felix.dm.ComponentDeclaration;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.impl.ComponentImpl;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceListener;
+import org.osgi.service.log.LogService;
+
+public final class FSTenantStorageProviderActivatorTest {
+
+    private static final String JAVA_IO_TMPDIR = 
System.getProperty("java.io.tmpdir");
+    private static File m_rootDirectory;
+
+    @Rule
+    public TestName m_testName = new TestName();
+
+    @BeforeClass
+    public static void setUpOnce() {
+        Random rand = new Random();
+        int randomInt = 1 + Math.abs(rand.nextInt());
+        m_rootDirectory =
+            new File(JAVA_IO_TMPDIR + File.separator + 
FSTenantStorageProviderActivatorTest.class.getSimpleName() + "_" + randomInt);
+        if (!m_rootDirectory.exists()) {
+            m_rootDirectory.mkdirs();
+        }
+    }
+
+    @Test
+    public void testActivatorUsesBundleStorageDirectory() throws Exception {
+
+        final Mockery mockContext = new Mockery();
+        final BundleContext bundleContext = 
mockContext.mock(BundleContext.class);
+
+        final File bundleStorageDir = new File(m_rootDirectory, 
m_testName.getMethodName());
+        bundleStorageDir.mkdir();
+
+        // make sure the configuration system property is not set
+        System.setProperty(FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME, 
"");
+
+        mockContext.checking(new Expectations() {
+            {
+                // allowing all service registration calls to the bundleContext
+                
allowing(bundleContext).addServiceListener(with(aNonNull(ServiceListener.class)),
+                    with(any(String.class)));
+
+                
allowing(bundleContext).createFilter((with(aNonNull(String.class))));
+                will(returnValue(mockContext.mock(Filter.class)));
+
+                
allowing(bundleContext).getServiceReference(LogService.class.getName());
+                will(returnValue(null));
+
+                
allowing(bundleContext).getServiceReferences(with(LogService.class.getName()), 
with(any(String.class)));
+                will(returnValue(null));
+
+                
allowing(bundleContext).registerService(with(ComponentDeclaration.class.getName()),
+                    with(aNonNull(Object.class)), with(any(Dictionary.class)));
+
+                // assert that the bundleContext storage directory is 
requested and return it
+                one(bundleContext).getDataFile(with(aNonNull(String.class)));
+                will(returnValue(bundleStorageDir));
+
+                // assert that the TenantStorageProvider is registered at the 
bundlecontext
+                
one(bundleContext).registerService(with(TenantStorageProvider.class.getName()),
+                    with(aNonNull(FSTenantStorageProvider.class)), 
with(any(Dictionary.class)));
+            }
+        });
+
+        // start thing up and execute some logic on the service
+        DependencyActivatorBase activatorBase = new 
FSTenantStorageProviderActivator();
+        activatorBase.start(bundleContext);
+
+        @SuppressWarnings(value="unchecked")
+        List<Object> services = 
activatorBase.getDependencyManager().getServices();
+        ComponentImpl component = (ComponentImpl) services.get(0);
+
+        TenantStorageProvider provider = (TenantStorageProvider) 
component.getService();
+        provider.store(new TenantEntity("1", "Bram"));
+
+        // assert that the bundleContext storage directory was used
+        String[] files = bundleStorageDir.list();
+        Assert.assertTrue(files.length > 0);
+
+        mockContext.assertIsSatisfied();
+    }
+
+    @Test
+    public void testActivatorUsesConfiguredStorageDirectory() throws Exception 
{
+        final Mockery mockContext = new Mockery();
+        final BundleContext bundleContext = 
mockContext.mock(BundleContext.class);
+
+        final File configuredStorageDir = new File(m_rootDirectory, 
m_testName.getMethodName());
+        configuredStorageDir.mkdir();
+
+        // make sure the configuration system property is set
+        System.setProperty(FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME, 
configuredStorageDir.getAbsolutePath());
+
+        mockContext.checking(new Expectations() {
+            {
+                // allowing all service registration calls to the bundleContext
+                
allowing(bundleContext).addServiceListener(with(aNonNull(ServiceListener.class)),
+                    with(any(String.class)));
+
+                
allowing(bundleContext).createFilter((with(aNonNull(String.class))));
+                will(returnValue(mockContext.mock(Filter.class)));
+
+                
allowing(bundleContext).getServiceReference(LogService.class.getName());
+                will(returnValue(null));
+
+                
allowing(bundleContext).getServiceReferences(with(LogService.class.getName()), 
with(any(String.class)));
+                will(returnValue(null));
+
+                
allowing(bundleContext).registerService(with(ComponentDeclaration.class.getName()),
+                    with(aNonNull(Object.class)), with(any(Dictionary.class)));
+
+                // assert that the TenantStorageProvider is registered at the 
bundlecontext
+                
one(bundleContext).registerService(with(TenantStorageProvider.class.getName()),
+                    with(aNonNull(FSTenantStorageProvider.class)), 
with(any(Dictionary.class)));
+            }
+        });
+
+        // start thing up and execute some logic on the service
+        DependencyActivatorBase activatorBase = new 
FSTenantStorageProviderActivator();
+        activatorBase.start(bundleContext);
+
+        @SuppressWarnings(value="unchecked")
+        List<Object> services = 
activatorBase.getDependencyManager().getServices();
+        ComponentImpl component = (ComponentImpl) services.get(0);
+
+        TenantStorageProvider provider = (TenantStorageProvider) 
component.getService();
+        provider.store(new TenantEntity("1", "Bram"));
+
+        // assert that the configured storage directory was used
+        String[] files = configuredStorageDir.list();
+        Assert.assertTrue(files.length > 0);
+
+        mockContext.assertIsSatisfied();
+    }
+
+    @Test(expected = TenantStorageException.class)
+    public void testActivatorThrowsExcpetionOnNonExistingDirectory() throws 
Exception {
+        final Mockery mockContext = new Mockery();
+        final BundleContext bundleContext = 
mockContext.mock(BundleContext.class);
+
+        final File configuredStorageDir = new File(m_rootDirectory, 
m_testName.getMethodName());
+
+        // make sure the configuration system property is set
+        System.setProperty(FSTenantStorageProvider.STORAGEDIR_PROPERTYNAME, 
configuredStorageDir.getAbsolutePath());
+
+        mockContext.checking(new Expectations() {
+            {
+                // allowing all service registration calls to the bundleContext
+                
allowing(bundleContext).addServiceListener(with(aNonNull(ServiceListener.class)),
+                    with(any(String.class)));
+
+                
allowing(bundleContext).createFilter((with(aNonNull(String.class))));
+                will(returnValue(mockContext.mock(Filter.class)));
+
+                
allowing(bundleContext).getServiceReference(LogService.class.getName());
+                will(returnValue(null));
+            }
+        });
+
+        DependencyActivatorBase activatorBase = new 
FSTenantStorageProviderActivator();
+        activatorBase.start(bundleContext);
+
+        mockContext.assertIsSatisfied();
+    }
+
+}

Modified: 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
      (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
      Tue Nov 16 23:51:21 2010
@@ -8,39 +8,39 @@
 
 import org.amdatu.core.tenant.TenantEntity;
 import org.amdatu.core.tenant.TenantStorageException;
-import org.junit.After;
+import 
org.amdatu.core.tenantstore.fs.osgi.FSTenantStorageProviderActivatorTest;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TestName;
 
 public class FSTenantStorageProviderTest {
 
-    final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");
+    private final static String JAVA_IO_TMPDIR = 
System.getProperty("java.io.tmpdir");
+    private static File m_rootDirectory;
 
-    private FSTenantStorageProvider m_TenantBundleStorageProvider;
-    private File m_RootDirectory;
+    @Rule
+    public TestName m_testName = new TestName();
 
-    @Before
-    public void setUp() throws TenantStorageException {
+    private File m_testDirectory;
+    private FSTenantStorageProvider m_TenantBundleStorageProvider;
 
+    @BeforeClass
+    public static void setUpOnce() throws TenantStorageException {
         Random rand = new Random();
-        int randomInt = 1 + rand.nextInt();
-
-        m_RootDirectory =
-            new File(JAVA_IO_TMPDIR + File.separator + 
"FSTenantStorageProviderTest_" + randomInt);
-        if (!m_RootDirectory.exists()) {
-            m_RootDirectory.mkdirs();
-        }
-        m_RootDirectory.deleteOnExit();
-
-        m_TenantBundleStorageProvider = new 
FSTenantStorageProvider(m_RootDirectory);
+        int randomInt = 1 + Math.abs(rand.nextInt());
+        m_rootDirectory =
+            new File(JAVA_IO_TMPDIR + File.separator + 
FSTenantStorageProviderActivatorTest.class.getSimpleName() + "_" + randomInt);
+        m_rootDirectory.mkdirs();
     }
 
-    @After
-    public void tearDown() {
-        if (m_RootDirectory != null && m_RootDirectory.exists()) {
-            m_RootDirectory.delete();
-        }
+    @Before
+    public void setUp() throws TenantStorageException {
+        m_testDirectory = new File(m_rootDirectory, 
m_testName.getMethodName());
+        m_testDirectory.mkdir();
+        m_TenantBundleStorageProvider = new 
FSTenantStorageProvider(m_testDirectory);
     }
 
     /**

Reply via email to