From d77187d33d79250ea3e7984a43aaa1abc351918c Mon Sep 17 00:00:00 2001 From: Sarath Date: Thu, 8 Jul 2021 17:31:45 +0530 Subject: [PATCH] [BAEL-4990] Throwing exceptions in constructor --- .../constructors/exception/Animal.java | 32 +++++++++++++ .../baeldung/constructors/exception/Bird.java | 13 +++++ .../exception/AnimalUnitTest.java | 47 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Animal.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Bird.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/exception/AnimalUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Animal.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Animal.java new file mode 100644 index 0000000000..1927bc4455 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Animal.java @@ -0,0 +1,32 @@ +package com.baeldung.constructors.exception; + +import java.io.File; +import java.io.IOException; + +public class Animal { + + public Animal() throws InstantiationException { + throw new InstantiationException("Cannot be instantiated"); + } + + public Animal(String id, int age) { + if (id == null) + throw new NullPointerException("Id cannot be null"); + if (age < 0) + throw new IllegalArgumentException("Age cannot be negative"); + } + + public Animal(File file) throws SecurityException, IOException { + // Avoiding Path traversal attacks + if (file.isAbsolute()) { + throw new SecurityException("Traversal attack - absolute path not allowed"); + } + // Avoiding directory traversal + // a/../..b/ + if (!file.getCanonicalPath() + .equals(file.getAbsolutePath())) { + throw new SecurityException("Directory traversal attempt?"); + } + } + +} diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Bird.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Bird.java new file mode 100644 index 0000000000..6e6e6558ba --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/exception/Bird.java @@ -0,0 +1,13 @@ +package com.baeldung.constructors.exception; + +public class Bird extends Animal { + + // Note that we are throwing parent exception + public Bird() throws ReflectiveOperationException { + super(); + } + + public Bird(String id, int age) { + super(id, age); + } +} diff --git a/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/exception/AnimalUnitTest.java b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/exception/AnimalUnitTest.java new file mode 100644 index 0000000000..8f7ed440f6 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/exception/AnimalUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.constructors.exception; + +import java.io.File; + +import org.assertj.core.api.Assertions; +import org.junit.Test; + +public class AnimalUnitTest { + + @Test + public void givenNoArgument_thenFails() { + Assertions.assertThatThrownBy(() -> { + new Animal(); + }) + .isInstanceOf(Exception.class); + } + + @Test + public void givenInvalidArg_thenFails() { + Assertions.assertThatThrownBy(() -> { + new Animal(null, 30); + }) + .isInstanceOf(NullPointerException.class); + } + + @Test(expected = Test.None.class) + public void givenValidArg_thenSuccess() { + new Animal("1234", 30); + } + + @Test + public void givenAbsolutePath_thenFails() { + Assertions.assertThatThrownBy(() -> { + new Animal(new File("temp.txt").getAbsoluteFile()); + }) + .isInstanceOf(SecurityException.class); + } + + @Test + public void givenDirectoryTraversalPath_thenFails() { + Assertions.assertThatThrownBy(() -> { + new Animal(new File(File.separator + ".." + File.separator + "temp.txt")); + }) + .isInstanceOf(SecurityException.class); + } + +}