Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
[JENKINS-26126] Implemented getDisplayName and getHelp.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 15, 2015
1 parent 4ba0db7 commit 5f6e8fd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Expand Up @@ -26,12 +26,14 @@

import hudson.Extension;
import com.google.common.primitives.Primitives;
import hudson.Util;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import java.beans.Introspector;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -59,12 +61,14 @@
import jenkins.model.Jenkins;
import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ObjectUtils;
import org.codehaus.groovy.reflection.ReflectionCache;
import org.kohsuke.stapler.ClassDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.NoStaplerConstructorException;
import org.kohsuke.stapler.lang.Klass;

/**
* Utility for converting between {@link Describable}s (and some other objects) and map-like representations.
Expand Down Expand Up @@ -210,19 +214,33 @@ public List<String> mandatoryParameters() {
}

/**
* Corresponds to {@link Descriptor#getDisplayName}.
* Corresponds to {@link Descriptor#getDisplayName} where available.
*/
public String getDisplayName() {
return ""; // TODO
for (Descriptor<?> d : getDescriptorList()) {
if (d.clazz == type) {
return d.getDisplayName();
}
}
return type.getSimpleName();
}

/**
* Loads help defined for this object as a whole or one of its parameters.
* Note that you may need to use {@link Util#replaceMacro(String, Map)}
* to replace {@code ${rootURL}} with some other value.
* @param parameter if specified, one of {@link #parameters}; else for the whole object
* @return some HTML, if available, else null
* @return some HTML (in English locale), if available, else null
* @see Descriptor#doHelp
*/
public @CheckForNull String getHelp(@CheckForNull String parameter) {
return null; // TODO
public @CheckForNull String getHelp(@CheckForNull String parameter) throws IOException {
for (Klass<?> c = Klass.java(type); c != null; c = c.getSuperClass()) {
URL u = c.getResource(parameter == null ? "help.html" : "help-" + parameter + ".html");
if (u != null) {
return IOUtils.toString(u, "UTF-8");
}
}
return null;
}

@Override public String toString() {
Expand Down
Expand Up @@ -89,7 +89,14 @@ public class DescribableHelperTest {
@Test public void schemaFor() throws Exception {
schema(C.class, "(text: String, flag: boolean, [shorty: short])");
schema(I.class, "(value: String, [flag: boolean], [text: String])");
// TODO test getDisplayName, getHelp
DescribableHelper.Schema schema = DescribableHelper.schemaFor(Impl1.class);
assertEquals("Implementation #1", schema.getDisplayName());
assertEquals("<div>Overall help.</div>", schema.getHelp(null));
assertEquals("<div>The text to display.</div>", schema.getHelp("text"));
schema = DescribableHelper.schemaFor(C.class);
assertEquals("C", schema.getDisplayName());
assertNull(schema.getHelp(null));
assertNull(schema.getHelp("text"));
}

public static final class C {
Expand Down Expand Up @@ -194,7 +201,7 @@ public String getText() {
}
@Extension public static final class DescriptorImpl extends Descriptor<Base> {
@Override public String getDisplayName() {
return "Impl1";
return "Implementation #1";
}
}
}
Expand Down
@@ -0,0 +1 @@
<div>The text to display.</div>
@@ -0,0 +1 @@
<div>Overall help.</div>

0 comments on commit 5f6e8fd

Please sign in to comment.