[maven-release-plugin]  copy for tag jackrabbit-oak-1.4.0

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/tags/jackrabbit-oak-1.4.0@1733893 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index c36ef84..19cc4d3 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -15,7 +15,7 @@
 The Oak effort is a part of the Apache Jackrabbit project.
 Apache Jackrabbit is a project of the Apache Software Foundation.
 
-Changes in Oak 1.2.0
+Changes since Oak 1.2.0
 ---------------------
 
 Sub-task
@@ -871,6 +871,8 @@
     [OAK-4082] - Don't pool prepared statements using lists
     [OAK-4083] - Simplify concurrency when loading data from the
     primary
+    [OAK-4085] - Malformed node type definition when reregistered after
+    upgrade
 
 Documentation
 
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/write/NodeTypeTemplateImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/write/NodeTypeTemplateImpl.java
index 1252226..3a10a86 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/write/NodeTypeTemplateImpl.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/write/NodeTypeTemplateImpl.java
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype.write;
 
+import static com.google.common.collect.Iterables.filter;
 import static org.apache.jackrabbit.JcrConstants.JCR_CHILDNODEDEFINITION;
 import static org.apache.jackrabbit.JcrConstants.JCR_HASORDERABLECHILDNODES;
 import static org.apache.jackrabbit.JcrConstants.JCR_ISMIXIN;
@@ -46,6 +47,7 @@
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
 
+import com.google.common.base.Predicate;
 import com.google.common.collect.Lists;
 
 import org.apache.jackrabbit.oak.api.Tree;
@@ -178,12 +180,16 @@
 
     private static void writeItemDefinitions(@Nonnull Tree nodeTypeTree, @CheckForNull List<? extends ItemDefinitionTemplate> itemDefTemplates,
                                              @Nonnull String nodeName, @Nonnull String primaryTypeName) throws RepositoryException {
-        Tree tree;
+        // first remove existing
+        for (Tree t : filter(nodeTypeTree.getChildren(), new SameNamePredicate(nodeName))) {
+            t.remove();
+        }
+        // now write definitions
         int index = 1;
         if (itemDefTemplates != null) {
             for (ItemDefinitionTemplate template : itemDefTemplates) {
                 String name = nodeName(nodeName, index);
-                tree = nodeTypeTree.getChild(name);
+                Tree tree = nodeTypeTree.getChild(name);
                 if (!tree.exists()) {
                     tree = nodeTypeTree.addChild(name);
                     tree.setProperty(
@@ -193,17 +199,27 @@
                 index++;
             }
         }
-        tree = nodeTypeTree.getChild(nodeName(nodeName, index++));
-        while (tree.exists()) {
-            tree.remove();
-            tree = nodeTypeTree.getChild(nodeName(nodeName, index++));
-        }
     }
 
     private static String nodeName(String name, int index) {
         return (index == 1) ? name : name + '[' + index + ']';
     }
 
+    private static final class SameNamePredicate implements Predicate<Tree> {
+
+        private final String name;
+
+        private SameNamePredicate(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public boolean apply(Tree t) {
+            String s = t.getName();
+            return s.equals(name) || s.startsWith(name + "[");
+        }
+    }
+
     //------------------------------------------------------------< public >--
 
     @Override
diff --git a/oak-jcr/pom.xml b/oak-jcr/pom.xml
index 29d19fc..2fa1c11 100644
--- a/oak-jcr/pom.xml
+++ b/oak-jcr/pom.xml
@@ -366,5 +366,11 @@
       <artifactId>commons-lang</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>net.lingala.zip4j</groupId>
+      <artifactId>zip4j</artifactId>
+      <version>1.3.2</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/UpgradeTest.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/UpgradeTest.java
new file mode 100644
index 0000000..b3a570d
--- /dev/null
+++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/UpgradeTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.jackrabbit.oak.jcr.nodetype;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.StringReader;
+
+import javax.jcr.Node;
+import javax.jcr.Repository;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.PropertyDefinition;
+
+import com.google.common.collect.Iterators;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.commons.cnd.CndImporter;
+import org.apache.jackrabbit.oak.jcr.Jcr;
+import org.apache.jackrabbit.oak.plugins.segment.SegmentStore;
+import org.junit.Test;
+
+import net.lingala.zip4j.core.ZipFile;
+
+import static org.apache.jackrabbit.oak.plugins.segment.SegmentNodeStore.newSegmentNodeStore;
+import static org.apache.jackrabbit.oak.plugins.segment.file.FileStore.newFileStore;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class UpgradeTest {
+
+    @Test
+    public void upgradeFrom10() throws Exception {
+        File testFolder = new File(new File("target"), UpgradeTest.class.getSimpleName());
+        File repoHome = new File(testFolder, "test-repo-1.0");
+        repoHome.delete();
+        File tmpZip = File.createTempFile("test-repo", null);
+        IOUtils.copy(NodeTypeTest.class.getResourceAsStream("test-repo-1.0.zip"),
+                new FileOutputStream(tmpZip));
+        ZipFile repoZip = new ZipFile(tmpZip);
+        repoZip.extractAll(testFolder.getPath());
+        tmpZip.delete();
+
+        SegmentStore store = newFileStore(repoHome).create();
+        Repository repo = new Jcr(newSegmentNodeStore(store).create()).createRepository();
+        Session s = repo.login(new SimpleCredentials("admin", "admin".toCharArray()));
+
+        Node myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
+        assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
+
+        NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
+        assertTrue(ntMgr.hasNodeType("test:MyType"));
+        NodeType nt = ntMgr.getNodeType("test:MyType");
+        PropertyDefinition[] pDefs = nt.getDeclaredPropertyDefinitions();
+        assertEquals(2, pDefs.length);
+        for (PropertyDefinition pd : pDefs) {
+            String name = pd.getName();
+            if (name.equals("test:mandatory")) {
+                assertTrue(pd.isMandatory());
+            } else if (name.equals("test:optional")) {
+                assertFalse(pd.isMandatory());
+            } else {
+                fail("Unexpected property definition: " + name);
+            }
+        }
+
+        // flip mandatory flag for test:mandatory
+        String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" +
+                "[test:MyType] > nt:unstructured\n" +
+                " - test:mandatory (string)\n" +
+                " - test:optional (string)";
+
+        CndImporter.registerNodeTypes(new StringReader(cnd), s, true);
+
+        myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
+        assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
+
+        nt = ntMgr.getNodeType("test:MyType");
+        pDefs = nt.getDeclaredPropertyDefinitions();
+        assertEquals(2, pDefs.length);
+        for (PropertyDefinition pd : pDefs) {
+            String name = pd.getName();
+            if (name.equals("test:mandatory")) {
+                assertFalse(pd.isMandatory());
+            } else if (name.equals("test:optional")) {
+                assertFalse(pd.isMandatory());
+            } else {
+                fail("Unexpected property definition: " + name);
+            }
+        }
+
+        s.logout();
+        if (repo instanceof JackrabbitRepository) {
+            ((JackrabbitRepository) repo).shutdown();
+        }
+        store.close();
+    }
+}
diff --git a/oak-jcr/src/test/resources/org/apache/jackrabbit/oak/jcr/nodetype/test-repo-1.0.zip b/oak-jcr/src/test/resources/org/apache/jackrabbit/oak/jcr/nodetype/test-repo-1.0.zip
new file mode 100644
index 0000000..b8e2b6b
--- /dev/null
+++ b/oak-jcr/src/test/resources/org/apache/jackrabbit/oak/jcr/nodetype/test-repo-1.0.zip
Binary files differ