Return-Path: X-Original-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BEDA5105E7 for ; Tue, 15 Oct 2013 00:42:44 +0000 (UTC) Received: (qmail 96674 invoked by uid 500); 15 Oct 2013 00:42:44 -0000 Delivered-To: apmail-jackrabbit-oak-commits-archive@jackrabbit.apache.org Received: (qmail 96636 invoked by uid 500); 15 Oct 2013 00:42:44 -0000 Mailing-List: contact oak-commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-commits@jackrabbit.apache.org Received: (qmail 96627 invoked by uid 99); 15 Oct 2013 00:42:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Oct 2013 00:42:43 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Oct 2013 00:42:42 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5EA6B23889ED; Tue, 15 Oct 2013 00:42:22 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1532157 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeImpl.java oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/nodetype/NodeTypeEqualsTest.java Date: Tue, 15 Oct 2013 00:42:22 -0000 To: oak-commits@jackrabbit.apache.org From: tripod@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131015004222.5EA6B23889ED@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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