jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
Subject svn commit: r1091263 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/commons/privilege/ test/java/org/apache/jackrabbit/commons/privilege/ test/resources/ test/resources/org/ test/resources/org/apache/ test/resources/...
Date Tue, 12 Apr 2011 00:32:30 GMT
Author: tripod
Date: Tue Apr 12 00:32:29 2011
New Revision: 1091263

URL: http://svn.apache.org/viewvc?rev=1091263&view=rev
Log:
JCR-2940 PrivilegeDefinition should implement equals and hashcode

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/PrivilegeHandlerTest.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/readtest.xml
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/writetest.xml
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeDefinition.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeXmlHandler.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeDefinition.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeDefinition.java?rev=1091263&r1=1091262&r2=1091263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeDefinition.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeDefinition.java
Tue Apr 12 00:32:29 2011
@@ -16,10 +16,13 @@
  */
 package org.apache.jackrabbit.commons.privilege;
 
+import java.util.Arrays;
+
 /**
  * <code>PrivilegeDefinition</code>
  */
 public class PrivilegeDefinition {
+
     private final String name;
     private final boolean isAbstract;
     private final String[] declaredAggregateNames;
@@ -27,7 +30,7 @@ public class PrivilegeDefinition {
     public PrivilegeDefinition(String name, boolean isAbstract, String[] declaredAggregateNames)
{
         this.name = name;
         this.isAbstract = isAbstract;
-        this.declaredAggregateNames = declaredAggregateNames;
+        this.declaredAggregateNames = declaredAggregateNames == null ? new String[0] : declaredAggregateNames;
     }
 
     public String getName() {
@@ -41,4 +44,26 @@ public class PrivilegeDefinition {
     public String[] getDeclaredAggregateNames() {
         return declaredAggregateNames;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        PrivilegeDefinition that = (PrivilegeDefinition) o;
+
+        if (isAbstract != that.isAbstract) return false;
+        if (!Arrays.equals(declaredAggregateNames, that.declaredAggregateNames)) return false;
+        if (name != null ? !name.equals(that.name) : that.name != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = name != null ? name.hashCode() : 0;
+        result = 31 * result + (isAbstract ? 1 : 0);
+        result = 31 * result + (declaredAggregateNames != null ? Arrays.hashCode(declaredAggregateNames)
: 0);
+        return result;
+    }
 }
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeXmlHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeXmlHandler.java?rev=1091263&r1=1091262&r2=1091263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeXmlHandler.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/privilege/PrivilegeXmlHandler.java
Tue Apr 12 00:32:29 2011
@@ -28,6 +28,7 @@ import org.xml.sax.helpers.DefaultHandle
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
@@ -39,8 +40,21 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
 /**
- * <code>PrivilegeXmlHandler</code> 
+ * The <code>PrivilegeXmlHandler</code> loads and stores privilege definitions
from a XML document using the
+ * following format:
+ * <xmp>
+ *  <!DOCTYPE privileges [
+ *  <!ELEMENT privileges (privilege)+>
+ *  <!ELEMENT privilege (contains)+>
+ *  <!ATTLIST privilege abstract (true|false) false>
+ *  <!ATTLIST privilege name NMTOKEN #REQUIRED>
+ *  <!ELEMENT contains EMPTY>
+ *  <!ATTLIST contains name NMTOKEN #REQUIRED>
+ * ]>
+ * </xmp>
  */
 class PrivilegeXmlHandler implements PrivilegeHandler {
 
@@ -154,6 +168,8 @@ class PrivilegeXmlHandler implements Pri
             }
 
             Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
             transformer.transform(new DOMSource(doc), new StreamResult(out));
 
         } catch (Exception e) {

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/PrivilegeHandlerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/PrivilegeHandlerTest.java?rev=1091263&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/PrivilegeHandlerTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/privilege/PrivilegeHandlerTest.java
Tue Apr 12 00:32:29 2011
@@ -0,0 +1,87 @@
+/*
+ * Copyright 1997-2011 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * This software is the confidential and proprietary information of
+ * Day Management AG, ("Confidential Information"). You shall not
+ * disclose such Confidential Information and shall use it only in
+ * accordance with the terms of the license agreement you entered into
+ * with Day.
+ */
+package org.apache.jackrabbit.commons.privilege;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * <code>PrivilegeReadTest</code>...
+ */
+public class PrivilegeHandlerTest extends TestCase {
+
+    /*
+    <privilege name="foo:testRead"/>
+    <privilege name="foo:testWrite"/>
+    <privilege abstract="true" name="foo:testAbstract"/>
+    <privilege abstract="false" name="foo:testNonAbstract"/>
+    <privilege name="foo:testAll">
+        <contains name="foo:testRead"/>
+        <contains name="foo:testWrite"/>
+    </privilege>
+
+     */
+    public static PrivilegeDefinition DEF_READ = new PrivilegeDefinition("foo:testRead",
false, null);
+    public static PrivilegeDefinition DEF_WRITE = new PrivilegeDefinition("foo:testWrite",
false, null);
+    public static PrivilegeDefinition DEF_ABSTRACT = new PrivilegeDefinition("foo:testAbstract",
true, null);
+    public static PrivilegeDefinition DEF_NON_ABSTRACT = new PrivilegeDefinition("foo:testNonAbstract",
false, null);
+    public static PrivilegeDefinition DEF_ALL = new PrivilegeDefinition("foo:testAll", false,
new String[]{"foo:testRead", "foo:testWrite"});
+    public static PrivilegeDefinition[] DEF_EXPECTED = new PrivilegeDefinition[]{
+            DEF_READ,
+            DEF_WRITE,
+            DEF_ABSTRACT,
+            DEF_NON_ABSTRACT,
+            DEF_ALL
+    };
+
+    public void testRead() throws Exception {
+        InputStream in = getClass().getResourceAsStream("readtest.xml");
+
+        PrivilegeDefinitionReader reader = new PrivilegeDefinitionReader(in, "text/xml");
+
+        Map<String, PrivilegeDefinition> defs = new HashMap<String, PrivilegeDefinition>();
+        for (PrivilegeDefinition def: reader.getPrivilegeDefinitions()) {
+            defs.put(def.getName(), def);
+        }
+        for (PrivilegeDefinition def: DEF_EXPECTED) {
+            PrivilegeDefinition e = defs.remove(def.getName());
+            assertNotNull("Definition " + def.getName() + " missing");
+            assertEquals("Definition mismatch.", def,  e);
+        }
+        assertTrue("Not all definitions present", defs.isEmpty());
+
+        // check for namespace
+        String fooUri = reader.getNamespaces().get("foo");
+        assertEquals("Namespace included", "http://www.foo.com/1.0", fooUri);
+    }
+
+    public void testWrite() throws Exception {
+
+        PrivilegeDefinitionWriter writer = new PrivilegeDefinitionWriter("text/xml");
+        Map<String, String> namespaces = new HashMap<String, String>();
+        namespaces.put("foo", "http://www.foo.com/1.0");
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        writer.writeDefinitions(out, DEF_EXPECTED, namespaces);
+
+        String result = out.toString("utf-8").trim();
+
+        byte[] buffer = new byte[0x10000];
+        int read = getClass().getResourceAsStream("writetest.xml").read(buffer);
+        String expected = new String(buffer, 0, read, "utf-8").trim();
+        assertEquals("Write", expected, result);
+    }
+
+}
\ No newline at end of file

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/readtest.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/readtest.xml?rev=1091263&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/readtest.xml
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/readtest.xml
Tue Apr 12 00:32:29 2011
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<privileges xmlns:foo="http://www.foo.com/1.0">
+    <privilege name="foo:testRead"/>
+    <privilege name="foo:testWrite"/>
+    <privilege abstract="true" name="foo:testAbstract"/>
+    <privilege abstract="false" name="foo:testNonAbstract"/>
+    <privilege name="foo:testAll">
+        <contains name="foo:testRead"/>
+        <contains name="foo:testWrite"/>
+    </privilege>
+</privileges>
\ No newline at end of file

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/writetest.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/writetest.xml?rev=1091263&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/writetest.xml
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/resources/org/apache/jackrabbit/commons/privilege/writetest.xml
Tue Apr 12 00:32:29 2011
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<privileges xmlns:foo="http://www.foo.com/1.0">
+<privilege abstract="false" name="foo:testRead"/>
+<privilege abstract="false" name="foo:testWrite"/>
+<privilege abstract="true" name="foo:testAbstract"/>
+<privilege abstract="false" name="foo:testNonAbstract"/>
+<privilege abstract="false" name="foo:testAll">
+<contains name="foo:testRead"/>
+<contains name="foo:testWrite"/>
+</privilege>
+</privileges>
\ No newline at end of file



Mime
View raw message