Skip to content

Commit d6f1c34

Browse files
committed
Cover solid, dashed and dotted lines with tests.
Done during the third technical debt session.
1 parent a89c6ab commit d6f1c34

File tree

3 files changed

+380
-0
lines changed

3 files changed

+380
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf.canvas.draw;
44+
45+
import com.itextpdf.kernel.colors.ColorConstants;
46+
import com.itextpdf.kernel.font.PdfFontFactory;
47+
import com.itextpdf.kernel.geom.AffineTransform;
48+
import com.itextpdf.kernel.geom.Matrix;
49+
import com.itextpdf.kernel.geom.Rectangle;
50+
import com.itextpdf.kernel.pdf.PdfDocument;
51+
import com.itextpdf.kernel.pdf.PdfString;
52+
import com.itextpdf.kernel.pdf.PdfWriter;
53+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
54+
import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;
55+
import com.itextpdf.kernel.pdf.canvas.parser.listener.CharacterRenderInfo;
56+
import com.itextpdf.test.ExtendedITextTest;
57+
import com.itextpdf.test.annotations.type.UnitTest;
58+
import org.junit.Assert;
59+
import org.junit.Test;
60+
import org.junit.experimental.categories.Category;
61+
62+
import java.io.ByteArrayOutputStream;
63+
import java.io.IOException;
64+
import java.util.Stack;
65+
66+
@Category(UnitTest.class)
67+
public class DashedLineTest extends ExtendedITextTest {
68+
69+
@Test
70+
public void defaultDashedLineTest01() {
71+
DashedLine dashedLine = new DashedLine();
72+
73+
Assert.assertEquals(ColorConstants.BLACK, dashedLine.getColor());
74+
Assert.assertEquals(1, dashedLine.getLineWidth(), 0.0001);
75+
}
76+
77+
78+
@Test
79+
public void dashedLineWithSetWidthTest01() {
80+
DashedLine dashedLine = new DashedLine(20);
81+
82+
Assert.assertEquals(ColorConstants.BLACK, dashedLine.getColor());
83+
Assert.assertEquals(20, dashedLine.getLineWidth(), 0.0001);
84+
}
85+
86+
@Test
87+
public void dashedLineSettersTest01() {
88+
DashedLine dashedLine = new DashedLine(15);
89+
Assert.assertEquals(ColorConstants.BLACK, dashedLine.getColor());
90+
Assert.assertEquals(15, dashedLine.getLineWidth(), 0.0001);
91+
92+
dashedLine.setColor(ColorConstants.RED);
93+
Assert.assertEquals(ColorConstants.RED, dashedLine.getColor());
94+
95+
dashedLine.setLineWidth(10);
96+
Assert.assertEquals(10, dashedLine.getLineWidth(), 0.0001);
97+
}
98+
99+
@Test
100+
public void dashedLineDrawTest01() {
101+
String expectedContent = "q\n" +
102+
"15 w\n" +
103+
"0 0 0 RG\n" +
104+
"[2] 2 d\n" +
105+
"100 107.5 m\n" +
106+
"200 107.5 l\n" +
107+
"S\n" +
108+
"Q\n";
109+
110+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
111+
PdfCanvas canvas = new PdfCanvas(tempDoc.addNewPage());
112+
113+
DashedLine dashedLine = new DashedLine(15);
114+
dashedLine.draw(canvas, new Rectangle(100, 100, 100, 100));
115+
116+
byte[] writtenContentBytes = canvas.getContentStream().getBytes();
117+
118+
Assert.assertArrayEquals(expectedContent.getBytes(), writtenContentBytes);
119+
}
120+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf.canvas.draw;
44+
45+
import com.itextpdf.kernel.colors.ColorConstants;
46+
import com.itextpdf.kernel.font.PdfFontFactory;
47+
import com.itextpdf.kernel.geom.AffineTransform;
48+
import com.itextpdf.kernel.geom.Matrix;
49+
import com.itextpdf.kernel.geom.Rectangle;
50+
import com.itextpdf.kernel.pdf.PdfDocument;
51+
import com.itextpdf.kernel.pdf.PdfString;
52+
import com.itextpdf.kernel.pdf.PdfWriter;
53+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
54+
import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;
55+
import com.itextpdf.kernel.pdf.canvas.parser.listener.CharacterRenderInfo;
56+
import com.itextpdf.test.ExtendedITextTest;
57+
import com.itextpdf.test.annotations.type.UnitTest;
58+
import org.junit.Assert;
59+
import org.junit.Test;
60+
import org.junit.experimental.categories.Category;
61+
62+
import java.io.ByteArrayOutputStream;
63+
import java.io.IOException;
64+
import java.util.Stack;
65+
66+
@Category(UnitTest.class)
67+
public class DottedLineTest extends ExtendedITextTest {
68+
69+
@Test
70+
public void defaultDottedLineTest01() {
71+
DottedLine dottedLine = new DottedLine();
72+
73+
Assert.assertEquals(ColorConstants.BLACK, dottedLine.getColor());
74+
Assert.assertEquals(1, dottedLine.getLineWidth(), 0.0001);
75+
Assert.assertEquals(4, dottedLine.getGap(), 0.0001);
76+
}
77+
78+
79+
@Test
80+
public void dottedLineWithSetWidthTest01() {
81+
DottedLine dottedLine = new DottedLine(20);
82+
83+
Assert.assertEquals(ColorConstants.BLACK, dottedLine.getColor());
84+
Assert.assertEquals(4, dottedLine.getGap(), 0.0001);
85+
Assert.assertEquals(20, dottedLine.getLineWidth(), 0.0001);
86+
}
87+
88+
@Test
89+
public void dottedLineWithSetWidthAndGapTest01() {
90+
DottedLine dottedLine = new DottedLine(10, 15);
91+
92+
Assert.assertEquals(ColorConstants.BLACK, dottedLine.getColor());
93+
Assert.assertEquals(10, dottedLine.getLineWidth(), 0.0001);
94+
Assert.assertEquals(15, dottedLine.getGap(), 0.0001);
95+
}
96+
97+
98+
99+
@Test
100+
public void dottedLineSettersTest01() {
101+
DottedLine dottedLine = new DottedLine(15);
102+
Assert.assertEquals(ColorConstants.BLACK, dottedLine.getColor());
103+
Assert.assertEquals(15, dottedLine.getLineWidth(), 0.0001);
104+
Assert.assertEquals(4, dottedLine.getGap(), 0.0001);
105+
106+
107+
dottedLine.setColor(ColorConstants.RED);
108+
Assert.assertEquals(ColorConstants.RED, dottedLine.getColor());
109+
110+
dottedLine.setLineWidth(10);
111+
Assert.assertEquals(10, dottedLine.getLineWidth(), 0.0001);
112+
113+
dottedLine.setGap(5);
114+
Assert.assertEquals(5, dottedLine.getGap(), 0.0001);
115+
116+
}
117+
118+
@Test
119+
public void dottedLineDrawTest01() {
120+
String expectedContent = "q\n" +
121+
"15 w\n" +
122+
"0 0 0 RG\n" +
123+
"[0 5] 2.5 d\n" +
124+
"1 J\n" +
125+
"100 107.5 m\n" +
126+
"200 107.5 l\n" +
127+
"S\n" +
128+
"Q\n";
129+
130+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
131+
PdfCanvas canvas = new PdfCanvas(tempDoc.addNewPage());
132+
133+
DottedLine dottedLine = new DottedLine(15, 5);
134+
dottedLine.draw(canvas, new Rectangle(100, 100, 100, 100));
135+
136+
byte[] writtenContentBytes = canvas.getContentStream().getBytes();
137+
138+
Assert.assertArrayEquals(expectedContent.getBytes(), writtenContentBytes);
139+
}
140+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf.canvas.draw;
44+
45+
import com.itextpdf.kernel.colors.ColorConstants;
46+
import com.itextpdf.kernel.font.PdfFontFactory;
47+
import com.itextpdf.kernel.geom.AffineTransform;
48+
import com.itextpdf.kernel.geom.Matrix;
49+
import com.itextpdf.kernel.geom.Rectangle;
50+
import com.itextpdf.kernel.pdf.PdfDocument;
51+
import com.itextpdf.kernel.pdf.PdfString;
52+
import com.itextpdf.kernel.pdf.PdfWriter;
53+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
54+
import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;
55+
import com.itextpdf.kernel.pdf.canvas.parser.listener.CharacterRenderInfo;
56+
import com.itextpdf.test.ExtendedITextTest;
57+
import com.itextpdf.test.annotations.type.UnitTest;
58+
import org.junit.Assert;
59+
import org.junit.Test;
60+
import org.junit.experimental.categories.Category;
61+
62+
import java.io.ByteArrayOutputStream;
63+
import java.io.IOException;
64+
import java.util.Stack;
65+
66+
@Category(UnitTest.class)
67+
public class SolidLineTest extends ExtendedITextTest {
68+
69+
@Test
70+
public void defaultSolidLineTest01() {
71+
SolidLine solidLine = new SolidLine();
72+
73+
Assert.assertEquals(ColorConstants.BLACK, solidLine.getColor());
74+
Assert.assertEquals(1, solidLine.getLineWidth(), 0.0001);
75+
}
76+
77+
78+
@Test
79+
public void solidLineWithSetWidthTest01() {
80+
SolidLine solidLine = new SolidLine(20);
81+
82+
Assert.assertEquals(ColorConstants.BLACK, solidLine.getColor());
83+
Assert.assertEquals(20, solidLine.getLineWidth(), 0.0001);
84+
}
85+
86+
87+
@Test
88+
public void solidLineSettersTest01() {
89+
SolidLine solidLine = new SolidLine(15);
90+
Assert.assertEquals(ColorConstants.BLACK, solidLine.getColor());
91+
Assert.assertEquals(15, solidLine.getLineWidth(), 0.0001);
92+
93+
solidLine.setColor(ColorConstants.RED);
94+
Assert.assertEquals(ColorConstants.RED, solidLine.getColor());
95+
96+
solidLine.setLineWidth(10);
97+
Assert.assertEquals(10, solidLine.getLineWidth(), 0.0001);
98+
}
99+
100+
@Test
101+
public void solidLineDrawTest01() {
102+
String expectedContent = "q\n" +
103+
"0 0 0 RG\n" +
104+
"15 w\n" +
105+
"100 107.5 m\n" +
106+
"200 107.5 l\n" +
107+
"S\n" +
108+
"Q\n";
109+
110+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
111+
PdfCanvas canvas = new PdfCanvas(tempDoc.addNewPage());
112+
113+
SolidLine solidLine = new SolidLine(15);
114+
solidLine.draw(canvas, new Rectangle(100, 100, 100, 100));
115+
116+
byte[] writtenContentBytes = canvas.getContentStream().getBytes();
117+
118+
Assert.assertArrayEquals(expectedContent.getBytes(), writtenContentBytes);
119+
}
120+
}

0 commit comments

Comments
 (0)