1 package com.fasterxml.jackson.databind.util; 2 3 import java.util.*; 4 import java.util.concurrent.atomic.AtomicReference; 5 6 import com.fasterxml.jackson.annotation.JsonInclude; 7 import com.fasterxml.jackson.databind.BaseMapTest; 8 import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; 9 import com.fasterxml.jackson.databind.type.TypeFactory; 10 11 public class BeanUtilTest extends BaseMapTest 12 { 13 static class IsGetters { isPrimitive()14 public boolean isPrimitive() { return false; } isWrapper()15 public Boolean isWrapper() { return false; } isNotGetter()16 public String isNotGetter() { return null; } is()17 public boolean is() { return false; } 18 } 19 20 static class Getters { getCallbacks()21 public String getCallbacks() { return null; } getMetaClass()22 public String getMetaClass() { return null; } get()23 public boolean get() { return false; } 24 } 25 26 static class Setters { setFoo()27 public void setFoo() { } notSetter()28 public void notSetter() { } set()29 public void set() { } 30 } 31 32 /* 33 /********************************************************** 34 /* Test methods 35 /********************************************************** 36 */ 37 testNameMangle()38 public void testNameMangle() 39 { 40 assertEquals("foo", BeanUtil.legacyManglePropertyName("getFoo", 3)); 41 assertEquals("foo", BeanUtil.stdManglePropertyName("getFoo", 3)); 42 43 assertEquals("url", BeanUtil.legacyManglePropertyName("getURL", 3)); 44 assertEquals("URL", BeanUtil.stdManglePropertyName("getURL", 3)); 45 } 46 testGetDefaultValue()47 public void testGetDefaultValue() 48 { 49 TypeFactory tf = TypeFactory.defaultInstance(); 50 // For collection/array/Map types, should give `NOT_EMPTY`: 51 assertEquals(JsonInclude.Include.NON_EMPTY, 52 BeanUtil.getDefaultValue(tf.constructType(Map.class))); 53 assertEquals(JsonInclude.Include.NON_EMPTY, 54 BeanUtil.getDefaultValue(tf.constructType(List.class))); 55 assertEquals(JsonInclude.Include.NON_EMPTY, 56 BeanUtil.getDefaultValue(tf.constructType(Object[].class))); 57 // as well as ReferenceTypes, String 58 assertEquals(JsonInclude.Include.NON_EMPTY, 59 BeanUtil.getDefaultValue(tf.constructType(AtomicReference.class))); 60 assertEquals("", 61 BeanUtil.getDefaultValue(tf.constructType(String.class))); 62 // primitive/wrappers have others 63 assertEquals(Integer.valueOf(0), 64 BeanUtil.getDefaultValue(tf.constructType(Integer.class))); 65 66 67 // but POJOs have no real default 68 assertNull(BeanUtil.getDefaultValue(tf.constructType(getClass()))); 69 } 70 testIsGetter()71 public void testIsGetter() throws Exception 72 { 73 _testIsGetter("isPrimitive", "primitive"); 74 _testIsGetter("isWrapper", "wrapper"); 75 _testIsGetter("isNotGetter", null); 76 _testIsGetter("is", null); 77 } 78 testOkNameForGetter()79 public void testOkNameForGetter() throws Exception 80 { 81 // mostly chosen to exercise groovy exclusion 82 _testOkNameForGetter("getCallbacks", "callbacks"); 83 _testOkNameForGetter("getMetaClass", "metaClass"); 84 _testOkNameForGetter("get", null); 85 } 86 testOkNameForSetter()87 public void testOkNameForSetter() throws Exception 88 { 89 _testOkNameForSetter("setFoo", "foo"); 90 _testOkNameForSetter("notSetter", null); 91 _testOkNameForSetter("set", null); 92 } 93 94 /* 95 /********************************************************** 96 /* Helper methods 97 /********************************************************** 98 */ 99 _testIsGetter(String name, String expName)100 private void _testIsGetter(String name, String expName) throws Exception { 101 _testIsGetter(name, expName, true); 102 _testIsGetter(name, expName, false); 103 } 104 _testIsGetter(String name, String expName, boolean useStd)105 private void _testIsGetter(String name, String expName, boolean useStd) throws Exception 106 { 107 AnnotatedMethod m = _method(IsGetters.class, name); 108 if (expName == null) { 109 assertNull(BeanUtil.okNameForIsGetter(m, name, useStd)); 110 } else { 111 assertEquals(expName, BeanUtil.okNameForIsGetter(m, name, useStd)); 112 } 113 } 114 _testOkNameForGetter(String name, String expName)115 private void _testOkNameForGetter(String name, String expName) throws Exception { 116 _testOkNameForGetter(name, expName, true); 117 _testOkNameForGetter(name, expName, false); 118 } 119 _testOkNameForGetter(String name, String expName, boolean useStd)120 private void _testOkNameForGetter(String name, String expName, boolean useStd) throws Exception { 121 AnnotatedMethod m = _method(Getters.class, name); 122 if (expName == null) { 123 assertNull(BeanUtil.okNameForGetter(m, useStd)); 124 } else { 125 assertEquals(expName, BeanUtil.okNameForGetter(m, useStd)); 126 } 127 } 128 _testOkNameForSetter(String name, String expName)129 private void _testOkNameForSetter(String name, String expName) throws Exception { 130 _testOkNameForSetter(name, expName, true); 131 _testOkNameForSetter(name, expName, false); 132 } 133 134 @SuppressWarnings("deprecation") _testOkNameForSetter(String name, String expName, boolean useStd)135 private void _testOkNameForSetter(String name, String expName, boolean useStd) throws Exception { 136 AnnotatedMethod m = _method(Setters.class, name); 137 if (expName == null) { 138 assertNull(BeanUtil.okNameForSetter(m, useStd)); 139 } else { 140 assertEquals(expName, BeanUtil.okNameForSetter(m, useStd)); 141 } 142 } 143 _method(Class<?> cls, String name, Class<?>...parameterTypes)144 private AnnotatedMethod _method(Class<?> cls, String name, Class<?>...parameterTypes) throws Exception { 145 return new AnnotatedMethod(null, cls.getMethod(name, parameterTypes), null, null); 146 } 147 } 148