Skip to content

Commit

Permalink
JENKINS-51331 Fix JEP-200 issue in ConsoleAuditLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszsek committed May 24, 2018
1 parent 2eac152 commit 100da55
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/main/java/hudson/plugins/audit_trail/ConsoleAuditLogger.java
Expand Up @@ -16,43 +16,51 @@ public class ConsoleAuditLogger extends AuditLogger {
public enum Output {STD_OUT, STD_ERR}

private final Output output;
private final PrintStream out;
private final String dateFormat;
private final SimpleDateFormat sdf;
private transient PrintStream out;
private transient SimpleDateFormat sdf;


@DataBoundConstructor
public ConsoleAuditLogger(Output output, String dateFormat) {
if (output == null)
if (output == null) {
throw new NullPointerException("output can not be null");
if(dateFormat == null)
}
if (dateFormat == null) {
throw new NullPointerException("dateFormat can not be null");
}

this.output = output;
switch (output) {
case STD_ERR:
out = System.err;
break;
case STD_OUT:
out = System.out;
break;
default:
if (output != Output.STD_ERR && output != Output.STD_OUT) {
throw new IllegalArgumentException("Unsupported output " + output);
}

this.dateFormat = dateFormat;
sdf = new SimpleDateFormat(dateFormat);

// validate the dataFormat
new SimpleDateFormat(dateFormat);
}

@Override
public void log(String event) {
synchronized (sdf) {
synchronized (output) {
this.out.println(sdf.format(new Date()) + " - " + event);
}
}

@Override
public void configure() {

synchronized (output) {
switch (output) {
case STD_ERR:
out = System.err;
break;
case STD_OUT:
out = System.out;
break;
}
sdf = new SimpleDateFormat(dateFormat);
}
}

public Output getOutput() {
Expand Down
@@ -0,0 +1,67 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Alan Harder
*
* 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 hudson.plugins.audit_trail;

import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author <a href="mailto:tomasz.sek.88@gmail.com">Tomasz Sęk</a>
*/
public class ConsoleAuditLoggerTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Issue("JENKINS-51331")
@Test
public void shouldConfigureConsoleAuditLogger() throws Exception {
// Given
JenkinsRule.WebClient wc = j.createWebClient();
HtmlPage configure = wc.goTo("configure");
HtmlForm form = configure.getFormByName("config");
j.getButtonByCaption(form, "Add Logger").click();
configure.getAnchorByText("Console").click();
wc.waitForBackgroundJavaScript(2000);

// When
j.submit(form);

// Then
// submit configuration page without any errors
AuditTrailPlugin plugin = Jenkins.get().getPlugin(AuditTrailPlugin.class);
assertEquals("amount of loggers", 1, plugin.getLoggers().size());
AuditLogger logger = plugin.getLoggers().get(0);
assertTrue("ConsoleAuditLogger should be configured", logger instanceof ConsoleAuditLogger);
assertEquals("output", ConsoleAuditLogger.Output.STD_OUT, ((ConsoleAuditLogger) logger).getOutput());
}
}

1 comment on commit 100da55

@oleg-nenashev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't work reliably, see #10

Please sign in to comment.