groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [1/6] groovy git commit: GROOVY-8251: Implement withCloseable on AutoCloseable (closes #572)
Date Tue, 18 Jul 2017 01:48:53 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 6a8232a5b -> 4fae56bb8


GROOVY-8251: Implement withCloseable on AutoCloseable (closes #572)

Relocate withAutoCloseable from NIO subproject to core. Since
AutoCloseable is not strictly an NIO related class and release
2.5.0 will target Java 7, the method should be available as part
of core.

Relocated withCloseable tests to core since that method has
already been moved to core and is deprecated in the NIO module.


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9d8e4335
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9d8e4335
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9d8e4335

Branch: refs/heads/master
Commit: 9d8e433533b8d5d8e84d768db5190291ddc6b598
Parents: 6a8232a
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sun Jul 16 09:16:12 2017 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Mon Jul 17 18:45:11 2017 -0700

----------------------------------------------------------------------
 .../runtime/DefaultGroovyMethodsSupport.java    |  15 ++-
 .../groovy/runtime/IOGroovyMethods.java         |  24 ++++
 .../groovy/runtime/IOGroovyMethodsTest.groovy   | 126 +++++++++++++++++++
 .../groovy/runtime/NioGroovyMethods.java        |  44 -------
 .../groovy/runtime/NioGroovyMethodsTest.groovy  |  69 ----------
 5 files changed, 162 insertions(+), 116 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9d8e4335/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.java
index a81f292..dc370d8 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.java
@@ -86,10 +86,19 @@ public class DefaultGroovyMethodsSupport {
      * @param c the thing to close
      */
     public static void closeWithWarning(Closeable c) {
-        if (c != null) {
+        closeWithWarning((AutoCloseable) c);
+    }
+
+    /**
+     * Close the AutoCloseable. Logging a warning if any problems occur.
+     *
+     * @param closeable the thing to close
+     */
+    public static void closeWithWarning(AutoCloseable closeable) {
+        if (closeable != null) {
             try {
-                c.close();
-            } catch (IOException e) {
+                closeable.close();
+            } catch (Exception e) {
                 LOG.warning("Caught exception during close(): " + e);
             }
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9d8e4335/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java
index e7c26fd..57ea844 100644
--- a/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java
@@ -1610,6 +1610,30 @@ public class IOGroovyMethods extends DefaultGroovyMethodsSupport {
         }
     }
 
+    /**
+     * Allows this AutoCloseable to be used within the closure, ensuring that it
+     * is closed once the closure has been executed and before this method returns.
+     *
+     * @param self the AutoCloseable
+     * @param action the closure taking the AutoCloseable as parameter
+     * @return the value returned by the closure
+     * @throws Exception if an Exception occurs.
+     * @since 2.5.0
+     */
+    public static <T, U extends AutoCloseable> T withAutoCloseable(U self, @ClosureParams(value=FirstParam.class)
Closure<T> action) throws Exception {
+        try {
+            T result = action.call(self);
+
+            U temp = self;
+            self = null;
+            temp.close();
+
+            return result;
+        } finally {
+            DefaultGroovyMethodsSupport.closeWithWarning(self);
+        }
+    }
+
     static void writeUTF16BomIfRequired(final Writer writer, final String charset) throws
IOException {
         writeUTF16BomIfRequired(writer, Charset.forName(charset));
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9d8e4335/src/test/org/codehaus/groovy/runtime/IOGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/IOGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/IOGroovyMethodsTest.groovy
new file mode 100644
index 0000000..b66936c
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/IOGroovyMethodsTest.groovy
@@ -0,0 +1,126 @@
+/*
+ *  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.codehaus.groovy.runtime
+
+class IOGroovyMethodsTest extends GroovyTestCase {
+
+    void testWithAutoCloseable() {
+        def closeable = new DummyAutoCloseable()
+        def closeableParam = null
+        def result = closeable.withAutoCloseable {
+            closeableParam = it
+            123
+        }
+        assert closeableParam == closeable
+        assert result == 123
+        assert closeable.closed
+    }
+
+    void testWithAutoCloseableDoesNotSuppressException() {
+        def closeable = new DummyAutoCloseable(new Exception('close exception'))
+        def message = shouldFail(UnsupportedOperationException) {
+            closeable.withAutoCloseable {
+                throw new UnsupportedOperationException('not a close exception')
+            }
+        }
+        assert closeable.closed
+        assert message == 'not a close exception'
+    }
+
+    void testWithAutoCloseableAndException() {
+        def closeable = new DummyAutoCloseable(new Exception('close exception'))
+        def result = null
+        def message = shouldFail(Exception) {
+            closeable.withAutoCloseable {
+                result = 123
+            }
+        }
+        assert result == 123
+        assert message == 'close exception'
+    }
+
+    void testWithCloseable() {
+        def closeable = new DummyCloseable()
+        def closeableParam = null
+        def result = closeable.withCloseable {
+            closeableParam = it
+            123
+        }
+        assert closeableParam == closeable
+        assert result == 123
+        assert closeable.closed
+    }
+
+    void testWithCloseableDoesNotSuppressException() {
+        def closeable = new DummyCloseable(new IOException('close ioexception'))
+        def message = shouldFail(UnsupportedOperationException) {
+            closeable.withCloseable {
+                throw new UnsupportedOperationException('not a close ioexception')
+            }
+        }
+        assert closeable.closed
+        assert message == 'not a close ioexception'
+    }
+
+    void testWithCloseableAndException() {
+        def closeable = new DummyCloseable(new IOException('close ioexception'))
+        def result = null
+        def message = shouldFail(IOException) {
+            closeable.withCloseable {
+                result = 123
+            }
+        }
+        assert result == 123
+        assert message == 'close ioexception'
+    }
+
+    // --------------------------------------------------------------------
+    // Helper Classes
+
+    static class DummyAutoCloseable implements AutoCloseable {
+        Exception throwOnClose
+        boolean closed
+        DummyAutoCloseable(Exception throwOnClose=null) {
+            this.throwOnClose = throwOnClose
+        }
+        @Override
+        void close() throws Exception {
+            closed = true
+            if (throwOnClose) {
+                throw throwOnClose
+            }
+        }
+    }
+
+    static class DummyCloseable implements Closeable {
+        Exception throwOnClose
+        boolean closed
+        DummyCloseable(Exception throwOnClose=null) {
+            this.throwOnClose = throwOnClose
+        }
+        @Override
+        void close() throws IOException {
+            closed = true
+            if (throwOnClose) {
+                throw throwOnClose
+            }
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/9d8e4335/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
b/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
index 6ac4837..385bcaa 100644
--- a/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
+++ b/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
@@ -50,7 +50,6 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.logging.Logger;
 import java.util.regex.Pattern;
 
 import groovy.io.FileType;
@@ -60,7 +59,6 @@ import groovy.lang.Closure;
 import groovy.lang.MetaClass;
 import groovy.lang.Writable;
 import groovy.transform.stc.ClosureParams;
-import groovy.transform.stc.FirstParam;
 import groovy.transform.stc.FromString;
 import groovy.transform.stc.PickFirstResolver;
 import groovy.transform.stc.SimpleType;
@@ -113,8 +111,6 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 public class NioGroovyMethods extends DefaultGroovyMethodsSupport {
 
-    private static final Logger LOG = Logger.getLogger(NioGroovyMethods.class.getName());
-
     /**
      * Provide the standard Groovy <code>size()</code> method for <code>Path</code>.
      *
@@ -1949,44 +1945,4 @@ public class NioGroovyMethods extends DefaultGroovyMethodsSupport {
         return IOGroovyMethods.withCloseable(self, action);
     }
 
-    /**
-     * Allows this autocloseable to be used within the closure, ensuring that it
-     * is closed once the closure has been executed and before this method returns.
-     *
-     * @param self the AutoCloseable
-     * @param action the closure taking the AutoCloseable as parameter
-     * @return the value returned by the closure
-     * @throws Exception if an Exception occurs.
-     * @since 2.5.0
-     */
-    public static <T, U extends AutoCloseable> T withAutoCloseable(U self, @ClosureParams(value=FirstParam.class)
Closure<T> action) throws Exception {
-        try {
-            T result = action.call(self);
-
-            AutoCloseable temp = self;
-            self = null;
-            temp.close();
-
-            return result;
-        } finally {
-            closeWithWarning(self);
-        }
-    }
-
-    /**
-     * Close the AutoCloseable. Logging a warning if any problems occur.
-     *
-     * @param c the thing to close
-     */
-    public static void closeWithWarning(AutoCloseable c) {
-        if (c != null) {
-            try {
-                c.close();
-            } catch (Exception e) {
-                LOG.warning("Caught exception during close(): " + e);
-            }
-        }
-    }
-
-
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9d8e4335/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
b/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
index 86bc596..01fc88e 100644
--- a/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
+++ b/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
@@ -543,73 +543,4 @@ class NioGroovyMethodsTest extends Specification {
         assert NioGroovyMethods.getBytes(path) == [-2, -1, 0, 72, 0, 101, 0, 108, 0, 108,
0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33] as byte[]
     }
 
-    def testWithCloseable() {
-        setup:
-        def closeable = new DummyCloseable()
-
-        when:
-        def closeableParam = null
-        def result = closeable.withCloseable {
-            closeableParam = it
-            123
-        }
-
-        then:
-        closeableParam == closeable
-        result == 123
-        closeable.closed
-    }
-
-    def testWithAutoCloseable() {
-        setup:
-            def closeable = new DummyAutoCloseable()
-
-        when:
-            def closeableParam = null
-            def result = closeable.withAutoCloseable {
-                closeableParam = it
-                123
-            }
-
-        then:
-            closeableParam == closeable
-            result == 123
-            closeable.closed
-    }
-
-    def testWithCloseableAndException() {
-        setup:
-        def closeable = new ExceptionDummyCloseable()
-
-        when:
-        closeable.close()
-
-        then:
-        thrown IOException
-    }
-}
-
-class DummyCloseable implements Closeable {
-    boolean closed = false
-
-    @Override
-    void close() throws IOException {
-        closed = true
-    }
-}
-
-class DummyAutoCloseable implements AutoCloseable {
-    boolean closed = false
-
-    @Override
-    void close() throws IOException {
-        closed = true
-    }
-}
-
-class ExceptionDummyCloseable implements Closeable {
-    @Override
-    void close() throws IOException {
-        throw new IOException('boom badaboom')
-    }
 }


Mime
View raw message