Skip to content

Commit 6c5ee64

Browse files
committed
Test coverage simple table
1 parent f627618 commit 6c5ee64

File tree

3 files changed

+113
-22
lines changed

3 files changed

+113
-22
lines changed

fj-doc-lib-simpletable/src/main/java/org/fugerit/java/doc/lib/simpletable/SimpleTableFacade.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package org.fugerit.java.doc.lib.simpletable;
22

3-
import java.util.Arrays;
43
import java.util.List;
5-
import java.util.stream.Collectors;
64

7-
import org.fugerit.java.core.cfg.ConfigRuntimeException;
8-
import org.fugerit.java.core.lang.helpers.StringUtils;
95
import org.fugerit.java.doc.lib.simpletable.model.SimpleTable;
106

117
public class SimpleTableFacade {
@@ -22,10 +18,7 @@ private SimpleTableFacade() {}
2218
* @return the new table model initialized
2319
*/
2420
public static SimpleTable newTable( Integer... colWidths ) {
25-
if ( colWidths == null ) {
26-
throw new ConfigRuntimeException( "Minimum one colunm must be provided" );
27-
}
28-
return newTable( Arrays.asList( colWidths ) );
21+
return newHelper().newTable( colWidths );
2922
}
3023

3124
/**
@@ -38,18 +31,7 @@ public static SimpleTable newTable( Integer... colWidths ) {
3831
* @return the new table model initialized
3932
*/
4033
public static SimpleTable newTable( List<Integer> colWidths ) {
41-
if ( colWidths == null ) {
42-
throw new ConfigRuntimeException( "Minimum one colunm must be provided" );
43-
} else {
44-
int sum = 0;
45-
for ( int v : colWidths ) {
46-
sum+=v;
47-
}
48-
if ( sum != 100 ) {
49-
throw new ConfigRuntimeException( "Column width sum must be 100, while is : "+sum );
50-
}
51-
}
52-
return new SimpleTable( StringUtils.concat( ";" , colWidths.stream().map( v -> v.toString() ).collect( Collectors.toList() ) ) );
34+
return newHelper().newTable( colWidths );
5335
}
5436

5537
/**

fj-doc-lib-simpletable/src/main/java/org/fugerit/java/doc/lib/simpletable/SimpleTableHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Integer getDefaultBorderWidth() {
3535
* @return the new table model initialized
3636
*/
3737
public SimpleTable newTable( Integer... colWidths ) {
38-
if ( colWidths == null ) {
38+
if ( colWidths == null || colWidths.length == 0 ) {
3939
throw new ConfigRuntimeException( "Minimum one colunm must be provided" );
4040
}
4141
return newTable( Arrays.asList( colWidths ) );
@@ -51,7 +51,7 @@ public SimpleTable newTable( Integer... colWidths ) {
5151
* @return the new table model initialized
5252
*/
5353
public SimpleTable newTable( List<Integer> colWidths ) {
54-
if ( colWidths == null ) {
54+
if ( colWidths == null || colWidths.isEmpty() ) {
5555
throw new ConfigRuntimeException( "Minimum one colunm must be provided" );
5656
} else {
5757
int sum = 0;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)