Skip to content

Commit

Permalink
✔️ SONAR - Use dedicated Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed May 12, 2016
1 parent f71888b commit a7e915b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/fim/Fim.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/fim/command/InitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/fim/command/exception/RepositoryException.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package org.fim.command.exception;

public class RepositoryException extends RuntimeException {
public RepositoryException() {
super();
}

public RepositoryException(String message, Throwable cause) {
super(message, cause);
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/fim/internal/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fim/internal/hash/FileHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/fim/internal/hash/FrontHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/fim/model/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.fim.model;

import com.google.common.base.MoreObjects;
import org.fim.command.exception.FimInternalError;

import java.util.Objects;

Expand All @@ -35,7 +36,7 @@ public class Range implements Comparable<Range> {
*/
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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/fim/FimTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/fim/model/RangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a7e915b

Please sign in to comment.