@Test
  public void convertToJsonTest() throws MalformedObjectNameException, AttributeNotFoundException {
    File file = new File("myFile");

    Map ret = (Map) converter.convertToJson(file, null, JsonConvertOptions.DEFAULT);
    assertEquals(ret.get("name"), "myFile");
    String name =
        (String) converter.convertToJson(file, Arrays.asList("name"), JsonConvertOptions.DEFAULT);
    assertEquals(name, "myFile");
  }
 @Test
 public void maxDepth()
     throws AttributeNotFoundException, NoSuchFieldException, IllegalAccessException {
   setOptionsViaReflection("maxDepth", 2);
   Map result = (Map) converter.extractObject(new SelfRefBean1(), new Stack<String>(), true);
   String c = (String) ((Map) result.get("bean2")).get("bean1");
   assertTrue("Recurence detected", c.contains("bean1: toString"));
 }
 @Test
 public void checkDeadLockDetection() throws AttributeNotFoundException {
   Map result = (Map) converter.extractObject(new SelfRefBean1(), new Stack<String>(), true);
   assertNotNull("Bean 2 is set", result.get("bean2"));
   assertNotNull("Bean2:Bean1 is set", ((Map) result.get("bean2")).get("bean1"));
   assertEquals(
       "Reference breackage", ((Map) result.get("bean2")).get("bean1").getClass(), String.class);
   assertTrue("Bean 3 should be resolved", result.get("bean3") instanceof Map);
 }
 @Test
 public void customNegativeSimpifier()
     throws MalformedObjectNameException, AttributeNotFoundException {
   ObjectName name = new ObjectName("java.lang:type=Memory");
   Map result = (Map) converter.extractObject(name, new Stack<String>(), true);
   // Since we removed the objectname simplifier from the list of simplifiers
   // explicitely, the converter should return the full blown object;
   assertEquals("type=Memory", result.get("canonicalKeyPropertyListString"));
 }
 private void setOptionsViaReflection(String pLimit, int pVal)
     throws NoSuchFieldException, IllegalAccessException {
   ObjectSerializationContext ctx = converter.getStackContextLocal().get();
   Field field = ObjectSerializationContext.class.getDeclaredField("options");
   field.setAccessible(true);
   JsonConvertOptions opts = (JsonConvertOptions) field.get(ctx);
   field = JsonConvertOptions.class.getDeclaredField(pLimit);
   field.setAccessible(true);
   field.set(opts, pVal);
 }
  @Test
  public void convertTransientValue() throws AttributeNotFoundException {
    TransientValueBean bean = new TransientValueBean();
    bean.value = "value";
    bean.transientValue = "transient";

    Map ret = (Map) converter.convertToJson(bean, null, JsonConvertOptions.DEFAULT);
    assertNull(ret.get("transientValue"));
    assertEquals(ret.get("value"), "value");
  }
  @Test
  public void setInnerValueTest()
      throws IllegalAccessException, AttributeNotFoundException, InvocationTargetException {
    InnerValueTestBean bean = new InnerValueTestBean("foo", "bar", "baz");

    Object oldValue =
        converter.setInnerValue(
            bean, "blub", new ArrayList<String>(Arrays.asList("map", "foo", "1")));

    assertEquals(oldValue, "baz");
    assertEquals(bean.getMap().get("foo").get(0), "bar");
    assertEquals(bean.getMap().get("foo").get(1), "blub");

    oldValue =
        converter.setInnerValue(bean, "fcn", new ArrayList<String>(Arrays.asList("array", "0")));

    assertEquals(oldValue, "bar");
    assertEquals(bean.getArray()[0], "fcn");
  }
Example #8
0
 /** {@inheritDoc} */
 @SuppressWarnings("PMD.CompareObjectsWithEquals")
 public Object extractObject(
     ObjectToJsonConverter pConverter, Object pValue, Stack<String> pExtraArgs, boolean jsonify)
     throws AttributeNotFoundException {
   ValueFaultHandler faultHandler = pConverter.getValueFaultHandler();
   if (!pExtraArgs.isEmpty()) {
     // Still some path elements available, so dive deeper
     String attribute = pExtraArgs.pop();
     Object attributeValue = extractBeanPropertyValue(pValue, attribute, faultHandler);
     return pConverter.extractObject(attributeValue, pExtraArgs, jsonify);
   } else {
     if (jsonify) {
       // We need the jsonfied value from here on.
       return exctractJsonifiedValue(pValue, pExtraArgs, pConverter, faultHandler);
     } else {
       // No jsonification requested, hence we are returning the object itself
       return pValue;
     }
   }
 }
 @Test
 public void maxObjects()
     throws NoSuchFieldException, IllegalAccessException, AttributeNotFoundException {
   setOptionsViaReflection("maxObjects", 1);
   Map<String, Object> result =
       (Map)
           converter.extractObject(
               new InnerValueTestBean("foo", "bar", "baz"), new Stack<String>(), true);
   boolean found = false;
   for (Object val : result.values()) {
     if (val instanceof String) {
       found = ((String) val).matches("^\\[.*(limit).*\\]$");
     }
   }
   assertTrue(found);
 }
Example #10
0
 @SuppressWarnings("PMD.CompareObjectsWithEquals")
 private Object extractJsonifiedPropertyValue(
     Object pValue,
     String pAttribute,
     Stack<String> pExtraArgs,
     ObjectToJsonConverter pConverter,
     ValueFaultHandler pFaultHandler)
     throws AttributeNotFoundException {
   Object value = extractBeanPropertyValue(pValue, pAttribute, pFaultHandler);
   if (value == null) {
     return null;
   } else if (value == pValue) {
     // Break Cycle
     return "[this]";
   } else {
     // Call into the converted recursively for any object known.
     return pConverter.extractObject(value, pExtraArgs, true /* jsonify */);
   }
 }
 @Test
 public void basics() throws AttributeNotFoundException {
   Map result = (Map) converter.extractObject(new SelfRefBean1(), new Stack<String>(), true);
   assertNotNull("Bean2 is set", result.get("bean2"));
   assertNotNull("Binary attribute is set", result.get("strong"));
 }
 @AfterMethod
 public void tearDown() {
   converter.clearContext();
 }
 @BeforeMethod
 public void setup() {
   converter = new ObjectToJsonConverter(new StringToObjectConverter(), null);
   converter.setupContext();
 }
 @Test
 public void fileSimplifier() throws AttributeNotFoundException {
   Map result = (Map) converter.extractObject(new File("/tmp"), new Stack<String>(), true);
   assertNull(result.get("parent"));
 }
 @Test
 public void customSimplifier() throws AttributeNotFoundException {
   Date date = new Date();
   Map result = (Map) converter.extractObject(date, new Stack<String>(), true);
   assertEquals(date.getTime(), result.get("millis"));
 }