0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
154N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.management;
0N/A
154N/Aimport java.io.ByteArrayOutputStream;
159N/Aimport java.io.File;
154N/Aimport java.io.IOException;
159N/Aimport java.io.PrintStream;
158N/Aimport java.util.Properties;
154N/Aimport org.junit.After;
154N/Aimport org.junit.Before;
89N/Aimport org.junit.Test;
0N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
0N/Aimport org.opensolaris.opengrok.util.FileUtilities;
0N/Aimport static org.junit.Assert.*;
0N/A
0N/Apublic class OGAgentTest {
92N/A
92N/A private Properties savedProperties;
92N/A private PrintStream savedSystemOut;
92N/A private PrintStream savedSystemErr;
92N/A private File logDir;
92N/A
154N/A @Before
92N/A public void setUp() {
0N/A savedProperties = (Properties) System.getProperties().clone();
92N/A savedSystemOut = System.out;
0N/A savedSystemErr = System.err;
0N/A }
92N/A
92N/A @After
92N/A public void tearDown() {
92N/A if (savedProperties != null) {
92N/A System.setProperties(savedProperties);
92N/A }
92N/A if (savedSystemOut != null) {
92N/A System.setOut(savedSystemOut);
92N/A }
92N/A if (savedSystemErr != null) {
92N/A System.setErr(savedSystemErr);
92N/A }
92N/A if (logDir != null && logDir.exists()) {
0N/A FileUtilities.removeDirs(logDir);
0N/A }
92N/A // TODO We should stop the agent here, so that it doesn't interfere
92N/A // with other tests. How can we do that?
92N/A }
92N/A
92N/A /**
92N/A * Create a directory we can use for logging. Will be stored in the field
92N/A * {@link #logDir}.
92N/A */
92N/A private void createLogDir() throws IOException {
92N/A assertNull(logDir);
92N/A logDir = FileUtilities.createTemporaryDirectory("logdir");
92N/A }
92N/A
92N/A /**
92N/A * Test that messages with too low severity are not printed to the console.
92N/A */
92N/A @Test
92N/A public void disableConsoleLogging() throws Exception {
154N/A
92N/A // Create a log directory
92N/A createLogDir();
92N/A String logDirName = logDir.getAbsolutePath();
92N/A System.setProperty("org.opensolaris.opengrok.management.logging.path",
92N/A logDirName);
154N/A
92N/A // Disable console logging for FINE messages
92N/A System.setProperty(
92N/A "org.opensolaris.opengrok.management.logging.consolelevel",
92N/A "INFO");
92N/A
92N/A // Redirect stdout and stderr
92N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
92N/A PrintStream printer = new PrintStream(baos, true);
92N/A System.setOut(printer);
92N/A System.setErr(printer);
92N/A
92N/A OGAgent.main(new String[0]); // ) oga = new OGAgent();
92N/A //oga.runOGA();
92N/A assertTrue(baos.toString().contains("Logging to " + logDirName));
92N/A baos.reset();
154N/A String loggedMessage = "Should go to console!";
154N/A String unloggedMessage = "Should not go to console";
154N/A OpenGrokLogger.getLogger().severe(loggedMessage);
154N/A OpenGrokLogger.getLogger().fine(unloggedMessage);
154N/A assertTrue(baos.toString().contains(loggedMessage));
154N/A assertFalse(baos.toString().contains(unloggedMessage));
154N/A }
154N/A}
154N/A