Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PCT failures #199

Merged
merged 7 commits into from
Jul 8, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -76,35 +76,39 @@ public void removeIntegrationTestMocks() throws Exception {

@Test
public void noNavigatorNoFactoryInstalled() throws Exception {
assertEquals(1, ExtensionList.lookup(SCMNavigatorDescriptor.class).size());
assertEquals(SingleSCMNavigator.DescriptorImpl.class, ExtensionList.lookup(SCMNavigatorDescriptor.class).get(0).getClass());
assertEquals(0, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size());
assertThat(ExtensionList.lookup(SCMNavigatorDescriptor.class),
hasItem(ExtensionList.lookupSingleton(SingleSCMNavigator.DescriptorImpl.class)));
assertEquals(0, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size()); // FIXME fails in PCT
chriskilding marked this conversation as resolved.
Show resolved Hide resolved
assertThat(newItemTypes(), containsInAnyOrder());
jglick marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void someNavigatorNoFactoryInstalled() throws Exception {
assertEquals(2, ExtensionList.lookup(SCMNavigatorDescriptor.class).size());
assertEquals(0, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size());
assertThat(ExtensionList.lookup(SCMNavigatorDescriptor.class),
hasItems(ExtensionList.lookupSingleton(SingleSCMNavigator.DescriptorImpl.class), ExtensionList.lookupSingleton(SomeNavigatorNoFactoryInstalledDescriptor.class)));
assertEquals(0, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size()); // FIXME fails in PCT
assertThat(newItemTypes(), containsInAnyOrder());
}
@TestExtension("someNavigatorNoFactoryInstalled")
public static class SomeNavigatorNoFactoryInstalledDescriptor extends MockNavigatorDescriptor {}

@Test
public void noNavigatorSomeFactoryInstalled() throws Exception {
assertEquals(1, ExtensionList.lookup(SCMNavigatorDescriptor.class).size());
assertEquals(SingleSCMNavigator.DescriptorImpl.class, ExtensionList.lookup(SCMNavigatorDescriptor.class).get(0).getClass());
assertEquals(1, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size());
assertThat(ExtensionList.lookup(SCMNavigatorDescriptor.class),
hasItem(ExtensionList.lookupSingleton(SingleSCMNavigator.DescriptorImpl.class)));
jglick marked this conversation as resolved.
Show resolved Hide resolved
assertThat(ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class),
hasItem(ExtensionList.lookupSingleton(NoNavigatorSomeFactoryInstalledDescriptor.class)));
assertThat(newItemTypes(), containsInAnyOrder());
}
@TestExtension("noNavigatorSomeFactoryInstalled")
public static class NoNavigatorSomeFactoryInstalledDescriptor extends OrganizationFolderTest.MockFactoryDescriptor {}

@Test
public void someNavigatorSomeFactoryInstalled() throws Exception {
assertEquals(2, ExtensionList.lookup(SCMNavigatorDescriptor.class).size());
assertEquals(1, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).size());
assertThat(ExtensionList.lookup(SCMNavigatorDescriptor.class),
hasItems(ExtensionList.lookupSingleton(SingleSCMNavigator.DescriptorImpl.class), ExtensionList.lookupSingleton(SomeNavigatorSomeFactoryInstalledDescriptor1.class)));
assertThat(ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class),
hasItem(ExtensionList.lookupSingleton(SomeNavigatorSomeFactoryInstalledDescriptor2.class)));
assertThat(newItemTypes(), containsInAnyOrder("MockNavigator"));
}
@TestExtension("someNavigatorSomeFactoryInstalled")
Original file line number Diff line number Diff line change
@@ -26,13 +26,15 @@

import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetric;
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetricDescriptor;
import hudson.ExtensionList;
import hudson.model.TopLevelItem;
import hudson.util.DescribableList;
import integration.harness.BasicMultiBranchProject;
import integration.harness.BasicMultiBranchProjectFactory;
import integration.harness.BasicSCMSourceCriteria;
import java.util.Collections;
import java.util.List;

import jenkins.scm.api.SCMSource;
import jenkins.scm.impl.SingleSCMNavigator;
import jenkins.scm.impl.SingleSCMSource;
@@ -47,13 +49,9 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.*;

public class OrganizationChildHealthMetricsPropertyTest {
@ClassRule
@@ -72,8 +70,7 @@ public void configRoundTrip() throws Exception {
c.createRepository("stuff");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = prj.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(OrganizationFolderTest.MockFactory.class, projectFactories.get(0).getClass());
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
projectFactories.add(new OrganizationFolderTest.MockFactory());
prj.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy",
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
package jenkins.branch;

import com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy;
import hudson.ExtensionList;
import hudson.model.TopLevelItem;
import integration.harness.BasicMultiBranchProject;
import integration.harness.BasicMultiBranchProjectFactory;
@@ -45,11 +46,10 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.hasItem;

public class OrganizationChildOrphanedItemsPropertyTest {
@ClassRule
@@ -68,8 +68,7 @@ public void configRoundTrip() throws Exception {
c.createRepository("stuff");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = prj.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(OrganizationFolderTest.MockFactory.class, projectFactories.get(0).getClass());
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
projectFactories.add(new OrganizationFolderTest.MockFactory());
prj.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy",
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
package jenkins.branch;

import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger;
import hudson.ExtensionList;
import hudson.model.TopLevelItem;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
@@ -49,12 +50,9 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;

public class OrganizationChildTriggersPropertyTest {
@@ -74,8 +72,7 @@ public void configRoundTrip() throws Exception {
c.createRepository("stuff");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = prj.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(OrganizationFolderTest.MockFactory.class, projectFactories.get(0).getClass());
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
projectFactories.add(new OrganizationFolderTest.MockFactory());
prj.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy",
15 changes: 5 additions & 10 deletions src/test/java/jenkins/branch/OrganizationFolderTest.java
Original file line number Diff line number Diff line change
@@ -62,10 +62,9 @@
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;

import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -81,8 +80,7 @@ public void configRoundTrip() throws Exception {
c.createRepository("stuff");
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
projectFactories.add(new MockFactory());
top.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy",
@@ -94,9 +92,7 @@ public void configRoundTrip() throws Exception {
assertEquals(SingleSCMNavigator.class, navigators.get(0).getClass());
assertEquals("stuff", ((SingleSCMNavigator) navigators.get(0)).getName());
projectFactories = top.getProjectFactories();
assertEquals(2, projectFactories.size());
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
assertEquals(MockFactory.class, projectFactories.get(1).getClass());
assertThat(projectFactories, containsInAnyOrder(instanceOf(MockFactory.class), instanceOf(MockFactory.class)));
chriskilding marked this conversation as resolved.
Show resolved Hide resolved
}
}

@@ -133,8 +129,7 @@ public void emptyViewEquality() throws Exception {
public void deletedMarker() throws Exception {
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
top.getNavigators().add(new SingleSCMNavigator("stuff", Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy", new NullSCM()))));
top.scheduleBuild2(0).getFuture().get();
top.getComputation().writeWholeLogTo(System.out);
56 changes: 56 additions & 0 deletions src/test/java/jenkins/branch/matchers/Extracting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package jenkins.branch.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class Extracting<Element, Property> extends TypeSafeDiagnosingMatcher<Iterable<Element>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer to not add a utility like this to this specific plugin. I have wanted something like this occasionally though; perhaps it could be added to jenkins-test-harness.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private final Function<? super Element, Property> extractor;
private final Matcher<?> nextMatcher;

private Extracting(Function<? super Element, Property> extractor, Matcher<?> nextMatcher) {
this.extractor = extractor;
this.nextMatcher = nextMatcher;
}

@Override
protected boolean matchesSafely(Iterable<Element> inColl, Description mismatchDescription) {
final Iterable<Property> collection = StreamSupport.stream(inColl.spliterator(), false)
.map(extractor)
.collect(Collectors.toList());

final boolean matches = nextMatcher.matches(collection);

if (!matches) {
nextMatcher.describeMismatch(collection, mismatchDescription);
}
return matches;
}


@Override
public void describeTo(Description description) {
description
.appendDescriptionOf(nextMatcher);
}

/**
* Extracts the specified property from a collection of things, and then lets you assert on the extracted properties. (The analog of AssertJ's <code>assertThat(things).extracting("foo")</code>.)
* <p>
* Use this in a matcher chain. For example, if you have list of POJOs with a property 'foo', you could write <code>assertThat(list, extracting(t -> t.getFoo(), hasItems("a", "b")))</code>.
jglick marked this conversation as resolved.
Show resolved Hide resolved
*
* @param <Element> type of the elements in the original collection
* @param <Property> type of extracted property
* @param extractor name of the property to extract from the collection
* @param nextMatcher matcher to test the extracted property's value(s)
* @return the extracting matcher
*/
public static <Element, Property> Matcher<Iterable<Element>> extracting(Function<? super Element, Property> extractor, Matcher<?> nextMatcher) {
return new Extracting<>(extractor, nextMatcher);
}
}
64 changes: 64 additions & 0 deletions src/test/java/jenkins/branch/matchers/ExtractingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package jenkins.branch.matchers;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class ExtractingTest {

@Test
public void shouldExtractNothingFromEmptyIterable() {
final Iterable<Tuple> data = Collections.emptyList();

assertThat(data, extracting(t -> t.getFoo(), is(empty())));
}

@Test
public void shouldExtractProperty() {
final Iterable<Tuple> data = Arrays.asList(tuple("a",1), tuple("b", 2));

assertThat(data, extracting(t -> t.getFoo(), hasItem("a")));
}

@Test
public void shouldExtractPropertyFromMultipleObjects() {
final Iterable<Tuple> data = Arrays.asList(tuple("a",1), tuple("b", 2));

assertThat(data, extracting(t -> t.getFoo(), hasItems("a", "b")));
assertThat(data, extracting(t -> t.getBar(), hasItems(1, 2)));
}

@Test
public void shouldSupportPartialMatchers() {
final Iterable<Tuple> data = Arrays.asList(tuple("a",1), tuple("b", 2), tuple("c", 3));

assertThat(data, extracting(t -> t.getFoo(), hasItems("a", "c")));
}

private static Tuple tuple(String foo, int bar) {
return new Tuple(foo, bar);
}

private static class Tuple {
private final String foo;
private final int bar;

Tuple(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}

public String getFoo() {
return foo;
}

public int getBar() {
return bar;
}
}
}