Review comments++

This commit is contained in:
Amit Dixit 2016-11-15 12:59:05 +05:30
parent ea7752c5e1
commit 4ff8fa3721
4 changed files with 494 additions and 501 deletions

View File

@ -1,95 +1,96 @@
/** /**
* The MIT License Copyright (c) 2016 Amit Dixit * 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 * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, * 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 * 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: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all copies or * The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software. * substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * 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 * 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, * 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, * 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. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.iluwatar.module; package com.iluwatar.module;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
/** /**
* The Module pattern can be considered a Creational pattern and a Structural pattern. It manages * 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. * 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 * 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 * initialization and finalization process of a static class or a class with static members with
* cleaner, more concise syntax and semantics. * cleaner, more concise syntax and semantics.
* <p> * <p>
* The below example demonstrates a use case for testing two different modules: File Logger and * The below example demonstrates a use case for testing two different modules: File Logger and
* Console Logger * Console Logger
* *
*/ */
public final class App { public final class App {
public static FileLoggerModule fileLoggerModule = null; public static FileLoggerModule fileLoggerModule;
public static ConsoleLoggerModule consoleLoggerModule = null; public static ConsoleLoggerModule consoleLoggerModule;
/** /**
* Following method performs the initialization * Following method performs the initialization
* *
* @throws FileNotFoundException if program is not able to find log files (output.txt and * @throws FileNotFoundException if program is not able to find log files (output.txt and
* error.txt) * error.txt)
*/ */
public static void prepare() throws FileNotFoundException { public static void prepare() throws FileNotFoundException {
fileLoggerModule = FileLoggerModule.getSingleton(); /* Create new singleton objects and prepare their modules */
consoleLoggerModule = ConsoleLoggerModule.getSingleton(); fileLoggerModule = FileLoggerModule.getSingleton();
consoleLoggerModule = ConsoleLoggerModule.getSingleton();
/* Prepare modules */
fileLoggerModule.prepare(); /* Prepare modules */
consoleLoggerModule.prepare(); fileLoggerModule.prepare();
} consoleLoggerModule.prepare();
}
/**
* Following method performs the finalization /**
*/ * Following method performs the finalization
public static void unprepare() { */
public static void unprepare() {
/* Close all resources */
fileLoggerModule.unprepare(); /* Close all resources */
consoleLoggerModule.unprepare(); fileLoggerModule.unprepare();
} consoleLoggerModule.unprepare();
}
/**
* Following method is main executor /**
* * Following method is main executor
* @param args for providing default program arguments *
*/ * @param args for providing default program arguments
public static void execute(final String... args) { */
public static void execute(final String... args) {
/* Send logs on file system */
fileLoggerModule.printString("Message"); /* Send logs on file system */
fileLoggerModule.printErrorString("Error"); fileLoggerModule.printString("Message");
fileLoggerModule.printErrorString("Error");
/* Send logs on console */
consoleLoggerModule.printString("Message"); /* Send logs on console */
consoleLoggerModule.printErrorString("Error"); consoleLoggerModule.printString("Message");
} consoleLoggerModule.printErrorString("Error");
}
/**
* Program entry point. /**
* * Program entry point.
* @param args command line args. *
* @throws FileNotFoundException if program is not able to find log files (output.txt and * @param args command line args.
* error.txt) * @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(); public static void main(final String... args) throws FileNotFoundException {
execute(args); prepare();
unprepare(); execute(args);
} unprepare();
}
private App() {}
} private App() {}
}

View File

@ -1,108 +1,104 @@
/** /**
* The MIT License Copyright (c) 2016 Amit Dixit * 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 * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, * 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 * 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: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all copies or * The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software. * substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * 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 * 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, * 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, * 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. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.iluwatar.module; package com.iluwatar.module;
import java.io.PrintStream; import java.io.PrintStream;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* The Module pattern can be considered a Creational pattern and a Structural pattern. It manages * The ConsoleLoggerModule is responsible for showing logs on System Console
* the creation and organization of other elements, and groups them as the structural pattern does. * <p>
* An object that applies this pattern can provide the equivalent of a namespace, providing the * The below example demonstrates a Console logger module, which can print simple and error messages
* initialization and finalization process of a static class or a class with static members with * in two designated formats
* cleaner, more concise syntax and semantics. */
* <p> public final class ConsoleLoggerModule {
* The below example demonstrates a Console logger module, which can print simple and error messages
* in two designated formats private static final Logger LOGGER = Logger.getLogger(ConsoleLoggerModule.class);
*/
public final class ConsoleLoggerModule { private static ConsoleLoggerModule singleton = null;
private static final Logger LOGGER = Logger.getLogger(ConsoleLoggerModule.class); public PrintStream output = null;
public PrintStream error = null;
private static ConsoleLoggerModule singleton = null;
private ConsoleLoggerModule() {}
public PrintStream output = null;
public PrintStream error = null; /**
* Static method to get single instance of class
private ConsoleLoggerModule() {} *
* @return singleton instance of ConsoleLoggerModule
/** */
* Static method to get single instance of class public static ConsoleLoggerModule getSingleton() {
*
* @return singleton instance of ConsoleLoggerModule if (ConsoleLoggerModule.singleton == null) {
*/ ConsoleLoggerModule.singleton = new ConsoleLoggerModule();
public static ConsoleLoggerModule getSingleton() { }
if (ConsoleLoggerModule.singleton == null) { return ConsoleLoggerModule.singleton;
ConsoleLoggerModule.singleton = new ConsoleLoggerModule(); }
}
/**
return ConsoleLoggerModule.singleton; * Following method performs the initialization
} */
public void prepare() {
/**
* Following method performs the initialization LOGGER.debug("ConsoleLoggerModule::prepare();");
*/
public void prepare() { this.output = new PrintStream(System.out);
this.error = new PrintStream(System.err);
LOGGER.debug("ConsoleLoggerModule::prepare();"); }
this.output = new PrintStream(System.out); /**
this.error = new PrintStream(System.err); * Following method performs the finalization
} */
public void unprepare() {
/**
* Following method performs the finalization if (this.output != null) {
*/
public void unprepare() { this.output.flush();
this.output.close();
if (this.output != null) { }
this.output.flush(); if (this.error != null) {
this.output.close();
} this.error.flush();
this.error.close();
if (this.error != null) { }
this.error.flush(); LOGGER.debug("ConsoleLoggerModule::unprepare();");
this.error.close(); }
}
/**
LOGGER.debug("ConsoleLoggerModule::unprepare();"); * Used to print a message
} *
* @param value will be printed on console
/** */
* Used to print a message public void printString(final String value) {
* this.output.println(value);
* @param value will be printed on console }
*/
public void printString(final String value) { /**
this.output.println(value); * Used to print a error message
} *
* @param value will be printed on error console
/** */
* Used to print a error message public void printErrorString(final String value) {
* this.error.println(value);
* @param value will be printed on error console }
*/ }
public void printErrorString(final String value) {
this.error.println(value);
}
}

View File

@ -1,116 +1,112 @@
/** /**
* The MIT License Copyright (c) 2016 Amit Dixit * 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 * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, * 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 * 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: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all copies or * The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software. * substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * 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 * 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, * 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, * 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. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.iluwatar.module; package com.iluwatar.module;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* The Module pattern can be considered a Creational pattern and a Structural pattern. It manages * The FileLoggerModule is responsible for showing logs on File System
* the creation and organization of other elements, and groups them as the structural pattern does. * <p>
* An object that applies this pattern can provide the equivalent of a namespace, providing the * The below example demonstrates a File logger module, which can print simple and error messages in
* initialization and finalization process of a static class or a class with static members with * two designated files
* cleaner, more concise syntax and semantics. */
* <p> public final class FileLoggerModule {
* The below example demonstrates a File logger module, which can print simple and error messages in
* two designated files private static final Logger LOGGER = Logger.getLogger(FileLoggerModule.class);
*/
public final class FileLoggerModule { private static FileLoggerModule singleton = null;
private static final Logger LOGGER = Logger.getLogger(FileLoggerModule.class); private static final String OUTPUT_FILE = "output.txt";
private static final String ERROR_FILE = "error.txt";
private static FileLoggerModule singleton = null;
public PrintStream output = null;
private static final String OUTPUT_FILE = "output.txt"; public PrintStream error = null;
private static final String ERROR_FILE = "error.txt";
private FileLoggerModule() {}
public PrintStream output = null;
public PrintStream error = null; /**
* Static method to get single instance of class
private FileLoggerModule() {} *
* @return singleton instance of FileLoggerModule
/** */
* Static method to get single instance of class public static FileLoggerModule getSingleton() {
*
* @return singleton instance of FileLoggerModule if (FileLoggerModule.singleton == null) {
*/ FileLoggerModule.singleton = new FileLoggerModule();
public static FileLoggerModule getSingleton() { }
if (FileLoggerModule.singleton == null) { return FileLoggerModule.singleton;
FileLoggerModule.singleton = new FileLoggerModule(); }
}
/**
return FileLoggerModule.singleton; * Following method performs the initialization
} *
* @throws FileNotFoundException if program is not able to find log files (output.txt and
/** * error.txt)
* Following method performs the initialization */
* public void prepare() throws FileNotFoundException {
* @throws FileNotFoundException if program is not able to find log files (output.txt and
* error.txt) LOGGER.debug("FileLoggerModule::prepare();");
*/
public void prepare() throws FileNotFoundException { this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE));
this.error = new PrintStream(new FileOutputStream(ERROR_FILE));
LOGGER.debug("FileLoggerModule::prepare();"); }
this.output = new PrintStream(new FileOutputStream(OUTPUT_FILE)); /**
this.error = new PrintStream(new FileOutputStream(ERROR_FILE)); * Following method performs the finalization
} */
public void unprepare() {
/**
* Following method performs the finalization if (this.output != null) {
*/
public void unprepare() { this.output.flush();
this.output.close();
if (this.output != null) { }
this.output.flush(); if (this.error != null) {
this.output.close();
} this.error.flush();
this.error.close();
if (this.error != null) { }
this.error.flush(); LOGGER.debug("FileLoggerModule::unprepare();");
this.error.close(); }
}
/**
LOGGER.debug("FileLoggerModule::unprepare();"); * Used to print a message
} *
* @param value will be printed in file
/** */
* Used to print a message public void printString(final String value) {
* this.output.println(value);
* @param value will be printed in file }
*/
public void printString(final String value) { /**
this.output.println(value); * Used to print a error message
} *
* @param value will be printed on error file
/** */
* Used to print a error message public void printErrorString(final String value) {
* this.error.println(value);
* @param value will be printed on error file }
*/ }
public void printErrorString(final String value) {
this.error.println(value);
}
}

View File

@ -1,182 +1,182 @@
/** /**
* The MIT License Copyright (c) 2016 Amit Dixit * 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 * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, * 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 * 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: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all copies or * The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software. * substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * 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 * 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, * 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, * 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. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.iluwatar.module; package com.iluwatar.module;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.Test; import org.junit.Test;
/** /**
* The Module pattern can be considered a Creational pattern and a Structural pattern. It manages * 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. * 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 * 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 * initialization and finalization process of a static class or a class with static members with
* cleaner, more concise syntax and semantics. * cleaner, more concise syntax and semantics.
* <p> * <p>
* The below example demonstrates a JUnit test for testing two different modules: File Logger and * The below example demonstrates a JUnit test for testing two different modules: File Logger and
* Console Logger * Console Logger
*/ */
public final class FileLoggerModuleTest { public final class FileLoggerModuleTest {
private static final Logger LOGGER = Logger.getLogger(FileLoggerModuleTest.class); private static final Logger LOGGER = Logger.getLogger(FileLoggerModuleTest.class);
private static final String OUTPUT_FILE = "output.txt"; private static final String OUTPUT_FILE = "output.txt";
private static final String ERROR_FILE = "error.txt"; private static final String ERROR_FILE = "error.txt";
private static final String MESSAGE = "MESSAGE"; private static final String MESSAGE = "MESSAGE";
private static final String ERROR = "ERROR"; private static final String ERROR = "ERROR";
/** /**
* This test verify that 'MESSAGE' is perfectly printed in output file * 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) * @throws IOException if program is not able to find log files (output.txt and error.txt)
*/ */
@Test @Test
public void positiveTestFileMessage() throws IOException { public void testFileMessage() throws IOException {
/* Get singletong instance of File Logger Module */ /* Get singletong instance of File Logger Module */
final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton();
/* Prepare the essential sub modules, to perform the sequence of jobs */ /* Prepare the essential sub modules, to perform the sequence of jobs */
fileLoggerModule.prepare(); fileLoggerModule.prepare();
/* Print 'Message' in file */ /* Print 'Message' in file */
fileLoggerModule.printString(MESSAGE); fileLoggerModule.printString(MESSAGE);
/* Test if 'Message' is printed in file */ /* Test if 'Message' is printed in file */
assertEquals(readFirstLine(OUTPUT_FILE), MESSAGE); assertEquals(readFirstLine(OUTPUT_FILE), MESSAGE);
/* Unprepare to cleanup the modules */ /* Unprepare to cleanup the modules */
fileLoggerModule.unprepare(); fileLoggerModule.unprepare();
} }
/** /**
* This test verify that nothing is printed in output file * 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) * @throws IOException if program is not able to find log files (output.txt and error.txt)
*/ */
@Test @Test
public void negativeTestFileMessage() throws IOException { public void testNoFileMessage() throws IOException {
/* Get singletong instance of File Logger Module */ /* Get singletong instance of File Logger Module */
final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton();
/* Prepare the essential sub modules, to perform the sequence of jobs */ /* Prepare the essential sub modules, to perform the sequence of jobs */
fileLoggerModule.prepare(); fileLoggerModule.prepare();
/* Test if nothing is printed in file */ /* Test if nothing is printed in file */
assertEquals(readFirstLine(OUTPUT_FILE), null); assertEquals(readFirstLine(OUTPUT_FILE), null);
/* Unprepare to cleanup the modules */ /* Unprepare to cleanup the modules */
fileLoggerModule.unprepare(); fileLoggerModule.unprepare();
} }
/** /**
* This test verify that 'ERROR' is perfectly printed in error file * 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 * @throws FileNotFoundException if program is not able to find log files (output.txt and
* error.txt) * error.txt)
*/ */
@Test @Test
public void positiveTestFileErrorMessage() throws FileNotFoundException { public void testFileErrorMessage() throws FileNotFoundException {
/* Get singletong instance of File Logger Module */ /* Get singletong instance of File Logger Module */
final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton();
/* Prepare the essential sub modules, to perform the sequence of jobs */ /* Prepare the essential sub modules, to perform the sequence of jobs */
fileLoggerModule.prepare(); fileLoggerModule.prepare();
/* Print 'Error' in file */ /* Print 'Error' in file */
fileLoggerModule.printErrorString(ERROR); fileLoggerModule.printErrorString(ERROR);
/* Test if 'Message' is printed in file */ /* Test if 'Message' is printed in file */
assertEquals(readFirstLine(ERROR_FILE), ERROR); assertEquals(readFirstLine(ERROR_FILE), ERROR);
/* Unprepare to cleanup the modules */ /* Unprepare to cleanup the modules */
fileLoggerModule.unprepare(); fileLoggerModule.unprepare();
} }
/** /**
* This test verify that nothing is printed in error file * This test verify that nothing is printed in error file
* *
* @throws FileNotFoundException if program is not able to find log files (output.txt and * @throws FileNotFoundException if program is not able to find log files (output.txt and
* error.txt) * error.txt)
*/ */
@Test @Test
public void negativeTestFileErrorMessage() throws FileNotFoundException { public void testNoFileErrorMessage() throws FileNotFoundException {
/* Get singletong instance of File Logger Module */ /* Get singletong instance of File Logger Module */
final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton(); final FileLoggerModule fileLoggerModule = FileLoggerModule.getSingleton();
/* Prepare the essential sub modules, to perform the sequence of jobs */ /* Prepare the essential sub modules, to perform the sequence of jobs */
fileLoggerModule.prepare(); fileLoggerModule.prepare();
/* Test if nothing is printed in file */ /* Test if nothing is printed in file */
assertEquals(readFirstLine(ERROR_FILE), null); assertEquals(readFirstLine(ERROR_FILE), null);
/* Unprepare to cleanup the modules */ /* Unprepare to cleanup the modules */
fileLoggerModule.unprepare(); fileLoggerModule.unprepare();
} }
/** /**
* Utility method to read first line of a file * Utility method to read first line of a file
* *
* @param file as file name to be read * @param file as file name to be read
* @return a string value as first line in file * @return a string value as first line in file
*/ */
private static final String readFirstLine(final String file) { private static final String readFirstLine(final String file) {
String firstLine = null; String firstLine = null;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
try { try {
/* Create a buffered reader */ /* Create a buffered reader */
bufferedReader = new BufferedReader(new FileReader(file)); bufferedReader = new BufferedReader(new FileReader(file));
while (bufferedReader.ready()) { while (bufferedReader.ready()) {
/* Read the line */ /* Read the line */
firstLine = bufferedReader.readLine(); firstLine = bufferedReader.readLine();
} }
LOGGER.info("ModuleTest::readFirstLine() : firstLine : " + firstLine); LOGGER.info("ModuleTest::readFirstLine() : firstLine : " + firstLine);
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.error("ModuleTest::readFirstLine()", e); LOGGER.error("ModuleTest::readFirstLine()", e);
} finally { } finally {
if (bufferedReader != null) { if (bufferedReader != null) {
try { try {
bufferedReader.close(); bufferedReader.close();
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.error("ModuleTest::readFirstLine()", e); LOGGER.error("ModuleTest::readFirstLine()", e);
} }
} }
} }
return firstLine; return firstLine;
} }
} }