tommysiu / beanio

Automatically exported from code.google.com/p/beanio
Apache License 2.0
0 stars 0 forks source link

@Group in beanio api does not work with FixedLengthParserBuilder #115

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?  If applicable, please provide a
mapping configuration and sample record input to recreate the problem.
1.the test class in groovy and using Spock testing framework is as follows: 

    package com.apps.abc.transfer

    import org.beanio.builder.FixedLengthParserBuilder
    import spock.lang.Specification
    import org.beanio.*
    import org.beanio.annotation.*
    import org.beanio.builder.StreamBuilder

    class test extends Specification
    {
    BeanWriter out;
    def setup()
    {
        StreamFactory streamFactory = StreamFactory.newInstance();
        StreamBuilder builder2 = new StreamBuilder("abc")
                .format("fixedlength")
                .parser(new FixedLengthParserBuilder())
                .addGroup(Main.class);

        streamFactory.define(builder2);
        StringWriter writer = new StringWriter()
        out = streamFactory.createWriter("abc", writer);
    }

    def "should write file"()
    {
        given:
        Header header = new Header()
        header.name = "A"
        BatchHeader batchHeader = new BatchHeader()
        batchHeader.name = "B"

        BatchDetail batchDetail = new BatchDetail()
        batchDetail.name = "C"

        BatchDetail batchDetail2 = new BatchDetail()
        batchDetail2.name = "E"

        BatchFooter batchFooter = new BatchFooter()
        batchFooter.name = "F"

        Batch batch = new Batch()
        batch.batchHeader = batchHeader
        batch.batchFooter = batchFooter
        ArrayList<BatchDetail> arrayList = new ArrayList<>()
        arrayList.add(batchDetail)
        arrayList.add(batchDetail2)
        batch.details = arrayList

        Main main = new Main()
        main.header = header
        main.batch = batch

        when:
        out.write(main)

        then:
        println("out : " + out)
    }

    @Group
    static class Main {
        @Record
        public Header header;
        @Group
        public Batch batch;
    }

    static class Header {
        @Field(at=0, rid=true, literal="H", length = 1 )
        public String type;
        @Field(at=1, length = 1)
        public String name;
    }

    static class Batch {
        @Record(order=1, maxOccurs=1)
        public BatchHeader batchHeader;
        @Record(order=2)
        public List<BatchDetail> details;
        @Record(order=3, maxOccurs=1)
        public BatchFooter batchFooter;
    }

    static class BatchHeader {
        @Field(at=0, rid=true, literal="BH", length = 2)
        public String type;
        @Field(at=1, length = 1)
        public String name;
    }

    static class BatchDetail {
        @Field(at=0, rid=true, literal="BD", length = 2)
        public String type;
        @Field(at=1, length = 1)
        public String name;
    }

    static class BatchFooter {
        @Field(at=0, rid=true, literal="BF", length = 2)
        public String type;
        @Field(at=1, length = 1)
        public String name;
    }

    }

What is the expected output? What do you see instead?
The file should be written out. Instead I get and error

What version of BeanIO are you using? What JDK version?
2.1.0.M2

Please provide any additional information below.
I am trying to use @Group with beanio. But I keep getting this error

    org.beanio.BeanWriterException: Bean identification failed: no record or group mapping for bean class 'class com.apps.abc.transfer.test$Main' at the current position
        at org.beanio.internal.parser.BeanWriterImpl.write(BeanWriterImpl.java:81)
        at org.beanio.internal.parser.BeanWriterImpl.write(BeanWriterImpl.java:51)
        at com.surveysampling.apps.rewardbatchprocessor.avios.transfer.test.should write file(test.groovy:81)

Original issue reported on code.google.com by joeysmit...@gmail.com on 24 Jul 2014 at 1:51

GoogleCodeExporter commented 8 years ago
I admit the error message may be confusing, but the issue is you did not set 
the record identifying fields.

header.type = "H"
batchHeader.type = "BH"
... etc

Original comment by kevin.s...@gmail.com on 27 Jul 2014 at 7:57