I'm increasingly convinced that is a problem of ModelMapper.
I find this model mapper issue: 
https://github.com/jhalterman/modelmapper/issues/13

I've attached a test to reproduce the problem.
The test shows that you cannot use an interface as properties source to map 
properties.
So I bypassed the problem using implementation class.

Il giorno mercoledì 9 ottobre 2013 14:48:31 UTC, Lukas Eder ha scritto:
>
> I'm not sure what you mean by this. 
>
>> But in Jooq 3.1.0 RecordImpl class is a package protected, so I need to 
>> “patch” it including this one in my source code tree and set the public 
>> visibility.
>>
>
I copied org.jooq.impl.RecordImpl java file from jooq sources, into my 
project, and then I have changed class vibility:
public class RecordImpl .... {
 //class code remains unchanged
}

Why do you need to patch RecordImpl? I don't see this from your example, 
> above... 
>
>> Can you make this class public?
>>
> Nope :-)
> Most classes in the org.jooq.impl package are package-private for a good 
> reason. I do not want to maintain an internal API through semantic 
> versioning.
>

Good point.
I agree with you :)
 

> You can create a record (which is in fact a RecordImpl) using 
> DSLContext.newRecord():
>
> http://www.jooq.org/javadoc/latest/org/jooq/DSLContext.html#newRecord(org.jooq.Table)
>
Cheers
Daniele 

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
package com.fhoster.livebase.dashboardDatabase;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.convention.NameTokenizers;
import org.modelmapper.spi.ValueReader;

public class ModelMapperTest {
  static interface MyRecord {
    Object getValue(String name);

    Collection<String> names();
  }

  static class MyRecordImpl implements MyRecord {
    private Map<String, Object> map = new HashMap<String, Object>();

    @Override
    public Object getValue(final String name) {
      return map.get(name);
    }

    @Override
    public Collection<String> names() {
      return map.keySet();
    }

    void addValue(final String key, final Object obj) {
      map.put(key, obj);
    }
  }

  static class MyRecordReader implements ValueReader<MyRecord> {
    @Override
    public Object get(final MyRecord source, final String memberName) {
      Object value = source.getValue(memberName.toLowerCase());
      return value;
    }

    @Override
    public Collection<String> memberNames(final MyRecord source) {
      return source.names();
    }

    @Override
    public String toString() {
      return "jOOQ";
    }
  }

  static class OtherClass {
    private String asd;
    private String email;

    public String getAsd() {
      return asd;
    }

    public String getEmail() {
      return email;
    }

    public void setAsd(final String asd) {
      this.asd = asd;
    }

    public void setEmail(final String email) {
      this.email = email;
    }
  }

  private static MyRecord getMyRecord() {
    MyRecordImpl r = new MyRecordImpl();
    r.addValue("password_hash", "hash");
    r.addValue("email", "[email protected]");
    return r;
  }

  @Test
  public void modelMapperTest_shouldWorks() throws Exception {
    MyRecord r = getMyRecord();

    ModelMapper mp = new ModelMapper();
    mp.getConfiguration().addValueReader(new MyRecordReader());
    mp.getConfiguration().setSourceNameTokenizer(NameTokenizers.UNDERSCORE);
    mp.addMappings(new PropertyMap<MyRecord, OtherClass>() {
      @Override
      protected void configure() {
        map(source("password_hash")).setAsd(null);
        map(source("email")).setEmail(null);
      }
    });

    OtherClass c = mp.map(r, OtherClass.class);
    assertThat(c.getEmail(), is(r.getValue("email")));
    assertThat(c.getAsd(), is(r.getValue("password_hash")));
  }

  @Test
  public void modelMapperTest_works() throws Exception {
    MyRecord r = getMyRecord();

    ModelMapper mp = new ModelMapper();
    mp.getConfiguration().addValueReader(new MyRecordReader());
    mp.getConfiguration().setSourceNameTokenizer(NameTokenizers.UNDERSCORE);
    mp.addMappings(new PropertyMap<MyRecordImpl, OtherClass>() {
      @Override
      protected void configure() {
        map(source("password_hash")).setAsd(null);
        map(source("email")).setEmail(null);
      }
    });

    OtherClass c = mp.map(r, OtherClass.class);
    assertThat(c.getEmail(), is(r.getValue("email")));
    assertThat(c.getAsd(), is(r.getValue("password_hash")));
  }
}

Reply via email to