|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.example; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static org.fest.assertions.Assertions.assertThat; |
| 11 | + |
| 12 | +public class ConversionTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void shouldApplyConversions() { |
| 16 | + Source source = new Source(); |
| 17 | + source.setFoo(42); |
| 18 | + source.setBar(23L); |
| 19 | + source.setZip(73); |
| 20 | + |
| 21 | + Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source); |
| 22 | + |
| 23 | + assertThat(target).isNotNull(); |
| 24 | + assertThat(target.getFoo()).isEqualTo(42L); |
| 25 | + assertThat(target.getBar()).isEqualTo(23); |
| 26 | + assertThat(target.getZip()).isEqualTo("73"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void shouldHandleNulls() { |
| 31 | + Source source = new Source(); |
| 32 | + Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source); |
| 33 | + |
| 34 | + assertThat(target).isNotNull(); |
| 35 | + assertThat(target.getFoo()).isEqualTo(0L); |
| 36 | + assertThat(target.getBar()).isEqualTo(0); |
| 37 | + assertThat(target.getZip()).isEqualTo("0"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldApplyConversionsToMappedProperties() { |
| 42 | + Source source = new Source(); |
| 43 | + source.setQax(42); |
| 44 | + source.setBaz(23L); |
| 45 | + |
| 46 | + Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source); |
| 47 | + |
| 48 | + assertThat(target).isNotNull(); |
| 49 | + assertThat(target.getBaz()).isEqualTo(42L); |
| 50 | + assertThat(target.getQax()).isEqualTo(23); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldApplyConversionsForReverseMapping() { |
| 55 | + Target target = new Target(); |
| 56 | + target.setFoo(42L); |
| 57 | + target.setBar(23); |
| 58 | + target.setZip("73"); |
| 59 | + |
| 60 | + Source source = SourceTargetMapper.INSTANCE.targetToSource(target); |
| 61 | + |
| 62 | + assertThat(source).isNotNull(); |
| 63 | + assertThat(source.getFoo()).isEqualTo(42); |
| 64 | + assertThat(source.getBar()).isEqualTo(23); |
| 65 | + assertThat(source.getZip()).isEqualTo(73); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldApplyConversionsToMappedPropertiesForReverseMapping() { |
| 70 | + Target target = new Target(); |
| 71 | + target.setQax(42); |
| 72 | + target.setBaz(23L); |
| 73 | + |
| 74 | + Source source = SourceTargetMapper.INSTANCE.targetToSource(target); |
| 75 | + |
| 76 | + assertThat(source).isNotNull(); |
| 77 | + assertThat(source.getBaz()).isEqualTo(42); |
| 78 | + assertThat(source.getQax()).isEqualTo(23); |
| 79 | + } |
| 80 | +} |
0 commit comments