Merge pull request #4568 from gangadkho/master

Java Custom Exception
This commit is contained in:
Loredana Crusoveanu 2018-07-01 18:52:24 +03:00 committed by GitHub
commit 2d6136b475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 120 additions and 8 deletions

View File

@ -10,8 +10,8 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
@ -197,12 +197,7 @@
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
@ -255,6 +250,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
@ -293,6 +289,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions>
<execution>
<goals>
@ -314,6 +311,7 @@
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>${onejar-maven-plugin.version}</version>
<executions>
<execution>
<configuration>
@ -331,6 +329,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions>
<execution>
<goals>
@ -478,6 +477,10 @@
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.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>
<icu4j.version>61.1</icu4j.version>
</properties>

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