RE: Creating a FreeStyleProject outside JUnit?

2013-01-18 Thread jserup
My first  approach was to simply:

public class JenkinsApiTest {
  public void test()  {
ItemGroupItem itemGroup = new MyItemGroup();
FreeStyleProject p = new FreeStyleProject(itemGroup, n0);
p.getConfigFile().asString();
  }

this results in the AbstractProject constructor being called:

protected AbstractProject(ItemGroup parent, String name) {
super(parent,name);

if(!Jenkins.getInstance().getNodes().isEmpty()) {
// if a new job is configured with Hudson that already has slave
nodes
// make it roamable by default
canRoam = true;
}
}

which throws a NullPointerException most likely because a Jenkins instance
has not been created. I seems that creating types from hudson.model.*
package requires a running Jenkins instance and hence the whole
HudsonTestCase setup is necessary



--
View this message in context: 
http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JUnit-tp4651594p4651931.html
Sent from the Jenkins users mailing list archive at Nabble.com.


Re: Creating a FreeStyleProject outside JUnit?

2013-01-16 Thread jserup
Ok makes sense but is possible to create eg. a FreeStyleProject /outside/ a
Junit TestRunner. Seems a bit strange that this can only be done through a
TestRunner.



--
View this message in context: 
http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JUnit-tp4651594p4651665.html
Sent from the Jenkins users mailing list archive at Nabble.com.


Re: Creating a FreeStyleProject outside JUnit?

2013-01-15 Thread Dean Yu
IIRC, you need to use a Junit TestRunner to run the test, so that Junit's
test lifecycle is followed.

  -- Dean 

On 1/15/13 11:22 AM, jserup johs.se...@gmail.com wrote:

I am creating a FreeStyleProject using this test class:

import org.jvnet.hudson.test.HudsonTestCase;
import org.apache.commons.io.FileUtils;
import hudson.model.*;
import hudson.tasks.Shell;

public class AppTest extends HudsonTestCase
{ 
public void test1() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new Shell(echo hello));

FreeStyleBuild build = project.scheduleBuild2(0).get();
System.out.println(build.getDisplayName()+ completed);

// TODO: change this to use HtmlUnit
String s = FileUtils.readFileToString(build.getLogFile());
assertTrue(s.contains(+ echo hello));
} 
} 

From: 

https://wiki.jenkins-ci.org/display/JENKINS/Unit+Test

But is it only possible to create a FreeStyleProject (and other project
types) from a JUnit test? If I do:

public class Main {
public void runit() throws Exception{
AppTest at = new AppTest();
at.test1();
} 

public static void main(String[] args) throws Exception {
Main main = new Main();
main.runit();
} 
} 


I get: 

Exception in thread main java.lang.NullPointerException
at
org.jvnet.hudson.test.HudsonTestCase.createUniqueProjectName(HudsonTestCas
e.java:688) 
at
org.jvnet.hudson.test.HudsonTestCase.createFreeStyleProject(HudsonTestCase
.java:652) 
at com.build.jenkins.AppTest.test1(AppTest.java:10)
at com.build.jenkins.Main.runit(MMain.java:7)
at com.build.jenkins.Main.main(MMain.java:13)

I have also looked at: hudson.triggers.TriggerStartTest which use:

@Rule public JenkinsRule j = new JenkinsRule();

instead. But moving that outside a Junit test class gives an error similar
to the above. 


Its seems that its only possible to create objects from hudson.model.*
inside JUnit test or am I missing an alternative approach?

Here are the dependencies that I use:

dependency
groupIdorg.jenkins-ci.main/groupId
artifactIdjenkins-core/artifactId
version1.498/version
/dependency

dependency
groupIdorg.jenkins-ci.main/groupId
artifactIdjenkins-test-harness/artifactId
version1.498/version
/dependency

dependency
groupIdorg.jenkins-ci.main/groupId
version1.498/version
artifactIdjenkins-war/artifactId
typewar/type
/dependency
dependency
groupIdorg.jenkins-ci.main/groupId
version1.498/version
artifactIdjenkins-war/artifactId
classifierwar-for-test/classifier
/dependency

available from : 

http://repo.jenkins-ci.org/webapp/browserepo.html?6



--
View this message in context:
http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JU
nit-tp4651594.html
Sent from the Jenkins users mailing list archive at Nabble.com.