Skip to content

Commit

Permalink
[FIXED JENKINS-17715] Display Name is not shown
Browse files Browse the repository at this point in the history
  • Loading branch information
ssogabe committed Apr 30, 2013
1 parent d477296 commit 55a68ac
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 37 deletions.
4 changes: 3 additions & 1 deletion changelog.html
Expand Up @@ -55,7 +55,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=>
<li class=bug>
Display Name is not shown.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-17715">issue 17715</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
66 changes: 62 additions & 4 deletions core/src/main/java/hudson/Functions.java
Expand Up @@ -974,6 +974,67 @@ public static List<TopLevelItem> getAllTopLevelItems(ItemGroup root) {
return Items.getAllItems(root, TopLevelItem.class);
}

/**
* Gets the relative name or display name to the given item from the specified group.
*
* @since 1.515
* @param p the Item we want the relative display name
* @param g the ItemGroup used as point of reference for the item
* @param useDisplayName if true, returns a display name, otherwise returns a name
* @return
* String like "foo » bar"
*/
public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplayName) {
if (p == null) return null;
if (g == null) return useDisplayName ? p.getFullDisplayName() : p.getFullName();
String separationString = useDisplayName ? " » " : "/";

// first list up all the parents
Map<ItemGroup,Integer> parents = new HashMap<ItemGroup,Integer>();
int depth=0;
while (g!=null) {
parents.put(g, depth++);
if (g instanceof Item)
g = ((Item)g).getParent();
else
g = null;
}

StringBuilder buf = new StringBuilder();
Item i=p;
while (true) {
if (buf.length()>0) buf.insert(0,separationString);
buf.insert(0,useDisplayName ? i.getDisplayName() : i.getName());
ItemGroup gr = i.getParent();

Integer d = parents.get(gr);
if (d!=null) {
String s="";
for (int j=d; j>0; j--)
s+=".." + separationString;
return s+buf;
}

if (gr instanceof Item)
i = (Item)gr;
else
return null;
}
}

/**
* Gets the name to the given item relative to given group.
*
* @since 1.515
* @param p the Item we want the relative display name
* @param g the ItemGroup used as point of reference for the item
* @return
* String like "foo » bar"
*/
public static String getRelativeNameFrom(Item p, ItemGroup g) {
return getRelativeNameFrom(p, g, false);
}


/**
* Gets the relative display name to the given item from the specified group.
Expand All @@ -985,10 +1046,7 @@ public static List<TopLevelItem> getAllTopLevelItems(ItemGroup root) {
* String like "foo » bar"
*/
public static String getRelativeDisplayNameFrom(Item p, ItemGroup g) {
if (p == null) return null;
String relativeName = p.getRelativeNameFrom(g);
if (relativeName == null) return null;
return relativeName.replace("/", " » ");
return getRelativeNameFrom(p, g, true);
}

public static Map<Thread,StackTraceElement[]> dumpAllThreads() {
Expand Down
45 changes: 13 additions & 32 deletions core/src/main/java/hudson/model/AbstractItem.java
Expand Up @@ -327,6 +327,18 @@ public final String getFullDisplayName() {
else return n+" \u00BB "+getDisplayName();
}

/**
* Gets the display name of the current item relative to the given group.
*

This comment has been minimized.

Copy link
@Vlatombe

Vlatombe Apr 30, 2013

Member

Should be updated to 1.515

This comment has been minimized.

Copy link
@ssogabe

ssogabe Apr 30, 2013

Author Member

done

* @since XXX
* @param p the ItemGroup used as point of reference for the item
* @return
* String like "foo » bar"
*/
public String getRelativeDisplayNameFrom(ItemGroup p) {
return Functions.getRelativeDisplayNameFrom(this, p);
}

/**
* This method only exists to disambiguate {@link #getRelativeNameFrom(ItemGroup)} and {@link #getRelativeNameFrom(Item)}
* @since 1.512
Expand All @@ -344,38 +356,7 @@ public String getRelativeNameFromGroup(ItemGroup p) {
* Nested ItemGroups are separated by / character.
*/
public String getRelativeNameFrom(ItemGroup p) {
if (p == null) return getFullName();
// first list up all the parents
Map<ItemGroup,Integer> parents = new HashMap<ItemGroup,Integer>();
int depth=0;
while (p!=null) {
parents.put(p, depth++);
if (p instanceof Item)
p = ((Item)p).getParent();
else
p = null;
}

StringBuilder buf = new StringBuilder();
Item i=this;
while (true) {
if (buf.length()>0) buf.insert(0,'/');
buf.insert(0,i.getName());
ItemGroup g = i.getParent();

Integer d = parents.get(g);
if (d!=null) {
String s="";
for (int j=d; j>0; j--)
s+="../";
return s+buf;
}

if (g instanceof Item)
i = (Item)g;
else
return null;
}
return Functions.getRelativeNameFrom(this, p);
}

public String getRelativeNameFrom(Item item) {
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/hudson/FunctionsTest.java
Expand Up @@ -28,6 +28,7 @@
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import hudson.model.Action;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.TopLevelItem;
import hudson.model.View;
Expand Down Expand Up @@ -171,6 +172,30 @@ public void testGetRelativeLinkTo_JobContainedInViewWithinItemGroup() throws Exc
assertEquals("job/i/", result);
}

@Test
public void testGetRelativeDisplayName() {
Item i = mock(Item.class);
when(i.getName()).thenReturn("jobName");
when(i.getFullDisplayName()).thenReturn("displayName");
assertEquals("displayName",Functions.getRelativeDisplayNameFrom(i, null));
}

@Test
public void testGetRelativeDisplayNameInsideItemGroup() {
Item i = mock(Item.class);
when(i.getName()).thenReturn("jobName");
when(i.getDisplayName()).thenReturn("displayName");
TopLevelItemAndItemGroup ig = mock(TopLevelItemAndItemGroup.class);
Jenkins j = mock(Jenkins.class);
when(ig.getName()).thenReturn("parent");
when(ig.getDisplayName()).thenReturn("parentDisplay");
when(ig.getParent()).thenReturn((ItemGroup) j);
when(i.getParent()).thenReturn(ig);

assertEquals("displayName", Functions.getRelativeDisplayNameFrom(i, ig));
assertEquals("parentDisplay » displayName", Functions.getRelativeDisplayNameFrom(i, j));
}

private void createMockAncestors(StaplerRequest req, Ancestor... ancestors) {
List<Ancestor> ancestorsList = Arrays.asList(ancestors);
when(req.getAncestors()).thenReturn(ancestorsList);
Expand Down

0 comments on commit 55a68ac

Please sign in to comment.