diff --git a/src/main/java/org/fim/Fim.java b/src/main/java/org/fim/Fim.java index ea684dc0..aa015c1d 100644 --- a/src/main/java/org/fim/Fim.java +++ b/src/main/java/org/fim/Fim.java @@ -40,7 +40,7 @@ import org.fim.command.VersionCommand; import org.fim.command.exception.BadFimUsageException; import org.fim.command.exception.DontWantToContinueException; -import org.fim.command.exception.RepositoryCreationException; +import org.fim.command.exception.RepositoryException; import org.fim.internal.SettingsManager; import org.fim.model.Command; import org.fim.model.Command.FimReposConstraint; @@ -74,7 +74,7 @@ public static void main(String[] args) throws Exception { System.exit(0); } catch (BadFimUsageException ex) { System.exit(-1); - } catch (RepositoryCreationException ex) { + } catch (RepositoryException ex) { System.exit(-2); } @@ -198,7 +198,7 @@ protected void run(String[] args, Context context) throws Exception { if (!Files.isWritable(context.getRepositoryRootDir())) { Logger.error(String.format("Not allowed to create the '%s' directory that holds the Fim repository", context.getRepositoryDotFimDir())); - throw new RepositoryCreationException(); + throw new RepositoryException(); } } else if (constraint == FimReposConstraint.MUST_EXIST) { findRepositoryRootDir(context); @@ -210,12 +210,12 @@ protected void run(String[] args, Context context) throws Exception { if (!Files.isWritable(context.getRepositoryStatesDir())) { Logger.error(String.format("Not allowed to modify States into the '%s' directory", context.getRepositoryStatesDir())); - throw new RepositoryCreationException(); + throw new RepositoryException(); } SettingsManager settingsManager = new SettingsManager(context); if (!Files.isWritable(settingsManager.getSettingsFile())) { Logger.error(String.format("Not allowed to save settings into the '%s' directory", context.getRepositoryDotFimDir())); - throw new RepositoryCreationException(); + throw new RepositoryException(); } } diff --git a/src/main/java/org/fim/command/InitCommand.java b/src/main/java/org/fim/command/InitCommand.java index dad3564b..19d342ba 100644 --- a/src/main/java/org/fim/command/InitCommand.java +++ b/src/main/java/org/fim/command/InitCommand.java @@ -19,7 +19,7 @@ package org.fim.command; import org.fim.command.exception.DontWantToContinueException; -import org.fim.command.exception.RepositoryCreationException; +import org.fim.command.exception.RepositoryException; import org.fim.internal.SettingsManager; import org.fim.internal.StateComparator; import org.fim.internal.StateGenerator; @@ -86,7 +86,7 @@ private void createRepository(Context context) { Files.createDirectories(context.getRepositoryStatesDir()); } catch (IOException ex) { Logger.error(String.format("Not able to create the '%s' directory that holds the Fim repository", context.getRepositoryDotFimDir()), ex, context.isDisplayStackTrace()); - throw new RepositoryCreationException(); + throw new RepositoryException(); } if (context.getHashMode() != hashAll) { diff --git a/src/main/java/org/fim/command/exception/RepositoryCreationException.java b/src/main/java/org/fim/command/exception/FimInternalError.java similarity index 86% rename from src/main/java/org/fim/command/exception/RepositoryCreationException.java rename to src/main/java/org/fim/command/exception/FimInternalError.java index 1a52809b..b4b43a4b 100644 --- a/src/main/java/org/fim/command/exception/RepositoryCreationException.java +++ b/src/main/java/org/fim/command/exception/FimInternalError.java @@ -18,5 +18,8 @@ */ package org.fim.command.exception; -public class RepositoryCreationException extends RuntimeException { +public class FimInternalError extends Error { + public FimInternalError(String message) { + super(message); + } } diff --git a/src/main/java/org/fim/command/exception/RepositoryException.java b/src/main/java/org/fim/command/exception/RepositoryException.java new file mode 100644 index 00000000..fa42ed26 --- /dev/null +++ b/src/main/java/org/fim/command/exception/RepositoryException.java @@ -0,0 +1,29 @@ +/* + * This file is part of Fim - File Integrity Manager + * + * Copyright (C) 2016 Etienne Vrignaud + * + * Fim is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Fim is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Fim. If not, see . + */ +package org.fim.command.exception; + +public class RepositoryException extends RuntimeException { + public RepositoryException() { + super(); + } + + public RepositoryException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/fim/internal/SettingsManager.java b/src/main/java/org/fim/internal/SettingsManager.java index 2fd7a8ac..a544b26c 100644 --- a/src/main/java/org/fim/internal/SettingsManager.java +++ b/src/main/java/org/fim/internal/SettingsManager.java @@ -20,6 +20,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import org.fim.command.exception.RepositoryException; import org.fim.model.Context; import org.fim.model.HashMode; import org.fim.model.Settings; @@ -62,7 +63,7 @@ private void load() { Gson gson = new Gson(); settings = gson.fromJson(reader, Settings.class); } catch (IOException ex) { - throw new RuntimeException("Error reading settings", ex); + throw new RepositoryException("Error reading settings", ex); } } @@ -71,7 +72,7 @@ public void save() { Gson gson = new GsonBuilder().setPrettyPrinting().create(); gson.toJson(settings, writer); } catch (IOException ex) { - throw new RuntimeException("Error saving settings", ex); + throw new RepositoryException("Error saving settings", ex); } } diff --git a/src/main/java/org/fim/internal/hash/FileHasher.java b/src/main/java/org/fim/internal/hash/FileHasher.java index fdd526f9..58c4170e 100644 --- a/src/main/java/org/fim/internal/hash/FileHasher.java +++ b/src/main/java/org/fim/internal/hash/FileHasher.java @@ -19,6 +19,7 @@ package org.fim.internal.hash; import org.apache.commons.lang3.SystemUtils; +import org.fim.command.exception.FimInternalError; import org.fim.model.Attribute; import org.fim.model.Context; import org.fim.model.FileAttribute; @@ -183,7 +184,7 @@ protected FileHash hashFile(Path file, long fileSize) throws IOException { } if (false == frontHasher.hashComplete()) { - throw new RuntimeException(String.format("Fim is not working correctly for file '%s' (size=%d). Some Hasher have not completed: small=%s, medium=%s, full=%s", + throw new FimInternalError(String.format("Fim is not working correctly for file '%s' (size=%d). Some Hasher have not completed: small=%s, medium=%s, full=%s", file, fileSize, frontHasher.getSmallBlockHasher().hashComplete(), frontHasher.getMediumBlockHasher().hashComplete(), frontHasher.getFullHasher().hashComplete())); } diff --git a/src/main/java/org/fim/internal/hash/FrontHasher.java b/src/main/java/org/fim/internal/hash/FrontHasher.java index 30204396..529466fc 100644 --- a/src/main/java/org/fim/internal/hash/FrontHasher.java +++ b/src/main/java/org/fim/internal/hash/FrontHasher.java @@ -18,6 +18,7 @@ */ package org.fim.internal.hash; +import org.fim.command.exception.FimInternalError; import org.fim.model.FileHash; import org.fim.model.HashMode; import org.fim.model.Range; @@ -96,7 +97,7 @@ public Range getNextRange(long filePosition) { nextSmallRange = smallBlockHasher.getNextRange(filePosition); return nextSmallRange; } - throw new RuntimeException(String.format("Fim is not working correctly. Unable to getNextRange for filePosition %d", filePosition)); + throw new FimInternalError(String.format("Fim is not working correctly. Unable to getNextRange for filePosition %d", filePosition)); } @Override @@ -121,7 +122,7 @@ private void update(Hasher hasher, long filePosition, ByteBuffer buffer) { @Override public String getHash() { - throw new RuntimeException("Not implemented"); + throw new FimInternalError("Not implemented"); } @Override diff --git a/src/main/java/org/fim/model/Range.java b/src/main/java/org/fim/model/Range.java index 8dca0d38..4f0594b2 100644 --- a/src/main/java/org/fim/model/Range.java +++ b/src/main/java/org/fim/model/Range.java @@ -19,6 +19,7 @@ package org.fim.model; import com.google.common.base.MoreObjects; +import org.fim.command.exception.FimInternalError; import java.util.Objects; @@ -35,7 +36,7 @@ public class Range implements Comparable { */ public Range(long from, long to) { if (from > to) { - throw new RuntimeException("'to' must be greater than 'from'"); + throw new FimInternalError("'to' must be greater than 'from'"); } this.from = from; @@ -76,7 +77,7 @@ public Range union(Range range) { public Range adjustToRange(Range range) { if (range != null) { if (range.getFrom() < from) { - throw new RuntimeException("The Range must begin inside the current Range"); + throw new FimInternalError("The Range must begin inside the current Range"); } if (range.getFrom() < to && range.getTo() > to) { diff --git a/src/test/java/org/fim/FimTest.java b/src/test/java/org/fim/FimTest.java index 53b6bf48..96b9e17e 100644 --- a/src/test/java/org/fim/FimTest.java +++ b/src/test/java/org/fim/FimTest.java @@ -20,7 +20,7 @@ import org.apache.commons.io.FileUtils; import org.fim.command.exception.BadFimUsageException; -import org.fim.command.exception.RepositoryCreationException; +import org.fim.command.exception.RepositoryException; import org.fim.model.Context; import org.fim.model.HashMode; import org.fim.tooling.RepositoryTool; @@ -64,11 +64,11 @@ public void fimRepositoryAlreadyExist() throws Exception { cut.run(new String[]{"init", "-y"}, context); } - @Test(expected = RepositoryCreationException.class) + @Test(expected = RepositoryException.class) public void fimRepositoryNotWritable() throws Exception { if (IS_OS_WINDOWS) { // Ignore this test for Windows - throw new RepositoryCreationException(); + throw new RepositoryException(); } tool.setReadOnly(rootDir); diff --git a/src/test/java/org/fim/model/RangeTest.java b/src/test/java/org/fim/model/RangeTest.java index bcf441cb..499837a0 100644 --- a/src/test/java/org/fim/model/RangeTest.java +++ b/src/test/java/org/fim/model/RangeTest.java @@ -18,6 +18,7 @@ */ package org.fim.model; +import org.fim.command.exception.FimInternalError; import org.fim.tooling.ObjectAssert; import org.junit.Before; import org.junit.Test; @@ -40,7 +41,7 @@ public void setup() { c = new Range(4, 5); } - @Test(expected = RuntimeException.class) + @Test(expected = FimInternalError.class) public void toMustBeGreaterThanFrom() { new Range(4, 3); } @@ -77,7 +78,7 @@ public void adjustingRanges() { assertThat(range1.adjustToRange(range2)).isEqualTo(new Range(4, 9)); } - @Test(expected = RuntimeException.class) + @Test(expected = FimInternalError.class) public void otherRangeCannotStartBefore() { Range range1 = new Range(4, 10); Range range2 = new Range(3, 15);