Search in sources :

Example 1 with Hudson

use of hudson.model.Hudson in project hudson-2.x by hudson.

the class SubClassGenerator method generate.

public <T> Class<? extends T> generate(Class<T> base, String name) {
    //?
    ClassWriter cw = new ClassWriter(false, false);
    cw.visit(49, ACC_PUBLIC, name.replace('.', '/'), null, Type.getInternalName(base), null);
    for (Constructor c : base.getDeclaredConstructors()) {
        Class[] et = c.getExceptionTypes();
        String[] exceptions = new String[et.length];
        for (int i = 0; i < et.length; i++) exceptions[i] = Type.getInternalName(et[i]);
        String methodDescriptor = getMethodDescriptor(c);
        MethodVisitor m = cw.visitMethod(c.getModifiers(), "<init>", methodDescriptor, null, exceptions);
        m.visitCode();
        int index = 1;
        m.visitVarInsn(ALOAD, 0);
        for (Class param : c.getParameterTypes()) {
            Type t = Type.getType(param);
            m.visitVarInsn(t.getOpcode(ILOAD), index);
            index += t.getSize();
        }
        m.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(base), "<init>", methodDescriptor);
        m.visitInsn(RETURN);
        m.visitMaxs(index, index);
        m.visitEnd();
    }
    cw.visitEnd();
    byte[] image = cw.toByteArray();
    Class<? extends T> c = defineClass(name, image, 0, image.length).asSubclass(base);
    Hudson h = Hudson.getInstance();
    if (// null only during tests.
    h != null)
        // can't change the field type as it breaks binary compatibility
        ((UberClassLoader) h.pluginManager.uberClassLoader).addNamedClass(name, c);
    return c;
}
Also used : Type(org.objectweb.asm.Type) Constructor(java.lang.reflect.Constructor) Hudson(hudson.model.Hudson) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with Hudson

use of hudson.model.Hudson in project hudson-2.x by hudson.

the class XStream2 method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) {
    // init() is too early to do this
    // defensive because some use of XStream happens before plugins are initialized.
    Hudson h = Hudson.getInstance();
    if (h != null && h.pluginManager != null && h.pluginManager.uberClassLoader != null) {
        setClassLoader(h.pluginManager.uberClassLoader);
    }
    Object o = super.unmarshal(reader, root, dataHolder);
    if (oldData.get() != null) {
        oldData.remove();
        if (o instanceof Saveable)
            OldDataMonitor.report((Saveable) o, "1.106");
    }
    return o;
}
Also used : Saveable(hudson.model.Saveable) Hudson(hudson.model.Hudson)

Example 3 with Hudson

use of hudson.model.Hudson in project hudson-2.x by hudson.

the class CascadingUtilTest method testRenameCascadingParentLinks.

@Test
@PrepareForTest(Hudson.class)
public void testRenameCascadingParentLinks() throws Exception {
    String oldName = "oldCascadingProject";
    String newName = "newCascadingProject";
    List<Job> jobs = new ArrayList<Job>();
    FreeStyleProject project1 = new FreeStyleProjectMock(oldName);
    FreeStyleProjectMock project2 = new FreeStyleProjectMock("child");
    project2.setCascadingProject(project1);
    jobs.add(project1);
    jobs.add(project2);
    mockStatic(Hudson.class);
    Hudson hudson = createMock(Hudson.class);
    expect(hudson.getAllItems(Job.class)).andReturn(jobs);
    expect(Hudson.getInstance()).andReturn(hudson);
    replay(Hudson.class, hudson);
    CascadingUtil.renameCascadingParentLinks(oldName, newName);
    verify(Hudson.class, hudson);
    assertEquals(newName, project2.getCascadingProjectName());
}
Also used : FreeStyleProjectMock(hudson.model.FreeStyleProjectMock) Hudson(hudson.model.Hudson) ArrayList(java.util.ArrayList) Job(hudson.model.Job) FreeStyleProject(hudson.model.FreeStyleProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with Hudson

use of hudson.model.Hudson in project hudson-2.x by hudson.

the class CascadingUtilTest method testGetAllItems.

@Test
@PrepareForTest(Hudson.class)
public void testGetAllItems() throws Exception {
    FreeStyleProject project1 = new FreeStyleProjectMock("p1");
    FreeStyleProjectMock project2 = new FreeStyleProjectMock("p2");
    FreeStyleProjectMock project3 = new FreeStyleProjectMock("p3");
    FreeStyleProjectMock project4 = new FreeStyleProjectMock("p4");
    List<Job> jobs = new ArrayList<Job>();
    jobs.add(project1);
    jobs.add(project2);
    jobs.add(project3);
    jobs.add(project4);
    Hudson hudson = createMock(Hudson.class);
    mockStatic(Hudson.class);
    expect(hudson.getAllItems(Job.class)).andReturn(jobs).anyTimes();
    expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
    replay(Hudson.class, hudson);
    project2.setCascadingProject(project1);
    CascadingUtil.linkCascadingProjectsToChild(project1, "p2");
    project3.setCascadingProject(project1);
    CascadingUtil.linkCascadingProjectsToChild(project1, "p3");
    project4.setCascadingProject(project3);
    CascadingUtil.linkCascadingProjectsToChild(project3, "p4");
    List<Job> jobs1 = CascadingUtil.getCascadingParents(Job.class, project1);
    List<Job> jobs2 = CascadingUtil.getCascadingParents(Job.class, project2);
    List<Job> jobs3 = CascadingUtil.getCascadingParents(Job.class, project3);
    List<Job> jobs4 = CascadingUtil.getCascadingParents(Job.class, project4);
    verify(Hudson.class, hudson);
    assertEquals(0, jobs1.size());
    assertFalse(jobs1.contains(project1));
    assertFalse(jobs1.contains(project2));
    assertFalse(jobs1.contains(project3));
    assertFalse(jobs1.contains(project4));
    assertEquals(3, jobs2.size());
    assertTrue(jobs2.contains(project1));
    assertFalse(jobs2.contains(project2));
    assertTrue(jobs2.contains(project3));
    assertTrue(jobs2.contains(project4));
    assertEquals(2, jobs3.size());
    assertTrue(jobs3.contains(project1));
    assertTrue(jobs3.contains(project2));
    assertFalse(jobs3.contains(project3));
    assertFalse(jobs3.contains(project4));
    assertEquals(3, jobs4.size());
    assertTrue(jobs4.contains(project1));
    assertTrue(jobs4.contains(project2));
    assertTrue(jobs4.contains(project3));
    assertFalse(jobs4.contains(project4));
}
Also used : FreeStyleProjectMock(hudson.model.FreeStyleProjectMock) Hudson(hudson.model.Hudson) ArrayList(java.util.ArrayList) FreeStyleProject(hudson.model.FreeStyleProject) Job(hudson.model.Job) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with Hudson

use of hudson.model.Hudson in project hudson-2.x by hudson.

the class LegacyMatrixConfigurationTest method testLoadLegacyMatrixProject.

/**
     * Tests unmarshalls MatrixProject configuration and checks whether properties are loaded correctly.
     *
     * @throws Exception if any.
     */
@Test
@Ignore
public void testLoadLegacyMatrixProject() throws Exception {
    Hudson hudson = createMock(Hudson.class);
    expect(hudson.getNodes()).andReturn(Lists.<Node>newArrayList()).anyTimes();
    expect(hudson.getInitLevel()).andReturn(InitMilestone.STARTED).anyTimes();
    expect(hudson.getRootDir()).andReturn(new File("./target/matrix")).anyTimes();
    ExtensionList<TransientProjectActionFactory> actionList = ExtensionList.create(hudson, TransientProjectActionFactory.class);
    expect(hudson.getExtensionList(TransientProjectActionFactory.class)).andReturn(actionList).anyTimes();
    ExtensionList<SaveableListener> saveableListenerList = ExtensionList.create(hudson, SaveableListener.class);
    expect(hudson.getExtensionList(SaveableListener.class)).andReturn(saveableListenerList).anyTimes();
    expect(hudson.getAllItems(MatrixConfiguration.class)).andReturn(Lists.<MatrixConfiguration>newArrayList()).anyTimes();
    mockStatic(Hudson.class);
    expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
    replayAll();
    MatrixProject project = (MatrixProject) Items.getConfigFile(config).read();
    project.setAllowSave(false);
    project.initProjectProperties();
    project.buildProjectProperties();
    verifyAll();
    assertEquals("/tmp/1", project.getProperty(AbstractProject.CUSTOM_WORKSPACE_PROPERTY_NAME).getValue());
    assertEquals(new Integer(7), CascadingUtil.getIntegerProjectProperty(project, AbstractProject.QUIET_PERIOD_PROPERTY_NAME).getValue());
    assertTrue(CascadingUtil.getBooleanProjectProperty(project, MatrixProject.RUN_SEQUENTIALLY_PROPERTY_NAME).getValue());
    assertEquals("!(label==\"win\" && DB==\"oracle\")", project.getProperty(MatrixProject.COMBINATION_FILTER_PROPERTY_NAME).getValue());
    assertEquals("label==\"unix\" && DB==\"mysql\"", project.getProperty(MatrixProject.TOUCH_STONE_COMBINATION_FILTER_PROPERTY_NAME).getValue());
    assertEquals(Result.SUCCESS, CascadingUtil.getResultProjectProperty(project, MatrixProject.TOUCH_STONE_RESULT_CONDITION_PROPERTY_NAME).getValue());
    assertEquals(new LogRotator(7, 7, 7, 7), CascadingUtil.getLogRotatorProjectProperty(project, MatrixProject.LOG_ROTATOR_PROPERTY_NAME).getValue());
    AxisList axes = CascadingUtil.getAxesListProjectProperty(project, MatrixProject.AXES_PROPERTY_NAME).getValue();
    assertEquals(2, axes.size());
    assertEquals("DB", axes.get(0).getName());
    assertEquals(2, axes.get(0).getValues().size());
    assertEquals("oracle", axes.get(0).getValues().get(0));
    assertEquals("mysql", axes.get(0).getValues().get(1));
    assertEquals("label", axes.get(1).getName());
    assertEquals(2, axes.get(1).getValues().size());
    assertEquals("unix", axes.get(1).getValues().get(0));
    assertEquals("win", axes.get(1).getValues().get(1));
    assertEquals(3, project.getActiveConfigurations().size());
    assertEquals(2, project.getActiveConfigurations().iterator().next().getCombination().size());
}
Also used : SaveableListener(hudson.model.listeners.SaveableListener) LogRotator(hudson.tasks.LogRotator) Hudson(hudson.model.Hudson) Node(hudson.model.Node) File(java.io.File) TransientProjectActionFactory(hudson.model.TransientProjectActionFactory) Ignore(org.junit.Ignore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Hudson (hudson.model.Hudson)43 FreeStyleProjectMock (hudson.model.FreeStyleProjectMock)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Job (hudson.model.Job)7 IOException (java.io.IOException)7 Before (org.junit.Before)7 FreeStyleProject (hudson.model.FreeStyleProject)5 AbstractProject (hudson.model.AbstractProject)4 File (java.io.File)3 Descriptor (hudson.model.Descriptor)2 TopLevelItem (hudson.model.TopLevelItem)2 UpdateSite (hudson.model.UpdateSite)2 CrumbIssuer (hudson.security.csrf.CrumbIssuer)2 Publisher (hudson.tasks.Publisher)2 JSONObject (net.sf.json.JSONObject)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 StaplerRequest (org.kohsuke.stapler.StaplerRequest)2 Provides (com.google.inject.Provides)1