Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Jan 3, 2024
1 parent 458943b commit d32d525
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
import hudson.model.Action;
import hudson.model.AbstractProject;
import hudson.model.CauseAction;
import hudson.model.MockBuildableItem;
import hudson.model.MockWaitingItem;
import hudson.model.Queue;
import hudson.model.Queue.Item;
import hudson.model.Queue.WaitingItem;
import hudson.model.queue.CauseOfBlockage;

import java.util.ArrayList;
Expand Down Expand Up @@ -214,7 +215,7 @@ public void shouldNotBlockBuildableItem() {
"refs/changes/1/1/1");
Item item = createItem(patchsetCreated, new String[] {"slaveA", "slaveB", "slaveC"});

CauseOfBlockage cause = dispatcher.canRun(new Queue.BuildableItem((WaitingItem)item));
CauseOfBlockage cause = dispatcher.canRun(new MockBuildableItem(item));
assertNull("Build should not be blocked", cause);
}

Expand Down Expand Up @@ -779,6 +780,6 @@ private Item createItem(GerritCause gerritCause, String[] slaves) {
}
when(gerritTriggerMock.gerritSlavesToWaitFor(any(String.class))).thenReturn(gerritSlaves);
}
return new WaitingItem(Calendar.getInstance(), abstractProjectMock, actions);
return new MockWaitingItem(abstractProjectMock, actions);
}
}
49 changes: 49 additions & 0 deletions src/test/java/hudson/model/MockBuildableItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hudson.model;

import hudson.model.queue.CauseOfBlockage;

/**
* Mock version of {@link Queue.BuildableItem} (which is final) that avoids {@link jenkins.model.TransientActionFactory}.
*/
public class MockBuildableItem extends Queue.Item {

/**
* Create a new mock buildable item.
*
* @param item The item.
*/
public MockBuildableItem(Queue.Item item) {
super(item);
}

@SuppressWarnings("deprecation") // avoid TransientActionFactory
@Override
public <T extends Action> T getAction(Class<T> type) {
for (Action a : getActions()) {
if (type.isInstance(a)) {
return type.cast(a);
}
}
return null;
}

@Override
public boolean isBuildable() {
return true;
}

@Override
public CauseOfBlockage getCauseOfBlockage() {
throw new UnsupportedOperationException();
}

@Override
void enter(Queue q) {
throw new UnsupportedOperationException();
}

@Override
boolean leave(Queue q) {
throw new UnsupportedOperationException();
}
}
55 changes: 55 additions & 0 deletions src/test/java/hudson/model/MockWaitingItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hudson.model;

import hudson.model.queue.CauseOfBlockage;
import hudson.model.queue.FutureImpl;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
* Mock version of {@link Queue.WaitingItem} (which is final) that avoids {@link jenkins.model.TransientActionFactory}.
*/
public class MockWaitingItem extends Queue.Item {

private static final AtomicLong COUNTER = new AtomicLong(0);

/**
* Create a new mock waiting item.
*
* @param project The project.
* @param actions The actions.
*/
public MockWaitingItem(Queue.Task project, List<Action> actions) {
super(project, actions, COUNTER.incrementAndGet(), new FutureImpl(project));
}

@SuppressWarnings("deprecation") // avoid TransientActionFactory
@Override
public <T extends Action> T getAction(Class<T> type) {
for (Action a : getActions()) {
if (type.isInstance(a)) {
return type.cast(a);
}
}
return null;
}

@Override
public boolean isBuildable() {
return false;
}

@Override
public CauseOfBlockage getCauseOfBlockage() {
throw new UnsupportedOperationException();
}

@Override
void enter(Queue q) {
throw new UnsupportedOperationException();
}

@Override
boolean leave(Queue q) {
throw new UnsupportedOperationException();
}
}

0 comments on commit d32d525

Please sign in to comment.