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