From 9f5ebe9b8247375b81fe3d61e8a8455299c809c3 Mon Sep 17 00:00:00 2001 From: Mohamed Kiswani Date: Tue, 22 Nov 2016 17:23:15 +0100 Subject: [PATCH 1/5] release 0.0.17 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b100f3..4b77c55 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Performance Deadlocks - [(Oracle JDK) deadlock in SSLSocketImpl between between write and close](http://bugs.java.com/view_bug.do?bug_id=8013809) -So, we should always protect our project and noy use a framwork or API directly and this is the main idea here +So, we should always protect our project and noy use a framework or API directly and this is the main idea here Main Features -------------- From 98550b7e72f560ff14ef63e208405d8d8b3ef510 Mon Sep 17 00:00:00 2001 From: Mohamed Kiswani Date: Tue, 22 Nov 2016 20:14:21 +0100 Subject: [PATCH 2/5] Documentation updated, fixes --- README.md | 156 +++++++++++++++++- pom.xml | 9 +- .../javaff/factory/AbstractFactory.java | 6 +- .../rhkiswani/javaff/format/FormatUtil.java | 2 +- .../javaff/locale/LocaleWorkersFactory.java | 2 +- .../rhkiswani/javaff/log/LogFactory.java | 2 +- .../rhkiswani/javaff/log/LogWrapper.java | 58 +++++++ src/main/resources/app/messages_en.properties | 1 + .../io/github/rhkiswani/javaff/TestMain.java | 11 ++ .../beans/withEqualsAnnotation/Employee.java | 9 + .../ExceptionHandlersFactoryTest.java | 24 +-- .../javaff/exceptions/ExceptionsTest.java | 62 ++++++- .../rhkiswani/javaff/logging/LoggingTest.java | 16 ++ 13 files changed, 328 insertions(+), 30 deletions(-) create mode 100644 src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java create mode 100644 src/test/java/io/github/rhkiswani/javaff/TestMain.java create mode 100644 src/test/java/io/github/rhkiswani/javaff/logging/LoggingTest.java diff --git a/README.md b/README.md index 4b77c55..3837adb 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,11 @@ JavaFF: Java Facade/Factories We all know the Golden Object Oriented rule **Don't talk to strangers** -We all know that you will never find the API/frameworks defects or magic or limitations the begging of the development. +We all know that you will never find the API/frameworks defects or magic or limitations the at begging of the development. -So imagine you are using an API in all your projects and after spending months or years that API you got a production issue because of it even if it so famous and mature, -changing the that API would be soo hard and costy !!! +Imagine yourself using API in all your projects for years. Everybody else is using it as well, it is famous and mature. Suddenly you have a huge production issue. Changing that API is too hard and costly at this stage! + +What now? Examples for famous bugs in very famous frameworks --------------------------------------------------- @@ -29,12 +30,149 @@ So, we should always protect our project and noy use a framework or API directly Main Features -------------- -- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers ...etc -- **You can control the implementation's through the class path without changing line of code** -- Smart Exception handling mechanism -- Default Implementations -- Many Utilises - +- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers, etc. + +- **You can control the implementations through the class path without changing line of code** +The below example shows how the implementation will be changed without changing the code: +Now I have the below dependencies in my pom.xml +```xml + + org.slf4j + slf4j-api + ${slf4.version} + provided + + + org.slf4j + slf4j-jdk14 + ${slf4.version} + provided + +``` +So when I call +```javascript + LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani"); + output: + Nov 22, 2016 6:54:07 PM io.github.rhkiswani.javaff.log.Slf4jLog info + INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani +``` + +When I remove the dependencies from the pom.xml and run the same code I will get + +```javascript + LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani"); + output: + Nov 22, 2016 7:00:15 PM io.github.rhkiswani.javaff.log.DefaultLog info + INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani + ``` + +- Transparent localization for logs, strings, exceptions + +- Centralized and Configurable Exception Handling by the class type below a full example +```java + + package io.github.rhkiswani.javaff; + + import io.github.rhkiswani.javaff.exceptions.ExceptionHandler; + import io.github.rhkiswani.javaff.exceptions.ExceptionHandlersFactory; + import io.github.rhkiswani.javaff.exceptions.ExceptionUtil; + + public class TestMain { + + public static void main(String[] args) { + //Any class is instance of ConsoleException will be handled here + ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + System.out.println("ConsoleException handler"); + } + }); + + //Any class is instance of MailException will be handled here + ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + System.out.println("MailException handler"); + } + }); + + + ExceptionUtil.handle(new ConsoleException()); + ExceptionUtil.handle(new SubConsoleException()); + ExceptionUtil.handle(new MailException()); + ExceptionUtil.handle(new SubMailException()); + + //Null pointer is not related to the perilous class's it will be handled by default handler for Throwable.class + //which is painting the stack trace by default + ExceptionUtil.handle(new NullPointerException()); + + //We decided to override the default implantation for Throwable.class + ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + System.out.println("Overridden handler"); + } + }); + + ExceptionUtil.handle(new NullPointerException()); + } + + private static class ConsoleException extends RuntimeException{ + + } + + private static class SubConsoleException extends ConsoleException{ + + } + + private static class MailException extends RuntimeException{ + + } + + private static class SubMailException extends MailException { + + } + + } + +``` + +- Logging with localization out of the box + +```java + LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani"); + output : INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani +``` + +```java + LogFactory.getLogger(LogFactory.class).info("normal msg num {0} date {1}", Integer.MAX_VALUE, new Date()); + INFO: normal msg num 2,147,483,647 date 11/22/16 6:06 PM +``` + +- Many Utilities, below is just examples + - Formatter's + ```java + System.out.println(FormatUtil.format("Mr {0} {1}", "Mohamed", "Kiswani")); + System.out.println(FormatUtil.format(new Date())); + System.out.println(FormatUtil.format(new Date(), "yyyy-MM-dd")); + System.out.println(FormatUtil.format(Integer.MAX_VALUE)); + + Mr Mohamed Kiswani + 11/22/16 6:16 PM + 2016-11-22 + 2,147,483,647 + ``` + + - Json Handlers + ```java + System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).toJson(new Employee(1000))); + output : {"id":0,"name":null,"empId":1000} + ``` + ```java + System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).fromJson("{\"id\":100,\"name\":null,\"empId\":1000}", Employee.class)); + output: Employee[id=100] + ``` + Prerequisites ------------- Requires JDK 1.7 or higher. diff --git a/pom.xml b/pom.xml index 0270e5c..ec97c7b 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,14 @@ org.slf4j slf4j-api - 1.7.21 + ${slf4.version} + provided + + + + org.slf4j + slf4j-jdk14 + ${slf4.version} provided diff --git a/src/main/java/io/github/rhkiswani/javaff/factory/AbstractFactory.java b/src/main/java/io/github/rhkiswani/javaff/factory/AbstractFactory.java index f40615c..938947f 100644 --- a/src/main/java/io/github/rhkiswani/javaff/factory/AbstractFactory.java +++ b/src/main/java/io/github/rhkiswani/javaff/factory/AbstractFactory.java @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; /** * @author Mohamed Kiswani @@ -57,7 +58,10 @@ protected IMP_TYPE create(Class targetClass){ if (userDefaultImpl != null){ return userDefaultImpl; } - for (Class aClass : map.keySet()) { + Set classSet = map.keySet(); + Class[] keys = classSet.toArray(new Class[classSet.size()]); + for (int i = keys.length - 1 ; i >=0 ; i--){ + Class aClass = keys[i]; if (aClass.isAssignableFrom(targetClass) || targetClass.isAnnotationPresent(aClass)){ return map.get(aClass); } diff --git a/src/main/java/io/github/rhkiswani/javaff/format/FormatUtil.java b/src/main/java/io/github/rhkiswani/javaff/format/FormatUtil.java index 1fd507e..3e6f4aa 100644 --- a/src/main/java/io/github/rhkiswani/javaff/format/FormatUtil.java +++ b/src/main/java/io/github/rhkiswani/javaff/format/FormatUtil.java @@ -34,6 +34,6 @@ public static T format(Object obj, Object... params){ if (obj == null){ return null; } - return (T) FormatFactory.getFormatter(obj.getClass()).format(String.valueOf(obj), params); + return (T) FormatFactory.getFormatter(obj.getClass()).format(obj, params); } } diff --git a/src/main/java/io/github/rhkiswani/javaff/locale/LocaleWorkersFactory.java b/src/main/java/io/github/rhkiswani/javaff/locale/LocaleWorkersFactory.java index dd189e8..6dba14c 100644 --- a/src/main/java/io/github/rhkiswani/javaff/locale/LocaleWorkersFactory.java +++ b/src/main/java/io/github/rhkiswani/javaff/locale/LocaleWorkersFactory.java @@ -27,8 +27,8 @@ public class LocaleWorkersFactory extends AbstractFactory{ private static LocaleWorkersFactory instance = new LocaleWorkersFactory(); private LocaleWorkersFactory(){ - add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build()); add(Object.class, new LocaleWorkerBuilder().path("app/").build()); + add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build()); } public static LocaleWorkersFactory instance(){ diff --git a/src/main/java/io/github/rhkiswani/javaff/log/LogFactory.java b/src/main/java/io/github/rhkiswani/javaff/log/LogFactory.java index 45b1016..dde635e 100644 --- a/src/main/java/io/github/rhkiswani/javaff/log/LogFactory.java +++ b/src/main/java/io/github/rhkiswani/javaff/log/LogFactory.java @@ -42,6 +42,6 @@ protected Log getDefault(Class targetClazz) { } public static Log getLogger(Class aClass) { - return instance.create(aClass); + return new LogWrapper(instance.create(aClass)); } } diff --git a/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java b/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java new file mode 100644 index 0000000..0d16921 --- /dev/null +++ b/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java @@ -0,0 +1,58 @@ +/* + * Copyright 2016 Mohamed Kiswani. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.rhkiswani.javaff.log; + +import io.github.rhkiswani.javaff.locale.LocaleUtil; + +/** + * @author Mohamed Kiswani + * @since 0.0.17 + * + */ +public class LogWrapper implements Log{ + + private final Log log; + + public LogWrapper(Log log) { + this.log = log; + } + + @Override + public void debug(String message, Object... params) { + log.debug(LocaleUtil.getString(message, params)); + } + + @Override + public void info(String message, Object... params) { + log.info(LocaleUtil.getString(message, params)); + } + + @Override + public void warn(String message, Object... params) { + log.warn(LocaleUtil.getString(message, params)); + } + + @Override + public void error(String message, Object... params) { + log.error(LocaleUtil.getString(message, params)); + } + + @Override + public void error(String message, Exception e, Object... params) { + log.error(LocaleUtil.getString(message, params), e); + } +} diff --git a/src/main/resources/app/messages_en.properties b/src/main/resources/app/messages_en.properties index e69de29..7b770df 100644 --- a/src/main/resources/app/messages_en.properties +++ b/src/main/resources/app/messages_en.properties @@ -0,0 +1 @@ +LOCALIZED_MSG=this is localized msg from messages_en.properties thanks for Mr {0} \ No newline at end of file diff --git a/src/test/java/io/github/rhkiswani/javaff/TestMain.java b/src/test/java/io/github/rhkiswani/javaff/TestMain.java new file mode 100644 index 0000000..bd17218 --- /dev/null +++ b/src/test/java/io/github/rhkiswani/javaff/TestMain.java @@ -0,0 +1,11 @@ +package io.github.rhkiswani.javaff; + +public class TestMain { + + public static void main(String[] args) { + + } + + + +} diff --git a/src/test/java/io/github/rhkiswani/javaff/beans/withEqualsAnnotation/Employee.java b/src/test/java/io/github/rhkiswani/javaff/beans/withEqualsAnnotation/Employee.java index ceca100..6e5230d 100644 --- a/src/test/java/io/github/rhkiswani/javaff/beans/withEqualsAnnotation/Employee.java +++ b/src/test/java/io/github/rhkiswani/javaff/beans/withEqualsAnnotation/Employee.java @@ -4,6 +4,15 @@ public class Employee extends Person{ private int empId; + public Employee() { + + } + + public Employee(int empId) { + super(); + this.empId = empId; + } + public int getEmpId() { return empId; } diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java index 088970f..a438f28 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java @@ -1,5 +1,6 @@ package io.github.rhkiswani.javaff.exceptions; +import io.github.rhkiswani.javaff.beans.withOutAnnotation.withEqualsAnnotation.EmployeeX; import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException; import org.junit.Before; import org.junit.Test; @@ -47,18 +48,17 @@ public void handle(Throwable t) { @Test public void testFactory() throws Exception { - //FIXME it's failing on travis but not local -// assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(DefaultExceptionHandler.class); -// ExceptionHandlersFactory.instance().overrideImp(Throwable.class, exceptionHandler); -// assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(TestExceptionHandler.class); -// assertThat(ExceptionHandlersFactory.getExceptionHandler(SQLException.class).getClass()).isEqualTo(TestExceptionHandler.class); -// assertThat(ExceptionHandlersFactory.getExceptionHandler(EmployeeX.class).getClass()).isEqualTo(DefaultExceptionHandler.class); -// -// try { -// ExceptionHandlersFactory.getExceptionHandler(null); -// }catch (Throwable t) { -// assertThat(t).isInstanceOf(IllegalParamException.class).hasMessage("Target Class cant be null"); -// } + assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(DefaultExceptionHandler.class); + ExceptionHandlersFactory.instance().overrideImp(Throwable.class, exceptionHandler); + assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(TestExceptionHandler.class); + assertThat(ExceptionHandlersFactory.getExceptionHandler(SQLException.class).getClass()).isEqualTo(TestExceptionHandler.class); + assertThat(ExceptionHandlersFactory.getExceptionHandler(EmployeeX.class).getClass()).isEqualTo(DefaultExceptionHandler.class); + + try { + ExceptionHandlersFactory.getExceptionHandler(null); + }catch (Throwable t) { + assertThat(t).isInstanceOf(IllegalParamException.class).hasMessage("Target Class cant be null"); + } } private class TestExceptionHandler implements ExceptionHandler { diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java index c6427d0..072dd5e 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java @@ -60,16 +60,70 @@ public void testHandle() throws Exception { assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(IllegalParamException.class); assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("Exception cant be null"); } - ExceptionHandlersFactory.instance().setDefault(new ExceptionHandler() { @Override public void handle(Throwable t) { - exceptionMsg = "Kiswani"; + exceptionMsg = "userDefaultHandler"; + } + }); + ExceptionUtil.handle(new NullPointerException()); + assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); + + //Any class is instance of ConsoleException will be handled here + ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "ConsoleException handler"; + } + }); + + //Any class is instance of MailException will be handled here + ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "MailException handler"; + } + }); + + + ExceptionUtil.handle(new ConsoleException()); + assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); + ExceptionHandlersFactory.instance().setDefault(null); + ExceptionUtil.handle(new ConsoleException()); + assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); + ExceptionUtil.handle(new SubConsoleException()); + assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); + ExceptionUtil.handle(new MailException()); + assertThat(exceptionMsg).isEqualTo("MailException handler"); + ExceptionUtil.handle(new SubMailException()); + assertThat(exceptionMsg).isEqualTo("MailException handler"); + + //We decided to override the default implantation for Throwable.class + ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "Overridden handler"; } }); + ExceptionUtil.handle(new NullPointerException()); - assertThat(exceptionMsg).isEqualTo("Kiswani"); - exceptionMsg = "dummyMsg"; + assertThat(exceptionMsg).isEqualTo("Overridden handler"); + + } + + private static class ConsoleException extends RuntimeException{ + } + private static class SubConsoleException extends ConsoleException{ + + } + + private static class MailException extends RuntimeException{ + + } + + private static class SubMailException extends MailException { + + } } diff --git a/src/test/java/io/github/rhkiswani/javaff/logging/LoggingTest.java b/src/test/java/io/github/rhkiswani/javaff/logging/LoggingTest.java new file mode 100644 index 0000000..b81894a --- /dev/null +++ b/src/test/java/io/github/rhkiswani/javaff/logging/LoggingTest.java @@ -0,0 +1,16 @@ +package io.github.rhkiswani.javaff.logging; + +import org.junit.Test; + +public class LoggingTest { + + @Test + public void testStringUtils() throws Exception { + +// assertThat().isEqualTo(null); +// assertThat(StringUtils.escape("Kiswani")).isEqualTo("Kiswani"); +// assertThat(StringUtils.escape("rhkiswani@gmail.com")).isEqualTo("rhkiswani%40gmail%2Ecom"); +// assertThat(StringUtils.escape("%$&+.,/@:;=?<>#%")).isEqualTo("%25%24%26%2B%2E%2C%2F%40%3A%3B%3D%3F%3C%3E%23%25"); + } + +} From ce5d57c92b82db2c649b0eed2ec1aa9c5bca6fff Mon Sep 17 00:00:00 2001 From: Mohamed Kiswani Date: Tue, 22 Nov 2016 20:17:50 +0100 Subject: [PATCH 3/5] Documentation updated, fixes --- .../javaff/exceptions/ExceptionHandlersFactoryTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java index a438f28..301f530 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java @@ -48,7 +48,6 @@ public void handle(Throwable t) { @Test public void testFactory() throws Exception { - assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(DefaultExceptionHandler.class); ExceptionHandlersFactory.instance().overrideImp(Throwable.class, exceptionHandler); assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(TestExceptionHandler.class); assertThat(ExceptionHandlersFactory.getExceptionHandler(SQLException.class).getClass()).isEqualTo(TestExceptionHandler.class); From 26d7715c338a0e8d9340ac2fb7eb91f328d39bbb Mon Sep 17 00:00:00 2001 From: Mohamed Kiswani Date: Tue, 22 Nov 2016 21:04:08 +0100 Subject: [PATCH 4/5] Documentation updated, fixes --- .../javaff/exceptions/ExceptionHandlersFactoryTest.java | 2 ++ .../io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java index 301f530..ca3d814 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java @@ -48,6 +48,8 @@ public void handle(Throwable t) { @Test public void testFactory() throws Exception { + //FIXME it's failing on travis but not local + assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(DefaultExceptionHandler.class); ExceptionHandlersFactory.instance().overrideImp(Throwable.class, exceptionHandler); assertThat(ExceptionHandlersFactory.getExceptionHandler(Throwable.class).getClass()).isEqualTo(TestExceptionHandler.class); assertThat(ExceptionHandlersFactory.getExceptionHandler(SQLException.class).getClass()).isEqualTo(TestExceptionHandler.class); diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java index 072dd5e..86221d6 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java @@ -108,7 +108,8 @@ public void handle(Throwable t) { ExceptionUtil.handle(new NullPointerException()); assertThat(exceptionMsg).isEqualTo("Overridden handler"); - + ExceptionHandlersFactory.instance().remove(MailException.class); + ExceptionHandlersFactory.instance().remove(ConsoleException.class); } private static class ConsoleException extends RuntimeException{ From a7d5d0250a8c226fe3b0051cd4204cea6b52cf53 Mon Sep 17 00:00:00 2001 From: Mohamed Kiswani Date: Tue, 22 Nov 2016 21:09:48 +0100 Subject: [PATCH 5/5] Documentation updated, fixes --- .../ExceptionHandlersFactoryTest.java | 124 +++++++++++++++++ .../javaff/exceptions/ExceptionsTest.java | 130 ------------------ 2 files changed, 124 insertions(+), 130 deletions(-) delete mode 100644 src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java index ca3d814..24b1485 100644 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionHandlersFactoryTest.java @@ -1,14 +1,18 @@ package io.github.rhkiswani.javaff.exceptions; import io.github.rhkiswani.javaff.beans.withOutAnnotation.withEqualsAnnotation.EmployeeX; +import io.github.rhkiswani.javaff.format.FormatUtil; import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; import java.sql.SQLException; import static org.assertj.core.api.Assertions.assertThat; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ExceptionHandlersFactoryTest { private ExceptionHandler exceptionHandler ; @@ -68,4 +72,124 @@ public void handle(Throwable t) { t.printStackTrace(); } }; + + String exceptionMsg = "dummyMsg"; + @Test + public void testSmartException() throws Exception { + try { + throw new SmartException(SmartException.EXCEEDS_LIMIT, "Test Name", 1000000); + }catch (Throwable t) { + assertThat(t).isInstanceOf(SmartException.class).hasMessage(FormatUtil.formatString("{0} MaxSize is {1}", "Test Name", 1000000)); + } + try { + throw new SmartException(new RuntimeException(new NullPointerException(SmartException.NOT_FOUND))); + }catch (Throwable t) { + assertThat(t).isInstanceOf(SmartException.class).hasMessage("java.lang.RuntimeException: java.lang.NullPointerException: NOT_FOUND"); + } + try { + throw new SmartException(SmartException.NOT_FOUND, new RuntimeException(new NullPointerException())); + }catch (Throwable t) { + assertThat(t).isInstanceOf(SmartException.class).hasMessage("{0} not found"); + } + + } + + @Test + public void testGetRootCause() throws Exception { + assertThat(ExceptionUtil.getRootCause(null)).isNull(); + try { + throw new SmartException(new RuntimeException(new NullPointerException(SmartException.NOT_FOUND))); + }catch (Throwable t) { + assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(NullPointerException.class); + assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("NOT_FOUND"); + } + try { + throw new SmartException(new NullPointerException(SmartException.NOT_FOUND)); + }catch (Throwable t) { + assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(NullPointerException.class); + assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("NOT_FOUND"); + } + try { + throw new SmartException(SmartException.NOT_FOUND, "Kiswani"); + }catch (Throwable t) { + assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(SmartException.class); + assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("Kiswani not found"); + } + } + + @Test + public void testHandle() throws Exception { + try { + ExceptionUtil.handle(null); + }catch (Throwable t) { + assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(IllegalParamException.class); + assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("Exception cant be null"); + } + ExceptionHandlersFactory.instance().setDefault(new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "userDefaultHandler"; + } + }); + ExceptionUtil.handle(new NullPointerException()); + assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); + + //Any class is instance of ConsoleException will be handled here + ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "ConsoleException handler"; + } + }); + + //Any class is instance of MailException will be handled here + ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "MailException handler"; + } + }); + + + ExceptionUtil.handle(new ConsoleException()); + assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); + ExceptionHandlersFactory.instance().setDefault(null); + ExceptionUtil.handle(new ConsoleException()); + assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); + ExceptionUtil.handle(new SubConsoleException()); + assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); + ExceptionUtil.handle(new MailException()); + assertThat(exceptionMsg).isEqualTo("MailException handler"); + ExceptionUtil.handle(new SubMailException()); + assertThat(exceptionMsg).isEqualTo("MailException handler"); + + //We decided to override the default implantation for Throwable.class + ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() { + @Override + public void handle(Throwable t) { + exceptionMsg = "Overridden handler"; + } + }); + + ExceptionUtil.handle(new NullPointerException()); + assertThat(exceptionMsg).isEqualTo("Overridden handler"); + ExceptionHandlersFactory.instance().remove(MailException.class); + ExceptionHandlersFactory.instance().remove(ConsoleException.class); + } + + private static class ConsoleException extends RuntimeException{ + + } + + private static class SubConsoleException extends ConsoleException{ + + } + + private static class MailException extends RuntimeException{ + + } + + private static class SubMailException extends MailException { + + } } diff --git a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java b/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java deleted file mode 100644 index 86221d6..0000000 --- a/src/test/java/io/github/rhkiswani/javaff/exceptions/ExceptionsTest.java +++ /dev/null @@ -1,130 +0,0 @@ -package io.github.rhkiswani.javaff.exceptions; - -import io.github.rhkiswani.javaff.format.FormatUtil; -import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException; -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ExceptionsTest { - - String exceptionMsg = "dummyMsg"; - @Test - public void testSmartException() throws Exception { - try { - throw new SmartException(SmartException.EXCEEDS_LIMIT, "Test Name", 1000000); - }catch (Throwable t) { - assertThat(t).isInstanceOf(SmartException.class).hasMessage(FormatUtil.formatString("{0} MaxSize is {1}", "Test Name", 1000000)); - } - try { - throw new SmartException(new RuntimeException(new NullPointerException(SmartException.NOT_FOUND))); - }catch (Throwable t) { - assertThat(t).isInstanceOf(SmartException.class).hasMessage("java.lang.RuntimeException: java.lang.NullPointerException: NOT_FOUND"); - } - try { - throw new SmartException(SmartException.NOT_FOUND, new RuntimeException(new NullPointerException())); - }catch (Throwable t) { - assertThat(t).isInstanceOf(SmartException.class).hasMessage("{0} not found"); - } - - } - - @Test - public void testGetRootCause() throws Exception { - assertThat(ExceptionUtil.getRootCause(null)).isNull(); - try { - throw new SmartException(new RuntimeException(new NullPointerException(SmartException.NOT_FOUND))); - }catch (Throwable t) { - assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(NullPointerException.class); - assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("NOT_FOUND"); - } - try { - throw new SmartException(new NullPointerException(SmartException.NOT_FOUND)); - }catch (Throwable t) { - assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(NullPointerException.class); - assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("NOT_FOUND"); - } - try { - throw new SmartException(SmartException.NOT_FOUND, "Kiswani"); - }catch (Throwable t) { - assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(SmartException.class); - assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("Kiswani not found"); - } - } - - @Test - public void testHandle() throws Exception { - try { - ExceptionUtil.handle(null); - }catch (Throwable t) { - assertThat(ExceptionUtil.getRootCause(t).getClass()).isEqualTo(IllegalParamException.class); - assertThat(ExceptionUtil.getRootCause(t).getMessage()).isEqualTo("Exception cant be null"); - } - ExceptionHandlersFactory.instance().setDefault(new ExceptionHandler() { - @Override - public void handle(Throwable t) { - exceptionMsg = "userDefaultHandler"; - } - }); - ExceptionUtil.handle(new NullPointerException()); - assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); - - //Any class is instance of ConsoleException will be handled here - ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() { - @Override - public void handle(Throwable t) { - exceptionMsg = "ConsoleException handler"; - } - }); - - //Any class is instance of MailException will be handled here - ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() { - @Override - public void handle(Throwable t) { - exceptionMsg = "MailException handler"; - } - }); - - - ExceptionUtil.handle(new ConsoleException()); - assertThat(exceptionMsg).isEqualTo("userDefaultHandler"); - ExceptionHandlersFactory.instance().setDefault(null); - ExceptionUtil.handle(new ConsoleException()); - assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); - ExceptionUtil.handle(new SubConsoleException()); - assertThat(exceptionMsg).isEqualTo("ConsoleException handler"); - ExceptionUtil.handle(new MailException()); - assertThat(exceptionMsg).isEqualTo("MailException handler"); - ExceptionUtil.handle(new SubMailException()); - assertThat(exceptionMsg).isEqualTo("MailException handler"); - - //We decided to override the default implantation for Throwable.class - ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() { - @Override - public void handle(Throwable t) { - exceptionMsg = "Overridden handler"; - } - }); - - ExceptionUtil.handle(new NullPointerException()); - assertThat(exceptionMsg).isEqualTo("Overridden handler"); - ExceptionHandlersFactory.instance().remove(MailException.class); - ExceptionHandlersFactory.instance().remove(ConsoleException.class); - } - - private static class ConsoleException extends RuntimeException{ - - } - - private static class SubConsoleException extends ConsoleException{ - - } - - private static class MailException extends RuntimeException{ - - } - - private static class SubMailException extends MailException { - - } -}