Skip to content
Permalink
Browse files

Adding a test for JENKINS-31516

  • Loading branch information...
amuniz committed Dec 4, 2015
1 parent b234e4a commit 292188856f5e576306cc076154b4ae14116d0a75
@@ -26,17 +26,27 @@

import hudson.model.ItemGroup;
import hudson.model.TaskListener;
import jenkins.branch.harness.MultiBranchImpl;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.impl.SingleSCMNavigator;
import hudson.scm.NullSCM;
import hudson.util.RingBufferLogHandler;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import jenkins.scm.api.SCMSource;
import jenkins.scm.impl.SingleSCMSource;
import org.junit.Test;
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Rule;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
@@ -65,15 +75,31 @@ public void configRoundTrip() throws Exception {
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
assertEquals(MockFactory.class, projectFactories.get(1).getClass());
}
@TestExtension("configRoundTrip")

@Test
public void indexChildrenOnOrganizationFolderIndex() throws Exception {
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
projectFactories.add(new MockFactory());
top.getNavigators().add(new SingleSCMNavigator("stuff", Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy", new NullSCM()))));
top = r.configRoundtrip(top);

RingBufferLogHandler logs = createJULTestHandler(); // switch to the mock log handler

top.setDescription("Org folder test");
top = r.configRoundtrip(top);
waitForLogFileMessage("Indexing multibranch project: stuff", logs);
}

@TestExtension
public static class ConfigRoundTripDescriptor extends MockFactoryDescriptor {}

public static class MockFactory extends MultiBranchProjectFactory {
@DataBoundConstructor
public MockFactory() {}
@Override
public MultiBranchProject<?, ?> createProject(ItemGroup<?> parent, String name, List<? extends SCMSource> scmSources, Map<String,Object> attributes, TaskListener listener) throws IOException, InterruptedException {
return null;
return new MultiBranchImpl(parent, name);
}
}
static abstract class MockFactoryDescriptor extends MultiBranchProjectFactoryDescriptor {
@@ -90,4 +116,30 @@ public String getDisplayName() {
}
}

private RingBufferLogHandler createJULTestHandler() throws SecurityException, IOException {
RingBufferLogHandler handler = new RingBufferLogHandler();
SimpleFormatter formatter = new SimpleFormatter();
handler.setFormatter(formatter);
Logger logger = Logger.getLogger(MultiBranchImpl.class.getName());
logger.addHandler(handler);
return handler;
}

private void waitForLogFileMessage(String string, RingBufferLogHandler logs) throws IOException, InterruptedException {
File rootDir = r.jenkins.getRootDir();
synchronized (rootDir) {
int limit = 0;
while (limit < 5) {
rootDir.wait(1000);
for (LogRecord r : logs.getView()) {
if (r.getMessage().contains(string)) {
return;
}
}
limit++;
}
}
Assert.assertTrue("Expected log not found: " + string, false);
}

}
@@ -29,6 +29,7 @@

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import hudson.Extension;
import hudson.model.FreeStyleBuild;
@@ -45,8 +46,9 @@

public class MultiBranchImpl extends MultiBranchProject<FreeStyleProject, FreeStyleBuild> {

private static final Logger LOGGER = Logger.getLogger(MultiBranchImpl.class.getName());

protected MultiBranchImpl(ItemGroup parent, String name) {
public MultiBranchImpl(ItemGroup parent, String name) {
super(parent, name);
}

@@ -55,11 +57,18 @@ protected MultiBranchImpl(ItemGroup parent, String name) {
return new BranchProjectFactoryImpl();
}

@Override
public boolean scheduleBuild() {
LOGGER.info("Indexing multibranch project: " + getDisplayName());
return super.scheduleBuild();
}

public static class BranchProjectFactoryImpl extends BranchProjectFactory<FreeStyleProject, FreeStyleBuild> {

@Override
public FreeStyleProject newInstance(Branch branch) {
FreeStyleProject job = new FreeStyleProject(getOwner(), branch.getName());
job.onCreatedFromScratch();
FreeStyleProject spied = spy(job);
// Do nothing.. Running the actual build is not desired/required (and not possible) in this tests.
when(spied.scheduleBuild()).thenReturn(false);

0 comments on commit 2921888

Please sign in to comment.
You can’t perform that action at this time.