Skip to content

Commit

Permalink
Added UI support for numbers and boolean plus tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhale authored and lbovet committed Sep 16, 2022
1 parent ec20f61 commit 6115206
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class AbstractTemplateResource extends ServerResource
private final static String VELOCITY_ENGINE_CONTEX_KEY = "template.resource.velocity.engine";
protected final static String ATTRIBUTE_MODEL_ATTRIBUTE = "attribute";
protected final static String VALUE_MODEL_ATTRIBUTE = "value";
protected final static String VALUE_TYPE_MODEL_ATTRIBUTE = "valueType";
protected final static String ITEMS_MODEL_ATTRIBUTE = "items";
protected final EncoderBean encoder = new EncoderBean();

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jminix/console/resource/AttributeResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public Map<String, Object> getModel()
} else if(value instanceof TabularData){
templateName = "tabular-attribute";
model.put(VALUE_MODEL_ATTRIBUTE, value);
} else {
model.put(VALUE_MODEL_ATTRIBUTE, value);
String valueType;
if (value instanceof Boolean) {
valueType = "checkbox";
} else if (value instanceof Number) {
valueType = "number";
} else {
valueType = "text";
}
model.put(VALUE_TYPE_MODEL_ATTRIBUTE, valueType);
}

return model;
Expand Down Expand Up @@ -173,6 +184,7 @@ public void update(Representation entity) throws ResourceException
for(MBeanAttributeInfo info : server.getMBeanInfo(new ObjectName(domain+":"+mbean)).getAttributes()) {
if(info.getName().equals(attributeName)) {
type = info.getType();
break;
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/jminix/templates/attribute.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
$!{attribute.description}
#end
#if ($attribute.writable)
<form method="POST"><input type="text" name="value" value="$value"/><input type="submit" value="update"/>
<form method="POST">
#if ($valueType=="checkbox")
<input type="$valueType" name="value" value="true" #if($value)checked#end/>
#elseif ($valueType=="number")
<input type="$valueType" name="value" value="$value" step="Any"/>
#else
<input type="$valueType" name="value" value="$value"/>
#end
<input type="submit" value="update"/>
#if($ok)
<span id="fade" style="color:green">Done</span>
<script>
Expand Down
48 changes: 47 additions & 1 deletion src/test/java/org/jminix/console/JMiniXStuff.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,58 @@

public final class JMiniXStuff implements JMiniXStuffMBean {

public static final String SIMPLE_STRING_VALUE = "This is a simple String";
public static final String[] STRING_ARRAY_VALUE = SIMPLE_STRING_VALUE.split(" ");

private boolean flag = true;
private int x = 10;

/**
* {@inheritDoc}
*/
@Override
public boolean getBoolean() {
return flag;
}

/**
* {@inheritDoc}
*/
@Override
public void setBoolean(boolean f) {
flag = f;
}

/**
* {@inheritDoc}
*/
@Override
public int getInt() {
return x;
}

/**
* {@inheritDoc}
*/
@Override
public void setInt(int v) {
x = v;
}

/**
* {@inheritDoc}
*/
@Override
public String getSimpleString() {
return "This is a simple String";
return SIMPLE_STRING_VALUE;
}

/**
* {@inheritDoc}
*/
@Override
public String[] getStringArray() {
return STRING_ARRAY_VALUE;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/jminix/console/JMiniXStuffMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@
* Our Test MBean.
*/
public interface JMiniXStuffMBean {
/**
* Attribute that returns a simple String.
*/
public boolean getBoolean();

public void setBoolean(boolean f);

public int getInt();
public void setInt(int v);

/**
* Attribute that returns a simple String.
*/
public String getSimpleString();

/**
* Attribute that returns a String array.
*/
public String[] getStringArray();

/**
* Operation that Returns a simple String.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.jminix.console.resource;

import static org.junit.Assert.assertEquals;

import java.lang.management.ManagementFactory;
import java.util.Map;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.jminix.console.JMiniXStuff;
import org.jminix.server.DefaultLocalServerConnectionProvider;
import org.jminix.type.AttributeFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.resource.Resource;

public class AttributeResourceTest {
private MBeanServer mbs;
private ObjectName mbeanName;

@Before
public void setup() throws Exception {
mbs = ManagementFactory.getPlatformMBeanServer();
mbeanName = new ObjectName("org.jminix.console:type=JMiniXStuff");
mbs.registerMBean(new JMiniXStuff(), mbeanName);
}

@After
public void teardown() throws Exception {
mbs.unregisterMBean(mbeanName);
}

@Test
public void testPrimitiveAttribute() {
AttributeResource res = new AttributeResource();
init(res);
Request req = res.getRequest();
req.getAttributes().put("attribute", "SimpleString");
Map<String,Object> model = res.getModel();
assertEquals(JMiniXStuff.SIMPLE_STRING_VALUE, model.get(AbstractTemplateResource.VALUE_MODEL_ATTRIBUTE));
}

@Test
public void testArrayAttribute() {
AttributeResource res = new AttributeResource();
init(res);
Request req = res.getRequest();
req.getAttributes().put("attribute", "StringArray");
Map<String,Object> model = res.getModel();
assertEquals(JMiniXStuff.STRING_ARRAY_VALUE, model.get(AbstractTemplateResource.ITEMS_MODEL_ATTRIBUTE));
}

@Test
public void testArrayFilter() {
AttributeResource res = new AttributeResource();
init(res, new AttributeFilter() {
@Override
public Object filter(Object object) {
return String.join(" ", (String[])object);
}
});
Request req = res.getRequest();
req.getAttributes().put("attribute", "StringArray");
Map<String,Object> model = res.getModel();
assertEquals(JMiniXStuff.SIMPLE_STRING_VALUE, model.get(AbstractTemplateResource.VALUE_MODEL_ATTRIBUTE));
}

private void init(Resource res) {
init(res, null);
}

private void init(Resource res, AttributeFilter attributeFilter) {
Context ctx = new Context();
ctx.getAttributes().put("serverProvider", new DefaultLocalServerConnectionProvider());
if (attributeFilter != null) {
ctx.getAttributes().put("attributeFilter", attributeFilter);
}
Request req = new Request();
req.getAttributes().put("domain", "org.jminix.console");
req.getAttributes().put("mbean", "type=JMiniXStuff");
req.getAttributes().put("server", "0");
Response resp = new Response(req);
res.init(ctx, req, resp);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jminix.resource;
package org.jminix.console.resource;

import static org.junit.Assert.assertEquals;

Expand Down

0 comments on commit 6115206

Please sign in to comment.