Skip to content

Commit

Permalink
[JENKINS-45892] Enhanced diagnostics (#2997)
Browse files Browse the repository at this point in the history
* [JENKINS-45892] Enhanced diagnostics.

* Refined fix which should avoid a needless warning when called from MultiBranchProject.onLoad.
  • Loading branch information
jglick authored and oleg-nenashev committed Sep 3, 2017
1 parent 4d60ca6 commit a032c59
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions core/src/main/java/hudson/XmlFile.java
Expand Up @@ -120,6 +120,7 @@ public final class XmlFile {
private final XStream xs;
private final File file;
private static final Map<Object, Void> beingWritten = Collections.synchronizedMap(new IdentityHashMap<>());
private static final ThreadLocal<File> writing = new ThreadLocal<>();

public XmlFile(File file) {
this(DEFAULT_XSTREAM,file);
Expand Down Expand Up @@ -175,10 +176,12 @@ public void write( Object o ) throws IOException {
try {
w.write("<?xml version='1.0' encoding='UTF-8'?>\n");
beingWritten.put(o, null);
writing.set(file);
try {
xs.toXML(o, w);
} finally {
beingWritten.remove(o);
writing.set(null);
}
w.commit();
} catch(StreamException e) {
Expand All @@ -200,11 +203,11 @@ public void write( Object o ) throws IOException {
* @since 2.74
*/
public static Object replaceIfNotAtTopLevel(Object o, Supplier<Object> replacement) {
if (beingWritten.containsKey(o)) {
File currentlyWriting = writing.get();
if (beingWritten.containsKey(o) || currentlyWriting == null) {
return o;
} else {
// Unfortunately we cannot easily tell which XML file is actually being saved here, at least without implementing a custom Converter.
LOGGER.log(Level.WARNING, "JENKINS-45892: reference to {0} being saved but not at top level", o);
LOGGER.log(Level.WARNING, "JENKINS-45892: reference to " + o + " being saved from unexpected " + currentlyWriting, new IllegalStateException());
return replacement.get();
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/src/test/java/hudson/model/AbstractItem2Test.java
Expand Up @@ -23,20 +23,26 @@
*/
package hudson.model;

import hudson.XmlFile;
import java.util.logging.Level;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class AbstractItem2Test {

@Rule
public RestartableJenkinsRule rr = new RestartableJenkinsRule();

@Rule
public LoggerRule logging = new LoggerRule().record(XmlFile.class, Level.WARNING).capture(100);

@Issue("JENKINS-45892")
@Test
public void badSerialization() {
Expand All @@ -50,6 +56,8 @@ public void evaluate() throws Throwable {
String text = p2.getConfigFile().asString();
assertThat(text, not(containsString("<description>this is p1</description>")));
assertThat(text, containsString("<fullName>p1</fullName>"));
assertThat(logging.getMessages().toString(), containsString(p1.toString()));
assertThat(logging.getMessages().toString(), containsString(p2.getConfigFile().toString()));
}
});
rr.addStep(new Statement() {
Expand Down

0 comments on commit a032c59

Please sign in to comment.