1
+ package test .org .fugerit .java .doc .lib .simpletable ;
2
+
3
+ import java .io .ByteArrayOutputStream ;
4
+ import java .util .ArrayList ;
5
+ import java .util .List ;
6
+
7
+ import org .fugerit .java .core .cfg .ConfigRuntimeException ;
8
+ import org .fugerit .java .core .function .SafeFunction ;
9
+ import org .fugerit .java .core .lang .helpers .BooleanUtils ;
10
+ import org .fugerit .java .doc .base .config .DocConfig ;
11
+ import org .fugerit .java .doc .base .config .DocException ;
12
+ import org .fugerit .java .doc .base .config .DocInput ;
13
+ import org .fugerit .java .doc .base .config .DocOutput ;
14
+ import org .fugerit .java .doc .base .config .DocTypeHandlerDefault ;
15
+ import org .fugerit .java .doc .freemarker .html .FreeMarkerHtmlTypeHandler ;
16
+ import org .fugerit .java .doc .lib .simpletable .SimpleTableDocConfig ;
17
+ import org .fugerit .java .doc .lib .simpletable .SimpleTableFacade ;
18
+ import org .fugerit .java .doc .lib .simpletable .SimpleTableHelper ;
19
+ import org .fugerit .java .doc .lib .simpletable .model .SimpleCell ;
20
+ import org .fugerit .java .doc .lib .simpletable .model .SimpleRow ;
21
+ import org .fugerit .java .doc .lib .simpletable .model .SimpleTable ;
22
+ import org .junit .Assert ;
23
+ import org .junit .Test ;
24
+
25
+ public class TestSimpleTable {
26
+
27
+ private void testTable ( SimpleTable table , SimpleTableHelper helper ) {
28
+ table .addRow ( helper .newHeaderRow ( "H1" , "H2" ) );
29
+ table .addRow ( helper .newNormalRow ( "C1" , "C2" ) );
30
+ table .withDocLanguage ( "it" ).withSheetName ( "a" ).withTableWidth ( "1" );
31
+ table .setDocLanguage ( "en" );
32
+ table .setSheetName ( "b" );
33
+ table .getDefaultBorderWidth ();
34
+ table .setDefaultBorderWidth ( SimpleCell .BORDER_WIDTH_UNSET );
35
+ SimpleCell cell = new SimpleCell ( "a" , SimpleCell .BORDER_WIDTH_UNSET );
36
+ SimpleRow row = new SimpleRow ();
37
+ row .addCell ( cell );
38
+ table .addRow ( row );
39
+ SafeFunction .apply ( () -> {
40
+ try ( ByteArrayOutputStream baos = new ByteArrayOutputStream () ) {
41
+ SimpleTableDocConfig .newConfigLatest ().processSimpleTable ( table , FreeMarkerHtmlTypeHandler .HANDLER , baos );
42
+ Assert .assertTrue ( baos .toByteArray ().length > 0 );
43
+ }
44
+ } );
45
+ Assert .assertThrows ( DocException .class , () -> {
46
+ try ( ByteArrayOutputStream baos = new ByteArrayOutputStream () ) {
47
+ SimpleTableDocConfig .newConfigLatest ().processSimpleTable ( table , new FailTypeHandler (), baos );
48
+ Assert .assertTrue ( baos .toByteArray ().length > 0 );
49
+ }
50
+ } );
51
+ }
52
+
53
+ @ Test
54
+ public void testHelper () {
55
+ Integer defaultBorder = 0 ;
56
+ SimpleTableHelper helper = SimpleTableFacade .newHelper ().withDefaultBorderWidth ( defaultBorder );
57
+ Assert .assertEquals ( defaultBorder , helper .getDefaultBorderWidth () );
58
+ List <Integer > lcw = new ArrayList <Integer >();
59
+ Assert .assertThrows ( ConfigRuntimeException .class , () -> helper .newTable ( lcw ) );
60
+ Assert .assertThrows ( ConfigRuntimeException .class , () -> helper .newTable () );
61
+ List <Integer > colWidths = null ;
62
+ Assert .assertThrows ( ConfigRuntimeException .class , () -> helper .newTable ( colWidths ) );
63
+ Integer [] colWidthsA = null ;
64
+ Assert .assertThrows ( ConfigRuntimeException .class , () -> helper .newTable ( colWidthsA ) );
65
+ Integer [] colWidths50 = { 20 , 30 };
66
+ Assert .assertThrows ( ConfigRuntimeException .class , () -> helper .newTable ( colWidths50 ) );
67
+ // new table ok
68
+ Assert .assertNotNull ( helper .newTableSingleColumn () );
69
+ SimpleTable table = helper .newTable ( 50 , 50 );
70
+ this .testTable (table , helper );
71
+ }
72
+
73
+ @ Test
74
+ public void testFacade () {
75
+ Assert .assertNotNull ( SimpleTableFacade .newTable ( 100 ) );
76
+ Assert .assertNotNull ( SimpleTableFacade .newTableSingleColumn () );
77
+ List <Integer > colW = new ArrayList <>();
78
+ colW .add ( 100 );
79
+ Assert .assertNotNull ( SimpleTableFacade .newTable ( colW ) );
80
+ }
81
+
82
+ @ Test
83
+ public void testCell () {
84
+ SimpleCell cell = new SimpleCell ( "test" , 10 ).bold ().bolditalic ().italic ().left ().rigt ().underline ();
85
+ cell .setContent ( "aaaa" );
86
+ Assert .assertNotNull ( SimpleCell .newCell ( "bbb" ) );
87
+ SimpleRow row = new SimpleRow ();
88
+ row .addCell ( "cccc" );
89
+ row .setHead ( BooleanUtils .BOOLEAN_TRUE );
90
+ }
91
+
92
+ }
93
+
94
+ class FailTypeHandler extends DocTypeHandlerDefault {
95
+
96
+ private static final long serialVersionUID = -938363784671460227L ;
97
+
98
+ public FailTypeHandler () {
99
+ super ( DocConfig .TYPE_PDF , "fail-pdf" );
100
+ }
101
+
102
+ @ Override
103
+ public void handle (DocInput docInput , DocOutput docOutput ) throws Exception {
104
+ if ( DocConfig .TYPE_PDF .equalsIgnoreCase ( docInput .getType () ) ) {
105
+ throw new DocException ( "Scenario exception" );
106
+ }
107
+ }
108
+
109
+ }
0 commit comments