Skip to content

Commit 2c8594c

Browse files
author
Artyom Yanchevsky
committed
Replace ExpectedException.none to Assert.assertThrows
DEVSIX-5287
1 parent df4013c commit 2c8594c

File tree

158 files changed

+1916
-2725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+1916
-2725
lines changed

barcodes/src/test/java/com/itextpdf/barcodes/BarcodeDataMatrixTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,19 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.kernel.utils.CompareTool;
5353
import com.itextpdf.test.ExtendedITextTest;
5454
import com.itextpdf.test.annotations.type.IntegrationTest;
55+
5556
import java.io.IOException;
5657
import org.junit.Assert;
5758
import org.junit.BeforeClass;
58-
import org.junit.Rule;
5959
import org.junit.Test;
6060
import org.junit.experimental.categories.Category;
61-
import org.junit.rules.ExpectedException;
6261

6362
@Category(IntegrationTest.class)
6463
public class BarcodeDataMatrixTest extends ExtendedITextTest {
6564

6665
public static final String destinationFolder = "./target/test/com/itextpdf/barcodes/BarcodeDataMatrix/";
6766
public static final String sourceFolder = "./src/test/resources/com/itextpdf/barcodes/";
6867

69-
@Rule
70-
public ExpectedException junitExpectedException = ExpectedException.none();
71-
7268
@BeforeClass
7369
public static void beforeClass() {
7470
createOrClearDestinationFolder(destinationFolder);
@@ -191,6 +187,7 @@ public void barcode07Test() {
191187
String aCode = "aBCdeFG12";
192188

193189
int result = bc.setCode(aCode);
190+
194191
Assert.assertEquals(result, BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG);
195192
}
196193

@@ -200,6 +197,7 @@ public void barcode08Test() {
200197
barcodeDataMatrix.setWidth(18);
201198
barcodeDataMatrix.setHeight(18);
202199
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
200+
203201
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
204202
}
205203

@@ -209,6 +207,7 @@ public void barcode09Test() {
209207
barcodeDataMatrix.setWidth(17);
210208
barcodeDataMatrix.setHeight(17);
211209
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
210+
212211
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_INVALID_SQUARE, result);
213212
}
214213

@@ -218,6 +217,7 @@ public void barcode10Test() {
218217
barcodeDataMatrix.setWidth(26);
219218
barcodeDataMatrix.setHeight(12);
220219
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
220+
221221
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
222222
}
223223

@@ -228,37 +228,38 @@ public void barcode11Test() {
228228
barcodeDataMatrix.setHeight(18);
229229
byte[] str = "AbcdFFghijklmnop".getBytes();
230230
int result = barcodeDataMatrix.setCode(str, 0, str.length);
231+
231232
Assert.assertEquals(BarcodeDataMatrix.DM_NO_ERROR, result);
232233
}
233234

234235
@Test
235236
public void barcode12Test() {
236-
junitExpectedException.expect(IndexOutOfBoundsException.class);
237237
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
238238
barcodeDataMatrix.setWidth(18);
239239
barcodeDataMatrix.setHeight(18);
240240
byte[] str = "AbcdFFghijklmnop".getBytes();
241-
barcodeDataMatrix.setCode(str, -1, str.length);
241+
242+
Exception e = Assert.assertThrows(IndexOutOfBoundsException.class, () -> barcodeDataMatrix.setCode(str, -1, str.length));
242243
}
243244

244245
@Test
245246
public void barcode13Test() {
246-
junitExpectedException.expect(IndexOutOfBoundsException.class);
247247
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
248248
barcodeDataMatrix.setWidth(18);
249249
barcodeDataMatrix.setHeight(18);
250250
byte[] str = "AbcdFFghijklmnop".getBytes();
251-
barcodeDataMatrix.setCode(str, 0, str.length + 1);
251+
252+
Assert.assertThrows(IndexOutOfBoundsException.class, () -> barcodeDataMatrix.setCode(str, 0, str.length + 1));
252253
}
253254

254255
@Test
255256
public void barcode14Test() {
256-
junitExpectedException.expect(IndexOutOfBoundsException.class);
257257
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
258258
barcodeDataMatrix.setWidth(18);
259259
barcodeDataMatrix.setHeight(18);
260260
byte[] str = "AbcdFFghijklmnop".getBytes();
261-
barcodeDataMatrix.setCode(str, 0, -1);
261+
262+
Assert.assertThrows(IndexOutOfBoundsException.class, () -> barcodeDataMatrix.setCode(str, 0, -1));
262263
}
263264

264265
@Test
@@ -268,6 +269,7 @@ public void barcode15Test() {
268269
barcodeDataMatrix.setHeight(18);
269270
byte[] str = "AbcdFFghijklmnop".getBytes();
270271
int result = barcodeDataMatrix.setCode(str, str.length, 0);
272+
271273
Assert.assertEquals(BarcodeDataMatrix.DM_NO_ERROR, result);
272274
}
273275

barcodes/src/test/java/com/itextpdf/barcodes/BarcodeEANUnitTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,14 @@ This file is part of the iText (R) project.
5151

5252
import java.io.ByteArrayOutputStream;
5353
import org.junit.Assert;
54-
import org.junit.Rule;
5554
import org.junit.Test;
5655
import org.junit.experimental.categories.Category;
57-
import org.junit.rules.ExpectedException;
5856

5957
@Category(UnitTest.class)
6058
public class BarcodeEANUnitTest extends ExtendedITextTest {
6159

6260
public static final float EPS = 0.0001f;
6361

64-
@Rule
65-
public ExpectedException junitExpectedException = ExpectedException.none();
66-
6762
@Test
6863
public void calculateEANParityTest() throws PdfException {
6964
int expectedParity = BarcodeEAN.calculateEANParity("1234567890");
@@ -227,9 +222,6 @@ public void getBarcodeSizeSUPP2Test() throws PdfException {
227222

228223
@Test
229224
public void getBarcodeSizeIncorrectTypeTest() throws PdfException {
230-
junitExpectedException.expect(PdfException.class);
231-
junitExpectedException.expectMessage("Invalid code type");
232-
233225
PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
234226

235227
Barcode1D barcode = new BarcodeEAN(document);
@@ -239,6 +231,7 @@ public void getBarcodeSizeIncorrectTypeTest() throws PdfException {
239231
barcode.setCodeType(1234);
240232

241233
// We do expect an exception here
242-
barcode.getBarcodeSize();
234+
Exception e = Assert.assertThrows(PdfException.class, () -> barcode.getBarcodeSize());
235+
Assert.assertEquals("Invalid code type", e.getMessage());
243236
}
244237
}

barcodes/src/test/java/com/itextpdf/barcodes/BarcodePDF417Test.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ This file is part of the iText (R) project.
6262
import java.io.IOException;
6363
import org.junit.Assert;
6464
import org.junit.BeforeClass;
65-
import org.junit.Rule;
6665
import org.junit.Test;
6766
import org.junit.experimental.categories.Category;
68-
import org.junit.rules.ExpectedException;
6967

7068
@Category(IntegrationTest.class)
7169
public class BarcodePDF417Test extends ExtendedITextTest {
@@ -77,9 +75,6 @@ public static void beforeClass() {
7775
createDestinationFolder(destinationFolder);
7876
}
7977

80-
@Rule
81-
public ExpectedException junitExpectedException = ExpectedException.none();
82-
8378
@Test
8479
public void barcode01Test() throws IOException, PdfException, InterruptedException {
8580
String filename = "barcode417_01.pdf";
@@ -440,9 +435,6 @@ public void barcode417OptionsWithBarcodeGenerationTest() {
440435

441436
@Test
442437
public void barcode417OptionsWithBarcodeGenerationInvalidSizeTest() {
443-
junitExpectedException.expect(PdfException.class);
444-
junitExpectedException.expectMessage("Invalid codeword size.");
445-
446438
ByteArrayOutputStream baos = new ByteArrayOutputStream();
447439
PdfWriter writer = new PdfWriter(baos);
448440
PdfDocument document = new PdfDocument(writer);
@@ -452,8 +444,11 @@ public void barcode417OptionsWithBarcodeGenerationInvalidSizeTest() {
452444

453445
BarcodePDF417 barcode = new BarcodePDF417();
454446
barcode.setOptions(64);
455-
barcode.placeBarcode(canvas, null);
456447

448+
Exception e = Assert.assertThrows(PdfException.class,
449+
() -> barcode.placeBarcode(canvas, null)
450+
);
451+
Assert.assertEquals("Invalid codeword size.", e.getMessage());
457452
Assert.assertEquals(64, barcode.getOptions());
458453
}
459454

forms/src/test/java/com/itextpdf/forms/XfdfUnitTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,17 @@ This file is part of the iText (R) project.
2828
import com.itextpdf.test.annotations.type.UnitTest;
2929

3030
import org.junit.Assert;
31-
import org.junit.Rule;
3231
import org.junit.Test;
3332
import org.junit.experimental.categories.Category;
34-
import org.junit.rules.ExpectedException;
3533

3634
@Category(UnitTest.class)
3735
public class XfdfUnitTest extends ExtendedITextTest {
3836

39-
@Rule
40-
public ExpectedException junitExpectedException = ExpectedException.none();
41-
4237
@Test
4338
public void fitObjectWithEmptyPageTest(){
44-
junitExpectedException.expect(XfdfException.class);
45-
junitExpectedException.expectMessage(XfdfException.PAGE_IS_MISSING);
46-
47-
FitObject fitObject = new FitObject(null);
48-
49-
Assert.fail();
39+
Exception e = Assert.assertThrows(XfdfException.class,
40+
() -> new FitObject(null)
41+
);
42+
Assert.assertEquals(XfdfException.PAGE_IS_MISSING, e.getMessage());
5043
}
51-
5244
}

forms/src/test/java/com/itextpdf/forms/XfdfWriterTest.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ This file is part of the iText (R) project.
5858

5959
import org.junit.Assert;
6060
import org.junit.BeforeClass;
61-
import org.junit.Rule;
6261
import org.junit.Test;
6362
import org.junit.experimental.categories.Category;
64-
import org.junit.rules.ExpectedException;
6563
import org.xml.sax.SAXException;
66-
6764
import javax.xml.parsers.ParserConfigurationException;
6865
import javax.xml.transform.TransformerException;
6966
import java.io.FileInputStream;
@@ -75,10 +72,6 @@ public class XfdfWriterTest extends ExtendedITextTest {
7572
public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/XfdfWriterTest/";
7673
public static final String destinationFolder = "./target/test/com/itextpdf/forms/XfdfWriterTest/";
7774

78-
@Rule
79-
public ExpectedException junitExpectedException = ExpectedException.none();
80-
81-
8275
@BeforeClass
8376
public static void beforeClass() {
8477
createDestinationFolder(destinationFolder);
@@ -1047,10 +1040,6 @@ public void xfdfDropDown() throws IOException, ParserConfigurationException, SAX
10471040

10481041
@Test
10491042
public void xfdfEmptyAttributeTest() {
1050-
1051-
junitExpectedException.expect(XfdfException.class);
1052-
junitExpectedException.expectMessage(XfdfException.ATTRIBUTE_NAME_OR_VALUE_MISSING);
1053-
10541043
XfdfObject xfdfObject = new XfdfObject();
10551044

10561045
AnnotsObject annots = new AnnotsObject();
@@ -1064,8 +1053,13 @@ public void xfdfEmptyAttributeTest() {
10641053
String valuePresent = "value";
10651054
String valueAbsent = null;
10661055

1067-
annot.addAttribute(new AttributeObject(nameAbsent, valuePresent));
1068-
annot.addAttribute(new AttributeObject(namePresent, valueAbsent));
1069-
1056+
Exception e = Assert.assertThrows(XfdfException.class,
1057+
() -> annot.addAttribute(new AttributeObject(nameAbsent, valuePresent))
1058+
);
1059+
Assert.assertEquals(XfdfException.ATTRIBUTE_NAME_OR_VALUE_MISSING, e.getMessage());
1060+
Exception e2 = Assert.assertThrows(XfdfException.class,
1061+
() -> annot.addAttribute(new AttributeObject(namePresent, valueAbsent))
1062+
);
1063+
Assert.assertEquals(XfdfException.ATTRIBUTE_NAME_OR_VALUE_MISSING, e2.getMessage());
10701064
}
10711065
}

forms/src/test/java/com/itextpdf/forms/xfa/XfaSecurityTest.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ This file is part of the iText (R) project.
5858
import java.io.IOException;
5959
import java.io.InputStream;
6060
import java.nio.charset.StandardCharsets;
61+
import org.junit.Assert;
6162
import org.junit.Before;
62-
import org.junit.Rule;
6363
import org.junit.Test;
6464
import org.junit.experimental.categories.Category;
65-
import org.junit.rules.ExpectedException;
6665

6766
@Category(IntegrationTest.class)
6867
public class XfaSecurityTest extends ExtendedITextTest {
@@ -78,9 +77,6 @@ public class XfaSecurityTest extends ExtendedITextTest {
7877
+ " uuid=\"36ac5111-55c5-4172-b0c1-0cbd783e2fcf\">\n"
7978
+ "</xdp:xdp>\n";
8079

81-
@Rule
82-
public ExpectedException junitExpectedException = ExpectedException.none();
83-
8480
@Before
8581
public void resetXmlParserFactoryToDefault() {
8682
XmlProcessorCreator.setXmlParserFactory(new DefaultSafeXmlParserFactory());
@@ -107,47 +103,46 @@ public void xfaExternalFileCustomFactoryTest() throws IOException {
107103
XmlProcessorCreator.setXmlParserFactory(new SecurityTestXmlParserFactory());
108104
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFileName),
109105
new PdfWriter(new ByteArrayOutputStream()))) {
110-
junitExpectedException.expect(PdfException.class);
111-
junitExpectedException.expectMessage(ExceptionTestUtil.getXxeTestMessage());
112-
PdfAcroForm.getAcroForm(pdfDoc, true);
106+
Exception e = Assert.assertThrows(PdfException.class,
107+
() -> PdfAcroForm.getAcroForm(pdfDoc, true)
108+
);
109+
Assert.assertEquals(ExceptionTestUtil.getXxeTestMessage(), e.getMessage());
113110
}
114111
}
115112

116113
@Test
117114
public void xfaExternalFileXfaFormTest() throws IOException {
118115
String inFileName = SOURCE_FOLDER + "xfaExternalFile.pdf";
119116
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFileName))) {
120-
junitExpectedException.expect(PdfException.class);
121-
junitExpectedException.expectMessage(DTD_EXCEPTION_MESSAGE);
122-
new XfaForm(pdfDoc);
117+
Exception e = Assert.assertThrows(PdfException.class, () -> new XfaForm(pdfDoc));
118+
Assert.assertEquals(DTD_EXCEPTION_MESSAGE, e.getMessage());
123119
}
124120
}
125121

126122
@Test
127123
public void xfaWithDtdXfaFormTest() throws IOException {
128124
try (InputStream inputStream = new ByteArrayInputStream(XFA_WITH_DTD_XML.getBytes(StandardCharsets.UTF_8))) {
129-
junitExpectedException.expect(PdfException.class);
130-
junitExpectedException.expectMessage(DTD_EXCEPTION_MESSAGE);
131-
new XfaForm(inputStream);
125+
Exception e = Assert.assertThrows(PdfException.class, () -> new XfaForm(inputStream));
126+
Assert.assertEquals(DTD_EXCEPTION_MESSAGE, e.getMessage());
132127
}
133128
}
134129

135130
@Test
136131
public void fillXfaFormTest() throws IOException {
137132
try (InputStream inputStream = new ByteArrayInputStream(XFA_WITH_DTD_XML.getBytes(StandardCharsets.UTF_8))) {
138133
XfaForm form = new XfaForm();
139-
junitExpectedException.expect(PdfException.class);
140-
junitExpectedException.expectMessage(DTD_EXCEPTION_MESSAGE);
141-
form.fillXfaForm(inputStream, true);
134+
Exception e = Assert.assertThrows(PdfException.class, () -> form.fillXfaForm(inputStream, true));
135+
Assert.assertEquals(DTD_EXCEPTION_MESSAGE, e.getMessage());
142136
}
143137
}
144138

145139
private void xfaSecurityExceptionTest(String inputFileName) throws IOException {
146140
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFileName),
147141
new PdfWriter(new ByteArrayOutputStream()))) {
148-
junitExpectedException.expect(PdfException.class);
149-
junitExpectedException.expectMessage(DTD_EXCEPTION_MESSAGE);
150-
PdfAcroForm.getAcroForm(pdfDoc, true);
142+
Exception e = Assert.assertThrows(PdfException.class,
143+
() -> PdfAcroForm.getAcroForm(pdfDoc, true)
144+
);
145+
Assert.assertEquals(DTD_EXCEPTION_MESSAGE, e.getMessage());
151146
}
152147
}
153148
}

0 commit comments

Comments
 (0)