Search in sources :

Example 1 with DbPrimitiveObject

use of org.simpleflatmapper.test.beans.DbPrimitiveObject in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithBoxReader.

@Test
public void testCustomReaderPrimitivesWithBoxReader() throws IOException {
    CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Boolean>() {

        @Override
        public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }
    })).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Byte>() {

        @Override
        public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }
    })).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Character>() {

        @Override
        public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }
    })).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Short>() {

        @Override
        public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }
    })).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Integer>() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }
    })).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Long>() {

        @Override
        public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }
    })).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Float>() {

        @Override
        public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }
    })).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Double>() {

        @Override
        public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }
    })).mapper();
    DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
    assertEquals(true, object.ispBoolean());
    assertEquals(35, object.getpByte());
    assertEquals(37, object.getpCharacter());
    assertEquals(39, object.getpShort());
    assertEquals(0x76666, object.getpInt());
    assertEquals(0xf76666l, object.getpLong());
    assertEquals(3.14f, object.getpFloat(), 0.0001);
    assertEquals(3.1526, object.getpDouble(), 0.000001);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) CellValueReader(org.simpleflatmapper.csv.CellValueReader) StringReader(java.io.StringReader) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) Test(org.junit.Test)

Example 2 with DbPrimitiveObject

use of org.simpleflatmapper.test.beans.DbPrimitiveObject in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithDirectReader.

@Test
public void testCustomReaderPrimitivesWithDirectReader() throws IOException {
    CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new BooleanCellValueReader() {

        @Override
        public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }

        @Override
        public boolean readBoolean(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }
    })).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new ByteCellValueReader() {

        @Override
        public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }

        @Override
        public byte readByte(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }
    })).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CharCellValueReader() {

        @Override
        public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }

        @Override
        public char readChar(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }
    })).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new ShortCellValueReader() {

        @Override
        public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }

        @Override
        public short readShort(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }
    })).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new IntegerCellValueReader() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }

        @Override
        public int readInt(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }
    })).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new LongCellValueReader() {

        @Override
        public long readLong(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }

        @Override
        public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }
    })).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new FloatCellValueReader() {

        @Override
        public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }

        @Override
        public float readFloat(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }
    })).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new DoubleCellValueReader() {

        @Override
        public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }

        @Override
        public double readDouble(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }
    })).mapper();
    DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
    assertEquals(true, object.ispBoolean());
    assertEquals(35, object.getpByte());
    assertEquals(37, object.getpCharacter());
    assertEquals(39, object.getpShort());
    assertEquals(0x76666, object.getpInt());
    assertEquals(0xf76666l, object.getpLong());
    assertEquals(3.14f, object.getpFloat(), 0.0001);
    assertEquals(3.1526, object.getpDouble(), 0.000001);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) StringReader(java.io.StringReader) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)2 Test (org.junit.Test)2 ParsingContext (org.simpleflatmapper.csv.ParsingContext)2 DbPrimitiveObject (org.simpleflatmapper.test.beans.DbPrimitiveObject)2 CellValueReader (org.simpleflatmapper.csv.CellValueReader)1