Skip to content
Permalink
Browse files

Merge pull request #19 from amuniz/getScmSources-nullpointer

[JENKINS-31432] NPE on SCMSourceOwner.getSCMSources() call
  • Loading branch information...
jglick committed Nov 9, 2015
2 parents 9c4bd01 + 01ef481 commit ed73d403bbd67ae93da743bbdfd653397468cee4
@@ -129,6 +129,12 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.24</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
@@ -187,7 +187,12 @@ public synchronized void setProjectFactory(BranchProjectFactory<P, R> projectFac
*/
@NonNull
public List<BranchSource> getSources() {
return sources.toList();
if (sources != null) {
return sources.toList();
} else {
// return empty, this object is still being constructed
return Collections.emptyList();
}
}

/**
@@ -205,9 +210,11 @@ public synchronized void setProjectFactory(BranchProjectFactory<P, R> projectFac
@NonNull
public List<SCMSource> getSCMSources() {
List<SCMSource> result = new ArrayList<SCMSource>();
for (BranchSource source : sources) {
result.add(source.getSource());
}
if (sources != null) {
for (BranchSource source : sources) {
result.add(source.getSource());
}
} // else, ok to ignore and return empty, this object is still being constructed
return result;
}

@@ -0,0 +1,68 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.branch;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import hudson.model.Item;
import hudson.model.listeners.ItemListener;
import jenkins.branch.harness.MultiBranchImpl;
import jenkins.scm.api.SCMSourceOwner;

public class MultibranchImplementationTest {

@Rule
public JenkinsRule j = new JenkinsRule();

public static Exception check = null;

@Test
public void createMultiBranchProjectWithListenerTest() throws Exception {
j.jenkins.createProject(MultiBranchImpl.class, "test");
if (check != null) {
throw check;
}
}

@TestExtension
public static class SCMSourceOwnerListener extends ItemListener {

@Override
public void onUpdated(Item item) {
if (item instanceof SCMSourceOwner) {
try {
((SCMSourceOwner) item).getSCMSources();
} catch (NullPointerException e) {
check = new Exception(e);
}
}
}

}

}
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.branch.harness;

import hudson.Extension;
import hudson.model.FreeStyleProject;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import jenkins.branch.Branch;

public class BranchProperty extends JobProperty<FreeStyleProject> {

private Branch b;

public BranchProperty(Branch b) {
this.b = b;
}

public Branch getBranch() {
return b;
}

@Override
public JobPropertyDescriptor getDescriptor() {
return new DescriptorImpl();
}

@Extension
public static class DescriptorImpl extends JobPropertyDescriptor {

@Override
public String getDisplayName() {
return "Branch property";
}
}
}
@@ -0,0 +1,110 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.branch.harness;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

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

import hudson.Extension;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.TopLevelItem;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import jenkins.branch.Branch;
import jenkins.branch.BranchProjectFactory;
import jenkins.branch.MultiBranchProject;
import jenkins.branch.MultiBranchProjectDescriptor;

public class MultiBranchImpl extends MultiBranchProject<FreeStyleProject, FreeStyleBuild> {


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

@Override
protected BranchProjectFactory<FreeStyleProject, FreeStyleBuild> newProjectFactory() {
return new BranchProjectFactoryImpl();
}

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

@Override
public FreeStyleProject newInstance(Branch branch) {
FreeStyleProject job = new FreeStyleProject(getOwner(), branch.getName());
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);
setBranch(spied, branch);
return spied;
}

@Override
public Branch getBranch(FreeStyleProject project) {
return project.getProperty(BranchProperty.class).getBranch();
}

@Override
public FreeStyleProject setBranch(FreeStyleProject project, Branch branch) {
try {
project.addProperty(new BranchProperty(branch));
} catch (IOException e) {
e.printStackTrace();
}
return project;
}

@Override
public boolean isProject(Item item) {
return item instanceof FreeStyleProject && ((FreeStyleProject) item).getProperty(BranchProperty.class) != null;
}

}

@Extension
public static class DescriptorImpl extends MultiBranchProjectDescriptor {

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

@Override
public TopLevelItem newInstance(ItemGroup parent, String name) {
return new MultiBranchImpl(parent, name);
}

@Override
public List<SCMDescriptor<?>> getSCMDescriptors() {
return SCM.all();
}
}
}

0 comments on commit ed73d40

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