Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3086 from tinexw/JENKINS-6290-TransientViewAction…
…Factory [JENKINS-6290] Add New View link to sidebar
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jenkins.model; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import hudson.Extension; | ||
import hudson.model.Action; | ||
import hudson.model.TransientViewActionFactory; | ||
import hudson.model.View; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Extension | ||
public class NewViewLink extends TransientViewActionFactory { | ||
|
||
@VisibleForTesting | ||
static final String ICON_FILE_NAME = "folder"; | ||
@VisibleForTesting | ||
public static final String URL_NAME = "newView"; | ||
|
||
@Override | ||
public List<Action> createFor(View v) { | ||
return Collections.singletonList(new Action() { | ||
|
||
@Override | ||
public String getIconFileName() { | ||
if (!hasPermission(v)) { | ||
return null; | ||
} | ||
|
||
return ICON_FILE_NAME; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
if (!hasPermission(v)) { | ||
return null; | ||
} | ||
|
||
return Messages.NewViewLink_NewView(); | ||
} | ||
|
||
@Override | ||
public String getUrlName() { | ||
return URL_NAME; | ||
} | ||
|
||
private boolean hasPermission(View view) { | ||
return view.hasPermission(View.CREATE); | ||
} | ||
|
||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jenkins.model; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import hudson.model.Action; | ||
import hudson.model.View; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
public class NewViewLinkTest { | ||
|
||
private NewViewLink newViewLink = new NewViewLink(); | ||
|
||
private View view = mock(View.class); | ||
|
||
@Test | ||
public void getActionsHasPermission() throws Exception { | ||
when(view.hasPermission(any())).thenReturn(true); | ||
|
||
final List<Action> actions = newViewLink.createFor(view); | ||
|
||
assertEquals(1, actions.size()); | ||
final Action action = actions.get(0); | ||
assertEquals(Messages.NewViewLink_NewView(), action.getDisplayName()); | ||
assertEquals(NewViewLink.ICON_FILE_NAME, action.getIconFileName()); | ||
assertEquals(NewViewLink.URL_NAME, action.getUrlName()); | ||
} | ||
|
||
@Test | ||
public void getActionsNoPermission() throws Exception { | ||
when(view.hasPermission(any())).thenReturn(false); | ||
|
||
final List<Action> actions = newViewLink.createFor(view); | ||
|
||
assertEquals(1, actions.size()); | ||
final Action action = actions.get(0); | ||
assertNull(action.getDisplayName()); | ||
assertNull(action.getIconFileName()); | ||
assertEquals(NewViewLink.URL_NAME, action.getUrlName()); | ||
} | ||
|
||
} |