jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1077927 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ jackrabbi...
Date Fri, 04 Mar 2011 13:05:33 GMT
Author: stefan
Date: Fri Mar  4 13:05:32 2011
New Revision: 1077927

URL: http://svn.apache.org/viewvc?rev=1077927&view=rev
Log:
JCR-2903: Session.importXml should close the input stream (as to JSR 283/JCR 2.0)

Added:
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueInputStreamTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/util/InputStreamWrapper.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ValueFactoryImpl.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/AbstractValueFactory.java
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientSession.java
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientWorkspace.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SerializationTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyConstraintViolationExceptionTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyInputStreamTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueConstraintViolationExceptionTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ShareableNodeTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ValueFactoryTest.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/SessionImpl.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceImpl.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ValueFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ValueFactoryImpl.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ValueFactoryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ValueFactoryImpl.java
Fri Mar  4 13:05:32 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.core.value;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.core.data.DataIdentifier;
 import org.apache.jackrabbit.core.data.DataStore;
 import org.apache.jackrabbit.spi.commons.conversion.NamePathResolver;
@@ -79,6 +80,9 @@ public class ValueFactoryImpl extends Va
             }
         } catch (IOException e) {
             throw new RepositoryException(e);
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(stream);
         }
     }
 
@@ -116,6 +120,9 @@ public class ValueFactoryImpl extends Va
             throw new RuntimeException(ex);
         } catch (RepositoryException ex) {
             throw new RuntimeException(ex);
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(value);
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
Fri Mar  4 13:05:32 2011
@@ -351,6 +351,11 @@ public abstract class AbstractSession im
             } else {
                 throw new InvalidSerializedDataException("XML parse error", e);
             }
+        } finally {
+            // JCR-2903
+            if (in != null) {
+                try { in.close(); } catch (IOException ignore) {}
+            }
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
Fri Mar  4 13:05:32 2011
@@ -61,6 +61,11 @@ public abstract class AbstractWorkspace 
             } else {
                 throw new InvalidSerializedDataException("XML parse error", e);
             }
+        } finally {
+            // JCR-2903
+            if (in != null) {
+                try { in.close(); } catch (IOException ignore) {}
+            }
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/AbstractValueFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/AbstractValueFactory.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/AbstractValueFactory.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/AbstractValueFactory.java
Fri Mar  4 13:05:32 2011
@@ -88,7 +88,12 @@ public abstract class AbstractValueFacto
      * {@inheritDoc}
      */
     public Value createValue(InputStream value) {
-        return new BinaryValue(value);
+        try {
+            return new BinaryValue(value);
+        } finally {
+            // JCR-2903
+            try { value.close(); } catch (IOException ignore) {}
+        }
     }
 
     /**
@@ -171,6 +176,9 @@ public abstract class AbstractValueFacto
             return new BinaryImpl(stream);
         } catch (IOException e) {
             throw new RepositoryException("failed to create Binary instance", e);
+        } finally {
+            // JCR-2903
+            try { stream.close(); } catch (IOException ignore) {}
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientSession.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientSession.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientSession.java
Fri Mar  4 13:05:32 2011
@@ -333,6 +333,9 @@ public class ClientSession extends Clien
             remote.importXML(path, buffer.toByteArray(), mode);
         } catch (RemoteException ex) {
             throw new RemoteRepositoryException(ex);
+        } finally {
+            // JCR-2903
+            try { xml.close(); } catch (IOException ignore) {}
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientWorkspace.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientWorkspace.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientWorkspace.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/client/ClientWorkspace.java
Fri Mar  4 13:05:32 2011
@@ -231,6 +231,9 @@ public class ClientWorkspace extends Cli
             remote.importXML(path, buffer.toByteArray(), uuidBehaviour);
         } catch (RemoteException ex) {
             throw new RemoteRepositoryException(ex);
+        } finally {
+            // JCR-2903
+            try { xml.close(); } catch (IOException ignore) {}
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
Fri Mar  4 13:05:32 2011
@@ -242,7 +242,7 @@ abstract class AbstractImportXmlTest ext
                 session.save();
             }
         } finally {
-            bin.close();
+            try { bin.close(); } catch (IOException ignore) {}
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SerializationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SerializationTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SerializationTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SerializationTest.java
Fri Mar  4 13:05:32 2011
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.test.api;
 
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
+import org.apache.jackrabbit.test.api.util.InputStreamWrapper;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -145,7 +146,7 @@ public class SerializationTest extends A
         } catch (VersionException e) {
             // success
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -160,7 +161,7 @@ public class SerializationTest extends A
         } catch (VersionException e) {
             // success
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -216,7 +217,7 @@ public class SerializationTest extends A
             } catch (LockException e) {
                 // success
             } finally {
-                in.close();
+                try { in.close(); } catch (IOException ignore) {}
             }
         } else {
             log.println("Locking not supported.");
@@ -353,7 +354,7 @@ public class SerializationTest extends A
         } catch (PathNotFoundException e) {
             // success
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -372,7 +373,7 @@ public class SerializationTest extends A
         } catch (PathNotFoundException e) {
             // success
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -416,7 +417,7 @@ public class SerializationTest extends A
                 }
             }
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -477,7 +478,7 @@ public class SerializationTest extends A
                 }
             }
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -510,7 +511,7 @@ public class SerializationTest extends A
             session.importXML(treeComparator.targetFolder, in,
                     ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // after logout/login, no nodes are in the session
@@ -533,7 +534,7 @@ public class SerializationTest extends A
             exportRepository(SAVEBINARY, RECURSE);
             doImportNoSave(treeComparator.targetFolder, in, CONTENTHANDLER);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // after logout/login, no nodes are in the session
@@ -545,11 +546,33 @@ public class SerializationTest extends A
         treeComparator.compare(treeComparator.CHECK_EMPTY);
     }
 
+//----------------< tests input stream handling contract >----------------------
+
+    /**
+     * Tests whether <code>Session.importXML</code> and <code>Workspace.importXML</code>
+     * obey the stream handling contract.
+     */
+    public void testStreamHandling() throws RepositoryException, IOException {
+        exportRepository(SAVEBINARY, RECURSE);
+
+        InputStreamWrapper in = new InputStreamWrapper(new FileInputStream(file));
+        session.importXML(treeComparator.targetFolder, in,
+                ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
+        assertTrue("Session.importXML(..., InputStream, ...) is expected to close the passed
input stream", in.isClosed());
+        session.refresh(false);
+
+        in = new InputStreamWrapper(new FileInputStream(file));
+        workspace.importXML(treeComparator.targetFolder, in,
+                ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
+        assertTrue("Workspace.importXML(..., InputStream, ...) is expected to close the passed
input stream", in.isClosed());
+    }
+
+
 //----------------< import test helper >--------------------------------------------------------
     /**
      * Helper method which imports the given FileInputStream using Workspace or Session
-     * and via the methods importXML respective getImportContentHandler. Teh target node
of the
-     * import is specified with its absolut path.
+     * and via the methods importXML respective getImportContentHandler. The target node
of the
+     * import is specified with its absolute path.
      *
      * @param absPath
      * @param in
@@ -729,7 +752,7 @@ public class SerializationTest extends A
         } catch (SAXException e) {
             fail("Error while parsing the imported repository: " + e);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyConstraintViolationExceptionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyConstraintViolationExceptionTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyConstraintViolationExceptionTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyConstraintViolationExceptionTest.java
Fri Mar  4 13:05:32 2011
@@ -270,9 +270,7 @@ public class SetPropertyConstraintViolat
         } catch (ConstraintViolationException e) {
             // success
         } finally {
-            try {
-                in.close();
-            } catch (IOException ignore) {}
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // test of signature setProperty(String name, Value value)

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyInputStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyInputStreamTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyInputStreamTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetPropertyInputStreamTest.java
Fri Mar  4 13:05:32 2011
@@ -18,10 +18,12 @@ package org.apache.jackrabbit.test.api;
 
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
+import org.apache.jackrabbit.test.api.util.InputStreamWrapper;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.ByteArrayInputStream;
 
@@ -74,8 +76,8 @@ public class SetPropertyInputStreamTest 
             assertTrue("Setting property with Node.setProperty(String, InputStream) and Session.save()
not working",
                     compareInputStreams(is1, in));
         } finally {
-            in.close();
-        }
+            try { in.close(); } catch (IOException ignore) {}
+         }
     }
 
     /**
@@ -93,8 +95,8 @@ public class SetPropertyInputStreamTest 
             assertTrue("Modifying property with Node.setProperty(String, InputStream) and
Session.save() not working",
                     compareInputStreams(is2, in));
         } finally {
-            in.close();
-        }
+            try { in.close(); } catch (IOException ignore) {}
+         }
     }
 
     /**
@@ -110,7 +112,7 @@ public class SetPropertyInputStreamTest 
             assertTrue("Setting property with Node.setProperty(String, InputStream) and parentNode.save()
not working",
                     compareInputStreams(is1, in));
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -129,8 +131,8 @@ public class SetPropertyInputStreamTest 
             assertTrue("Modifying property with Node.setProperty(String, InputStream) and
parentNode.save() not working",
                     compareInputStreams(is2, in));
         } finally {
-            in.close();
-        }
+            try { in.close(); } catch (IOException ignore) {}
+         }
     }
 
     /**
@@ -174,6 +176,16 @@ public class SetPropertyInputStreamTest 
     }
 
     /**
+     * Tests whether the passed input stream is closed.
+     * @throws Exception
+     */
+    public void testInputStreamClosed() throws Exception {
+        InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(bytes1));
+        testNode.setProperty(propertyName1, in);
+        assertTrue("Node.setProperty(..., InputStream) is expected to close the passed input
stream", in.isClosed());
+    }
+
+    /**
      * helper function: InputStream comparison
      */
     private boolean compareInputStreams(InputStream f1, InputStream f2) {

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java
Fri Mar  4 13:05:32 2011
@@ -110,7 +110,7 @@ public class SetValueBinaryTest extends 
         try {
             compareStream(data, in);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -127,7 +127,7 @@ public class SetValueBinaryTest extends 
             try {
                 compareStream(data, in);
             } finally {
-                in.close();
+                try { in.close(); } catch (IOException ignore) {}
             }
         } finally {
             bin.dispose();
@@ -144,13 +144,13 @@ public class SetValueBinaryTest extends 
             property1.setValue(in);
             node.save();
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
         in = property1.getValue().getStream();
         try {
             compareStream(data, in);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
     }
 
@@ -168,7 +168,7 @@ public class SetValueBinaryTest extends 
             try {
                 compareStream(data, in);
             } finally {
-                in.close();
+                try { in.close(); } catch (IOException ignore) {}
             }
         } finally {
             bin.dispose();

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueConstraintViolationExceptionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueConstraintViolationExceptionTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueConstraintViolationExceptionTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueConstraintViolationExceptionTest.java
Fri Mar  4 13:05:32 2011
@@ -102,9 +102,7 @@ public class SetValueConstraintViolation
         } catch (ConstraintViolationException e) {
             // success
         } finally {
-            try {
-                in.close();
-            } catch (IOException ignore) {}
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // test of signature setValue(Value value)

Added: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueInputStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueInputStreamTest.java?rev=1077927&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueInputStreamTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueInputStreamTest.java
Fri Mar  4 13:05:32 2011
@@ -0,0 +1,104 @@
+/*
+ * 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.test.api;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+import org.apache.jackrabbit.test.api.util.InputStreamWrapper;
+
+import javax.jcr.Binary;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Tests the {@link javax.jcr.Property#setValue(java.io.InputStream)} method.
+ * <p>
+ * Configuration requirements:
+ * <p>
+ * The node at {@link #testRoot} must allow a
+ * child node of type {@link #testNodeType} with name {@link #nodeName1}. The
+ * node type {@link #testNodeType} must define a single value binary property
+ * with name {@link #propertyName1}. As a special case, if the specified node
+ * type automatically adds a jcr:content child node of type nt:resource, and
+ * <code>propertyName1</code> is specified as "jcr:data", that binary property
+ * is used instead.
+ *
+ * @test
+ * @sources SetValueInputStreamTest.java
+ * @executeClass org.apache.jackrabbit.test.api.SetValueInputStreamTest
+ * @keywords level2
+ */
+public class SetValueInputStreamTest extends AbstractJCRTest {
+
+    /**
+     * The binary data
+     */
+    private byte[] data;
+
+    /**
+     * The node with the binary property
+     */
+    private Node node;
+
+    /**
+     * The binary property
+     */
+    private Property property1;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // initialize some binary value
+        data = createRandomString(10).getBytes();
+
+        // create a new node under the testRootNode
+        node = testRootNode.addNode(nodeName1, testNodeType);
+        testRootNode.save();
+
+        // special case for repositories that do allow binary property
+        // values, but only on jcr:content/jcr:data
+        if (propertyName1.equals("jcr:data") && node.hasNode("jcr:content")
+            && node.getNode("jcr:content").isNodeType("nt:resource") && !
node.hasProperty("jcr:data")) {
+            node = node.getNode("jcr:content");
+        }
+
+        // create a new single-value property and save it
+        property1 = node.setProperty(propertyName1, superuser.getValueFactory().createValue(new
ByteArrayInputStream(new byte[0])));
+        superuser.save();
+    }
+
+    protected void tearDown() throws Exception {
+        node = null;
+        property1 = null;
+        super.tearDown();
+    }
+
+    /**
+     * Tests whether <code>Property.setValue(InputStream)</code> obeys the
+     * stream handling contract.
+     */
+    public void testInputStreamClosed() throws RepositoryException, IOException {
+        InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(data));
+        property1.setValue(in);
+        assertTrue("Property.setValue(InputStream) is expected to close the passed input
stream", in.isClosed());
+    }
+}

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ShareableNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ShareableNodeTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ShareableNodeTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ShareableNodeTest.java
Fri Mar  4 13:05:32 2011
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.test.api;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
@@ -675,7 +676,7 @@ public class ShareableNodeTest extends A
             workspace.importXML(testRootNode.getPath(), in,
                     ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // verify shared set consists of two nodes
@@ -728,7 +729,7 @@ public class ShareableNodeTest extends A
         try {
             workspace.importXML(a3.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // verify there's another element in the shared set
@@ -785,7 +786,7 @@ public class ShareableNodeTest extends A
         try {
             workspace.importXML(a3.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // verify there's another element in the shared set
@@ -843,7 +844,7 @@ public class ShareableNodeTest extends A
             session.importXML(a3.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
             session.save();
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // verify there's another element in the shared set
@@ -901,7 +902,7 @@ public class ShareableNodeTest extends A
             session.importXML(a3.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
             session.save();
         } finally {
-            in.close();
+            try { in.close(); } catch (IOException ignore) {}
         }
 
         // verify there's another element in the shared set

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ValueFactoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ValueFactoryTest.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ValueFactoryTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/ValueFactoryTest.java
Fri Mar  4 13:05:32 2011
@@ -18,7 +18,9 @@ package org.apache.jackrabbit.test.api;
 
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
+import org.apache.jackrabbit.test.api.util.InputStreamWrapper;
 
+import javax.jcr.Binary;
 import javax.jcr.Session;
 import javax.jcr.ValueFactory;
 import javax.jcr.Node;
@@ -319,4 +321,21 @@ public class ValueFactoryTest extends Ab
         }
 
     }
+
+    /**
+     * Tests whether a passed <code>InputStream</code> is closed
+     * by the implementation.
+     *
+     * @throws RepositoryException
+     */
+    public void testInputStream() throws RepositoryException {
+        InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
+        valueFactory.createValue(in);
+        assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed
input stream", in.isClosed());
+
+        in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
+        Binary bin = valueFactory.createBinary(in);
+        assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed
input stream", in.isClosed());
+        bin.dispose();
+    }
 }

Added: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/util/InputStreamWrapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/util/InputStreamWrapper.java?rev=1077927&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/util/InputStreamWrapper.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/util/InputStreamWrapper.java
Fri Mar  4 13:05:32 2011
@@ -0,0 +1,44 @@
+/*
+ * 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.test.api.util;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Helper class which allows to check whether the #close() method has been
+ * called on this stream.
+ */
+public class InputStreamWrapper extends FilterInputStream {
+
+    private boolean closed;
+
+    public InputStreamWrapper(InputStream in) {
+        super(in);
+        closed = false;
+    }
+
+    public void close() throws IOException {
+        closed = true;
+        super.close();
+    }
+
+    public boolean isClosed() {
+        return closed;
+    }
+}

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/SessionImpl.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/SessionImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/SessionImpl.java
Fri Mar  4 13:05:32 2011
@@ -51,6 +51,7 @@ import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
 import org.apache.commons.collections.map.ReferenceMap;
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.commons.AbstractSession;
 import org.apache.jackrabbit.jcr2spi.config.CacheBehaviour;
 import org.apache.jackrabbit.jcr2spi.config.RepositoryConfig;
@@ -409,6 +410,9 @@ public class SessionImpl extends Abstrac
             }
         } catch (ParserConfigurationException e) {
             throw new RepositoryException("SAX parser configuration error", e);
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(in);
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceImpl.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceImpl.java
Fri Mar  4 13:05:32 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.jcr2spi;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.jcr2spi.config.CacheBehaviour;
 import org.apache.jackrabbit.jcr2spi.config.RepositoryConfig;
 import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyManager;
@@ -339,8 +340,13 @@ public class WorkspaceImpl implements Wo
         int options = ItemStateValidator.CHECK_ACCESS | ItemStateValidator.CHECK_LOCK | ItemStateValidator.CHECK_VERSIONING;
         getValidator().checkIsWritable(parentState, options);
 
-        // run the import
-        wspManager.execute(WorkspaceImport.create(parentState, in, uuidBehavior));
+        try {
+            // run the import
+            wspManager.execute(WorkspaceImport.create(parentState, in, uuidBehavior));
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(in);
+        }
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java?rev=1077927&r1=1077926&r2=1077927&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
Fri Mar  4 13:05:32 2011
@@ -30,6 +30,7 @@ import javax.jcr.Value;
 import javax.jcr.ValueFactory;
 import javax.jcr.ValueFormatException;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.spi.commons.conversion.IllegalNameException;
 import org.apache.jackrabbit.spi.commons.conversion.MalformedPathException;
 import org.apache.jackrabbit.spi.commons.conversion.NamePathResolver;
@@ -158,6 +159,9 @@ public class ValueFactoryQImpl implement
             throw new RuntimeException(ex);
         } catch (RepositoryException ex) {
             throw new RuntimeException(ex);
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(value);
         }
     }
 
@@ -208,6 +212,9 @@ public class ValueFactoryQImpl implement
             throw new RuntimeException(ex);
         } catch (RepositoryException ex) {
             throw new RuntimeException(ex);
+        } finally {
+            // JCR-2903
+            IOUtils.closeQuietly(stream);
         }
     }
 



Mime
View raw message