Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #19 from amuniz/getScmSources-nullpointer
[JENKINS-31432] NPE on SCMSourceOwner.getSCMSources() call
- Loading branch information
Showing
5 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/jenkins/branch/MultibranchImplementationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
} | |||
} | |||
|
|||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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"; | |||
} | |||
} | |||
} |
110 changes: 110 additions & 0 deletions
110
src/test/java/jenkins/branch/harness/MultiBranchImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); | |||
} | |||
} | |||
} |