Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAP-125 IoStreams#out: add parameter throwError if file exists #294

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import oap.logstream.disk.DiskLoggerBackend;
import oap.logstream.net.client.SocketLoggerBackend;
import oap.logstream.net.server.SocketLoggerServer;
import oap.message.server.MessageHttpHandler;
import oap.message.client.MessageSender;
import oap.message.server.MessageHttpHandler;
import oap.template.BinaryUtils;
import oap.template.Types;
import oap.testng.Fixtures;
Expand Down
13 changes: 13 additions & 0 deletions oap-stdlib-test/src/test/java/oap/io/IoStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static oap.testng.Asserts.assertFile;
import static oap.testng.Asserts.pathOfTestResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class IoStreamsTest extends Fixtures {
private final TestDirectoryFixture testDirectoryFixture;
Expand Down Expand Up @@ -151,4 +152,16 @@ public void compressionLevel() {
System.out.println( encoding + ":\t" + content.length() + " -> " + path.toFile().length() );
}
}

@Test
public void testThrowIfFileExists() throws IOException {
Path testFile = testDirectoryFixture.testPath( "testFile" );
testFile.toFile().createNewFile();

assertThatThrownBy( () -> {
try( var _ = IoStreams.out( testFile, new IoStreams.OutOptions().withThrowIfFileExists( true ) ) ) {
System.out.println( "!" );
}
} ).isInstanceOf( oap.io.FileExistsException.class );
}
}
3 changes: 1 addition & 2 deletions oap-stdlib-test/src/test/java/oap/util/ThrowablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.UncheckedIOException;

public class ThrowablesTest {
@Test( expectedExceptions = UncheckedIOException.class, expectedExceptionsMessageRegExp = "java.io.IOException: test" )
@Test( expectedExceptions = oap.io.IOException.class, expectedExceptionsMessageRegExp = "java.io.IOException: test" )
public void propagateIOException() {
throw Throwables.propagate( new IOException( "test" ) );
}
Expand Down
13 changes: 13 additions & 0 deletions oap-stdlib/src/main/java/oap/io/FileExistsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package oap.io;

import java.io.File;

public class FileExistsException extends IOException {
public FileExistsException( File file ) {
super( "File " + file + " exists" );
}

public FileExistsException( IOException cause ) {
super( cause );
}
}
11 changes: 11 additions & 0 deletions oap-stdlib/src/main/java/oap/io/FileNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oap.io;

public class FileNotFoundException extends IOException {
public FileNotFoundException( String message ) {
super( message );
}

public FileNotFoundException( java.io.FileNotFoundException cause ) {
super( cause );
}
}
23 changes: 11 additions & 12 deletions oap-stdlib/src/main/java/oap/io/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.CopyOption;
Expand Down Expand Up @@ -509,7 +508,7 @@ public static void rename( Path sourcePath, Path destPath ) {
ensureFile( destPath );
java.nio.file.Files.move( sourcePath, destPath, ATOMIC_MOVE, REPLACE_EXISTING );
} catch( IOException e ) {
throw new UncheckedIOException( "cannot rename " + sourcePath + " to " + destPath, e );
throw new oap.io.IOException( "cannot rename " + sourcePath + " to " + destPath, e );
}
}

Expand All @@ -526,7 +525,7 @@ public static Path ensureDirectory( Path path ) {
}
}

public static void move( Path source, Path target, CopyOption... options ) {
public static void move( Path source, Path target, CopyOption... options ) throws oap.io.IOException {
try {
ensureFile( target );
java.nio.file.Files.move( source, target, options );
Expand All @@ -535,39 +534,39 @@ public static void move( Path source, Path target, CopyOption... options ) {
}
}

public static void setPosixPermissions( Path path, Set<PosixFilePermission> permissions ) {
public static void setPosixPermissions( Path path, Set<PosixFilePermission> permissions ) throws oap.io.IOException {
try {
java.nio.file.Files.setPosixFilePermissions( path, permissions );
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static void setPosixPermissions( Path path, PosixFilePermission... permissions ) {
public static void setPosixPermissions( Path path, PosixFilePermission... permissions ) throws oap.io.IOException {
setPosixPermissions( path, Sets.of( permissions ) );
}

public static Set<PosixFilePermission> getPosixPermissions( Path path ) {
public static Set<PosixFilePermission> getPosixPermissions( Path path ) throws oap.io.IOException {
try {
return java.nio.file.Files.getPosixFilePermissions( path, LinkOption.NOFOLLOW_LINKS );
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static boolean isDirectoryEmpty( Path directory ) {
public static boolean isDirectoryEmpty( Path directory ) throws oap.io.IOException {
try( DirectoryStream<Path> dirStream = java.nio.file.Files.newDirectoryStream( directory ) ) {
return !dirStream.iterator().hasNext();
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static void setLastModifiedTime( Path path, DateTime dateTime ) {
public static void setLastModifiedTime( Path path, DateTime dateTime ) throws oap.io.IOException {
setLastModifiedTime( path, dateTime.getMillis() );
}

public static void setLastModifiedTime( Path path, long ms ) {
public static void setLastModifiedTime( Path path, long ms ) throws oap.io.IOException {
try {
java.nio.file.Files.setLastModifiedTime( path, FileTime.fromMillis( ms ) );
} catch( IOException e ) {
Expand Down Expand Up @@ -654,7 +653,7 @@ public static boolean fileNotEmpty( Path path ) {
return isFileNotEmpty( path );
}

public static boolean isFileNotEmpty( final Path path ) {
public static boolean isFileNotEmpty( final Path path ) throws oap.io.IOException {
try( InputStream is = IoStreams.in( path );
InputStreamReader isr = new InputStreamReader( is );
BufferedReader reader = new BufferedReader( isr ) ) {
Expand All @@ -670,15 +669,15 @@ public static boolean exists( Path path ) {
return java.nio.file.Files.exists( path );
}

public static long getLastModifiedTime( Path path ) {
public static long getLastModifiedTime( Path path ) throws oap.io.IOException {
try {
return java.nio.file.Files.getLastModifiedTime( path ).toMillis();
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static boolean createFile( Path file ) {
public static boolean createFile( Path file ) throws oap.io.IOException {
try {
java.nio.file.Files.createFile( file );
return true;
Expand Down
18 changes: 18 additions & 0 deletions oap-stdlib/src/main/java/oap/io/IOException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package oap.io;

public class IOException extends RuntimeException {
public IOException() {
}

public IOException( String message ) {
super( message );
}

public IOException( String message, Throwable cause ) {
super( message, cause );
}

public IOException( Throwable cause ) {
super( cause );
}
}
Loading
Loading