diff --git a/core/src/main/java/hudson/model/DisplayNameListener.java b/core/src/main/java/hudson/model/DisplayNameListener.java new file mode 100644 index 0000000000..4549ef1d5e --- /dev/null +++ b/core/src/main/java/hudson/model/DisplayNameListener.java @@ -0,0 +1,94 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2011, Yahoo!, 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 hudson.model; + + + +import java.io.IOException; +import java.util.logging.Logger; + +import hudson.Extension; +import hudson.model.listeners.ItemListener; + +/** + * @author kingfai + * + */ +@Extension +public class DisplayNameListener extends ItemListener { + + private final static Logger logger = Logger.getLogger(DisplayNameListener.class.getName()); + + @Override + /** + * Called after the user has clicked OK in the New Job page when + * Copy existing job has been selected. + * The fields in item will be displayed in when the config page is loaded + * displayed. + */ + public void onCopied(Item src, Item item) { + // bug 5056825 - Display name field should be cleared when you copy a job. + if(item instanceof AbstractItem) { + AbstractItem dest = (AbstractItem)item; + try { + dest.setDisplayName(null); + } + catch(IOException ioe) { + logger.warning(String.format("onCopied():Exception while trying to clear the displayName for Item.name:%s. Exception:%s", + item.getName(), ioe.toString())); + } + } + } + + @Override + /** + * Called after the user has changed the project name of a job and then + * clicked on submit. + * @param item The item that has been renamed. The new project name is already + * in item.getName() + * @param oldName the old name + * @param newName the new name + */ + public void onRenamed(Item item, String oldName, String newName) { + // bug 5077308 - Display name field should be cleared when you rename a job. + if(item instanceof AbstractItem) { + AbstractItem abstractItem = (AbstractItem)item; + if(oldName.equals(abstractItem.getDisplayName())) { + // the user renamed the job, but the old project name which is shown as the + // displayname if no displayname was set, has been set into the displayname field. + // This means that the displayname was never set, so we want to set it + // to null as it was before + try { + logger.info(String.format("onRenamed():Setting displayname to null for item.name=%s", item.getName())); + abstractItem.setDisplayName(null); + } + catch(IOException ioe) { + logger.warning(String.format("onRenamed():Exception while trying to clear the displayName for Item.name:%s. Exception:%s", + item.getName(), ioe.toString())); + } + } + } + } + +} diff --git a/core/src/test/java/hudson/model/DisplayNameListenerTest.java b/core/src/test/java/hudson/model/DisplayNameListenerTest.java new file mode 100644 index 0000000000..70973e7c21 --- /dev/null +++ b/core/src/test/java/hudson/model/DisplayNameListenerTest.java @@ -0,0 +1,78 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2011, Yahoo!, 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 hudson.model; + +import junit.framework.Assert; + +import org.junit.Test; + +public class DisplayNameListenerTest { + + @Test + public void testOnCopied() throws Exception { + DisplayNameListener listener = new DisplayNameListener(); + StubJob src = new StubJob(); + src.doSetName("src"); + StubJob dest = new StubJob(); + dest.doSetName("dest"); + dest.setDisplayName("this should be cleared"); + + // make sure the displayname and the name are different at this point + Assert.assertFalse(dest.getName().equals(dest.getDisplayName())); + + listener.onCopied(src, dest); + // make sure the displayname is equals to the name as it should be null + Assert.assertEquals(dest.getName(), dest.getDisplayName()); + } + + @Test + public void testOnRenamedOldNameEqualsDisplayName() throws Exception { + DisplayNameListener listener = new DisplayNameListener(); + final String oldName = "old job name"; + final String newName = "new job name"; + StubJob src = new StubJob(); + src.doSetName(newName); + src.setDisplayName(oldName); + + listener.onRenamed(src, oldName, newName); + + Assert.assertEquals(newName, src.getDisplayName()); + } + + @Test + public void testOnRenamedOldNameNotEqualDisplayName() throws Exception { + DisplayNameListener listener = new DisplayNameListener(); + final String oldName = "old job name"; + final String newName = "new job name"; + final String displayName = "the display name"; + StubJob src = new StubJob(); + src.doSetName(newName); + src.setDisplayName(displayName); + + listener.onRenamed(src, oldName, oldName); + + // make sure displayname is still intact + Assert.assertEquals(displayName, src.getDisplayName()); + } +} diff --git a/core/src/test/java/hudson/model/JobTest.java b/core/src/test/java/hudson/model/JobTest.java index 0edea06f25..8260d1b2b9 100644 --- a/core/src/test/java/hudson/model/JobTest.java +++ b/core/src/test/java/hudson/model/JobTest.java @@ -1,6 +1,5 @@ package hudson.model; -import java.util.SortedMap; import junit.framework.Assert; @@ -11,41 +10,7 @@ import org.mockito.Mockito; public class JobTest { private final String DISPLAY_NAME_PARAMETER_NAME = "displayName"; - private final String DEFAULT_STUB_JOB_NAME = "StubJob"; - @SuppressWarnings({ "unchecked", "rawtypes" }) - private class StubJob extends Job { - - public StubJob() { - super(null, DEFAULT_STUB_JOB_NAME); - } - - @Override - public boolean isBuildable() { - // TODO Auto-generated method stub - return false; - } - - @Override - protected SortedMap _getRuns() { - // TODO Auto-generated method stub - return null; - } - - @Override - protected void removeRun(Run run) { - // TODO Auto-generated method stub - - } - - /** - * Override save so that nothig happens when setDisplayName() is called - */ - @Override - public void save() { - - } - } @Test public void testSetDisplayName() throws Exception { @@ -73,6 +38,6 @@ public class JobTest { j.setDisplayNameFromRequest(req); // make sure the getDisplayName returns the project name - Assert.assertEquals(DEFAULT_STUB_JOB_NAME, j.getDisplayName()); + Assert.assertEquals(StubJob.DEFAULT_STUB_JOB_NAME, j.getDisplayName()); } } diff --git a/core/src/test/java/hudson/model/StubJob.java b/core/src/test/java/hudson/model/StubJob.java new file mode 100644 index 0000000000..7f8173b356 --- /dev/null +++ b/core/src/test/java/hudson/model/StubJob.java @@ -0,0 +1,67 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2011, Yahoo!, 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 hudson.model; + +import java.util.SortedMap; + +/** + * @author kingfai + * + */ +@SuppressWarnings({ "rawtypes", "unchecked" }) +class StubJob extends Job { + + public final static String DEFAULT_STUB_JOB_NAME = "StubJob"; + + public StubJob() { + super(null, DEFAULT_STUB_JOB_NAME); + } + + @Override + public boolean isBuildable() { + // TODO Auto-generated method stub + return false; + } + + @Override + protected SortedMap _getRuns() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected void removeRun(Run run) { + // TODO Auto-generated method stub + + } + + /** + * Override save so that nothig happens when setDisplayName() is called + */ + @Override + public void save() { + + } + +} diff --git a/test/src/test/java/hudson/model/DisplayNameTest.java b/test/src/test/java/hudson/model/DisplayNameTest.java new file mode 100644 index 0000000000..ba557e202d --- /dev/null +++ b/test/src/test/java/hudson/model/DisplayNameTest.java @@ -0,0 +1,90 @@ +/* + * The MIT License + * + * Copyright 2011 Yahoo!, 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 hudson.model; + +import jenkins.model.Jenkins; + +import org.junit.Assert; +import org.junit.Test; +import org.jvnet.hudson.test.HudsonTestCase; + +/** + * @author kingfai + * + */ +public class DisplayNameTest extends HudsonTestCase { + + @Test + public void testRenameJobWithNoDisplayName() throws Exception { + final String projectName = "projectName"; + final String newProjectName = "newProjectName"; + FreeStyleProject project = createFreeStyleProject(projectName); + Assert.assertEquals(projectName, project.getDisplayName()); + + project.renameTo(newProjectName); + Assert.assertEquals(newProjectName, project.getDisplayName()); + } + + @Test + public void testRenameJobWithDisplayName() throws Exception { + final String projectName = "projectName"; + final String newProjectName = "newProjectName"; + final String displayName = "displayName"; + FreeStyleProject project = createFreeStyleProject(projectName); + project.setDisplayName(displayName); + Assert.assertEquals(displayName, project.getDisplayName()); + + project.renameTo(newProjectName); + Assert.assertEquals(displayName, project.getDisplayName()); + } + + @SuppressWarnings("rawtypes") + @Test + public void testCopyJobWithNoDisplayName() throws Exception { + final String projectName = "projectName"; + final String newProjectName = "newProjectName"; + FreeStyleProject project = createFreeStyleProject(projectName); + Assert.assertEquals(projectName, project.getDisplayName()); + + AbstractProject newProject = Jenkins.getInstance().copy((AbstractProject)project, newProjectName); + Assert.assertEquals(newProjectName, newProject.getName()); + Assert.assertEquals(newProjectName, newProject.getDisplayName()); + } + + @SuppressWarnings("rawtypes") + @Test + public void testCopyJobWithDisplayName() throws Exception { + final String projectName = "projectName"; + final String newProjectName = "newProjectName"; + final String oldDisplayName = "oldDisplayName"; + FreeStyleProject project = createFreeStyleProject(projectName); + project.setDisplayName(oldDisplayName); + Assert.assertEquals(oldDisplayName, project.getDisplayName()); + + AbstractProject newProject = Jenkins.getInstance().copy((AbstractProject)project, newProjectName); + Assert.assertEquals(newProjectName, newProject.getName()); + Assert.assertEquals(newProjectName, newProject.getDisplayName()); + + } +}