package org.dozer.functional_tests.annotation;

import org.dozer.Mapping;
import org.dozer.functional_tests.AbstractFunctionalTest;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

/**
 * @author dmitry.buzdin
 */
public class AnnotationMappingTest extends AbstractFunctionalTest {

  private User source;
  private UserDto destination;

  @Before
  public void setUp() throws Exception {
    super.setUp();
    source = new User();
    destination = new UserDto();
  }

  @Test
  public void shouldMapProperies() {
    source.setId(1L);

    UserDto result = mapper.map(source, UserDto.class);
    
    assertThat(result.getPk(), equalTo("1"));
    assertThat(result.getId(), nullValue());
  }

  @Test
  public void shouldMapProperies_Backwards() {
    source.setAge(new Short("1"));

    UserDto result = mapper.map(source, UserDto.class);
                                         
    assertThat(result.getYears(), equalTo("1"));
  }

  @Test
  public void shouldMapFields() {
    source.name = "name";

    UserDto result = mapper.map(source, UserDto.class);

    assertThat(result.username, equalTo("name"));
    assertThat(result.name, nullValue());
  }

  @Test
  public void shouldMapFields_Backwards() {
    source.freeText = "text";

    UserDto result = mapper.map(source, UserDto.class);

    assertThat(result.comment, equalTo("text"));
  }

  public static class User {
    Long id;
    Short age;

    @Mapping("username")
    private String name;

    String freeText;

    @Mapping("pk")
    public Long getId() {
      return id;
    }

    public void setId(Long id) {
      this.id = id;
    }

    public Short getAge() {
      return age;
    }

    public void setAge(Short age) {
      this.age = age;
    }
  }

  public static class UserDto {
    String id;
    String years;
    String pk;
    String name;
    String username;
    @Mapping("freeText")
    String comment;

    public String getPk() {
      return pk;
    }

    public void setPk(String pk) {
      this.pk = pk;
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    @Mapping("age")
    public String getYears() {
      return years;
    }

    public void setYears(String years) {
      this.years = years;
    }
  }

}

@@ -443,16 +443,45 @@
 source;%0A
+  private SubUser subSource;%0A
   privat
@@ -503,16 +503,16 @@
 nation;%0A
-
 %0A  @Befo
@@ -595,24 +595,55 @@
 new User();%0A
+    subSource = new SubUser();%0A
     destinat
@@ -1193,21 +1193,23 @@
 rce.
-name = 
+setName(
 %22name%22
+)
 ;%0A%0A 
@@ -1551,24 +1551,475 @@
 xt%22));%0A  %7D%0A%0A
+  @Test%0A  public void shouldMapFields_Inherited() %7B%0A    subSource.setName(%22name%22);%0A%0A    UserDto result = mapper.map(subSource, UserDto.class);%0A%0A    assertThat(result.username, equalTo(%22name%22));%0A    assertThat(result.name, nullValue());%0A  %7D%0A%0A  @Test%0A  public void shouldMapFields_Backwards_Inherited() %7B%0A    subSource.freeText = %22text%22;%0A%0A    UserDto result = mapper.map(subSource, UserDto.class);%0A%0A    assertThat(result.comment, equalTo(%22text%22));%0A  %7D%0A%0A
   public sta
@@ -2383,18 +2383,135 @@
 ;%0A    %7D%0A
+%0A
   
+  public void setName(String name) %7B%0A      this.name = name;%0A    %7D%0A  %7D%0A%0A  public static class SubUser extends User %7B
 %7D%0A%0A  pub

test case for #93