Skip to content

Commit

Permalink
Test for types and converters in inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
greenrobot committed Aug 3, 2016
1 parent a6192e6 commit c8f7167
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.greenrobot.greendao.test.entityannotation;

import org.greenrobot.greendao.test.AbstractDaoTestLongPk;
import org.greenrobot.greendao.test.entityannotation.TypesInInnerClass.MyInnerType;

public class TypesInInnerClassTest extends AbstractDaoTestLongPk<TypesInInnerClassDao, TypesInInnerClass> {

public TypesInInnerClassTest() {
super(TypesInInnerClassDao.class);
}

@Override
protected TypesInInnerClass createEntity(Long key) {
TypesInInnerClass entity = new TypesInInnerClass();
entity.setId(key);
entity.setType(new MyInnerType("cafe"));
return entity;
}

public void testType() {
TypesInInnerClass entity = createEntity(1L);
dao.insert(entity);
TypesInInnerClass entity2 = dao.load(1L);
assertNotSame(entity, entity2);
assertEquals("cafe", entity2.getType().value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.greenrobot.greendao.test.entityannotation;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.converter.PropertyConverter;

@Entity
public class TypesInInnerClass {
static class MyInnerType {

public MyInnerType(String value) {
this.value = value;
}

String value;
}

static class MyInnerTypeConverter implements PropertyConverter<MyInnerType, Long> {

@Override
public MyInnerType convertToEntityProperty(Long databaseValue) {
return databaseValue!=null? new MyInnerType(Long.toHexString(databaseValue)): null;
}

@Override
public Long convertToDatabaseValue(MyInnerType entityProperty) {
return entityProperty!= null? Long.parseLong(entityProperty.value, 16): null;
}
}

@Id
Long id;

@Convert(converter = MyInnerTypeConverter.class, columnType = Long.class)
MyInnerType type;

public MyInnerType getType() {
return this.type;
}

public void setType(MyInnerType type) {
this.type = type;
}

public Long getId() {
return this.id;
}

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

@Generated(hash = 1322981681)
public TypesInInnerClass(Long id, MyInnerType type) {
this.id = id;
this.type = type;
}

@Generated(hash = 1754325029)
public TypesInInnerClass() {
}

}

0 comments on commit c8f7167

Please sign in to comment.