cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [1/2] cayenne git commit: CAY-2471 Support multiple XML project versions
Date Wed, 05 Sep 2018 09:06:24 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 399fc83ce -> b8b4e00ab


CAY-2471 Support multiple XML project versions


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

Branch: refs/heads/master
Commit: 9c39fa7b53371a24740682234e99945bf232889d
Parents: 399fc83
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Wed Sep 5 12:01:37 2018 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Wed Sep 5 12:01:37 2018 +0300

----------------------------------------------------------------------
 RELEASE-NOTES.txt                               |  1 +
 .../configuration/xml/VersionAwareHandler.java  |  7 +-
 .../xml/XMLDataChannelDescriptorLoader.java     |  9 ++-
 .../xml/VersionAwareHandlerTest.java            | 67 ++++++++++++++++++++
 4 files changed, 80 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c39fa7b/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index f3f65af..5821001 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -15,6 +15,7 @@ Changes/New Features:
 
 CAY-2446 Run Disjoint By Id queries outside of synchronized block
 CAY-2447 Crypto support for LocalDateTime
+CAY-2471 Support multiple XML project versions
 
 Bug Fixes:
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c39fa7b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
index 2a0fb5c..310eb86 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/VersionAwareHandler.java
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne.configuration.xml;
 
+import java.util.Arrays;
 import java.util.Objects;
 
 import org.apache.cayenne.CayenneRuntimeException;
@@ -44,7 +45,7 @@ public abstract class VersionAwareHandler extends NamespaceAwareNestedTagHandler
     @Override
     protected boolean processElement(String namespaceURI, String localName, Attributes attributes)
throws SAXException {
         if(rootTag.equals(localName)) {
-            validateVersion(attributes);
+            validateVersion(attributes, XMLDataChannelDescriptorLoader.SUPPORTED_PROJECT_VERSIONS);
             validateNamespace(namespaceURI);
         } else {
             throw new CayenneRuntimeException("Illegal XML root tag: %s, expected: %s", localName,
rootTag);
@@ -52,9 +53,9 @@ public abstract class VersionAwareHandler extends NamespaceAwareNestedTagHandler
         return false;
     }
 
-    protected void validateVersion(Attributes attributes) {
+    protected void validateVersion(Attributes attributes, String[] supportedVersions) {
         String version = attributes.getValue("project-version");
-        if(!XMLDataChannelDescriptorLoader.CURRENT_PROJECT_VERSION.equals(version)) {
+        if(Arrays.binarySearch(supportedVersions, version) < 0) {
             throw new CayenneRuntimeException("Unsupported project version: %s, please upgrade
project using Modeler or " +
                     "include cayenne-project-compatibility module v%s",
                     version, LocalizedStringsHandler.getString("cayenne.version"));

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c39fa7b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
index 1f3f6f1..3b90432 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/xml/XMLDataChannelDescriptorLoader.java
@@ -40,6 +40,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.util.Arrays;
 
 /**
  * @since 3.1
@@ -49,7 +50,13 @@ public class XMLDataChannelDescriptorLoader implements DataChannelDescriptorLoad
 
 	private static final Logger logger = LoggerFactory.getLogger(XMLDataChannelDescriptorLoader.class);
 
-	static final String CURRENT_PROJECT_VERSION = "10";
+	/**
+	 * Versions of project XML files that this loader can read.
+	 */
+	static final String[] SUPPORTED_PROJECT_VERSIONS = {"10"};
+	static {
+		Arrays.sort(SUPPORTED_PROJECT_VERSIONS);
+	}
 
 	/**
 	 * @deprecated the caller should use password resolving strategy instead of

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c39fa7b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/VersionAwareHandlerTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/VersionAwareHandlerTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/VersionAwareHandlerTest.java
new file mode 100644
index 0000000..5a5b823
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/configuration/xml/VersionAwareHandlerTest.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.configuration.xml;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.junit.Before;
+import org.junit.Test;
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * @since 4.1
+ */
+public class VersionAwareHandlerTest {
+
+    private static String[] VERSION_SET_1 = {"10", "11", "9"}; // sorted as strings
+    private static String[] VERSION_SET_2 = {"10"};
+
+    VersionAwareHandler handler;
+
+    @Before
+    public void createHandler() {
+        handler = new VersionAwareHandler(new LoaderContext(null, null), "test"){
+        };
+    }
+
+    private Attributes createAttributesWithVersion(String version) {
+        AttributesImpl attributes = new AttributesImpl();
+        attributes.addAttribute("", "project-version", "project-version", "", version);
+        return attributes;
+    }
+
+    @Test
+    public void validateCorrectVersion() {
+        handler.validateVersion(createAttributesWithVersion("9"), VERSION_SET_1);
+        handler.validateVersion(createAttributesWithVersion("10"), VERSION_SET_1);
+        handler.validateVersion(createAttributesWithVersion("11"), VERSION_SET_1);
+        handler.validateVersion(createAttributesWithVersion("10"), VERSION_SET_2);
+    }
+
+    @Test(expected = CayenneRuntimeException.class)
+    public void validateIncorrectVersion1() {
+        handler.validateVersion(createAttributesWithVersion("8"), VERSION_SET_1);
+    }
+
+    @Test(expected = CayenneRuntimeException.class)
+    public void validateIncorrectVersion2() {
+        handler.validateVersion(createAttributesWithVersion("11"), VERSION_SET_2);
+    }
+}
\ No newline at end of file


Mime
View raw message