Java Custom Exception

This commit is contained in:
gangadkho 2018-06-26 14:48:41 +08:00
parent 3cab703646
commit a0b0e1c705
7 changed files with 120 additions and 7 deletions

View File

@ -198,11 +198,6 @@
<artifactId>mail</artifactId> <artifactId>mail</artifactId>
<version>${javax.mail.version}</version> <version>${javax.mail.version}</version>
</dependency> </dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -250,6 +245,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration> <configuration>
<archive> <archive>
<manifest> <manifest>
@ -288,6 +284,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions> <executions>
<execution> <execution>
<goals> <goals>
@ -309,6 +306,7 @@
<plugin> <plugin>
<groupId>com.jolira</groupId> <groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId> <artifactId>onejar-maven-plugin</artifactId>
<version>${onejar-maven-plugin.version}</version>
<executions> <executions>
<execution> <execution>
<configuration> <configuration>
@ -326,6 +324,7 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions> <executions>
<execution> <execution>
<goals> <goals>
@ -473,6 +472,11 @@
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version> <maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version> <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<javax.mail.version>1.5.0-b01</javax.mail.version> <javax.mail.version>1.5.0-b01</javax.mail.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -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;
}
}

View File

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

View File

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

View File

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

View File

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