Skip to content

Commit

Permalink
Test case to reproduce JENKINS-31432
Browse files Browse the repository at this point in the history
  • Loading branch information
amuniz committed Nov 6, 2015
1 parent 4d7ce28 commit 731b45e
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/jenkins/branch/MultibranchImplementationTest.java
@@ -0,0 +1,70 @@
/*
* 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 java.io.IOException;

import org.junit.Assert;
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 boolean check = true;

@Test
public void createMultiBranchProjectWithListenerTest() throws IOException {
j.jenkins.createProject(MultiBranchImpl.class, "test");
Assert.assertTrue("SCMSourceOwner.getSCMSources() should never throw NullPointerException", 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 = false;
throw e;
}
}
}

}

}
58 changes: 58 additions & 0 deletions src/test/java/jenkins/branch/harness/BranchProperty.java
@@ -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";
}
}
}
124 changes: 124 additions & 0 deletions src/test/java/jenkins/branch/harness/MultiBranchImpl.java
@@ -0,0 +1,124 @@
/*
* 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();
}

/*
@Override
public List<SCMSource> getSCMSources() {
if (getSourcesList() == null) {
// test code is generating a NullPointer when calling it from an ItemListener on the SCMSourceOwner
// It seems that the object is not fully initialized when the ItemListener uses it.
// Perhaps it needs to be reproduced and investigated in a branch-api test.
return new ArrayList<SCMSource>();
}
return super.getSCMSources();
}
*/

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);
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 731b45e

Please sign in to comment.