Skip to content

Commit ae815da

Browse files
committed
Merge branch 'master' of https://github.com/jenkinsci/cloudbees-folder-plugin into session
2 parents 79bb435 + 5ed27a3 commit ae815da

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
365365
V itemFromDir;
366366
V item;
367367
String name;
368-
item = itemFromDir = byDirName.get(childName);
369368
var legacyName = subdir.getName();
369+
item = itemFromDir = byDirName.get(legacyName);
370370
try {
371371
if (item == null) {
372372
XmlFile xmlFile = Items.getConfigFile(subdir);
@@ -378,6 +378,13 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
378378
}
379379
}
380380
name = childNameGenerator.writeItemName(parent, item, effectiveSubdir, childName);
381+
String computedName = childNameGenerator.itemNameFromItem(parent, item);
382+
if (computedName == null) {
383+
computedName = subdir.getName();
384+
}
385+
if (!computedName.equals(name)) {
386+
throw new IllegalStateException("Computed name '" + computedName + "' does not match name written to file '" + name + "'");
387+
}
381388
if (item instanceof AbstractItem) {
382389
var abstractItem = (AbstractItem) item;
383390
if (itemFromDir != null && !legacyName.equals(name) && abstractItem.getDisplayNameOrNull() == null) {
@@ -400,7 +407,15 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
400407

401408
@Override
402409
public String getItemName(File dir, I item) {
403-
return childNameGenerator().readItemName(dir);
410+
String name = childNameGenerator().readItemName(dir);
411+
String computedName = childNameGenerator().itemNameFromItem(this, item);
412+
if (computedName == null) {
413+
computedName = dir.getName();
414+
}
415+
if (!computedName.equals(name)) {
416+
throw new IllegalStateException("Computed name '" + computedName + "' does not match name read from file '" + name + "'");
417+
}
418+
return name;
404419
}
405420

406421
protected final I itemsPut(String name, I item) {

src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import static org.hamcrest.Matchers.is;
6969
import static org.hamcrest.Matchers.notNullValue;
7070
import static org.junit.Assert.assertEquals;
71+
import static org.junit.Assert.assertSame;
7172

7273

7374
/**
@@ -114,8 +115,15 @@ public void createdFromScratch() throws Throwable {
114115
assertThat("Item loaded from disk", i, instanceOf(ComputedFolderImpl.class));
115116
instance = (ComputedFolderImpl) i;
116117
checkComputedFolder(instance, 0);
118+
var items = new ArrayList<>(instance.getItems());
117119
instance.doReload();
118120
checkComputedFolder(instance, 0);
121+
var newItems = new ArrayList<>(instance.getItems());
122+
// Check child items identity is preserved
123+
assertThat("Items are the same", items, is(newItems));
124+
for (int k = 0; k < items.size(); k++) {
125+
assertSame("Individual items must be the same", items.get(k), newItems.get(k));
126+
}
119127
});
120128
}
121129

0 commit comments

Comments
 (0)