cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r889063 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/configuration/ main/java/org/apache/cayenne/runtime/ test/java/org/apache/cayenne/configuration/ test/java/org/apache/cayenne/resource/m...
Date Thu, 10 Dec 2009 02:46:51 GMT
Author: aadamchik
Date: Thu Dec 10 02:46:49 2009
New Revision: 889063

URL: http://svn.apache.org/viewvc?rev=889063&view=rev
Log:
CAY-1329 DI-based cayenne-project module

Cayenne DI refactoring

* Changing DataChannelDescriptorLoader load() method sig to take Resource instead of location.
* Changing DataMapLoader load() method sig to take Resource instead of parent, location, name.

  This would allow to reuse the loaders with the Modeler to load stuff from files instead
of the classpath
  (pr any other Cayenne tools)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/resource/mock/MockResource.java
      - copied, changed from r889057, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptorLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataMapLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLPoolingDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/runtime/DataDomainProvider.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/runtime/DataDomainProviderTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptorLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptorLoader.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptorLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptorLoader.java
Thu Dec 10 02:46:49 2009
@@ -18,8 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.configuration;
 
-import org.apache.cayenne.CayenneRuntimeException;
-import org.apache.cayenne.runtime.CayenneRuntime;
+import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.resource.Resource;
 
 /**
  * An object that can load a named {@link DataChannelDescriptor} from some configuration
@@ -30,11 +30,9 @@
 public interface DataChannelDescriptorLoader {
 
     /**
-     * Loads a DataChannelDescriptor from some configuration source, usually an XML file
+     * Loads a DataChannelDescriptor from some configuration resource, usually an XML file
      * found on classpath.
-     * 
-     * @param runtimeName the name of {@link CayenneRuntime} that invoked this method.
-     *            Configuration resource names are normally derived from the runtime name.
      */
-    DataChannelDescriptor load(String runtimeName) throws CayenneRuntimeException;
+    DataChannelDescriptor load(Resource configurationResource)
+            throws ConfigurationException;
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
Thu Dec 10 02:46:49 2009
@@ -20,16 +20,14 @@
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.resource.Resource;
 
 /**
- * A loader of DataMaps, resolved relatively to a given {@link DataChannelDescriptor}.
+ * A loader of DataMaps.
  * 
  * @since 3.1
  */
 public interface DataMapLoader {
 
-    DataMap load(
-            DataChannelDescriptor parentDataChannelDescriptor,
-            String location,
-            String name) throws CayenneRuntimeException;
+    DataMap load(Resource configurationResource) throws CayenneRuntimeException;
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java
Thu Dec 10 02:46:49 2009
@@ -18,14 +18,23 @@
  ****************************************************************/
 package org.apache.cayenne.configuration;
 
-import java.util.Collection;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
 
-import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.resource.Resource;
-import org.apache.cayenne.resource.ResourceLocator;
+import org.apache.cayenne.util.Util;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
 
 /**
  * @since 3.1
@@ -34,62 +43,217 @@
 
     private static Log logger = LogFactory.getLog(XMLDataChannelDescriptorLoader.class);
 
-    @Inject
-    protected ResourceLocator resourceLocator;
+    static final String DOMAIN_TAG = "domain";
+    static final String MAP_TAG = "map";
+    static final String NODE_TAG = "node";
+    static final String PROPERTY_TAG = "property";
+    static final String MAP_REF_TAG = "map-ref";
+
+    private static final Map<String, String> dataSourceFactoryNameMapping;
+
+    static {
+        dataSourceFactoryNameMapping = new HashMap<String, String>();
+        dataSourceFactoryNameMapping.put(
+                "org.apache.cayenne.conf.DriverDataSourceFactory",
+                XMLPoolingDataSourceFactory.class.getName());
+        dataSourceFactoryNameMapping.put(
+                "org.apache.cayenne.conf.JNDIDataSourceFactory",
+                JNDIDataSourceFactory.class.getName());
+        dataSourceFactoryNameMapping.put(
+                "org.apache.cayenne.conf.DBCPDataSourceFactory",
+                DBCPDataSourceFactory.class.getName());
+    }
 
     @Inject
     protected DataMapLoader dataMapLoader;
 
-    protected String getResourceName(String runtimeName) {
-        if (runtimeName == null) {
-            throw new NullPointerException("Null rumtimeName");
+    public DataChannelDescriptor load(Resource configurationResource)
+            throws ConfigurationException {
+
+        if (configurationResource == null) {
+            throw new NullPointerException("Null configurationResource");
+        }
+
+        URL configurationURL = configurationResource.getURL();
+
+        DataChannelDescriptor descriptor = new DataChannelDescriptor();
+        descriptor.setConfigurationResource(configurationResource);
+
+        DataChannelHandler rootHandler;
+
+        InputStream in = null;
+
+        try {
+            in = configurationURL.openStream();
+            XMLReader parser = Util.createXmlReader();
+
+            rootHandler = new DataChannelHandler(descriptor, parser);
+            parser.setContentHandler(rootHandler);
+            parser.setErrorHandler(rootHandler);
+            parser.parse(new InputSource(in));
+        }
+        catch (Exception e) {
+            throw new ConfigurationException(
+                    "Error loading configuration from %s",
+                    e,
+                    configurationURL);
+        }
+        finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            }
+            catch (IOException ioex) {
+                logger.info("failure closing input stream for "
+                        + configurationURL
+                        + ", ignoring", ioex);
+            }
+        }
+
+        return descriptor;
+    }
+
+    /**
+     * Converts the names of standard Cayenne-supplied DataSourceFactories from the legacy
+     * names to the current names.
+     */
+    private String convertDataSourceFactory(String dataSourceFactory) {
+
+        if (dataSourceFactory == null) {
+            return null;
         }
 
-        return "cayenne-" + runtimeName + ".xml";
+        String converted = dataSourceFactoryNameMapping.get(dataSourceFactory);
+        return converted != null ? converted : dataSourceFactory;
     }
 
-    public DataChannelDescriptor load(String runtimeName) throws CayenneRuntimeException
{
+    final class DataChannelHandler extends SAXNestedTagHandler {
+
+        private DataChannelDescriptor descriptor;
+
+        DataChannelHandler(DataChannelDescriptor dataChannelDescriptor, XMLReader parser)
{
+            super(parser, null);
+            this.descriptor = dataChannelDescriptor;
+        }
+
+        @Override
+        protected ContentHandler createChildTagHandler(
+                String namespaceURI,
+                String localName,
+                String name,
+                Attributes atts) {
+
+            if (localName.equals(DOMAIN_TAG)) {
+                return new DataChannelChildrenHandler(parser, this);
+            }
 
-        long t0 = System.currentTimeMillis();
-        if (logger.isDebugEnabled()) {
-            logger.debug("starting configuration loading: " + runtimeName);
+            logger.info(unexpectedTagMessage(localName, DOMAIN_TAG));
+            return super.createChildTagHandler(namespaceURI, localName, name, atts);
         }
+    }
 
-        String resourceName = getResourceName(runtimeName);
-        Collection<Resource> configurations = resourceLocator.findResources(resourceName);
+    final class DataChannelChildrenHandler extends SAXNestedTagHandler {
 
-        if (configurations.isEmpty()) {
-            throw new CayenneRuntimeException(
-                    "[%s] : Configuration file \"%s\" is not found.",
-                    getClass().getName(),
-                    resourceName);
+        private DataChannelDescriptor descriptor;
+
+        DataChannelChildrenHandler(XMLReader parser, DataChannelHandler parentHandler) {
+            super(parser, parentHandler);
+            this.descriptor = parentHandler.descriptor;
         }
 
-        Resource configurationResource = configurations.iterator().next();
+        @Override
+        protected ContentHandler createChildTagHandler(
+                String namespaceURI,
+                String localName,
+                String name,
+                Attributes attributes) {
+
+            if (localName.equals(PROPERTY_TAG)) {
+
+                String key = attributes.getValue("", "name");
+                String value = attributes.getValue("", "value");
+                if (key != null && value != null) {
+                    descriptor.getProperties().put(key, value);
+                }
+            }
+            else if (localName.equals(MAP_TAG)) {
+
+                String dataMapName = attributes.getValue("", "name");
+                String dataMapLocation = attributes.getValue("", "location");
+
+                Resource baseResource = descriptor.getConfigurationResource();
+
+                Resource dataMapResource = baseResource
+                        .getRelativeResource(dataMapLocation);
+
+                DataMap dataMap = dataMapLoader.load(dataMapResource);
+                dataMap.setName(dataMapName);
+                dataMap.setLocation(dataMapLocation);
+
+                descriptor.getDataMaps().add(dataMap);
+            }
+            else if (localName.equals(NODE_TAG)) {
+
+                String nodeName = attributes.getValue("", "name");
+                if (nodeName == null) {
+                    throw new ConfigurationException("Error: <node> without 'name'.");
+                }
+
+                DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+                descriptor.getDataNodeDescriptors().add(nodeDescriptor);
+
+                nodeDescriptor.setName(nodeName);
+                nodeDescriptor.setAdapterType(attributes.getValue("", "adapter"));
+
+                String location = attributes.getValue("", "datasource");
+                nodeDescriptor.setLocation(location);
+
+                String dataSourceFactory = attributes.getValue("", "factory");
+                nodeDescriptor
+                        .setDataSourceFactoryType(convertDataSourceFactory(dataSourceFactory));
+                nodeDescriptor.setSchemaUpdateStrategyType(attributes.getValue(
+                        "",
+                        "schema-update-strategy"));
+
+                // this may be bogus for some nodes, such as JNDI, but here we can't
+                // tell for sure
+                if (location != null) {
+                    nodeDescriptor.setConfigurationResource(descriptor
+                            .getConfigurationResource()
+                            .getRelativeResource(location));
+                }
+
+                return new DataNodeChildrenHandler(parser, this, nodeDescriptor);
+            }
 
-        // no support for multiple configs yet, but this is not a hard error
-        if (configurations.size() > 1) {
-            logger.info("found "
-                    + configurations.size()
-                    + " Cayenne configurations, will use the first one: "
-                    + configurationResource.getURL());
+            return super.createChildTagHandler(namespaceURI, localName, name, attributes);
         }
+    }
 
-        DataChannelDescriptor descriptor = new XMLDataChannelDescriptorLoaderAction(
-                dataMapLoader,
-                logger).load(configurationResource);
+    final class DataNodeChildrenHandler extends SAXNestedTagHandler {
 
-        descriptor.setName(runtimeName);
+        private DataNodeDescriptor nodeDescriptor;
 
-        long t1 = System.currentTimeMillis();
+        DataNodeChildrenHandler(XMLReader parser, SAXNestedTagHandler parentHandler,
+                DataNodeDescriptor nodeDescriptor) {
+            super(parser, parentHandler);
+            this.nodeDescriptor = nodeDescriptor;
+        }
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("finished configuration loading: "
-                    + runtimeName
-                    + " in "
-                    + (t1 - t0)
-                    + " ms.");
+        @Override
+        protected ContentHandler createChildTagHandler(
+                String namespaceURI,
+                String localName,
+                String name,
+                Attributes attributes) {
+
+            if (localName.equals(MAP_REF_TAG)) {
+
+                String mapName = attributes.getValue("", "name");
+                nodeDescriptor.getDataMapNames().add(mapName);
+            }
+            return super.createChildTagHandler(namespaceURI, localName, name, attributes);
         }
-        return descriptor;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataMapLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataMapLoader.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataMapLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataMapLoader.java
Thu Dec 10 02:46:49 2009
@@ -37,27 +37,17 @@
 
     private static Log logger = LogFactory.getLog(XMLDataMapLoader.class);
 
-    public DataMap load(
-            DataChannelDescriptor parentDataChannelDescriptor,
-            String location,
-            String name) throws CayenneRuntimeException {
-
-        Resource baseResource = parentDataChannelDescriptor.getConfigurationResource();
-        if (baseResource == null) {
-            throw new NullPointerException("Null parentDataChannelDescriptor's resource");
-        }
-
-        Resource dataMapResource = baseResource.getRelativeResource(location);
+    public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
 
         // TODO: andrus 11.27.2009 - deprecate MapLoader and implement a loader
         // here. MapLoader is in the wrong place, exposes ContentHandler methods and
         // implements if/else contextless matching of tags... should use something like
         // SAXNestedTagHandler instead.
         MapLoader mapLoader = new MapLoader();
-        URL url = dataMapResource.getURL();
+        URL url = configurationResource.getURL();
 
         InputStream in = null;
-        
+
         DataMap map;
 
         try {
@@ -66,7 +56,10 @@
             map = mapLoader.loadDataMap(new InputSource(in));
         }
         catch (Exception e) {
-            throw new CayenneRuntimeException("Error loading configuration from %s", e, url);
+            throw new CayenneRuntimeException(
+                    "Error loading configuration from %s",
+                    e,
+                    url);
         }
         finally {
             try {
@@ -80,9 +73,7 @@
                         ioex);
             }
         }
-        
-        map.setName(name);
-        map.setLocation(location);
+
         return map;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLPoolingDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLPoolingDataSourceFactory.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLPoolingDataSourceFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLPoolingDataSourceFactory.java
Thu Dec 10 02:46:49 2009
@@ -69,12 +69,11 @@
         Resource resource = nodeDescriptor.getConfigurationResource();
 
         if (resource == null) {
-            logger.info("Null configuration resource for node descriptor with location '"
+            String message = "Null configuration resource for node descriptor with location
'"
                     + nodeDescriptor.getLocation()
-                    + "'");
-            throw new ConfigurationException(
-                    "Null configuration resource for node descriptor with location '%s'",
-                    nodeDescriptor.getLocation());
+                    + "'";
+            logger.info(message);
+            throw new ConfigurationException(message);
         }
 
         DataSourceInfo dataSourceDescriptor = new DataSourceInfo();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/runtime/DataDomainProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/runtime/DataDomainProvider.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/runtime/DataDomainProvider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/runtime/DataDomainProvider.java
Thu Dec 10 02:46:49 2009
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.runtime;
 
+import java.util.Collection;
+
 import javax.sql.DataSource;
 
 import org.apache.cayenne.ConfigurationException;
@@ -36,6 +38,10 @@
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.resource.Resource;
+import org.apache.cayenne.resource.ResourceLocator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * A {@link DataChannel} provider that provides a single instance of DataDomain configured
@@ -45,6 +51,11 @@
  */
 public class DataDomainProvider implements Provider<DataDomain> {
 
+    private static Log logger = LogFactory.getLog(DataDomainProvider.class);
+
+    @Inject
+    protected ResourceLocator resourceLocator;
+
     @Inject
     protected DataChannelDescriptorLoader loader;
 
@@ -93,10 +104,45 @@
     protected void createDataChannel() throws Exception {
         String runtimeName = configurationProperties
                 .get(RuntimeProperties.CAYENNE_RUNTIME_NAME);
-        DataChannelDescriptor descriptor = loader.load(runtimeName);
 
-        DataDomain dataDomain = new DataDomain(descriptor.getName());
+        long t0 = System.currentTimeMillis();
+        if (logger.isDebugEnabled()) {
+            logger.debug("starting configuration loading: " + runtimeName);
+        }
+
+        String resourceName = getResourceName(runtimeName);
+        Collection<Resource> configurations = resourceLocator.findResources(resourceName);
+
+        if (configurations.isEmpty()) {
+            throw new ConfigurationException(
+                    "Configuration file \"%s\" is not found.",
+                    resourceName);
+        }
+
+        Resource configurationResource = configurations.iterator().next();
+
+        // no support for multiple configs yet, but this is not a hard error
+        if (configurations.size() > 1) {
+            logger.info("found "
+                    + configurations.size()
+                    + " configurations, will use the first one: "
+                    + configurationResource.getURL());
+        }
+
+        DataChannelDescriptor descriptor = loader.load(configurationResource);
+        descriptor.setName(runtimeName);
+
+        long t1 = System.currentTimeMillis();
 
+        if (logger.isDebugEnabled()) {
+            logger.debug("finished configuration loading: "
+                    + runtimeName
+                    + " in "
+                    + (t1 - t0)
+                    + " ms.");
+        }
+
+        DataDomain dataDomain = new DataDomain(descriptor.getName());
         dataDomain.initWithProperties(descriptor.getProperties());
 
         for (DataMap dataMap : descriptor.getDataMaps()) {
@@ -127,7 +173,6 @@
                         .getName());
             }
             else {
-
                 SchemaUpdateStrategy strategy = objectFactory.newInstance(
                         SchemaUpdateStrategy.class,
                         schemaUpdateStrategyType);
@@ -149,4 +194,17 @@
         this.dataDomain = dataDomain;
     }
 
+    /**
+     * Returns the name of DataDomain configuration resource derived from runtime name.
+     * Resource name is determined based on the naming convention.
+     * "cayenne-<runtimename>.xml".
+     */
+    protected String getResourceName(String runtimeName) {
+        if (runtimeName == null) {
+            throw new NullPointerException("Null rumtimeName");
+        }
+
+        return "cayenne-" + runtimeName + ".xml";
+    }
+
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderTest.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderTest.java
Thu Dec 10 02:46:49 2009
@@ -18,23 +18,18 @@
  ****************************************************************/
 package org.apache.cayenne.configuration;
 
-import java.io.IOException;
 import java.net.URL;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.Enumeration;
 import java.util.Iterator;
 
 import junit.framework.TestCase;
 
-import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.di.Module;
 import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.resource.Resource;
-import org.apache.cayenne.resource.ResourceLocator;
 import org.apache.cayenne.resource.URLResource;
 
 public class XMLDataChannelDescriptorLoaderTest extends TestCase {
@@ -42,32 +37,10 @@
     public void testLoadEmpty() {
 
         // create dependencies
-        final String testConfigName = "testConfig1";
-
-        final ResourceLocator locator = new ResourceLocator() {
-
-            public Collection<Resource> findResources(String name) {
-
-                String baseUrl = getClass().getPackage().getName().replace('.', '/');
-
-                Enumeration<URL> en;
-                try {
-                    en = getClass().getClassLoader().getResources(
-                            baseUrl + "/cayenne-" + testConfigName + ".xml");
-                }
-                catch (IOException e) {
-                    throw new RuntimeException("error finding test resource: ", e);
-                }
-
-                URL url = en.nextElement();
-                return Collections.<Resource> singleton(new URLResource(url));
-            }
-        };
 
         Module testModule = new Module() {
 
             public void configure(Binder binder) {
-                binder.bind(ResourceLocator.class).toInstance(locator);
                 binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
             }
         };
@@ -78,27 +51,23 @@
         XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
         injector.injectMembers(loader);
 
-        DataChannelDescriptor descriptor = loader.load(testConfigName);
+        String testConfigName = "testConfig1";
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/cayenne-" + testConfigName + ".xml");
+        DataChannelDescriptor descriptor = loader.load(new URLResource(url));
 
         assertNotNull(descriptor);
-        assertEquals(testConfigName, descriptor.getName());
+        assertNull(descriptor.getName());
     }
 
-    public void testLoad_MissingConfig() {
+    public void testLoad_MissingConfig() throws Exception {
 
         // create dependencies
-        final ResourceLocator locator = new ResourceLocator() {
-
-            public Collection<Resource> findResources(String name) {
-
-                return Collections.emptyList();
-            }
-        };
 
         Module testModule = new Module() {
 
             public void configure(Binder binder) {
-                binder.bind(ResourceLocator.class).toInstance(locator);
                 binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
             }
         };
@@ -110,10 +79,10 @@
         injector.injectMembers(loader);
 
         try {
-            loader.load("testConfig1_missing");
+            loader.load(new URLResource(new URL("file:///no_such_resource")));
             fail("No exception was thrown on bad absent config name");
         }
-        catch (CayenneRuntimeException e) {
+        catch (ConfigurationException e) {
             // expected
         }
     }
@@ -121,32 +90,10 @@
     public void testLoadDataMap() {
 
         // create dependencies
-        final String testConfigName = "testConfig2";
-
-        final ResourceLocator locator = new ResourceLocator() {
-
-            public Collection<Resource> findResources(String name) {
-
-                String baseUrl = getClass().getPackage().getName().replace('.', '/');
-
-                Enumeration<URL> en;
-                try {
-                    en = getClass().getClassLoader().getResources(
-                            baseUrl + "/cayenne-" + testConfigName + ".xml");
-                }
-                catch (IOException e) {
-                    throw new RuntimeException("error finding test resource: ", e);
-                }
-
-                URL url = en.nextElement();
-                return Collections.<Resource> singleton(new URLResource(url));
-            }
-        };
 
         Module testModule = new Module() {
 
             public void configure(Binder binder) {
-                binder.bind(ResourceLocator.class).toInstance(locator);
                 binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
             }
         };
@@ -157,11 +104,16 @@
         XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
         injector.injectMembers(loader);
 
-        DataChannelDescriptor descriptor = loader.load(testConfigName);
+        String testConfigName = "testConfig2";
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/cayenne-" + testConfigName + ".xml");
+
+        DataChannelDescriptor descriptor = loader.load(new URLResource(url));
 
         assertNotNull(descriptor);
 
-        assertEquals(testConfigName, descriptor.getName());
+        assertNull(descriptor.getName());
 
         Collection<DataMap> maps = descriptor.getDataMaps();
         assertEquals(1, maps.size());
@@ -171,32 +123,10 @@
     public void testLoadDataEverything() {
 
         // create dependencies
-        final String testConfigName = "testConfig3";
-
-        final ResourceLocator locator = new ResourceLocator() {
-
-            public Collection<Resource> findResources(String name) {
-
-                String baseUrl = getClass().getPackage().getName().replace('.', '/');
-
-                Enumeration<URL> en;
-                try {
-                    en = getClass().getClassLoader().getResources(
-                            baseUrl + "/cayenne-" + testConfigName + ".xml");
-                }
-                catch (IOException e) {
-                    throw new RuntimeException("error finding test resource: ", e);
-                }
-
-                URL url = en.nextElement();
-                return Collections.<Resource> singleton(new URLResource(url));
-            }
-        };
 
         Module testModule = new Module() {
 
             public void configure(Binder binder) {
-                binder.bind(ResourceLocator.class).toInstance(locator);
                 binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
             }
         };
@@ -207,11 +137,16 @@
         XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
         injector.injectMembers(loader);
 
-        DataChannelDescriptor descriptor = loader.load(testConfigName);
+        String testConfigName = "testConfig3";
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/cayenne-" + testConfigName + ".xml");
+
+        DataChannelDescriptor descriptor = loader.load(new URLResource(url));
 
         assertNotNull(descriptor);
 
-        assertEquals(testConfigName, descriptor.getName());
+        assertNull(descriptor.getName());
 
         Collection<DataMap> maps = descriptor.getDataMaps();
         assertEquals(2, maps.size());

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/resource/mock/MockResource.java
(from r889057, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/resource/mock/MockResource.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/resource/mock/MockResource.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java&r1=889057&r2=889063&rev=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DataMapLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/resource/mock/MockResource.java
Thu Dec 10 02:46:49 2009
@@ -16,20 +16,20 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.configuration;
+package org.apache.cayenne.resource.mock;
 
-import org.apache.cayenne.CayenneRuntimeException;
-import org.apache.cayenne.map.DataMap;
+import java.net.URL;
+
+import org.apache.cayenne.resource.Resource;
+
+public class MockResource implements Resource {
+
+    public Resource getRelativeResource(String relativePath) {
+        return null;
+    }
+
+    public URL getURL() {
+        return null;
+    }
 
-/**
- * A loader of DataMaps, resolved relatively to a given {@link DataChannelDescriptor}.
- * 
- * @since 3.1
- */
-public interface DataMapLoader {
-
-    DataMap load(
-            DataChannelDescriptor parentDataChannelDescriptor,
-            String location,
-            String name) throws CayenneRuntimeException;
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/runtime/DataDomainProviderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/runtime/DataDomainProviderTest.java?rev=889063&r1=889062&r2=889063&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/runtime/DataDomainProviderTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/runtime/DataDomainProviderTest.java
Thu Dec 10 02:46:49 2009
@@ -18,13 +18,14 @@
  ****************************************************************/
 package org.apache.cayenne.runtime;
 
+import java.util.Collection;
 import java.util.Collections;
 
 import javax.sql.DataSource;
 
 import junit.framework.TestCase;
 
-import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.DataNode;
@@ -50,6 +51,9 @@
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.di.Module;
 import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.resource.Resource;
+import org.apache.cayenne.resource.ResourceLocator;
+import org.apache.cayenne.resource.mock.MockResource;
 
 public class DataDomainProviderTest extends TestCase {
 
@@ -82,11 +86,18 @@
         nodeDescriptor2.setLocation("testDataNode2.driver.xml");
         testDescriptor.getDataNodeDescriptors().add(nodeDescriptor2);
 
+        final ResourceLocator locator = new ResourceLocator() {
+
+            public Collection<Resource> findResources(String name) {
+                assertEquals("cayenne-" + testConfigName + ".xml", name);
+                return Collections.<Resource> singleton(new MockResource());
+            }
+        };
+
         final DataChannelDescriptorLoader testLoader = new DataChannelDescriptorLoader()
{
 
-            public DataChannelDescriptor load(String runtimeName)
-                    throws CayenneRuntimeException {
-                assertEquals(testConfigName, runtimeName);
+            public DataChannelDescriptor load(Resource configurationResource)
+                    throws ConfigurationException {
                 return testDescriptor;
             }
         };
@@ -99,6 +110,7 @@
         Module testModule = new Module() {
 
             public void configure(Binder binder) {
+                binder.bind(ResourceLocator.class).toInstance(locator);
                 binder.bind(RuntimeProperties.class).toInstance(testProperties);
                 binder.bind(DataChannelDescriptorLoader.class).toInstance(testLoader);
                 binder.bind(SchemaUpdateStrategy.class).toInstance(



Mime
View raw message