Skip to content
Permalink
Browse files

[FIXED JENKINS-18028] Providing a supported way to move an item betwe…

…en folders, firing changes as we go.

Originally-Committed-As: 9fc87f4a22ef4e32445451f5114fb800b6b73e9e
  • Loading branch information
jglick committed Dec 19, 2013
1 parent 6186e0b commit 9d53ebca9d228881bfe8b2c07b431cc81417e4f8
@@ -186,6 +186,10 @@ private ViewGroupMixIn vgmixin() {
}
}

@Override public void renameTo(String newName) throws IOException {
super.renameTo(newName); // just to make it public
}

@Override public void onDeleted(TopLevelItem item) throws IOException {
ItemListener.fireOnDeleted(item);
items.remove(item.getName());
@@ -206,6 +210,10 @@ private ViewGroupMixIn vgmixin() {
return item;
}

@Override public void remove(TopLevelItem item) throws IOException, IllegalArgumentException {
items.remove(item.getName());
}

@Override public TopLevelItemDescriptor getDescriptor() {
return Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
}
@@ -25,9 +25,12 @@
package org.jvnet.hudson.test;

import hudson.model.FreeStyleProject;
import org.junit.Test;
import hudson.model.Item;
import hudson.model.Items;
import hudson.model.listeners.ItemListener;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;

public class MockFolderTest {

@@ -39,4 +42,68 @@
assertEquals("dir/p", p.getFullName());
}

@Test public void moving() throws Exception {
MockFolder top = j.createFolder("top");
FreeStyleProject p = top.createProject(FreeStyleProject.class, "p");
MockFolder sub = top.createProject(MockFolder.class, "sub");
assertNews("created=top created=top/p created=top/sub");
Items.move(p, j.jenkins);
assertEquals(j.jenkins, p.getParent());
assertEquals(p, j.jenkins.getItem("p"));
assertNull(top.getItem("p"));
assertNews("moved=p;from=top/p");
Items.move(p, sub);
assertEquals(sub, p.getParent());
assertEquals(p, sub.getItem("p"));
assertNull(j.jenkins.getItem("p"));
assertNews("moved=top/sub/p;from=p");
Items.move(sub, j.jenkins);
assertEquals(sub, p.getParent());
assertEquals(p, sub.getItem("p"));
assertEquals(j.jenkins, sub.getParent());
assertEquals(sub, j.jenkins.getItem("sub"));
assertNull(top.getItem("sub"));
assertNews("moved=sub;from=top/sub moved=sub/p;from=top/sub/p");
Items.move(sub, top);
assertNews("moved=top/sub;from=sub moved=top/sub/p;from=sub/p");
assertEquals(sub, top.getItem("sub"));
sub.renameTo("lower");
assertNews("renamed=top/lower;from=sub moved=top/lower;from=top/sub moved=top/lower/p;from=top/sub/p");
top.renameTo("upper");
assertNews("renamed=upper;from=top moved=upper;from=top moved=upper/lower;from=top/lower moved=upper/lower/p;from=top/lower/p");
assertEquals(p, sub.getItem("p"));
p.renameTo("j");
assertNews("renamed=upper/lower/j;from=p moved=upper/lower/j;from=upper/lower/p");
top.renameTo("upperz");
assertNews("renamed=upperz;from=upper moved=upperz;from=upper moved=upperz/lower;from=upper/lower moved=upperz/lower/j;from=upper/lower/j");
assertEquals(sub, top.getItem("lower"));
sub.renameTo("upperzee");
assertNews("renamed=upperz/upperzee;from=lower moved=upperz/upperzee;from=upperz/lower moved=upperz/upperzee/j;from=upperz/lower/j");
Items.move(sub, j.jenkins);
assertNews("moved=upperzee;from=upperz/upperzee moved=upperzee/j;from=upperz/upperzee/j");
assertEquals(p, j.jenkins.getItemByFullName("upperzee/j"));
}
private void assertNews(String expected) {
L l = j.jenkins.getExtensionList(ItemListener.class).get(L.class);
assertEquals(expected, l.b.toString().trim());
l.b.delete(0, l.b.length());
}
@TestExtension("moving") public static class L extends ItemListener {
final StringBuilder b = new StringBuilder();
@Override public void onCreated(Item item) {
b.append(" created=").append(item.getFullName());
}
@Override public void onDeleted(Item item) {
b.append(" deleted=").append(item.getFullName());
}
@Override public void onRenamed(Item item, String oldName, String newName) {
assertEquals(item.getName(), newName);
b.append(" renamed=").append(item.getFullName()).append(";from=").append(oldName);
}
@Override public void onLocationChanged(Item item, String oldFullName, String newFullName) {
assertEquals(item.getFullName(), newFullName);
b.append(" moved=").append(newFullName).append(";from=").append(oldFullName);
}
}

}

0 comments on commit 9d53ebc

Please sign in to comment.
You can’t perform that action at this time.