Skip to content

Commit

Permalink
[JENKINS-11762] Changes to add a configurable display name to jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert So authored and kohsuke committed Jan 3, 2012
1 parent 907686a commit 05b4665
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/src/main/resources/lib/hudson/abstractItemLink.jelly
@@ -0,0 +1,33 @@
<!--
The MIT License
Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt,
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<st:documentation>
Displays a link when given an AbstractItem. It is assumed that that Abstract
Item is passed in ${it}
</st:documentation>
<a href="${it.absoluteUrl}" tooltip="${it.name}">${it.displayName}</a>
</j:jelly>
66 changes: 66 additions & 0 deletions core/src/test/java/hudson/model/AbstractItemTest.java
@@ -0,0 +1,66 @@
/**
*
*/
package hudson.model;

import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;

/**
* @author kingfai
*
*/
@SuppressWarnings("unchecked")
public class AbstractItemTest {

private class StubAbstractItem extends AbstractItem {

protected StubAbstractItem() {
// sending in null as parent as I don't care for my current tests
super(null, "StubAbatractItem");
}

@SuppressWarnings("rawtypes")
@Override
public Collection<? extends Job> getAllJobs() {
return null;
}

/**
* Override save so that nothig happens when setDisplayName() is called
*/
@Override
public void save() {

}
}

@Test
public void testSetDisplayName() throws Exception {
final String displayName = "testDisplayName";
StubAbstractItem i = new StubAbstractItem();
i.setDisplayName(displayName);
Assert.assertEquals(displayName, i.getDisplayName());
}

@Test
public void testGetDefaultDisplayName() {
final String name = "the item name";
StubAbstractItem i = new StubAbstractItem();
i.doSetName(name);
// assert that if the displayname is not set, the name is actually returned
Assert.assertEquals(name, i.getDisplayName());

}

@Test
public void testSearchNameIsName() throws Exception {
final String name = "the item name jlrtlekjtekrjkjr";
StubAbstractItem i = new StubAbstractItem();
i.doSetName(name);

Assert.assertEquals(i.getName(), i.getSearchName());
}
}
78 changes: 78 additions & 0 deletions core/src/test/java/hudson/model/JobTest.java
@@ -0,0 +1,78 @@
package hudson.model;

import java.util.SortedMap;

import junit.framework.Assert;

import org.junit.Test;
import org.kohsuke.stapler.StaplerRequest;
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 {
final String displayName = "testSetDisplayName";
// create a mock stapler request that returns a display name
StaplerRequest req = Mockito.mock(StaplerRequest.class);
Mockito.when(req.getParameter(DISPLAY_NAME_PARAMETER_NAME)).thenReturn(displayName);

StubJob j = new StubJob();
// call setDisplayNameFromRequest
j.setDisplayNameFromRequest(req);

// make sure the displayname has been set
Assert.assertEquals(displayName, j.getDisplayName());
}

@Test
public void testSetDisplayNameZeroLength() throws Exception {
// create a mock stapler request that returns a ""
StaplerRequest req = Mockito.mock(StaplerRequest.class);
Mockito.when(req.getParameter(DISPLAY_NAME_PARAMETER_NAME)).thenReturn("");

StubJob j = new StubJob();
// call setDisplayNameFromRequest
j.setDisplayNameFromRequest(req);

// make sure the getDisplayName returns the project name
Assert.assertEquals(DEFAULT_STUB_JOB_NAME, j.getDisplayName());
}
}
68 changes: 68 additions & 0 deletions core/src/test/java/hudson/model/ViewTest.java
@@ -0,0 +1,68 @@
package hudson.model;

import hudson.search.SearchIndex;
import hudson.search.SearchIndexBuilder;
import hudson.search.SearchItem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class ViewTest {

/*
* This test verifies that urls and displaynames in the TopLevelItem list
* are added to the SearchIndexBuilder
*/
@Test
public void testAddDisplayNamesToSearchIndex() {
final String url1 = "url1";
final String displayName1 = "displayName1";
final String url2 = "url2";
final String displayName2 = "displayName2";

SearchIndexBuilder sib = new SearchIndexBuilder();
// mock the items to be indexed
TopLevelItem item1 = Mockito.mock(TopLevelItem.class);
Mockito.when(item1.getSearchUrl()).thenReturn(url1);
Mockito.when(item1.getDisplayName()).thenReturn(displayName1);
TopLevelItem item2 = Mockito.mock(TopLevelItem.class);
Mockito.when(item2.getSearchUrl()).thenReturn(url2);
Mockito.when(item2.getDisplayName()).thenReturn(displayName2);
Collection<TopLevelItem> items = new ArrayList<TopLevelItem>();
items.add(item1);
items.add(item2);

// mock the view class except for the addDisplayNamesToSearchIndex() call as that
// is what we are testing
View view = Mockito.mock(View.class);
Mockito.doCallRealMethod().when(view).addDisplayNamesToSearchIndex(sib, items);

// now make the actual call to index items
view.addDisplayNamesToSearchIndex(sib, items);

// make and index with sib
SearchIndex index = sib.make();

// now make sure we can fetch item1 from the index
List<SearchItem> result = new ArrayList<SearchItem>();
index.find(displayName1, result);
Assert.assertEquals(1, result.size());
SearchItem actual = result.get(0);
Assert.assertEquals(actual.getSearchName(), item1.getDisplayName());
Assert.assertEquals(actual.getSearchUrl(), item1.getSearchUrl());

// clear the result array for the next search result to test
result.clear();
// make sure we can fetch item 2 from the index
index.find(displayName2, result);
Assert.assertEquals(1, result.size());
actual = result.get(0);
Assert.assertEquals(actual.getSearchName(), item2.getDisplayName());
Assert.assertEquals(actual.getSearchUrl(), item2.getSearchUrl());
}
}

0 comments on commit 05b4665

Please sign in to comment.