Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public class ModelMapperReMappingTest {
  2.  
  3. interface Configurable {
  4.  
  5. void configure();
  6.  
  7. }
  8.  
  9. static class B1 {
  10.  
  11. String prop;
  12.  
  13. public String getProp() {
  14. return prop;
  15. }
  16.  
  17. public void setProp(String prop) {
  18. this.prop = prop;
  19. }
  20. }
  21.  
  22. static class A1 {
  23.  
  24. private B1 b;
  25.  
  26. public B1 getB() {
  27. return b;
  28. }
  29.  
  30. public void setB(B1 b) {
  31. this.b = b;
  32. }
  33.  
  34. }
  35.  
  36. static class B2 implements Configurable {
  37.  
  38. String prop;
  39.  
  40. @Override
  41. public void configure() {
  42. System.out.println("Configuring");
  43. }
  44.  
  45. public String getProp() {
  46. return prop;
  47. }
  48.  
  49. public void setProp(String prop) {
  50. this.prop = prop;
  51. }
  52. }
  53.  
  54. static class A2 {
  55.  
  56. private B2 b;
  57.  
  58. public B2 getB() {
  59. return b;
  60. }
  61.  
  62. public void setB(B2 b) {
  63. this.b = b;
  64. }
  65. }
  66.  
  67. class ConfigurableEntityProvider implements Provider<Object> {
  68. @Override
  69. public Object get(ProvisionRequest<Object> request) {
  70. if (Configurable.class.isAssignableFrom(request.getRequestedType())) {
  71. try {
  72. Configurable entity = (Configurable) request.getRequestedType().newInstance();
  73. entity.configure();
  74. return entity;
  75. } catch (InstantiationException e) {
  76. return null;
  77. } catch (IllegalAccessException e) {
  78. return null;
  79. }
  80. } else {
  81. return null;
  82. }
  83. }
  84. }
  85.  
  86. @Test
  87. public void testWithoutMapping() {
  88. ModelMapper modelMapper = new ModelMapper();
  89. modelMapper.getConfiguration().setProvider(new ConfigurableEntityProvider());
  90. A1 aSource = new A1();
  91. B1 bSource = new B1();
  92. bSource.setProp("value");
  93. aSource.setB(bSource);
  94. A2 target = modelMapper.map(aSource, A2.class);
  95. B2 bTarget = target.getB();
  96. Assert.assertEquals("value", target.getB().getProp());
  97. bSource.setProp("value2");
  98. modelMapper.map(aSource, target);
  99. Assert.assertEquals("value2", target.getB().getProp());
  100. Assert.assertEquals(System.identityHashCode(bTarget), System.identityHashCode(target.getB()));
  101. }
  102.  
  103. @Test
  104. public void testWithMapping() {
  105. ModelMapper modelMapper = new ModelMapper();
  106. modelMapper.addMappings(new PropertyMap<A1, A2>() {
  107. @Override
  108. protected void configure() {
  109. map(source.getB()).setB(null);
  110. }
  111. });
  112. modelMapper.getConfiguration().setProvider(new ConfigurableEntityProvider());
  113. A1 aSource = new A1();
  114. B1 bSource = new B1();
  115. bSource.setProp("value");
  116. aSource.setB(bSource);
  117. A2 target = modelMapper.map(aSource, A2.class);
  118. B2 bTarget = target.getB();
  119. Assert.assertEquals("value", target.getB().getProp());
  120. bSource.setProp("value2");
  121. modelMapper.map(aSource, target);
  122. Assert.assertEquals("value2", target.getB().getProp());
  123. Assert.assertEquals(System.identityHashCode(bTarget), System.identityHashCode(target.getB()));
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement