Author: tripod
Date: Tue Oct 15 00:42:21 2013
New Revision: 1532157
URL: http://svn.apache.org/r1532157
Log:
OAK-1086 NodeTypes of successive calls to node.getPrimaryNodetypes() are not
equal
- implement equals and hashcode based on the CND of the node type definition.
Added:
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java?rev=1532157&r1=1532156&r2=1532157&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java
Tue Oct 15 00:42:21 2013
@@ -31,6 +31,8 @@ import static org.apache.jackrabbit.oak.
import static
org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_QUERYABLE;
import static
org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.RESIDUAL_NAME;
+import java.io.IOException;
+import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
@@ -49,6 +51,7 @@ import javax.jcr.nodetype.ItemDefinition
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeDefinition;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.PropertyDefinition;
@@ -57,6 +60,8 @@ import com.google.common.collect.Iterabl
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+
+import org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefWriter;
import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Tree;
@@ -97,6 +102,8 @@ class NodeTypeImpl extends AbstractTypeD
private static final String[] NO_NAMES = new String[0];
+ private String cnd;
+
NodeTypeImpl(Tree type, NamePathMapper mapper) {
super(type, mapper);
}
@@ -405,13 +412,68 @@ class NodeTypeImpl extends AbstractTypeD
return internalCanRemoveItem(propertyName,
Arrays.asList(getPropertyDefinitions()));
}
+ /**
+ * Returns the namespace neutral CND of the given node type definition.
+ * @param def the node type definition
+ * @return the CND
+ * @throws IOException if an error occurs.
+ */
+ private static String getCnd(NodeTypeDefinition def) throws IOException {
+ StringWriter out = new StringWriter();
+ CompactNodeTypeDefWriter cndWriter = new CompactNodeTypeDefWriter(out,
new CompactNodeTypeDefWriter.NamespaceMapping(){
+ @Override
+ public String getNamespaceURI(String s) {
+ return s;
+ }
+ }, false);
+ cndWriter.write(def);
+ return out.toString();
+ }
+
+ /**
+ * Returns the namespace neutral CND of the this node type definition.
+ * @return the CND
+ */
+ private String getCnd() {
+ if (cnd == null) {
+ try {
+ cnd = getCnd(this);
+ } catch (IOException e) {
+ log.error("Internal error while writing CND for {}", this);
+ cnd = getName();
+ }
+ }
+ return cnd;
+ }
+
//-------------------------------------------------------------< Object
>---
@Override
public String toString() {
return getName();
}
- //-----------------------------------------------------------< internal
>---
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o instanceof NodeTypeImpl) {
+ return getCnd().equals(((NodeTypeImpl) o).getCnd());
+ } else if (o instanceof NodeType) {
+ try {
+ return getCnd().equals(getCnd((NodeType) o));
+ } catch (IOException e) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return getCnd().hashCode();
+ }
+
+//-----------------------------------------------------------< internal >---
private boolean internalCanRemoveItem(String itemName,
Iterable<? extends ItemDefinition>
definitions) {
Added:
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java?rev=1532157&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java
(added)
+++
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java
Tue Oct 15 00:42:21 2013
@@ -0,0 +1,41 @@
+/*
+ * 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 javax.jcr.nodetype.NodeType;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class NodeTypeEqualsTest extends AbstractJCRTest {
+
+ /**
+ * Tests if 2 NodeType objects are equals if they refer to the same node
type. OAK-1086.
+ * @throws Exception
+ */
+ @Test
+ public void testNodeTypeEquals() throws Exception {
+ NodeType nt = testRootNode.getPrimaryNodeType();
+ Assert.assertEquals("Same NoteType could be equal", nt,
testRootNode.getPrimaryNodeType());
+ }
+
+
+}
\ No newline at end of file