diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index f2f1e185a9..9747b78d4a 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -1874,6 +1874,19 @@ public final class Hudson extends Node implements ItemGroup, Stapl * if the project of the given name already exists. */ public synchronized TopLevelItem createProject( TopLevelItemDescriptor type, String name ) throws IOException { + return createProject(type, name, true); + } + + /** + * Creates a new job. + * @param type Descriptor for job type + * @param name Name for job + * @param notify Whether to fire onCreated method for all ItemListeners + * @throws IllegalArgumentException + * if a project of the give name already exists. + */ + public synchronized TopLevelItem createProject( TopLevelItemDescriptor type, String name, boolean notify ) + throws IOException { if(items.containsKey(name)) throw new IllegalArgumentException(); @@ -1886,6 +1899,11 @@ public final class Hudson extends Node implements ItemGroup, Stapl item.save(); items.put(name,item); + + if (notify) + for (ItemListener l : ItemListener.all()) + l.onCreated(item); + return item; } @@ -2447,16 +2465,13 @@ public final class Hudson extends Node implements ItemGroup, Stapl rsp.setStatus(HttpServletResponse.SC_OK); return result; } else { - // create empty job and redirect to the project config screen if(mode==null) { rsp.sendError(SC_BAD_REQUEST); return null; } + // create empty job and redirect to the project config screen result = createProject(Items.getDescriptor(mode), name); } - - for (ItemListener l : ItemListener.all()) - l.onCreated(result); } // send the browser to the config page @@ -2494,6 +2509,10 @@ public final class Hudson extends Node implements ItemGroup, Stapl // load it TopLevelItem result = (TopLevelItem)Items.load(this,configXml.getParentFile()); items.put(name,result); + + for (ItemListener l : ItemListener.all()) + l.onCreated(result); + return result; } catch (IOException e) { // if anything fails, delete the config file to avoid further confusion @@ -2514,7 +2533,7 @@ public final class Hudson extends Node implements ItemGroup, Stapl */ @SuppressWarnings({"unchecked"}) public T copy(T src, String name) throws IOException { - T result = (T)createProject(src.getDescriptor(),name); + T result = (T)createProject(src.getDescriptor(),name,false); // copy config Util.copyFile(Items.getConfigFile(src).getFile(),Items.getConfigFile(result).getFile()); diff --git a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java new file mode 100644 index 0000000000..768f148e9e --- /dev/null +++ b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java @@ -0,0 +1,68 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Alan Harder + * + * 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 hudson.model.listeners; + +import hudson.cli.CreateJobCommand; +import hudson.model.Item; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Collections; +import java.util.Locale; +import org.jvnet.hudson.test.HudsonTestCase; + +/** + * Tests for ItemListener events. + * @author Alan.Harder@sun.com + */ +public class ItemListenerTest extends HudsonTestCase { + private ItemListener listener; + private StringBuffer events = new StringBuffer(); + + @Override protected void setUp() throws Exception { + super.setUp(); + listener = new ItemListener() { + @Override public void onCreated(Item item) { + events.append('C'); + } + @Override public void onCopied(Item src, Item item) { + events.append('Y'); + } + }; + ItemListener.all().add(0, listener); + } + + public void testOnCreatedviaCLI() { + CreateJobCommand cmd = new CreateJobCommand(); + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(buf); + cmd.main(Collections.singletonList("testJob"), Locale.ENGLISH, + new ByteArrayInputStream(("" + + "").getBytes()), + out, out); + out.flush(); + assertNotNull("job should be created: " + buf, hudson.getItem("testJob")); + assertEquals("onCreated event should be triggered: " + buf, "C", events.toString()); + } +}