use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.
the class Util method messagesToJson.
/**
* Print set of messages into json array
*
* @param set set of messages
* @return json array containing the set of messages
*/
@SuppressWarnings("unchecked")
public static JSONArray messagesToJson(SortedSet<Message> set) {
JSONArray array = new JSONArray();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
for (Message m : set) {
JSONObject message = new JSONObject();
message.put("class", Util.encode(m.getClassName()));
message.put("expiration", Util.encode(df.format(m.getExpiration())));
message.put("created", Util.encode(df.format(m.getCreated())));
message.put("text", Util.encode(m.getText()));
JSONArray tags = new JSONArray();
for (String t : m.getTags()) {
tags.add(Util.encode(t));
}
message.put("tags", tags);
array.add(message);
}
return array;
}
use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.
the class RuntimeEnvironmentTest method testCanAcceptMessage.
@Test
public void testCanAcceptMessage() throws Exception {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
instance.removeAllMessages();
Message m1 = new NormalMessage();
m1.addTag("main");
m1.setExpiration(new Date(System.currentTimeMillis() - 3000));
Assert.assertFalse(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() - 2000));
Assert.assertFalse(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() - 1000));
Assert.assertFalse(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() - 1));
Assert.assertFalse(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() - 0));
Assert.assertTrue(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() + 1));
Assert.assertTrue(instance.canAcceptMessage(m1));
m1.setExpiration(new Date(System.currentTimeMillis() + 5000));
Assert.assertEquals(0, instance.getMessagesInTheSystem());
for (int i = 0; i < instance.getMessageLimit(); i++) {
Message m2 = new NormalMessage();
m2.addTag("main");
m2.setText("text");
m2.setExpiration(new Date(System.currentTimeMillis() + 5000));
m2.setCreated(new Date(System.currentTimeMillis() + i));
Assert.assertTrue(instance.canAcceptMessage(m2));
m2.apply(instance);
Assert.assertEquals(i + 1, instance.getMessagesInTheSystem());
}
Assert.assertEquals(instance.getMessageLimit(), instance.getMessagesInTheSystem());
for (int i = 0; i < instance.getMessageLimit() * 2; i++) {
Message m2 = new NormalMessage();
m2.addTag("main");
m2.setText("text");
m2.setExpiration(new Date(System.currentTimeMillis() + 5000));
m2.setCreated(new Date(System.currentTimeMillis() + i + instance.getMessageLimit()));
Assert.assertFalse(instance.canAcceptMessage(m2));
m2.apply(instance);
Assert.assertEquals(instance.getMessageLimit(), instance.getMessagesInTheSystem());
}
instance.removeAllMessages();
}
Aggregations