From a0b0e1c70532b18663ab717307f2694dbd87c303 Mon Sep 17 00:00:00 2001 From: gangadkho Date: Tue, 26 Jun 2018 14:48:41 +0800 Subject: [PATCH] Java Custom Exception --- core-java/pom.xml | 18 +++++--- .../baeldung/customexception/FileManager.java | 43 +++++++++++++++++++ .../IncorrectFileExtensionException.java | 9 ++++ .../IncorrectFileNameException.java | 9 ++++ ...correctFileExtensionExceptionUnitTest.java | 29 +++++++++++++ .../IncorrectFileNameExceptionUnitTest.java | 19 ++++++++ .../correctFileNameWithoutProperExtension | 0 7 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/customexception/FileManager.java create mode 100644 core-java/src/main/java/com/baeldung/customexception/IncorrectFileExtensionException.java create mode 100644 core-java/src/main/java/com/baeldung/customexception/IncorrectFileNameException.java create mode 100644 core-java/src/test/java/com/baeldung/customexception/IncorrectFileExtensionExceptionUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/customexception/IncorrectFileNameExceptionUnitTest.java create mode 100644 core-java/src/test/resources/correctFileNameWithoutProperExtension diff --git a/core-java/pom.xml b/core-java/pom.xml index a823d836e8..ecbdae52f2 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -10,8 +10,8 @@ com.baeldung parent-java - 0.0.1-SNAPSHOT - ../parent-java + 0.0.1-SNAPSHOT + ../parent-java @@ -198,11 +198,6 @@ mail ${javax.mail.version} - - javax.mail - mail - 1.5.0-b01 - @@ -250,6 +245,7 @@ org.apache.maven.plugins maven-jar-plugin + ${maven-jar-plugin.version} @@ -288,6 +284,7 @@ org.apache.maven.plugins maven-shade-plugin + ${maven-shade-plugin.version} @@ -309,6 +306,7 @@ com.jolira onejar-maven-plugin + ${onejar-maven-plugin.version} @@ -326,6 +324,7 @@ org.springframework.boot spring-boot-maven-plugin + ${spring-boot-maven-plugin.version} @@ -473,6 +472,11 @@ 3.0.0-M1 1.6.0 1.5.0-b01 + 3.0.2 + 1.4.4 + 3.1.1 + 2.0.3.RELEASE + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/customexception/FileManager.java b/core-java/src/main/java/com/baeldung/customexception/FileManager.java new file mode 100644 index 0000000000..b6f4d960aa --- /dev/null +++ b/core-java/src/main/java/com/baeldung/customexception/FileManager.java @@ -0,0 +1,43 @@ +package com.baeldung.customexception; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class FileManager { + + public static String getFirstLine(String fileName) throws IncorrectFileNameException { + try (Scanner file = new Scanner(new File(fileName))) { + if (file.hasNextLine()) { + return file.nextLine(); + } else { + throw new IllegalArgumentException("Non readable file"); + } + } catch (FileNotFoundException err) { + if (!isCorrectFileName(fileName)) { + throw new IncorrectFileNameException("Incorrect filename : " + fileName, err); + } + // Logging etc + } catch (IllegalArgumentException err) { + if (!containsExtension(fileName)) { + throw new IncorrectFileExtensionException("Filename does not contain extension : " + fileName, err); + } + // Other error cases and logging + } + return "Default First Line"; + } + + private static boolean containsExtension(String fileName) { + if (fileName.contains(".txt") || fileName.contains(".doc")) + return true; + return false; + } + + private static boolean isCorrectFileName(String fileName) { + if (fileName.equals("wrongFileName.txt")) + return false; + else + return true; + } + +} diff --git a/core-java/src/main/java/com/baeldung/customexception/IncorrectFileExtensionException.java b/core-java/src/main/java/com/baeldung/customexception/IncorrectFileExtensionException.java new file mode 100644 index 0000000000..c6dc6d6964 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/customexception/IncorrectFileExtensionException.java @@ -0,0 +1,9 @@ +package com.baeldung.customexception; + +public class IncorrectFileExtensionException extends RuntimeException{ + private static final long serialVersionUID = 1L; + + public IncorrectFileExtensionException(String errorMessage, Throwable err) { + super(errorMessage, err); + } +} diff --git a/core-java/src/main/java/com/baeldung/customexception/IncorrectFileNameException.java b/core-java/src/main/java/com/baeldung/customexception/IncorrectFileNameException.java new file mode 100644 index 0000000000..a804cadb84 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/customexception/IncorrectFileNameException.java @@ -0,0 +1,9 @@ +package com.baeldung.customexception; + +public class IncorrectFileNameException extends Exception { + private static final long serialVersionUID = 1L; + + public IncorrectFileNameException(String errorMessage, Throwable err) { + super(errorMessage, err); + } +} diff --git a/core-java/src/test/java/com/baeldung/customexception/IncorrectFileExtensionExceptionUnitTest.java b/core-java/src/test/java/com/baeldung/customexception/IncorrectFileExtensionExceptionUnitTest.java new file mode 100644 index 0000000000..230698f719 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/customexception/IncorrectFileExtensionExceptionUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.customexception; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; + +import org.junit.Test; + +public class IncorrectFileExtensionExceptionUnitTest { + + @Test + public void testWhenCorrectFileExtensionGiven_ReceivesNoException() throws IncorrectFileNameException { + assertThat(FileManager.getFirstLine("correctFileNameWithProperExtension.txt")).isEqualTo("Default First Line"); + } + + @Test(expected = IncorrectFileExtensionException.class) + public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException { + StringBuffer sBuffer = new StringBuffer(); + sBuffer.append("src"); + sBuffer.append(File.separator); + sBuffer.append("test"); + sBuffer.append(File.separator); + sBuffer.append("resources"); + sBuffer.append(File.separator); + sBuffer.append("correctFileNameWithoutProperExtension"); + FileManager.getFirstLine(sBuffer.toString()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/customexception/IncorrectFileNameExceptionUnitTest.java b/core-java/src/test/java/com/baeldung/customexception/IncorrectFileNameExceptionUnitTest.java new file mode 100644 index 0000000000..acb05eb763 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/customexception/IncorrectFileNameExceptionUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.customexception; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class IncorrectFileNameExceptionUnitTest { + + @Test(expected = IncorrectFileNameException.class) + public void testWhenIncorrectFileNameExceptionThrown_ReceivesIncorrectFileNameException() throws IncorrectFileNameException { + FileManager.getFirstLine("wrongFileName.txt"); + } + + @Test + public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException { + assertThat(FileManager.getFirstLine("correctFileName.txt")).isEqualTo("Default First Line"); + } + +} diff --git a/core-java/src/test/resources/correctFileNameWithoutProperExtension b/core-java/src/test/resources/correctFileNameWithoutProperExtension new file mode 100644 index 0000000000..e69de29bb2