[TAMAYA-164] Enabled a disabled unit test and added some JavaDoc.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/7c98c75e Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/7c98c75e Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/7c98c75e Branch: refs/heads/master Commit: 7c98c75e38d92465944f800deb0c34684f2b6be2 Parents: 09fda83 Author: Oliver B. Fischer <[email protected]> Authored: Sun Dec 4 16:23:49 2016 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sun Dec 4 16:23:49 2016 +0100 ---------------------------------------------------------------------- code/core/pom.xml | 17 ++++++ .../tamaya/core/internal/BannerManager.java | 59 ++++++++++++++------ .../tamaya/core/internal/BannerManagerTest.java | 33 ++++++----- .../src/test/resources/java-security.policy | 5 ++ pom.xml | 2 + 5 files changed, 87 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7c98c75e/code/core/pom.xml ---------------------------------------------------------------------- diff --git a/code/core/pom.xml b/code/core/pom.xml index 18e1986..5c7be94 100644 --- a/code/core/pom.xml +++ b/code/core/pom.xml @@ -67,4 +67,21 @@ under the License. </dependency> </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <forkCount>1</forkCount> + <!-- + ! Add -Djava.security.debug=all for debugging if needed + !--> + <argLine>-Djava.security.policy=${project.basedir}/src/test/resources/java-security.policy</argLine> + </configuration> + + </plugin> + </plugins> + </build> + </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7c98c75e/code/core/src/main/java/org/apache/tamaya/core/internal/BannerManager.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/BannerManager.java b/code/core/src/main/java/org/apache/tamaya/core/internal/BannerManager.java index 36906f7..52247c9 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/BannerManager.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/BannerManager.java @@ -20,18 +20,49 @@ package org.apache.tamaya.core.internal; import org.apache.tamaya.ConfigurationProvider; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Locale; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; +import static org.apache.tamaya.core.internal.BannerManager.BANNER_RESOURCE_PATH; + +/** + * Controls the output of the banner of Tamaya. + * + * <p>This class controls if and how the banner of Tamaya is presented the user. + * The banner is provided by the Tamaya Core under the resource path + * {@value BANNER_RESOURCE_PATH}.</p> + * + * <p>The behavior of the banner manager can be controlled by + * specifying the configuration key {@code tamaya.banner} with one of + * the three folowing values: + * + * <dl> + * <dt>OFF</dt> + * <dd>Banner will not be shown</dd> + * <dt>CONSOLE</dt> + * <dd>The banner will be printed on STDOUT</dd> + * <dt>LOGGER</dt> + * <dd>The banner will be logged</dd> + * </dl> + * + * In case of any other value the banner will not be shown. + * </p> + * + * + * + * @see BannerTarget + */ class BannerManager { + /** + * The resouce path to the file containing the banner of Tamaya. + */ + protected final static String BANNER_RESOURCE_PATH = "/tamaya-banner.txt"; + enum BannerTarget { OFF, CONSOLE, LOGGER } @@ -72,19 +103,15 @@ abstract class AbstractBannerPrinter implements BannerPrinter { @Override public void outputBanner() { - try { - URL url = ConfigurationProvider.class.getResource("/tamaya-banner.txt"); - - if (url != null) { - Path path = Paths.get(url.toURI()); - List<String> content = Files.readAllLines(path, StandardCharsets.UTF_8); + try (InputStream in = ConfigurationProvider.class.getResourceAsStream(BANNER_RESOURCE_PATH)) { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String line; - for (String line : content) { - outputSingleLine(line); - } + while ((line = reader.readLine()) != null) { + outputSingleLine(line); } } catch (Exception e) { - log.log(Level.FINE, "Failed to output the banner of tamaya.", e); + log.log(Level.WARNING, "Failed to output the banner of tamaya.", e); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7c98c75e/code/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java index 7e3c3bc..db8021c 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/BannerManagerTest.java @@ -18,31 +18,37 @@ */ package org.apache.tamaya.core.internal; -import org.junit.Ignore; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; import java.io.PrintStream; -import java.util.logging.Logger; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.Permission; -@RunWith(MockitoJUnitRunner.class) public class BannerManagerTest { - @Mock - private PrintStream printStream; - - @Mock - private Logger logger; - - @Ignore @Test public void valueConsoleSendsBannerToSystemOut() { + + SecurityManager sm = new SecurityManager(); + AccessControlContext con = AccessController.getContext(); + + Permission p = new RuntimePermission("setIO"); + + /* + * Here we check the precondition for this unit test + * and the correct setup of the test enviroment + * The JVM must have been started with + * -Djava.security.policy=<path_to_core_module</src/test/resources/java-security.policy + */ + sm.checkPermission(p, con); + PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); System.setOut(printStream); + standard.println("Changed stream for STDOUT successfully"); try { BannerManager bm = new BannerManager("console"); @@ -59,6 +65,7 @@ public class BannerManagerTest { public void invalidValueAvoidsLoggingToConsonle() { PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); System.setOut(printStream); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7c98c75e/code/core/src/test/resources/java-security.policy ---------------------------------------------------------------------- diff --git a/code/core/src/test/resources/java-security.policy b/code/core/src/test/resources/java-security.policy new file mode 100644 index 0000000..95b6ab3 --- /dev/null +++ b/code/core/src/test/resources/java-security.policy @@ -0,0 +1,5 @@ + +grant { + permission java.lang.RuntimePermission "setIO"; +}; + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7c98c75e/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 9c72c4c..48ba0fb 100644 --- a/pom.xml +++ b/pom.xml @@ -519,6 +519,8 @@ under the License. <exclude>**/*.json</exclude> <exclude>**/*.md</exclude> <exclude>**/*.md.vm</exclude> + <!-- Java Security Policy files can't have comments --> + <exclude>**/*.policy</exclude> <exclude>src/site/asciidoc/temp-properties-files-for-site/attributes.adoc</exclude> <exclude>readme/**</exclude> </excludes>
