You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2017/05/15 22:32:52 UTC

svn commit: r1795249 - in /directory/shared/branches/shared-value/util/src: main/java/org/apache/directory/api/util/FileUtils.java test/java/org/apache/directory/api/util/FileUtilsTest.java

Author: elecharny
Date: Mon May 15 22:32:52 2017
New Revision: 1795249

URL: http://svn.apache.org/viewvc?rev=1795249&view=rev
Log:
Fixed a READ access

Added:
    directory/shared/branches/shared-value/util/src/test/java/org/apache/directory/api/util/FileUtilsTest.java
Modified:
    directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/FileUtils.java

Modified: directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/FileUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/FileUtils.java?rev=1795249&r1=1795248&r2=1795249&view=diff
==============================================================================
--- directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/FileUtils.java (original)
+++ directory/shared/branches/shared-value/util/src/main/java/org/apache/directory/api/util/FileUtils.java Mon May 15 22:32:52 2017
@@ -31,7 +31,6 @@ import java.io.OutputStream;
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
-import java.nio.file.OpenOption;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.util.List;
@@ -471,14 +470,14 @@ public final class FileUtils
             }
         }
 
-        OpenOption option = StandardOpenOption.READ;
-        
         if ( append )
         {
-            option = StandardOpenOption.APPEND;
+            return Files.newOutputStream( Paths.get( file.getPath() ), StandardOpenOption.CREATE, StandardOpenOption.APPEND );
+        }
+        else
+        {
+            return Files.newOutputStream( Paths.get( file.getPath() ) );
         }
-        
-        return Files.newOutputStream( Paths.get( file.getPath() ), option );
     }
 
 

Added: directory/shared/branches/shared-value/util/src/test/java/org/apache/directory/api/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/util/src/test/java/org/apache/directory/api/util/FileUtilsTest.java?rev=1795249&view=auto
==============================================================================
--- directory/shared/branches/shared-value/util/src/test/java/org/apache/directory/api/util/FileUtilsTest.java (added)
+++ directory/shared/branches/shared-value/util/src/test/java/org/apache/directory/api/util/FileUtilsTest.java Mon May 15 22:32:52 2017
@@ -0,0 +1,129 @@
+/*
+ *   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.directory.api.util;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+
+/**
+ * Tests for FileUtils.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class FileUtilsTest
+{
+
+    @Rule
+    public TemporaryFolder tmpFolder = new TemporaryFolder();
+
+
+    @Test
+    public void testOpenOutputStreamAppendToNonExistingFileInNonExistingFolder() throws Exception
+    {
+        File nonExistingFile = new File( tmpFolder.getRoot(),
+            "testOpenOutputStreamAppendToNonExistingFileInNonExistingFolder/testOpenOutputStreamAppendToNonExistingFileInNonExistingFolder" );
+
+        OutputStream os1 = FileUtils.openOutputStream( nonExistingFile, true );
+        os1.write( 'a' );
+        os1.write( 'b' );
+        os1.write( 'c' );
+        os1.close();
+
+        OutputStream os2 = FileUtils.openOutputStream( nonExistingFile, true );
+        os2.write( 'x' );
+        os2.write( 'y' );
+        os2.write( 'z' );
+        os2.close();
+
+        String content = FileUtils.readFileToString( nonExistingFile, StandardCharsets.UTF_8 );
+
+        assertEquals( "abcxyz", content );
+    }
+
+
+    @Test
+    public void testOpenOutputStreamAppendToExistingFile() throws Exception
+    {
+        File existingFile = tmpFolder.newFile( "testOpenOutputStreamAppendToExistingFile" );
+        FileUtils.writeStringToFile( existingFile, "abc", StandardCharsets.UTF_8, false );
+
+        OutputStream os = FileUtils.openOutputStream( existingFile, true );
+        os.write( 'x' );
+        os.write( 'y' );
+        os.write( 'z' );
+        os.close();
+
+        String content = FileUtils.readFileToString( existingFile, StandardCharsets.UTF_8 );
+
+        assertEquals( "abcxyz", content );
+    }
+
+
+    @Test
+    public void testOpenOutputStreamNotAppendToNonExistingFile() throws Exception
+    {
+        File nonExistingFile = new File( tmpFolder.getRoot(),
+            "testOpenOutputStreamNotAppendToNonExistingFile/testOpenOutputStreamNotAppendToNonExistingFile" );
+
+        OutputStream os1 = FileUtils.openOutputStream( nonExistingFile, false );
+        os1.write( 'a' );
+        os1.write( 'b' );
+        os1.write( 'c' );
+        os1.close();
+
+        OutputStream os2 = FileUtils.openOutputStream( nonExistingFile, false );
+        os2.write( 'x' );
+        os2.write( 'y' );
+        os2.write( 'z' );
+        os2.close();
+
+        String content = FileUtils.readFileToString( nonExistingFile, StandardCharsets.UTF_8 );
+
+        assertEquals( "xyz", content );
+    }
+
+
+    @Test
+    public void testOpenOutputStreamNotAppendToExistingFile() throws Exception
+    {
+        File existingFile = tmpFolder.newFile( "testOpenOutputStreamNotAppendToExistingFile" );
+        FileUtils.writeStringToFile( existingFile, "abc", StandardCharsets.UTF_8, false );
+
+        OutputStream os = FileUtils.openOutputStream( existingFile, false );
+        os.write( 'x' );
+        os.write( 'y' );
+        os.write( 'z' );
+        os.close();
+
+        String content = FileUtils.readFileToString( existingFile, StandardCharsets.UTF_8 );
+
+        assertEquals( "xyz", content );
+    }
+}