0

I want Bean.Io mapping to through exception when records are not present in file(Blank File). But it's not happening.Though it has validation occurs="0+" in place . Also tried minOccurs=1 maxOccurs=unbounded

My mapping file

<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03">

    <stream name="Records" format="fixedlength"  strict="true">

        <record name="SampleRecord" class="com.test.SampleRecord" **occurs="0+"**>

            <field name="mobileNumber" type="string" position="0" length="10" regex="[0-9]*" required="true"/>
            <field name="alternateMobileNumber" type="string" position="10" length="20" regex="[0-9]*" required="false"/>

        </record>
    </stream>

</beanio>
ammy
  • 306
  • 1
  • 6
  • 23

1 Answers1

1

You can try this mapping.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

    <stream name="Records" format="fixedlength"  strict="true" minOccurs="1">
      <record name="SampleRecord" class="com.test.SampleRecord" occurs="0+">
        <field name="mobileNumber" type="string" position="0" length="10" regex="[0-9]*" required="true"/>
        <field name="alternateMobileNumber" type="string" position="10" length="20" regex="[0-9]*" required="false"/>
      </record>
    </stream>

</beanio>

Note the attribute minOccurs="1" on the stream element. The documentation states this:

minOccurs - The minimum number of times the record layout must be read from an input stream. Defaults to 0.

Thus, changing minOccurs to 1 causes BeanIO to throw an exception with an empty string as input.

nicoschl
  • 2,346
  • 1
  • 17
  • 17
  • Could you please mark it as accepted? It will make it easier for other people to know the correct solution – nicoschl Aug 25 '21 at 10:40