From ea7752c5e183d2981c29ce45bb4d989ef2eefab6 Mon Sep 17 00:00:00 2001 From: Amit Dixit Date: Thu, 27 Oct 2016 18:30:07 +0530 Subject: [PATCH] checkstyle errors removed checkstyle errors removed --- module/etc/module.urm.puml | 43 +++ .../main/java/com/iluwatar/module/App.java | 101 +++--- .../iluwatar/module/ConsoleLoggerModule.java | 122 +++---- .../com/iluwatar/module/FileLoggerModule.java | 151 +++++---- .../java/com/iluwatar/module/AppTest.java | 10 +- .../iluwatar/module/FileLoggerModuleTest.java | 182 ++++++++++ .../java/com/iluwatar/module/ModuleTest.java | 315 ------------------ 7 files changed, 425 insertions(+), 499 deletions(-) create mode 100644 module/etc/module.urm.puml create mode 100644 module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java delete mode 100644 module/src/test/java/com/iluwatar/module/ModuleTest.java diff --git a/module/etc/module.urm.puml b/module/etc/module.urm.puml new file mode 100644 index 000000000..d78f12da8 --- /dev/null +++ b/module/etc/module.urm.puml @@ -0,0 +1,43 @@ +@startuml +package com.iluwatar.module { + class App { + + consoleLoggerModule : ConsoleLoggerModule {static} + + fileLoggerModule : FileLoggerModule {static} + - App() + + execute(args : String[]) {static} + + main(args : String[]) {static} + + prepare() {static} + + unprepare() {static} + } + class ConsoleLoggerModule { + - LOGGER : Logger {static} + + error : PrintStream + + output : PrintStream + - singleton : ConsoleLoggerModule {static} + - ConsoleLoggerModule() + + getSingleton() : ConsoleLoggerModule {static} + + prepare() + + printErrorString(value : String) + + printString(value : String) + + unprepare() + } + class FileLoggerModule { + - ERROR_FILE : String {static} + - LOGGER : Logger {static} + - OUTPUT_FILE : String {static} + + error : PrintStream + + output : PrintStream + - singleton : FileLoggerModule {static} + - FileLoggerModule() + + getSingleton() : FileLoggerModule {static} + + prepare() + + printErrorString(value : String) + + printString(value : String) + + unprepare() + } +} +FileLoggerModule --> "-singleton" FileLoggerModule +App --> "-consoleLoggerModule" ConsoleLoggerModule +ConsoleLoggerModule --> "-singleton" ConsoleLoggerModule +App --> "-fileLoggerModule" FileLoggerModule +@enduml \ No newline at end of file diff --git a/module/src/main/java/com/iluwatar/module/App.java b/module/src/main/java/com/iluwatar/module/App.java index a3ecdb31c..cfa50975f 100644 --- a/module/src/main/java/com/iluwatar/module/App.java +++ b/module/src/main/java/com/iluwatar/module/App.java @@ -21,64 +21,75 @@ package com.iluwatar.module; import java.io.FileNotFoundException; /** - * The Module pattern can be considered a Creational pattern and a Structural - * pattern. It manages the creation and organization of other elements, and - * groups them as the structural pattern does. An object that applies this - * pattern can provide the equivalent of a namespace, providing the - * initialization and finalization process of a static class or a class with - * static members with cleaner, more concise syntax and semantics. + * The Module pattern can be considered a Creational pattern and a Structural pattern. It manages + * the creation and organization of other elements, and groups them as the structural pattern does. + * An object that applies this pattern can provide the equivalent of a namespace, providing the + * initialization and finalization process of a static class or a class with static members with + * cleaner, more concise syntax and semantics. *

- * The below example demonstrates a use case for testing two different modules: - * File Logger and Console Logger + * The below example demonstrates a use case for testing two different modules: File Logger and + * Console Logger * */ public final class App { - public static FileLoggerModule fileLoggerModule = null; - public static ConsoleLoggerModule consoleLoggerModule = null; + public static FileLoggerModule fileLoggerModule = null; + public static ConsoleLoggerModule consoleLoggerModule = null; - public static void prepare() throws FileNotFoundException { + /** + * Following method performs the initialization + * + * @throws FileNotFoundException if program is not able to find log files (output.txt and + * error.txt) + */ + public static void prepare() throws FileNotFoundException { - fileLoggerModule = FileLoggerModule.getSingleton(); - consoleLoggerModule = ConsoleLoggerModule.getSingleton(); + fileLoggerModule = FileLoggerModule.getSingleton(); + consoleLoggerModule = ConsoleLoggerModule.getSingleton(); - /* Prepare modules */ - fileLoggerModule.prepare(); - consoleLoggerModule.prepare(); - } + /* Prepare modules */ + fileLoggerModule.prepare(); + consoleLoggerModule.prepare(); + } - public static void unprepare() { + /** + * Following method performs the finalization + */ + public static void unprepare() { - /* Close all resources */ - fileLoggerModule.unprepare(); - consoleLoggerModule.unprepare(); - } + /* Close all resources */ + fileLoggerModule.unprepare(); + consoleLoggerModule.unprepare(); + } - public static final void execute(final String... args) { + /** + * Following method is main executor + * + * @param args for providing default program arguments + */ + public static void execute(final String... args) { - /* Send logs on file system */ - fileLoggerModule.printString("Message"); - fileLoggerModule.printErrorString("Error"); + /* Send logs on file system */ + fileLoggerModule.printString("Message"); + fileLoggerModule.printErrorString("Error"); - /* Send logs on console */ - consoleLoggerModule.printString("Message"); - consoleLoggerModule.printErrorString("Error"); - } + /* Send logs on console */ + consoleLoggerModule.printString("Message"); + consoleLoggerModule.printErrorString("Error"); + } - /** - * Program entry point. - * - * @param args - * command line args. - * @throws FileNotFoundException - */ - public static final void main(final String... args) - throws FileNotFoundException { - prepare(); - execute(args); - unprepare(); - } + /** + * Program entry point. + * + * @param args command line args. + * @throws FileNotFoundException if program is not able to find log files (output.txt and + * error.txt) + */ + public static void main(final String... args) throws FileNotFoundException { + prepare(); + execute(args); + unprepare(); + } - private App() { - } + private App() {} } diff --git a/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java b/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java index 097f85b0e..8f5941951 100644 --- a/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java +++ b/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java @@ -18,89 +18,91 @@ */ package com.iluwatar.module; -import java.io.FileNotFoundException; import java.io.PrintStream; import org.apache.log4j.Logger; /** - * The Module pattern can be considered a Creational pattern and a Structural - * pattern. It manages the creation and organization of other elements, and - * groups them as the structural pattern does. An object that applies this - * pattern can provide the equivalent of a namespace, providing the - * initialization and finalization process of a static class or a class with - * static members with cleaner, more concise syntax and semantics. + * The Module pattern can be considered a Creational pattern and a Structural pattern. It manages + * the creation and organization of other elements, and groups them as the structural pattern does. + * An object that applies this pattern can provide the equivalent of a namespace, providing the + * initialization and finalization process of a static class or a class with static members with + * cleaner, more concise syntax and semantics. *

- * The below example demonstrates a Console logger module, which can print - * simple and error messages in two designated formats + * The below example demonstrates a Console logger module, which can print simple and error messages + * in two designated formats */ -public class ConsoleLoggerModule { +public final class ConsoleLoggerModule { - private static final Logger logger = Logger - .getLogger(ConsoleLoggerModule.class); + private static final Logger LOGGER = Logger.getLogger(ConsoleLoggerModule.class); - private static ConsoleLoggerModule singleton = null; + private static ConsoleLoggerModule singleton = null; - public PrintStream output = null; - public PrintStream error = null; + public PrintStream output = null; + public PrintStream error = null; - private ConsoleLoggerModule() { - } + private ConsoleLoggerModule() {} - public static final ConsoleLoggerModule getSingleton() { + /** + * Static method to get single instance of class + * + * @return singleton instance of ConsoleLoggerModule + */ + public static ConsoleLoggerModule getSingleton() { - if (ConsoleLoggerModule.singleton == null) { - ConsoleLoggerModule.singleton = new ConsoleLoggerModule(); - } + if (ConsoleLoggerModule.singleton == null) { + ConsoleLoggerModule.singleton = new ConsoleLoggerModule(); + } - return ConsoleLoggerModule.singleton; - } + return ConsoleLoggerModule.singleton; + } - /** - * - * @throws FileNotFoundException - */ - public final void prepare() { + /** + * Following method performs the initialization + */ + public void prepare() { - logger.debug("ConsoleLoggerModule::prepare();"); + LOGGER.debug("ConsoleLoggerModule::prepare();"); - this.output = new PrintStream(System.out); - this.error = new PrintStream(System.err); - } + this.output = new PrintStream(System.out); + this.error = new PrintStream(System.err); + } - /** - * - */ - public final void unprepare() { + /** + * Following method performs the finalization + */ + public void unprepare() { - if (this.output != null) { + if (this.output != null) { - this.output.flush(); - this.output.close(); - } + this.output.flush(); + this.output.close(); + } - if (this.error != null) { + if (this.error != null) { - this.error.flush(); - this.error.close(); - } + this.error.flush(); + this.error.close(); + } - logger.debug("ConsoleLoggerModule::unprepare();"); - } + LOGGER.debug("ConsoleLoggerModule::unprepare();"); + } - /** - * - * @param value - */ - public final void printString(final String value) { - this.output.println(value); - } + /** + * Used to print a message + * + * @param value will be printed on console + */ + public void printString(final String value) { + this.output.println(value); + } - /** - * - * @param value - */ - public final void printErrorString(final String value) { - this.error.println(value); - } + /** + * Used to print a error message + * + * @param value will be printed on error console + */ + public void printErrorString(final String value) { + this.error.println(value); + } } diff --git a/module/src/main/java/com/iluwatar/module/FileLoggerModule.java b/module/src/main/java/com/iluwatar/module/FileLoggerModule.java index d1df062de..fae66191b 100644 --- a/module/src/main/java/com/iluwatar/module/FileLoggerModule.java +++ b/module/src/main/java/com/iluwatar/module/FileLoggerModule.java @@ -1,23 +1,20 @@ /** * The MIT License Copyright (c) 2016 Amit Dixit * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.iluwatar.module; @@ -28,86 +25,92 @@ import java.io.PrintStream; import org.apache.log4j.Logger; /** - * The Module pattern can be considered a Creational pattern and a Structural - * pattern. It manages the creation and organization of other elements, and - * groups them as the structural pattern does. An object that applies this - * pattern can provide the equivalent of a namespace, providing the - * initialization and finalization process of a static class or a class with - * static members with cleaner, more concise syntax and semantics. + * The Module pattern can be considered a Creational pattern and a Structural pattern. It manages + * the creation and organization of other elements, and groups them as the structural pattern does. + * An object that applies this pattern can provide the equivalent of a namespace, providing the + * initialization and finalization process of a static class or a class with static members with + * cleaner, more concise syntax and semantics. *

- * The below example demonstrates a File logger module, which can print simple - * and error messages in two designated files + * The below example demonstrates a File logger module, which can print simple and error messages in + * two designated files */ public final class FileLoggerModule { - private static final Logger logger = Logger - .getLogger(FileLoggerModule.class); + private static final Logger LOGGER = Logger.getLogger(FileLoggerModule.class); - private static FileLoggerModule singleton = null; + private static FileLoggerModule singleton = null; - private static final String OUTPUT_FILE = "output.txt"; - private static final String ERROR_FILE = "error.txt"; + private static final String OUTPUT_FILE = "output.txt"; + private static final String ERROR_FILE = "error.txt"; - public PrintStream output = null; - public PrintStream error = null; + public PrintStream output = null; + public PrintStream error = null; - private FileLoggerModule() { - } + private FileLoggerModule() {} - public static final FileLoggerModule getSingleton() { + /** + * Static method to get single instance of class + * + * @return singleton instance of FileLoggerModule + */ + public static FileLoggerModule getSingleton() { - if (FileLoggerModule.singleton == null) { - FileLoggerModule.singleton = new FileLoggerModule(); - } + if (FileLoggerModule.singleton == null) { + FileLoggerModule.singleton = new FileLoggerModule(); + } - return FileLoggerModule.singleton; - } + return FileLoggerModule.singleton; + } - /** - * - * @throws FileNotFoundException - */ - public final void prepare() throws FileNotFoundException { + /** + * Following method performs the initialization + * + * @throws FileNotFoundException if program is not able to find log files (output.txt and + * error.txt) + */ + public void prepare() throws FileNotFoundException { - logger.debug("FileLoggerModule::prepare();"); + LOGGER.debug("FileLoggerModule::prepare();"); - this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE)); - this.error = new PrintStream(new FileOutputStream(ERROR_FILE)); - } + this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE)); + this.error = new PrintStream(new FileOutputStream(ERROR_FILE)); + } - /** - * - */ - public final void unprepare() { + /** + * Following method performs the finalization + */ + public void unprepare() { - if (this.output != null) { + if (this.output != null) { - this.output.flush(); - this.output.close(); - } + this.output.flush(); + this.output.close(); + } - if (this.error != null) { + if (this.error != null) { - this.error.flush(); - this.error.close(); - } + this.error.flush(); + this.error.close(); + } - logger.debug("FileLoggerModule::unprepare();"); - } + LOGGER.debug("FileLoggerModule::unprepare();"); + } - /** - * - * @param value - */ - public final void printString(final String value) { - this.output.println(value); - } + /** + * Used to print a message + * + * @param value will be printed in file + */ + public void printString(final String value) { + this.output.println(value); + } - /** - * - * @param value - */ - public final void printErrorString(final String value) { - this.error.println(value); - } + /** + * Used to print a error message + * + * @param value will be printed on error file + */ + public void printErrorString(final String value) { + this.error.println(value); + } } diff --git a/module/src/test/java/com/iluwatar/module/AppTest.java b/module/src/test/java/com/iluwatar/module/AppTest.java index 0f20b4959..21b32407d 100644 --- a/module/src/test/java/com/iluwatar/module/AppTest.java +++ b/module/src/test/java/com/iluwatar/module/AppTest.java @@ -29,9 +29,9 @@ import org.junit.Test; */ public final class AppTest { - @Test - public void test() throws FileNotFoundException { - final String[] args = {}; - App.main(args); - } + @Test + public void test() throws FileNotFoundException { + final String[] args = {}; + App.main(args); + } } diff --git a/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java b/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java new file mode 100644 index 000000000..ecefb307a --- /dev/null +++ b/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java @@ -0,0 +1,182 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.module; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +import org.apache.log4j.Logger; +import org.junit.Test; + +/** + * The Module pattern can be considered a Creational pattern and a Structural pattern. It manages + * the creation and organization of other elements, and groups them as the structural pattern does. + * An object that applies this pattern can provide the equivalent of a namespace, providing the + * initialization and finalization process of a static class or a class with static members with + * cleaner, more concise syntax and semantics. + *

+ * The below example demonstrates a JUnit test for testing two different modules: File Logger and + * Console Logger + */ +public final class FileLoggerModuleTest { + + private static final Logger LOGGER = Logger.getLogger(FileLoggerModuleTest.class); + + private static final String OUTPUT_FILE = "output.txt"; + private static final String ERROR_FILE = "error.txt"; + + private static final String MESSAGE = "MESSAGE"; + private static final String ERROR = "ERROR"; + + + /** + * This test verify that 'MESSAGE' is perfectly printed in output file + * + * @throws IOException if program is not able to find log files (output.txt and error.txt) + */ + @Test + public void positiveTestFileMessage() throws IOException { + + /* Get singletong instance of File Logger Module */ + final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); + + /* Prepare the essential sub modules, to perform the sequence of jobs */ + fileLoggerModule.prepare(); + + /* Print 'Message' in file */ + fileLoggerModule.printString(MESSAGE); + + /* Test if 'Message' is printed in file */ + assertEquals(readFirstLine(OUTPUT_FILE), MESSAGE); + + /* Unprepare to cleanup the modules */ + fileLoggerModule.unprepare(); + } + + /** + * This test verify that nothing is printed in output file + * + * @throws IOException if program is not able to find log files (output.txt and error.txt) + */ + @Test + public void negativeTestFileMessage() throws IOException { + + /* Get singletong instance of File Logger Module */ + final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); + + /* Prepare the essential sub modules, to perform the sequence of jobs */ + fileLoggerModule.prepare(); + + /* Test if nothing is printed in file */ + assertEquals(readFirstLine(OUTPUT_FILE), null); + + /* Unprepare to cleanup the modules */ + fileLoggerModule.unprepare(); + } + + /** + * This test verify that 'ERROR' is perfectly printed in error file + * + * @throws FileNotFoundException if program is not able to find log files (output.txt and + * error.txt) + */ + @Test + public void positiveTestFileErrorMessage() throws FileNotFoundException { + + /* Get singletong instance of File Logger Module */ + final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); + + /* Prepare the essential sub modules, to perform the sequence of jobs */ + fileLoggerModule.prepare(); + + /* Print 'Error' in file */ + fileLoggerModule.printErrorString(ERROR); + + /* Test if 'Message' is printed in file */ + assertEquals(readFirstLine(ERROR_FILE), ERROR); + + /* Unprepare to cleanup the modules */ + fileLoggerModule.unprepare(); + } + + /** + * This test verify that nothing is printed in error file + * + * @throws FileNotFoundException if program is not able to find log files (output.txt and + * error.txt) + */ + @Test + public void negativeTestFileErrorMessage() throws FileNotFoundException { + + /* Get singletong instance of File Logger Module */ + final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); + + /* Prepare the essential sub modules, to perform the sequence of jobs */ + fileLoggerModule.prepare(); + + /* Test if nothing is printed in file */ + assertEquals(readFirstLine(ERROR_FILE), null); + + /* Unprepare to cleanup the modules */ + fileLoggerModule.unprepare(); + } + + /** + * Utility method to read first line of a file + * + * @param file as file name to be read + * @return a string value as first line in file + */ + private static final String readFirstLine(final String file) { + + String firstLine = null; + BufferedReader bufferedReader = null; + try { + + /* Create a buffered reader */ + bufferedReader = new BufferedReader(new FileReader(file)); + + while (bufferedReader.ready()) { + + /* Read the line */ + firstLine = bufferedReader.readLine(); + } + + LOGGER.info("ModuleTest::readFirstLine() : firstLine : " + firstLine); + + } catch (final IOException e) { + LOGGER.error("ModuleTest::readFirstLine()", e); + } finally { + + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (final IOException e) { + LOGGER.error("ModuleTest::readFirstLine()", e); + } + } + } + + return firstLine; + } +} diff --git a/module/src/test/java/com/iluwatar/module/ModuleTest.java b/module/src/test/java/com/iluwatar/module/ModuleTest.java deleted file mode 100644 index cf3435850..000000000 --- a/module/src/test/java/com/iluwatar/module/ModuleTest.java +++ /dev/null @@ -1,315 +0,0 @@ -/** - * The MIT License Copyright (c) 2016 Amit Dixit - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and - * associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT - * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.iluwatar.module; - -import static org.junit.Assert.assertEquals; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; - -import org.apache.log4j.Logger; -import org.junit.Test; - -/** - * The Module pattern can be considered a Creational pattern and a Structural - * pattern. It manages the creation and organization of other elements, and - * groups them as the structural pattern does. An object that applies this - * pattern can provide the equivalent of a namespace, providing the - * initialization and finalization process of a static class or a class with - * static members with cleaner, more concise syntax and semantics. - *

- * The below example demonstrates a JUnit test for testing two different - * modules: File Logger and Console Logger - */ -public class ModuleTest { - - private static final Logger logger = Logger.getLogger(ModuleTest.class); - - private static final String OUTPUT_FILE = "output.txt"; - private static final String ERROR_FILE = "error.txt"; - - private static final String MESSAGE = "MESSAGE"; - private static final String ERROR = "ERROR"; - - /** - * This test verify that 'MESSAGE' is perfectly printed in output file - * - * @throws IOException - */ - @Test - public void positiveTestConsoleMessage() throws IOException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final FileLoggerModule fileLoggerModule = FileLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - fileLoggerModule.prepare(); - - /* Print 'Message' in file */ - fileLoggerModule.printString(MESSAGE); - - /* Test if 'Message' is printed on console */ - assertEquals(readFirstLine(), MESSAGE); - - /* Unprepare to cleanup the modules */ - fileLoggerModule.unprepare(); - } - - /** - * This test verify that nothing is printed in output file - * - * @throws IOException - */ - @Test - public void negativeTestConsoleMessage() throws IOException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final ConsoleLoggerModule consoleLoggerModule = ConsoleLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - consoleLoggerModule.prepare(); - - /* Test if nothing is printed on console */ - assertEquals(readFirstLine(), null); - - /* Unprepare to cleanup the modules */ - consoleLoggerModule.unprepare(); - } - - /** - * This test verify that 'ERROR' is perfectly printed in error file - * - * @throws FileNotFoundException - */ - @Test - public void positiveTestConsoleErrorMessage() { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final ConsoleLoggerModule consoleLoggerModule = ConsoleLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - consoleLoggerModule.prepare(); - - /* Print 'Error' in file */ - consoleLoggerModule.printErrorString(ERROR); - - /* Test if 'Message' is printed on console */ - assertEquals(readFirstLine(), ERROR); - - /* Unprepare to cleanup the modules */ - consoleLoggerModule.unprepare(); - } - - /** - * This test verify that nothing is printed in error file - * - * @throws FileNotFoundException - */ - @Test - public void negativeTestConsoleErrorMessage() { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final ConsoleLoggerModule consoleLoggerModule = ConsoleLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - consoleLoggerModule.prepare(); - - /* Test if nothing is printed on console */ - assertEquals(readFirstLine(), null); - - /* Unprepare to cleanup the modules */ - consoleLoggerModule.unprepare(); - } - - /** - * This test verify that 'MESSAGE' is perfectly printed in output file - * - * @throws IOException - */ - @Test - public void positiveTestFileMessage() throws IOException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final FileLoggerModule fileLoggerModule = FileLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - fileLoggerModule.prepare(); - - /* Print 'Message' in file */ - fileLoggerModule.printString(MESSAGE); - - /* Test if 'Message' is printed in file */ - assertEquals(readFirstLine(OUTPUT_FILE), MESSAGE); - - /* Unprepare to cleanup the modules */ - fileLoggerModule.unprepare(); - } - - /** - * This test verify that nothing is printed in output file - * - * @throws IOException - */ - @Test - public void negativeTestFileMessage() throws IOException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final FileLoggerModule fileLoggerModule = FileLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - fileLoggerModule.prepare(); - - /* Test if nothing is printed in file */ - assertEquals(readFirstLine(OUTPUT_FILE), null); - - /* Unprepare to cleanup the modules */ - fileLoggerModule.unprepare(); - } - - /** - * This test verify that 'ERROR' is perfectly printed in error file - * - * @throws FileNotFoundException - */ - @Test - public void positiveTestFileErrorMessage() throws FileNotFoundException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final FileLoggerModule fileLoggerModule = FileLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - fileLoggerModule.prepare(); - - /* Print 'Error' in file */ - fileLoggerModule.printErrorString(ERROR); - - /* Test if 'Message' is printed in file */ - assertEquals(readFirstLine(ERROR_FILE), ERROR); - - /* Unprepare to cleanup the modules */ - fileLoggerModule.unprepare(); - } - - /** - * This test verify that nothing is printed in error file - * - * @throws FileNotFoundException - */ - @Test - public void negativeTestFileErrorMessage() throws FileNotFoundException { - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - final FileLoggerModule fileLoggerModule = FileLoggerModule - .getSingleton(); - - /* Prepare the essential sub modules, to perform the sequence of jobs */ - fileLoggerModule.prepare(); - - /* Test if nothing is printed in file */ - assertEquals(readFirstLine(ERROR_FILE), null); - - /* Unprepare to cleanup the modules */ - fileLoggerModule.unprepare(); - } - - /** - * Utility method to read first line of a file - * - * @param file - * @return - */ - private static final String readFirstLine() { - - String firstLine = null; - BufferedReader bufferedReader = null; - try { - - /* Create a buffered reader */ - bufferedReader = new BufferedReader( - new InputStreamReader(System.in)); - - /* Read the line */ - firstLine = bufferedReader.readLine(); - - logger.info("ModuleTest::readFirstLineFromConsole() : firstLine : " - + firstLine); - - } catch (final IOException e) { - logger.error("ModuleTest::readFirstLineFromConsole()", e); - } finally { - - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (final IOException e) { - logger.error("ModuleTest::readFirstLineFromConsole()", e); - } - } - } - - return firstLine; - } - - /** - * Utility method to read first line of a file - * - * @param file - * @return - */ - private static final String readFirstLine(final String file) { - - String firstLine = null; - BufferedReader bufferedReader = null; - try { - - /* Create a buffered reader */ - bufferedReader = new BufferedReader(new FileReader(file)); - - /* Read the line */ - firstLine = bufferedReader.readLine(); - - logger.info("ModuleTest::readFirstLine() : firstLine : " - + firstLine); - - } catch (final IOException e) { - logger.error("ModuleTest::readFirstLine()", e); - } finally { - - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (final IOException e) { - logger.error("ModuleTest::readFirstLine()", e); - } - } - } - - return firstLine; - } -}