cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: CAY-2355 Project compatibility module can't handle standalone data maps + make XMLDataMapLoader stateless
Date Thu, 24 Aug 2017 09:50:56 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 0101fc1ca -> a1c9e2dd2


CAY-2355 Project compatibility module can't handle standalone data maps
  + make XMLDataMapLoader stateless


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/a1c9e2dd
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/a1c9e2dd
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/a1c9e2dd

Branch: refs/heads/master
Commit: a1c9e2dd2489164dc4e3411753d43866efc1b8da
Parents: 0101fc1
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Thu Aug 24 12:48:08 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Thu Aug 24 12:48:08 2017 +0300

----------------------------------------------------------------------
 .../xml/CompatibilityDataMapLoader.java         | 33 ++++++---
 .../CompatibilityUpgradeService.java            | 13 ++++
 ...patibilityDataChannelDescriptorLoaderIT.java | 55 ++++-----------
 .../xml/CompatibilityDataMapLoaderIT.java       | 71 ++++++++++++++++++++
 .../compatibility/CompatibilityTestModule.java  | 67 ++++++++++++++++++
 .../CompatibilityUpgradeServiceIT.java          | 52 +++++++-------
 .../configuration/xml/XMLDataMapLoader.java     | 16 ++---
 7 files changed, 219 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/main/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/main/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoader.java
b/cayenne-project-compatibility/src/main/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoader.java
index 77ce2cd..3f4c8a5 100644
--- a/cayenne-project-compatibility/src/main/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoader.java
+++ b/cayenne-project-compatibility/src/main/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoader.java
@@ -27,9 +27,13 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.project.compatibility.CompatibilityUpgradeService;
 import org.apache.cayenne.project.compatibility.DocumentProvider;
+import org.apache.cayenne.project.upgrade.UpgradeService;
 import org.apache.cayenne.resource.Resource;
 import org.apache.cayenne.util.Util;
 import org.w3c.dom.Document;
@@ -42,15 +46,31 @@ import org.xml.sax.XMLReader;
 public class CompatibilityDataMapLoader extends XMLDataMapLoader {
 
     @Inject
+    Provider<UpgradeService> upgradeServiceProvider;
+
+    @Inject
     DocumentProvider documentProvider;
 
     @Override
     public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
         Document document = documentProvider.getDocument(configurationResource.getURL());
+        // no document yet in provider, maybe DataMap is directly loaded
         if(document == null) {
-            return super.load(configurationResource);
+            if(!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
+                throw new ConfigurationException("CompatibilityUpgradeService expected");
+            }
+            // try to upgrade datamap directly
+            CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService)upgradeServiceProvider.get();
+            upgradeService.upgradeDataMap(configurationResource);
+            document = documentProvider.getDocument(configurationResource.getURL());
+
+            // still no document, try to load it without upgrade, though it likely will fail
+            if(document == null) {
+                return super.load(configurationResource);
+            }
         }
 
+        final DataMap[] maps = new DataMap[1];
         try {
             DOMSource source = new DOMSource(document);
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -61,12 +81,7 @@ public class CompatibilityDataMapLoader extends XMLDataMapLoader {
 
             XMLReader parser = Util.createXmlReader();
             LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
-            loaderContext.addDataMapListener(new DataMapLoaderListener() {
-                @Override
-                public void onDataMapLoaded(DataMap dataMap) {
-                    map = dataMap;
-                }
-            });
+            loaderContext.addDataMapListener(dataMap -> maps[0] = dataMap);
             RootDataMapHandler rootHandler = new RootDataMapHandler(loaderContext);
 
             parser.setContentHandler(rootHandler);
@@ -76,10 +91,10 @@ public class CompatibilityDataMapLoader extends XMLDataMapLoader {
             throw new CayenneRuntimeException("Error loading configuration from %s", e, configurationResource.getURL());
         }
 
-        if(map == null) {
+        if(maps[0] == null) {
             throw new CayenneRuntimeException("Unable to load data map from %s", configurationResource.getURL());
         }
-
+        DataMap map = maps[0];
         if(map.getName() == null) {
             // set name based on location if no name provided by map itself
             map.setName(mapNameFromLocation(configurationResource.getURL().getFile()));

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/main/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeService.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/main/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeService.java
b/cayenne-project-compatibility/src/main/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeService.java
index 3444d37..8937148 100644
--- a/cayenne-project-compatibility/src/main/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeService.java
+++ b/cayenne-project-compatibility/src/main/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeService.java
@@ -27,6 +27,8 @@ import org.apache.cayenne.project.upgrade.DefaultUpgradeService;
 import org.apache.cayenne.project.upgrade.UpgradeUnit;
 import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler;
 import org.apache.cayenne.resource.Resource;
+import org.apache.cayenne.util.Util;
+import org.w3c.dom.Document;
 
 /**
  * @since 4.1
@@ -52,6 +54,17 @@ public class CompatibilityUpgradeService extends DefaultUpgradeService
{
         return resource;
     }
 
+    public Resource upgradeDataMap(Resource resource) {
+        List<UpgradeHandler> handlerList = getHandlersForVersion(loadProjectVersion(resource));
+        Document document =  Util.readDocument(resource.getURL());
+        UpgradeUnit upgradeUnit = new UpgradeUnit(resource, document);
+        for(UpgradeHandler handler : handlerList) {
+            handler.processDataMapDom(upgradeUnit);
+        }
+        documentProvider.putDocument(upgradeUnit.getResource().getURL(), upgradeUnit.getDocument());
+        return upgradeUnit.getResource();
+    }
+
     public void upgradeModel(Resource resource, DataChannelDescriptor descriptor) {
         List<UpgradeHandler> handlerList = getHandlersForVersion(loadProjectVersion(resource));
         for(UpgradeHandler handler : handlerList) {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataChannelDescriptorLoaderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataChannelDescriptorLoaderIT.java
b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataChannelDescriptorLoaderIT.java
index 79c1d54..02396a5 100644
--- a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataChannelDescriptorLoaderIT.java
+++ b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataChannelDescriptorLoaderIT.java
@@ -21,38 +21,20 @@ package org.apache.cayenne.configuration.xml;
 
 import java.net.URL;
 
-import org.apache.cayenne.configuration.ConfigurationNameMapper;
 import org.apache.cayenne.configuration.ConfigurationTree;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.DataChannelDescriptorLoader;
 import org.apache.cayenne.configuration.DataMapLoader;
-import org.apache.cayenne.configuration.DefaultConfigurationNameMapper;
-import org.apache.cayenne.di.AdhocObjectFactory;
-import org.apache.cayenne.di.Binder;
-import org.apache.cayenne.di.ClassLoaderManager;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
-import org.apache.cayenne.di.Module;
-import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory;
-import org.apache.cayenne.di.spi.DefaultClassLoaderManager;
 import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.project.ProjectSaver;
-import org.apache.cayenne.project.compatibility.CompatibilityUpgradeService;
-import org.apache.cayenne.project.compatibility.DefaultDocumentProvider;
-import org.apache.cayenne.project.compatibility.DocumentProvider;
-import org.apache.cayenne.project.upgrade.UpgradeService;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V10;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V7;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V8;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V9;
+import org.apache.cayenne.project.compatibility.CompatibilityTestModule;
 import org.apache.cayenne.resource.Resource;
 import org.apache.cayenne.resource.URLResource;
 import org.junit.Test;
 import org.xml.sax.XMLReader;
 
 import static org.junit.Assert.*;
-import static org.mockito.Mockito.mock;
 
 /**
  * @since 4.1
@@ -61,30 +43,7 @@ public class CompatibilityDataChannelDescriptorLoaderIT {
 
     @Test
     public void testLoad() throws Exception {
-        Injector injector = DIBootstrap.createInjector(new Module() {
-            @Override
-            public void configure(Binder binder) {
-                binder.bind(UpgradeService.class).to(CompatibilityUpgradeService.class);
-                binder.bind(DocumentProvider.class).to(DefaultDocumentProvider.class);
-                binder.bind(DataChannelDescriptorLoader.class).to(CompatibilityDataChannelDescriptorLoader.class);
-                binder.bind(DataMapLoader.class).to(CompatibilityDataMapLoader.class);
-
-                binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
-                binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
-                binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
-                binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
-
-                binder.bindList(UpgradeHandler.class)
-                        .add(UpgradeHandler_V7.class)
-                        .add(UpgradeHandler_V8.class)
-                        .add(UpgradeHandler_V9.class)
-                        .add(UpgradeHandler_V10.class);
-
-                binder.bind(ProjectSaver.class).toInstance(mock(ProjectSaver.class));
-
-                binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
-            }
-        });
+        Injector injector = getInjector();
 
         DataChannelDescriptorLoader loader = injector.getInstance(DataChannelDescriptorLoader.class);
         assertTrue(loader instanceof CompatibilityDataChannelDescriptorLoader);
@@ -105,4 +64,14 @@ public class CompatibilityDataChannelDescriptorLoaderIT {
         assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
     }
 
+    private Injector getInjector() {
+        return DIBootstrap.createInjector(
+            new CompatibilityTestModule(),
+            binder -> {
+                binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
+                binder.bind(DataChannelDescriptorLoader.class).to(CompatibilityDataChannelDescriptorLoader.class);
+                binder.bind(DataMapLoader.class).to(CompatibilityDataMapLoader.class);
+            }
+        );
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoaderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoaderIT.java
b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoaderIT.java
new file mode 100644
index 0000000..0bb15b9
--- /dev/null
+++ b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/configuration/xml/CompatibilityDataMapLoaderIT.java
@@ -0,0 +1,71 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.cayenne.configuration.xml;
+
+import java.net.URL;
+
+import org.apache.cayenne.configuration.DataMapLoader;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.project.compatibility.CompatibilityTestModule;
+import org.apache.cayenne.resource.Resource;
+import org.apache.cayenne.resource.URLResource;
+import org.junit.Test;
+import org.xml.sax.XMLReader;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @since 4.1
+ */
+public class CompatibilityDataMapLoaderIT {
+
+    @Test
+    public void testLoad() throws Exception {
+        Injector injector = getInjector();
+
+        DataMapLoader loader = injector.getInstance(DataMapLoader.class);
+        assertTrue(loader instanceof CompatibilityDataMapLoader);
+
+        URL resourceUrl = getClass().getResource("../../project/compatibility/test-map-v6.map.xml");
+        Resource resource = new URLResource(resourceUrl);
+
+        DataMap dataMap = loader.load(resource);
+        assertNotNull(dataMap);
+        assertEquals(1, dataMap.getDbEntities().size());
+        assertEquals(1, dataMap.getObjEntities().size());
+        assertNotNull(dataMap.getObjEntity("Artist"));
+        assertNotNull(dataMap.getDbEntity("Artist"));
+        assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
+    }
+
+    private Injector getInjector() {
+        return DIBootstrap.createInjector(
+                new CompatibilityTestModule(),
+                binder -> {
+                    binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
+                    binder.bind(DataMapLoader.class).to(CompatibilityDataMapLoader.class);
+                }
+        );
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityTestModule.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityTestModule.java
b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityTestModule.java
new file mode 100644
index 0000000..88f80d3
--- /dev/null
+++ b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityTestModule.java
@@ -0,0 +1,67 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.cayenne.project.compatibility;
+
+import org.apache.cayenne.configuration.ConfigurationNameMapper;
+import org.apache.cayenne.configuration.DataChannelDescriptorLoader;
+import org.apache.cayenne.configuration.DefaultConfigurationNameMapper;
+import org.apache.cayenne.configuration.xml.DefaultHandlerFactory;
+import org.apache.cayenne.configuration.xml.HandlerFactory;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.ClassLoaderManager;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory;
+import org.apache.cayenne.di.spi.DefaultClassLoaderManager;
+import org.apache.cayenne.project.ProjectSaver;
+import org.apache.cayenne.project.upgrade.UpgradeService;
+import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler;
+import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V10;
+import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V7;
+import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V8;
+import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V9;
+
+import static org.mockito.Mockito.mock;
+
+/**
+ * @since 4.1
+ */
+public class CompatibilityTestModule implements Module {
+
+    @Override
+    public void configure(Binder binder) {
+        binder.bind(UpgradeService.class).to(CompatibilityUpgradeService.class);
+        binder.bind(DocumentProvider.class).to(DefaultDocumentProvider.class);
+
+        binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
+        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
+        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
+        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
+
+        binder.bindList(UpgradeHandler.class)
+                .add(UpgradeHandler_V7.class)
+                .add(UpgradeHandler_V8.class)
+                .add(UpgradeHandler_V9.class)
+                .add(UpgradeHandler_V10.class);
+
+        binder.bind(ProjectSaver.class).toInstance(mock(ProjectSaver.class));
+        binder.bind(DataChannelDescriptorLoader.class).toInstance(mock(DataChannelDescriptorLoader.class));
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeServiceIT.java
----------------------------------------------------------------------
diff --git a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeServiceIT.java
b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeServiceIT.java
index 8bfbea4..db86dc8 100644
--- a/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeServiceIT.java
+++ b/cayenne-project-compatibility/src/test/java/org/apache/cayenne/project/compatibility/CompatibilityUpgradeServiceIT.java
@@ -21,25 +21,15 @@ package org.apache.cayenne.project.compatibility;
 
 import java.net.URL;
 
-import org.apache.cayenne.configuration.DataChannelDescriptorLoader;
-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.project.ProjectSaver;
 import org.apache.cayenne.project.upgrade.UpgradeService;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V10;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V7;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V8;
-import org.apache.cayenne.project.upgrade.handlers.UpgradeHandler_V9;
 import org.apache.cayenne.resource.Resource;
 import org.apache.cayenne.resource.URLResource;
 import org.junit.Test;
 import org.w3c.dom.Document;
 
 import static org.junit.Assert.*;
-import static org.mockito.Mockito.mock;
 
 /**
  * @since 4.1
@@ -47,22 +37,8 @@ import static org.mockito.Mockito.mock;
 public class CompatibilityUpgradeServiceIT {
 
     @Test
-    public void testUpgradeDom() throws Exception {
-        Injector injector = DIBootstrap.createInjector(new Module() {
-            @Override
-            public void configure(Binder binder) {
-                binder.bind(UpgradeService.class).to(CompatibilityUpgradeService.class);
-                binder.bind(DocumentProvider.class).to(DefaultDocumentProvider.class);
-                binder.bindList(UpgradeHandler.class)
-                        .add(UpgradeHandler_V7.class)
-                        .add(UpgradeHandler_V8.class)
-                        .add(UpgradeHandler_V9.class)
-                        .add(UpgradeHandler_V10.class);
-
-                binder.bind(ProjectSaver.class).toInstance(mock(ProjectSaver.class));
-                binder.bind(DataChannelDescriptorLoader.class).toInstance(mock(DataChannelDescriptorLoader.class));
-            }
-        });
+    public void testUpgradeFullProjectDom() throws Exception {
+        Injector injector = getInjector();
 
         CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService)injector
                 .getInstance(UpgradeService.class);
@@ -87,4 +63,28 @@ public class CompatibilityUpgradeServiceIT {
         assertEquals(2, dataMapDocument.getElementsByTagName("db-attribute").getLength());
     }
 
+    @Test
+    public void testUpgradeStandAloneDataMapDom() throws Exception {
+        Injector injector = getInjector();
+
+        CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService)injector
+                .getInstance(UpgradeService.class);
+
+        DocumentProvider documentProvider = injector.getInstance(DocumentProvider.class);
+
+        URL dataMapUrl = getClass().getResource("test-map-v6.map.xml");
+        Document dataMapDocument = documentProvider.getDocument(dataMapUrl);
+        assertNull(dataMapDocument);
+
+        Resource resource = new URLResource(dataMapUrl);
+        upgradeService.upgradeDataMap(resource);
+
+        dataMapDocument = documentProvider.getDocument(dataMapUrl);
+        assertNotNull(dataMapDocument);
+        assertEquals("10", dataMapDocument.getDocumentElement().getAttribute("project-version"));
+    }
+
+    private Injector getInjector() {
+        return DIBootstrap.createInjector(new CompatibilityTestModule());
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a1c9e2dd/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataMapLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataMapLoader.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataMapLoader.java
index 0e50471..fa61842 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataMapLoader.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataMapLoader.java
@@ -24,7 +24,6 @@ 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.util.Util;
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
 
@@ -44,18 +43,14 @@ public class XMLDataMapLoader implements DataMapLoader {
     @Inject
     protected Provider<XMLReader> xmlReaderProvider;
 
-    DataMap map;
-
     public synchronized DataMap load(Resource configurationResource) throws CayenneRuntimeException
{
+
+        final DataMap[] maps = new DataMap[1];
+
         try(InputStream in = configurationResource.getURL().openStream()) {
             XMLReader parser = xmlReaderProvider.get();
             LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
-            loaderContext.addDataMapListener(new DataMapLoaderListener() {
-                @Override
-                public void onDataMapLoaded(DataMap dataMap) {
-                    map = dataMap;
-                }
-            });
+            loaderContext.addDataMapListener(dataMap -> maps[0] = dataMap);
             RootDataMapHandler rootHandler = new RootDataMapHandler(loaderContext);
 
             parser.setContentHandler(rootHandler);
@@ -67,9 +62,10 @@ public class XMLDataMapLoader implements DataMapLoader {
             throw new CayenneRuntimeException("Error loading configuration from %s", e, configurationResource.getURL());
         }
 
-        if(map == null) {
+        if(maps[0] == null) {
             throw new CayenneRuntimeException("Unable to load data map from %s", configurationResource.getURL());
         }
+        DataMap map = maps[0];
 
         if(map.getName() == null) {
             // set name based on location if no name provided by map itself


Mime
View raw message