Skip to content
Permalink
Browse files

[JENKINS-43507] Refactoring MockSCM to traits

  • Loading branch information...
stephenc committed Apr 12, 2017
1 parent 37da6da commit a3485be06b4841f37d052ed99d7b188cd6f93815
@@ -0,0 +1,28 @@
package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.scm.api.SCMBuilder;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;

public class MockSCMBuilder extends SCMBuilder<MockSCMBuilder,MockSCM> {

private final MockSCMSource source;

public MockSCMBuilder(@NonNull MockSCMSource source, @NonNull SCMHead head,
@CheckForNull SCMRevision revision) {
super(MockSCM.class, head, revision);
this.source = source;
}

@Override
public MockSCM build() {
SCMRevision revision = getRevision();
return new MockSCM(source, getHead(),
revision instanceof MockSCMRevision || revision instanceof MockChangeRequestSCMRevision
? revision
: null);
}

}
@@ -0,0 +1,49 @@
package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.scm.SCMDescriptor;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceRequestBuilder;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

public class MockSCMDiscoverBranches extends SCMSourceTrait {

@DataBoundConstructor
public MockSCMDiscoverBranches() {
}

@Override
protected <B extends SCMSourceRequestBuilder<B, R>, R extends SCMSourceRequest> void decorateRequest(B builder) {
if (builder instanceof MockSCMSourceRequestBuilder) {
((MockSCMSourceRequestBuilder) builder).withBranches(true);
}
}

@Override
public boolean isCategoryEnabled(@NonNull SCMHeadCategory category) {
return category.isUncategorized();
}

@Extension
public static final class DescriptorImpl extends SCMSourceTraitDescriptor {

@Override
public String getDisplayName() {
return "";
}

@Override
public boolean isApplicableTo(Class<? extends SCMSourceRequestBuilder> requestBuilderClass) {
return MockSCMSourceRequestBuilder.class.isAssignableFrom(requestBuilderClass);
}

@Override
public boolean isApplicableTo(SCMDescriptor<?> scm) {
return scm instanceof MockSCM.DescriptorImpl;
}
}
}
@@ -0,0 +1,90 @@
package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.scm.SCMDescriptor;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceRequestBuilder;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.impl.ChangeRequestSCMHeadCategory;
import org.codehaus.plexus.util.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

public class MockSCMDiscoverChangeRequests extends SCMSourceTrait {

private final Set<ChangeRequestCheckoutStrategy> strategies;

public MockSCMDiscoverChangeRequests(Collection<ChangeRequestCheckoutStrategy> strategies) {
this.strategies =
strategies.isEmpty() ? EnumSet.of(ChangeRequestCheckoutStrategy.HEAD) : EnumSet.copyOf(strategies);
}

public MockSCMDiscoverChangeRequests(ChangeRequestCheckoutStrategy... strategies) {
this(Arrays.asList(strategies));
}

@DataBoundConstructor
public MockSCMDiscoverChangeRequests(String strategiesStr) {
this(fromString(strategiesStr));
}

private static Set<ChangeRequestCheckoutStrategy> fromString(String strategiesStr) {
Set<ChangeRequestCheckoutStrategy> strategies = EnumSet.noneOf(ChangeRequestCheckoutStrategy.class);
for (String s : StringUtils.split(strategiesStr, ", ")) {
try {
strategies.add(ChangeRequestCheckoutStrategy.valueOf(s.trim()));
} catch (IllegalArgumentException e) {
// ignore
}
}
return strategies;
}

public String getStrategiesStr() {
StringBuilder r = new StringBuilder();
for (ChangeRequestCheckoutStrategy s : strategies) {
r.append(s.name()).append(", ");
}
return r.toString();
}


@Override
protected <B extends SCMSourceRequestBuilder<B, R>, R extends SCMSourceRequest> void decorateRequest(B builder) {
if (builder instanceof MockSCMSourceRequestBuilder) {
((MockSCMSourceRequestBuilder) builder).withChangeRequests(true);
((MockSCMSourceRequestBuilder) builder).withCheckoutStrategies(strategies);
}
}

@Override
public boolean isCategoryEnabled(@NonNull SCMHeadCategory category) {
return category instanceof ChangeRequestSCMHeadCategory;
}

@Extension
public static final class DescriptorImpl extends SCMSourceTraitDescriptor {

@Override
public String getDisplayName() {
return "";
}

@Override
public boolean isApplicableTo(Class<? extends SCMSourceRequestBuilder> requestBuilderClass) {
return MockSCMSourceRequestBuilder.class.isAssignableFrom(requestBuilderClass);
}

@Override
public boolean isApplicableTo(SCMDescriptor<?> scm) {
return scm instanceof MockSCM.DescriptorImpl;
}
}
}
@@ -0,0 +1,50 @@
package jenkins.scm.impl.mock;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.scm.SCMDescriptor;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceRequestBuilder;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.impl.TagSCMHeadCategory;
import org.kohsuke.stapler.DataBoundConstructor;

public class MockSCMDiscoverTags extends SCMSourceTrait {

@DataBoundConstructor
public MockSCMDiscoverTags() {
}

@Override
protected <B extends SCMSourceRequestBuilder<B, R>, R extends SCMSourceRequest> void decorateRequest(B builder) {
if (builder instanceof MockSCMSourceRequestBuilder) {
((MockSCMSourceRequestBuilder) builder).withTags(true);
}
}

@Override
public boolean isCategoryEnabled(@NonNull SCMHeadCategory category) {
return category instanceof TagSCMHeadCategory;
}

@Extension
public static final class DescriptorImpl extends SCMSourceTraitDescriptor {

@Override
public String getDisplayName() {
return "";
}

@Override
public boolean isApplicableTo(Class<? extends SCMSourceRequestBuilder> requestBuilderClass) {
return MockSCMSourceRequestBuilder.class.isAssignableFrom(requestBuilderClass);
}

@Override
public boolean isApplicableTo(SCMDescriptor<?> scm) {
return scm instanceof MockSCM.DescriptorImpl;
}
}
}
@@ -28,47 +28,53 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import jenkins.scm.api.SCMHead;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMNavigatorDescriptor;
import jenkins.scm.api.SCMNavigatorEvent;
import jenkins.scm.api.SCMNavigatorOwner;
import jenkins.scm.api.SCMSourceObserver;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

public class MockSCMNavigator extends SCMNavigator {

private final String controllerId;
private final boolean includeBranches;
private final boolean includeTags;
private final boolean includeChangeRequests;
private final List<SCMSourceTrait> traits;
private transient MockSCMController controller;

@DataBoundConstructor
public MockSCMNavigator(String controllerId, boolean includeBranches, boolean includeTags,
boolean includeChangeRequests) {
public MockSCMNavigator(String controllerId, List<SCMSourceTrait> traits) {
this.controllerId = controllerId;
this.includeBranches = includeBranches;
this.includeTags = includeTags;
this.includeChangeRequests = includeChangeRequests;
this.traits = new ArrayList<SCMSourceTrait>(traits);
}

public MockSCMNavigator(MockSCMController controller, boolean includeBranches, boolean includeTags,
boolean includeChangeRequests) {
public MockSCMNavigator(String controllerId, SCMSourceTrait... traits) {
this(controllerId, Arrays.asList(traits));
}

public MockSCMNavigator(MockSCMController controller, List<SCMSourceTrait> traits) {
this.controllerId = controller.getId();
this.controller = controller;
this.includeBranches = includeBranches;
this.includeTags = includeTags;
this.includeChangeRequests = includeChangeRequests;
this.traits = new ArrayList<SCMSourceTrait>(traits);
}

public MockSCMNavigator(MockSCMController controller, SCMSourceTrait... traits) {
this(controller, Arrays.asList(traits));
}

public String getControllerId() {
@@ -82,16 +88,8 @@ private MockSCMController controller() {
return controller;
}

public boolean isIncludeBranches() {
return includeBranches;
}

public boolean isIncludeTags() {
return includeTags;
}

public boolean isIncludeChangeRequests() {
return includeChangeRequests;
public List<SCMSourceTrait> getTraits() {
return Collections.unmodifiableList(traits);
}

@Override
@@ -115,8 +113,7 @@ public void visitSources(@NonNull SCMSourceObserver observer) throws IOException
controller().applyLatency();
controller().checkFaults(name, null, null, false);
SCMSourceObserver.ProjectObserver po = observer.observe(name);
po.addSource(new MockSCMSource(getId() + "::" + name,
controller, name, includeBranches, includeTags, includeChangeRequests));
po.addSource(new MockSCMSource(getId() + "::" + name, controller, name, traits));
po.complete();
}
}
@@ -165,5 +162,29 @@ public ListBoxModel doFillControllerIdItems() {
}
return result;
}

public List<SCMSourceTraitDescriptor> getTraitDescriptors() {
MockSCM.DescriptorImpl scmDescriptor =
ExtensionList.lookup(Descriptor.class).get(MockSCM.DescriptorImpl.class);
List<SCMSourceTraitDescriptor> result = new ArrayList<SCMSourceTraitDescriptor>();
for (Descriptor<SCMSourceTrait> d : Jenkins.getActiveInstance().getDescriptorList(SCMSourceTrait.class)) {
if (d instanceof SCMSourceTraitDescriptor) {
SCMSourceTraitDescriptor descriptor = (SCMSourceTraitDescriptor) d;
if (!descriptor.isApplicableTo(
scmDescriptor)) {
continue;
}
if (!descriptor.isApplicableTo(MockSCMSourceRequestBuilder.class)) {
continue;
}
result.add(descriptor);
}
}
return result;
}

public List<SCMSourceTrait> getDefaultTraits() {
return Collections.<SCMSourceTrait>singletonList(new MockSCMDiscoverBranches());
}
}
}

0 comments on commit a3485be

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