public void testDetection() throws Exception {
    Map<String, Object> result = writeAndMap(MAPPER, new SimpleBean());
    assertEquals(3, result.size());
    assertEquals("1", result.get("jaxb"));
    assertEquals("2", result.get("jaxb2"));
    assertEquals("3", result.get("jaxb3"));

    result = writeAndMap(MAPPER, new SimpleBean2());
    assertEquals(3, result.size());
    assertEquals("1", result.get("jaxb"));
    assertEquals("2", result.get("jaxb2"));
    assertEquals("3", result.get("jaxb3"));
  }
  private void _testMapsWithLinefeeds(boolean useBytes) throws Exception {
    String CSV =
        "A,B,C\n"
            + "data11,data12\n"
            + "data21,data22,data23\r\n"
            + "data31,\"data32 data32\ndata32 data32\",data33\n"
            + "data41,\"data42 data42\r\ndata42\",data43\n";

    CsvSchema cs = CsvSchema.emptySchema().withHeader();
    ObjectReader or = MAPPER.readerFor(HashMap.class).with(cs);

    MappingIterator<Map<String, String>> mi;

    if (useBytes) {
      mi = or.readValues(CSV.getBytes("UTF-8"));
    } else {
      mi = or.readValues(CSV);
    }

    assertTrue(mi.hasNext());
    Map<String, String> map = mi.nextValue();
    assertNotNull(map);
    assertEquals("data11", map.get("A"));
    assertEquals("data12", map.get("B"));
    assertEquals(2, map.size());

    assertTrue(mi.hasNext());
    map = mi.nextValue();
    assertNotNull(map);
    assertEquals(3, map.size());

    // then entries with linefeeds
    assertTrue(mi.hasNext());
    map = mi.nextValue();
    assertNotNull(map);
    assertEquals(3, map.size());
    assertEquals("data31", map.get("A"));
    assertEquals("data32 data32\ndata32 data32", map.get("B"));
    assertEquals("data33", map.get("C"));

    assertTrue(mi.hasNext());
    map = mi.nextValue();
    assertNotNull(map);
    assertEquals(3, map.size());
    assertEquals("data41", map.get("A"));
    assertEquals("data42 data42\r\ndata42", map.get("B"));
    assertEquals("data43", map.get("C"));

    assertFalse(mi.hasNext());
    mi.close();
  }
  /*
   * Unit test to test [JACKSON-177]
   */
  public void testHibernateCglib() throws Exception {
    /* 03-Sep-2010, tatu: This often fails form Eclipse (on some platforms like Mac OS X),
     *   so let's only run it from Ant/CLI
     */
    if (!runsFromAnt()) {
      return;
    }

    Enhancer enh = new Enhancer();
    enh.setInterfaces(new Class[] {BeanInterfaceHib.class});
    enh.setCallback(
        new MethodInterceptor() {
          @Override
          public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
              throws Throwable {
            if ("getX".equals(method.getName())) {
              return Integer.valueOf(13);
            }
            return proxy.invokeSuper(obj, args);
          }
        });
    BeanInterfaceHib bean = (BeanInterfaceHib) enh.create();
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> result = writeAndMap(mapper, bean);
    assertEquals(1, result.size());
    assertEquals(Integer.valueOf(13), result.get("x"));
  }
 public void testGlobalVisibilityForGetters() {
   ObjectMapper m = new ObjectMapper();
   m.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
   POJOPropertiesCollector coll = collector(m, SimpleGetterVisibility.class, true);
   // should be 1, expect that we disabled getter auto-detection, so
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(0, props.size());
 }
 public void testSimpleGetterVisibility() {
   POJOPropertiesCollector coll = collector(mapper, SimpleGetterVisibility.class, true);
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(1, props.size());
   POJOPropertyBuilder prop = props.get("a");
   assertNotNull(prop);
   assertFalse(prop.hasSetter());
   assertTrue(prop.hasGetter());
   assertFalse(prop.hasField());
 }
 public void testSimpleIgnoreAndRename() {
   POJOPropertiesCollector coll = collector(mapper, IgnoredRenamedSetter.class, true);
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(1, props.size());
   POJOPropertyBuilder prop = props.get("y");
   assertNotNull(prop);
   assertTrue(prop.hasSetter());
   assertFalse(prop.hasGetter());
   assertFalse(prop.hasField());
 }
 public void testMergeWithRename() {
   POJOPropertiesCollector coll = collector(mapper, MergedProperties.class, true);
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(1, props.size());
   POJOPropertyBuilder prop = props.get("x");
   assertNotNull(prop);
   assertTrue(prop.hasSetter());
   assertFalse(prop.hasGetter());
   assertTrue(prop.hasField());
 }
 public void testCollectionOfIgnored() {
   POJOPropertiesCollector coll = collector(mapper, ImplicitIgnores.class, false);
   // should be 1, due to ignorals
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(1, props.size());
   // but also have 2 ignored properties
   Collection<String> ign = coll.getIgnoredPropertyNames();
   assertEquals(2, ign.size());
   assertTrue(ign.contains("a"));
   assertTrue(ign.contains("b"));
 }
 public void testSimpleFieldVisibility() {
   // false -> deserialization
   POJOPropertiesCollector coll = collector(mapper, SimpleFieldDeser.class, false);
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(1, props.size());
   POJOPropertyBuilder prop = props.get("values");
   assertNotNull(prop);
   assertFalse(prop.hasSetter());
   assertFalse(prop.hasGetter());
   assertTrue(prop.hasField());
 }
 public void testExplicitIgnoralWithBean() throws Exception {
   IgnoreSome value = new IgnoreSome();
   Map<String, Object> result = writeAndMap(MAPPER, value);
   assertEquals(2, result.size());
   // verify that specified fields are ignored
   assertFalse(result.containsKey("b"));
   assertFalse(result.containsKey("c"));
   // and that others are not
   assertEquals(Integer.valueOf(value.a), result.get("a"));
   assertEquals(value.getD(), result.get("d"));
 }
 public void testExplicitIgnoralWithMap() throws Exception {
   // test simulating need to filter out metadata like class name
   MyMap value = new MyMap();
   value.put("a", "b");
   value.put("@class", MyMap.class.getName());
   Map<String, Object> result = writeAndMap(MAPPER, value);
   assertEquals(1, result.size());
   // verify that specified field is ignored
   assertFalse(result.containsKey("@class"));
   // and that others are not
   assertEquals(value.get("a"), result.get("a"));
 }
 public void testSimpleKeyDeser() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   SimpleModule module = new SimpleModule("test", Version.unknownVersion());
   module.addKeyDeserializer(String.class, new ContextualDeser("???"));
   mapper.registerModule(module);
   MapBean result = mapper.readValue("{\"map\":{\"a\":3}}", MapBean.class);
   Map<String, Integer> map = result.map;
   assertNotNull(map);
   assertEquals(1, map.size());
   Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
   assertEquals(Integer.valueOf(3), entry.getValue());
   assertEquals("map:a", entry.getKey());
 }
  /** Test to verify that we can mix "untyped" access as Maps with schema information... */
  public void testSimpleAsMaps() throws Exception {
    CsvSchema schema = MAPPER.schemaFor(FiveMinuteUser.class);
    MappingIterator<Map<?, ?>> it =
        MAPPER.reader(schema).forType(Map.class).readValues("Joe,Smith,MALE,false,");
    assertTrue(it.hasNext());
    Map<?, ?> result = it.nextValue();
    assertEquals(5, result.size());
    assertEquals("Joe", result.get("firstName"));
    assertEquals("Smith", result.get("lastName"));
    assertEquals("MALE", result.get("gender"));
    assertEquals("false", result.get("verified"));
    assertEquals("", result.get("userImage"));

    assertFalse(it.hasNextValue());
    it.close();
  }
  // [Issue#41]
  public void testIncorrectDups41() throws Exception {
    final String INPUT = "\"foo\",\"bar\",\"foo\"";
    CsvSchema schema =
        CsvSchema.builder().addColumn("Col1").addColumn("Col2").addColumn("Col3").build();

    MappingIterator<Object> iter = MAPPER.readerFor(Object.class).with(schema).readValues(INPUT);

    Map<?, ?> m = (Map<?, ?>) iter.next();
    assertFalse(iter.hasNextValue());
    iter.close();

    if (m.size() != 3) {
      fail("Should have 3 entries, but got: " + m);
    }
    assertEquals("foo", m.get("Col1"));
    assertEquals("bar", m.get("Col2"));
    assertEquals("foo", m.get("Col3"));
  }
Beispiel #15
0
 @Override
 public boolean hasSingleElement(Map<?, ?> value) {
   return (value.size() == 1);
 }
 public void testStaticMethods() throws Exception {
   ObjectMapper m = new ObjectMapper();
   Map<String, Object> result = writeAndMap(m, new GetterBean());
   assertEquals(1, result.size());
   assertEquals(Integer.valueOf(3), result.get("x"));
 }
 // Unit test for verifying that a single @JsonIgnore can remove the
 // whole property, unless explicit property marker exists
 public void testEmpty() {
   POJOPropertiesCollector coll = collector(mapper, Empty.class, true);
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(0, props.size());
 }