Skip to content

Commit

Permalink
Fix JENKINS-28161
Browse files Browse the repository at this point in the history
This just fixes the NPE symptoms, but I'm afraid that
the reason that an event has a null value has some deeper issue somewhere.
  • Loading branch information
rsandell committed May 7, 2015
1 parent a9de653 commit d8577ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Expand Up @@ -60,11 +60,19 @@ public class BuildMemory {
*
* @author James E. Blair <jeblair@hp.com>
*/
private class GerritTriggeredEventComparator implements Comparator<GerritTriggeredEvent> {
static class GerritTriggeredEventComparator implements Comparator<GerritTriggeredEvent> {
@Override
public int compare(GerritTriggeredEvent o1, GerritTriggeredEvent o2) {
return new Integer(((java.lang.Object)o1).hashCode()).compareTo(
new Integer(((java.lang.Object)o2).hashCode()));
if (o1 == null && o2 == null) {
return 0;
}
if (o1 != null && o2 == null) {
return 1;
}
if (o1 == null && o2 != null) {
return -1;
}
return new Integer(o1.hashCode()).compareTo(o2.hashCode());
}
}

Expand Down
@@ -0,0 +1,55 @@
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model;

import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeMerged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Comparator;

import static org.junit.Assert.*;

/**
* Tests {@link com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.GerritTriggeredEventComparator}.
*
* @author Robert Sandell &lt;rsandell@cloudbees.com&gt;.
*/
@RunWith(Parameterized.class)
public class GerritTriggeredEventComparatorTest {

private final GerritTriggeredEvent o1;
private final GerritTriggeredEvent o2;
private final int expected;
private final Comparator<GerritTriggeredEvent> comparator;

@Parameterized.Parameters(name= "{index}: {0}, {1} == {2}")
public static Iterable<Object[]> permutations() {
PatchsetCreated event = Setup.createPatchsetCreated();
ChangeMerged merged = Setup.createChangeMerged();
return Arrays.asList(new Object[][]{
{null, null, 0},
{null, event, -1},
{event, null, 1},
{event, event, 0},
{event, merged, new Integer(event.hashCode()).compareTo(merged.hashCode())},
{merged, event, new Integer(merged.hashCode()).compareTo(event.hashCode())}
});
}

public GerritTriggeredEventComparatorTest(GerritTriggeredEvent o1, GerritTriggeredEvent o2, int expected) {
this.o1 = o1;
this.o2 = o2;
this.expected = expected;
comparator = new BuildMemory.GerritTriggeredEventComparator();
}

@Test
public void testCompare() throws Exception {
assertEquals(expected, comparator.compare(o1, o2));
}
}

0 comments on commit d8577ac

Please sign in to comment.