Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-16301] Verifying current load/save implementation can round-…
…trip fingerprints.
  • Loading branch information
jglick committed May 22, 2013
1 parent 63ecb82 commit 41edc58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/hudson/model/Fingerprint.java
Expand Up @@ -578,7 +578,7 @@ public void onRenamed(Item item, String oldName, String newName) {
*/
private final Hashtable<String,RangeSet> usages = new Hashtable<String,RangeSet>();

private PersistedList<FingerprintFacet> facets = new PersistedList<FingerprintFacet>(this);
PersistedList<FingerprintFacet> facets = new PersistedList<FingerprintFacet>(this);

/**
* Lazily computed immutable {@link FingerprintFacet}s created from {@link TransientFingerprintFacetFactory}.
Expand Down Expand Up @@ -712,15 +712,19 @@ public synchronized void add(AbstractBuild b) throws IOException {
* Records that a build of a job has used this file.
*/
public synchronized void add(String jobFullName, int n) throws IOException {
synchronized(usages) {
addWithoutSaving(jobFullName, n);
save();
}

void addWithoutSaving(String jobFullName, int n) {
synchronized(usages) { // XXX why not synchronized (this) like some, though not all, other accesses?
RangeSet r = usages.get(jobFullName);
if(r==null) {
r = new RangeSet();
usages.put(jobFullName,r);
}
r.add(n);
}
save();
}

/**
Expand Down
45 changes: 45 additions & 0 deletions core/src/test/java/hudson/model/FingerprintTest.java
Expand Up @@ -23,15 +23,21 @@
*/
package hudson.model;

import hudson.Util;
import hudson.model.Fingerprint.RangeSet;
import java.io.File;
import jenkins.model.FingerprintFacet;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* @author Kohsuke Kawaguchi
*/
public class FingerprintTest {

@Rule public TemporaryFolder tmp = new TemporaryFolder();

@Test public void rangeSet() {
RangeSet rs = new RangeSet();
Expand Down Expand Up @@ -128,4 +134,43 @@ public class FingerprintTest {
Fingerprint.load(new File(FingerprintTest.class.getResource("fingerprint.xml").toURI())).toString());
}

@Test public void roundTrip() throws Exception {
Fingerprint f = new Fingerprint(new Fingerprint.BuildPtr("foo", 13), "stuff.jar", SOME_MD5);
f.addWithoutSaving("some", 1);
f.addWithoutSaving("some", 2);
f.addWithoutSaving("some", 3);
f.addWithoutSaving("some", 10);
f.addWithoutSaving("other", 6);
File xml = tmp.newFile();
f.save(xml);
Fingerprint f2 = Fingerprint.load(xml);
assertEquals(f.toString(), f2.toString());
f.facets.setOwner(Saveable.NOOP);
f.facets.add(new TestFacet(f, 123, "val"));
f.save(xml);
//System.out.println(FileUtils.readFileToString(xml));
f2 = Fingerprint.load(xml);
assertEquals(f.toString(), f2.toString());
assertEquals(1, f2.facets.size());
TestFacet facet = (TestFacet) f2.facets.get(0);
assertEquals(f2, facet.getFingerprint());
}
private static byte[] toByteArray(String md5sum) {
byte[] data = new byte[16];
for( int i=0; i<md5sum.length(); i+=2 )
data[i/2] = (byte)Integer.parseInt(md5sum.substring(i,i+2),16);
return data;
}
private static final byte[] SOME_MD5 = toByteArray(Util.getDigestOf("whatever"));
public static final class TestFacet extends FingerprintFacet {
final String property;
public TestFacet(Fingerprint fingerprint, long timestamp, String property) {
super(fingerprint, timestamp);
this.property = property;
}
@Override public String toString() {
return "TestFacet[" + property + "@" + getTimestamp() + "]";
}
}

}

0 comments on commit 41edc58

Please sign in to comment.