2014-11-19 23:51:53 +01:00
/ * *
*
2018-05-09 23:06:12 +02:00
* Copyright 2014 Anno van Vliet , All rights reserved .
2014-11-19 23:51:53 +01:00
*
* Licensed under the Apache License , Version 2 . 0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
package org.jivesoftware.smackx.xdatalayout.packet ;
2020-04-06 13:25:07 +02:00
import static org.junit.jupiter.api.Assertions.assertEquals ;
import static org.junit.jupiter.api.Assertions.assertNotNull ;
2014-11-19 23:51:53 +01:00
import java.io.IOException ;
import java.io.InputStreamReader ;
import org.jivesoftware.smack.util.PacketParserUtils ;
2019-05-06 22:06:13 +02:00
import org.jivesoftware.smack.xml.XmlPullParser ;
import org.jivesoftware.smack.xml.XmlPullParserException ;
2017-06-14 17:12:43 +02:00
2014-11-19 23:51:53 +01:00
import org.jivesoftware.smackx.xdata.packet.DataForm ;
import org.jivesoftware.smackx.xdata.provider.DataFormProvider ;
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref ;
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref ;
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section ;
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text ;
import org.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider ;
2017-06-14 17:12:43 +02:00
2020-04-06 13:25:07 +02:00
import org.junit.jupiter.api.Test ;
2014-11-19 23:51:53 +01:00
/ * *
* Unit tests for DataForm reading and parsing .
*
* @author Anno van Vliet
*
* /
public class DataLayoutTest {
2016-01-18 14:35:28 +01:00
// @formatter:off
private static final String TEST_OUTPUT_2 =
" <page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'> "
+ " <fieldref var='testField1'/> "
+ " <section label='section Label'> "
+ " <text>SectionText</text> "
+ " </section> "
+ " <text>PageText</text> "
+ " </page> " ;
private static final String TEST_OUTPUT_SPECIAL =
" <page xmlns='http://jabber.org/protocol/xdata-layout' label='Label - & \ u00E9 \ u00E1 '> "
+ " <fieldref var='testField1'/> "
+ " <section label='section Label - & \ u00E9 \ u00E1 '> "
+ " <text>SectionText - & \ u00E9 \ u00E1 </text> "
+ " </section> "
+ " <text>PageText - & \ u00E9 \ u00E1 </text> "
2016-01-13 15:04:01 +01:00
+ " <section label='<html>Number of Persons by<br/> Nationality and Status</html>'> "
2016-01-18 14:35:28 +01:00
+ " <reportedref/> "
+ " </section> "
+ " <text><html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html></text> "
+ " </page> " ;
// @formatter:on
2014-11-19 23:51:53 +01:00
private static final String TEST_INPUT_1 = " xdata-layout-sample.xml " ;
@Test
2015-03-13 09:27:18 +01:00
public void testLayout ( ) throws Exception {
2014-11-19 23:51:53 +01:00
DataLayout layout = new DataLayout ( " Label " ) ;
Fieldref reffield = new Fieldref ( " testField1 " ) ;
layout . getPageLayout ( ) . add ( reffield ) ;
Section section = new Section ( " section Label " ) ;
section . getSectionLayout ( ) . add ( new Text ( " SectionText " ) ) ;
layout . getPageLayout ( ) . add ( section ) ;
2016-11-29 12:01:41 +01:00
layout . getPageLayout ( ) . add ( new Text ( " PageText " ) ) ;
2015-03-17 11:33:02 +01:00
2019-02-04 13:27:41 +01:00
assertNotNull ( layout . toXML ( ) ) ;
String output = layout . toXML ( ) . toString ( ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( TEST_OUTPUT_2 , output ) ;
2015-03-17 11:33:02 +01:00
2014-11-22 23:16:45 +01:00
XmlPullParser parser = PacketParserUtils . getParserFor ( output ) ;
2015-03-17 11:33:02 +01:00
2014-11-20 23:52:01 +01:00
layout = DataLayoutProvider . parse ( parser ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
assertEquals ( 3 , layout . getPageLayout ( ) . size ( ) ) ;
assertEquals ( " Label " , layout . getLabel ( ) ) ;
2019-02-04 13:27:41 +01:00
assertNotNull ( layout . toXML ( ) ) ;
output = layout . toXML ( ) . toString ( ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( TEST_OUTPUT_2 , output ) ;
}
@Test
2015-03-24 21:38:21 +01:00
public void testLayoutSpecialCharacters ( ) throws XmlPullParserException , IOException {
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
DataLayout layout = new DataLayout ( " Label - & \ u00E9 \ u00E1 " ) ;
Fieldref reffield = new Fieldref ( " testField1 " ) ;
layout . getPageLayout ( ) . add ( reffield ) ;
Section section = new Section ( " section Label - & \ u00E9 \ u00E1 " ) ;
2016-11-29 12:01:41 +01:00
section . getSectionLayout ( ) . add ( new Text ( " SectionText - & \ u00E9 \ u00E1 " ) ) ;
2014-11-19 23:51:53 +01:00
layout . getPageLayout ( ) . add ( section ) ;
2016-11-29 12:01:41 +01:00
layout . getPageLayout ( ) . add ( new Text ( " PageText - & \ u00E9 \ u00E1 " ) ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
section = new Section ( " <html>Number of Persons by<br/> Nationality and Status</html> " ) ;
section . getSectionLayout ( ) . add ( new Reportedref ( ) ) ;
layout . getPageLayout ( ) . add ( section ) ;
2016-11-29 12:01:41 +01:00
layout . getPageLayout ( ) . add ( new Text ( " <html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html> " ) ) ;
2014-11-19 23:51:53 +01:00
2015-03-17 11:33:02 +01:00
2019-02-04 13:27:41 +01:00
assertNotNull ( layout . toXML ( ) ) ;
String output = layout . toXML ( ) . toString ( ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( TEST_OUTPUT_SPECIAL , output ) ;
2015-03-17 11:33:02 +01:00
2014-11-22 23:16:45 +01:00
XmlPullParser parser = PacketParserUtils . getParserFor ( output ) ;
2015-03-17 11:33:02 +01:00
2014-11-20 23:52:01 +01:00
layout = DataLayoutProvider . parse ( parser ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
assertEquals ( 5 , layout . getPageLayout ( ) . size ( ) ) ;
assertEquals ( " Label - & \ u00E9 \ u00E1 " , layout . getLabel ( ) ) ;
section = ( Section ) layout . getPageLayout ( ) . get ( 1 ) ;
assertEquals ( " section Label - & \ u00E9 \ u00E1 " , section . getLabel ( ) ) ;
2017-05-23 16:45:04 +02:00
Text text = ( Text ) layout . getPageLayout ( ) . get ( 2 ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( " PageText - & \ u00E9 \ u00E1 " , text . getText ( ) ) ;
section = ( Section ) layout . getPageLayout ( ) . get ( 3 ) ;
assertEquals ( " <html>Number of Persons by<br/> Nationality and Status</html> " , section . getLabel ( ) ) ;
text = ( Text ) layout . getPageLayout ( ) . get ( 4 ) ;
assertEquals ( " <html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html> " , text . getText ( ) ) ;
2015-03-17 11:33:02 +01:00
2019-02-04 13:27:41 +01:00
assertNotNull ( layout . toXML ( ) ) ;
output = layout . toXML ( ) . toString ( ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( TEST_OUTPUT_SPECIAL , output ) ;
}
@Test
2015-03-13 09:27:18 +01:00
public void testLayoutFromFile ( ) throws Exception {
2014-11-19 23:51:53 +01:00
DataFormProvider pr = new DataFormProvider ( ) ;
2015-03-17 11:33:02 +01:00
2019-05-06 22:06:13 +02:00
InputStreamReader inputStreamReader = new InputStreamReader ( this . getClass ( ) . getResourceAsStream ( TEST_INPUT_1 ) , " UTF-8 " ) ;
XmlPullParser parser = PacketParserUtils . getParserFor ( inputStreamReader ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
DataForm form = pr . parse ( parser ) ;
2016-11-29 12:01:41 +01:00
assertNotNull ( form ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( 1 , form . getExtensionElements ( ) . size ( ) ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
DataLayout layout = ( DataLayout ) form . getExtensionElements ( ) . get ( 0 ) ;
2015-03-17 11:33:02 +01:00
2014-11-19 23:51:53 +01:00
assertEquals ( 5 , layout . getPageLayout ( ) . size ( ) ) ;
assertEquals ( " Label - & \ u00E9 \ u00E1 " , layout . getLabel ( ) ) ;
Section section = ( Section ) layout . getPageLayout ( ) . get ( 1 ) ;
assertEquals ( " section Label - & \ u00E9 \ u00E1 " , section . getLabel ( ) ) ;
2017-05-23 16:45:04 +02:00
Text text = ( Text ) layout . getPageLayout ( ) . get ( 2 ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( " PageText - & \ u00E9 \ u00E1 " , text . getText ( ) ) ;
section = ( Section ) layout . getPageLayout ( ) . get ( 3 ) ;
assertEquals ( " <html>Number of Persons by<br/> Nationality and Status</html> " , section . getLabel ( ) ) ;
text = ( Text ) layout . getPageLayout ( ) . get ( 4 ) ;
assertEquals ( " <html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html> " , text . getText ( ) ) ;
2015-03-17 11:33:02 +01:00
2019-02-04 13:27:41 +01:00
assertNotNull ( layout . toXML ( ) ) ;
String output = layout . toXML ( ) . toString ( ) ;
2014-11-19 23:51:53 +01:00
assertEquals ( TEST_OUTPUT_SPECIAL , output ) ;
}
}