added databasecomponent and now using Derby in the unit tests.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 15 May 2008 21:21:58 +0000 (21:21 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 15 May 2008 21:21:58 +0000 (21:21 +0000)
As a result, no external Mysql database must be setup before running
the unit tests. It is now checkout and go!!

trunk/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java
trunk/system/general/src/test/java/org/wamblee/system/components/DatabaseComponent.java [new file with mode: 0644]
trunk/system/general/src/test/java/org/wamblee/system/components/DatabaseComponentFactory.java
trunk/system/general/src/test/java/org/wamblee/system/core/AbstractComponentTest.java

index 6567e4e74067cf13f563e5e0f7f4ab6aa4cdc4be..04f0480125c36ff5737258b728b64461b3d3b11d 100644 (file)
@@ -159,7 +159,10 @@ public abstract class AbstractComponent<Type> implements Component<Type> {
                        Object aService, Scope aScope) {
                LOG.info("Interface '" + getQualifiedName() + "."
                                + aDescriptor.getName() + "' started.");
-               _remaining.get().remove(aDescriptor);
+               if ( !_remaining.get().remove(aDescriptor) ) { 
+                   throw new SystemAssemblyException("Component '" + getQualifiedName() + "' started an unexpected interface '" + 
+                           aDescriptor + "' that was not registerd as a provided interface before");
+               }
                aScope.publishInterface(aDescriptor, aService);
        }
 
diff --git a/trunk/system/general/src/test/java/org/wamblee/system/components/DatabaseComponent.java b/trunk/system/general/src/test/java/org/wamblee/system/components/DatabaseComponent.java
new file mode 100644 (file)
index 0000000..5ec5851
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+package org.wamblee.system.components;
+
+import java.util.Properties;
+
+import org.wamblee.persistence.Database;
+import org.wamblee.persistence.DerbyDatabase;
+import org.wamblee.system.components.ORMappingConfig.DatabaseType;
+import org.wamblee.system.core.AbstractComponent;
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.Scope;
+
+public class DatabaseComponent extends AbstractComponent<Database> {
+    
+    private static ProvidedInterface DB_PROPS = new DefaultProvidedInterface("dbProps", Properties.class);
+    
+    private Database _database; 
+    
+    public DatabaseComponent(String aName, Database aDatabase) { 
+        super(aName); 
+        _database = aDatabase;
+        addProvidedInterface(DB_PROPS);
+    }
+
+    @Override
+    protected Database doStart(Scope aScope) {
+        _database.start(); 
+        
+        Properties props = new Properties();
+        if ( _database instanceof DerbyDatabase ) { 
+            props.put("database.type", DatabaseType.DERBY.toString());
+        } else { 
+            throw new IllegalArgumentException("Unknown database type " + _database); 
+        }
+        props.put("database.driver", _database.getDriverClassName());
+        props.put("database.url", _database.getJdbcUrl());
+        props.put("database.username", _database.getUsername());
+        props.put("database.password", _database.getPassword());
+
+        addInterface(DB_PROPS, props, aScope);
+        return _database; 
+    }
+
+    @Override
+    protected void doStop(Database aRuntime) {
+        _database.stop();
+    }
+
+}
index c6ddeee12d086d2640b0c61123e24efe28502afd..540c09ae12bb9e9bff0945c6f88c9acab50c6112 100644 (file)
 package org.wamblee.system.components;
 
 import org.wamblee.io.ClassPathResource;
+import org.wamblee.persistence.DerbyDatabase;
 import org.wamblee.system.core.Container;
 
 public class DatabaseComponentFactory {
 
     public static void addDatabaseConfig(Container aContainer) {
         try {
+            /*
             aContainer
                     .addComponent(new PropertyComponent(
                             "dbProps",
                             new ClassPathResource(
                                     "properties/test.org.wamblee.security.database.properties")));
+            */
+            aContainer.addComponent(new DatabaseComponent("db",
+                    new DerbyDatabase())); 
         } catch (Exception e) {
             throw new RuntimeException("Could not add database configuration",
                     e);
index ebda473d1b46cabf8e1b90082c0b397f926344e6..39d7dd61506d4ef23f048ffb11f211c3b9f83747 100644 (file)
@@ -42,4 +42,27 @@ public class AbstractComponentTest extends TestCase {
                }
                fail();
        }
+       
+       public void testUnexpectedServicesStarted() { 
+           try {
+            Component component = new AbstractComponent("xx",
+                    new ProvidedInterface[0], new RequiredInterface[0]) {
+                @Override
+                protected Object doStart(Scope aScope) {
+                    addInterface(new DefaultProvidedInterface("x", Integer.class), 100, aScope);
+                    return null; 
+                }
+
+                @Override
+                protected void doStop(Object aRuntime) {
+                    // Empty.
+                }
+            };
+            component.start(new DefaultScope(component.getProvidedInterfaces()));
+        } catch (SystemAssemblyException e) {
+            //e.printStackTrace();
+            return;
+        }
+        fail();
+       }
 }