Skip to content

Commit 8d88ca4

Browse files
author
Nikita Kovaliov
committed
Use assertThrows instead of the ExpectedException
DEVSIX-5698
1 parent 2cace80 commit 8d88ca4

35 files changed

+486
-676
lines changed

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,24 @@ This file is part of the iText (R) project.
5757
import java.io.IOException;
5858
import org.junit.Assert;
5959
import org.junit.BeforeClass;
60-
import org.junit.Rule;
6160
import org.junit.Test;
6261
import org.junit.experimental.categories.Category;
63-
import org.junit.rules.ExpectedException;
6462

6563
@Category(IntegrationTest.class)
6664
public class BarcodeCodabarTest extends ExtendedITextTest {
6765

68-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/barcodes/";
69-
public static final String destinationFolder = "./target/test/com/itextpdf/barcodes/Codabar/";
70-
71-
@Rule
72-
public ExpectedException junitExpectedException = ExpectedException.none();
66+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/barcodes/";
67+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/barcodes/Codabar/";
7368

7469
@BeforeClass
7570
public static void beforeClass() {
76-
createDestinationFolder(destinationFolder);
71+
createDestinationFolder(DESTINATION_FOLDER);
7772
}
7873

7974
@Test
8075
public void barcode01Test() throws IOException, PdfException, InterruptedException {
8176
String filename = "codabar.pdf";
82-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
77+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
8378
PdfDocument document = new PdfDocument(writer);
8479

8580
PdfPage page = document.addNewPage();
@@ -94,51 +89,47 @@ public void barcode01Test() throws IOException, PdfException, InterruptedExcepti
9489
document.close();
9590

9691
Assert.assertNull(new CompareTool()
97-
.compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder,
92+
.compareByContent(DESTINATION_FOLDER + filename, SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER,
9893
"diff_"));
9994
}
10095

10196
@Test
10297
public void barcodeHasNoAbcdAsStartCharacterTest() {
103-
junitExpectedException.expect(IllegalArgumentException.class);
104-
junitExpectedException.expectMessage(
105-
BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER);
106-
10798
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
10899
BarcodeCodabar codabar = new BarcodeCodabar(pdfDocument);
109-
codabar.getBarsCodabar("qbcd");
100+
Exception exception = Assert.assertThrows(IllegalArgumentException.class,
101+
() -> codabar.getBarsCodabar("qbcd"));
102+
Assert.assertEquals(BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER,
103+
exception.getMessage());
110104
}
111105

112106
@Test
113107
public void barcodeHasNoAbcdAsStopCharacterTest() {
114-
junitExpectedException.expect(IllegalArgumentException.class);
115-
junitExpectedException.expectMessage(
116-
BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER);
117-
118108
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
119109
BarcodeCodabar codabar = new BarcodeCodabar(pdfDocument);
120-
codabar.getBarsCodabar("abcf");
110+
Exception exception = Assert.assertThrows(IllegalArgumentException.class,
111+
() -> codabar.getBarsCodabar("abcf"));
112+
Assert.assertEquals(BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER,
113+
exception.getMessage());
121114
}
122115

123116
@Test
124117
public void barcodeHasNoAbcdAsStartAndStopCharacterTest() {
125-
junitExpectedException.expect(IllegalArgumentException.class);
126-
junitExpectedException.expectMessage(
127-
BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER);
128-
129118
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
130119
BarcodeCodabar codabar = new BarcodeCodabar(pdfDocument);
131-
codabar.getBarsCodabar("qbcq");
120+
Exception exception = Assert.assertThrows(IllegalArgumentException.class,
121+
() -> codabar.getBarsCodabar("qbcq"));
122+
Assert.assertEquals(BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_ONE_ABCD_AS_START_STOP_CHARACTER,
123+
exception.getMessage());
132124
}
133125

134126
@Test
135127
public void barcodeHasNoStartAndStopCharacterTest() {
136-
junitExpectedException.expect(IllegalArgumentException.class);
137-
junitExpectedException.expectMessage(
138-
BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_AT_LEAST_START_AND_STOP_CHARACTER);
139-
140128
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
141129
BarcodeCodabar codabar = new BarcodeCodabar(pdfDocument);
142-
codabar.getBarsCodabar("");
130+
Exception exception = Assert.assertThrows(IllegalArgumentException.class,
131+
() -> codabar.getBarsCodabar(""));
132+
Assert.assertEquals(BarcodeExceptionMessageConstant.CODABAR_MUST_HAVE_AT_LEAST_START_AND_STOP_CHARACTER,
133+
exception.getMessage());
143134
}
144135
}

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

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.barcodes;
4444

45+
import com.itextpdf.barcodes.exceptions.BarcodeExceptionMessageConstant;
4546
import com.itextpdf.io.image.ImageData;
4647
import com.itextpdf.io.image.ImageDataFactory;
47-
import com.itextpdf.barcodes.exceptions.BarcodeExceptionMessageConstant;
48-
import com.itextpdf.kernel.exceptions.PdfException;
4948
import com.itextpdf.kernel.colors.ColorConstants;
49+
import com.itextpdf.kernel.exceptions.PdfException;
5050
import com.itextpdf.kernel.pdf.PdfDocument;
5151
import com.itextpdf.kernel.pdf.PdfPage;
5252
import com.itextpdf.kernel.pdf.PdfReader;
@@ -63,28 +63,24 @@ This file is part of the iText (R) project.
6363
import java.io.IOException;
6464
import org.junit.Assert;
6565
import org.junit.BeforeClass;
66-
import org.junit.Rule;
6766
import org.junit.Test;
6867
import org.junit.experimental.categories.Category;
69-
import org.junit.rules.ExpectedException;
7068

7169
@Category(IntegrationTest.class)
7270
public class BarcodePDF417Test extends ExtendedITextTest {
73-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/barcodes/";
74-
public static final String destinationFolder = "./target/test/com/itextpdf/barcodes/BarcodePDF417/";
7571

76-
@Rule
77-
public ExpectedException junitExpectedException = ExpectedException.none();
72+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/barcodes/";
73+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/barcodes/BarcodePDF417/";
7874

7975
@BeforeClass
8076
public static void beforeClass() {
81-
createDestinationFolder(destinationFolder);
77+
createOrClearDestinationFolder(DESTINATION_FOLDER);
8278
}
8379

8480
@Test
8581
public void barcode01Test() throws IOException, PdfException, InterruptedException {
8682
String filename = "barcode417_01.pdf";
87-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
83+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
8884
PdfDocument document = new PdfDocument(writer);
8985

9086
PdfPage page = document.addNewPage();
@@ -101,14 +97,15 @@ public void barcode01Test() throws IOException, PdfException, InterruptedExcepti
10197

10298
document.close();
10399

104-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
100+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename, SOURCE_FOLDER + "cmp_" + filename,
101+
DESTINATION_FOLDER, "diff_"));
105102
}
106103

107104
@Test
108105
public void barcode02Test() throws IOException, PdfException, InterruptedException {
109106
String filename = "barcode417_02.pdf";
110-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
111-
PdfReader reader = new PdfReader(sourceFolder + "DocumentWithTrueTypeFont1.pdf");
107+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
108+
PdfReader reader = new PdfReader(SOURCE_FOLDER + "DocumentWithTrueTypeFont1.pdf");
112109
PdfDocument document = new PdfDocument(reader, writer);
113110

114111
PdfCanvas canvas = new PdfCanvas(document.getLastPage());
@@ -124,13 +121,14 @@ public void barcode02Test() throws IOException, PdfException, InterruptedExcepti
124121

125122
document.close();
126123

127-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
124+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename, SOURCE_FOLDER + "cmp_" + filename,
125+
DESTINATION_FOLDER, "diff_"));
128126
}
129127

130128
@Test
131129
public void macroPDF417Test01() throws IOException, InterruptedException {
132130
String filename = "barcode417Macro_01.pdf";
133-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
131+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
134132
PdfDocument pdfDocument = new PdfDocument(writer);
135133

136134
PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.addNewPage());
@@ -140,7 +138,8 @@ public void macroPDF417Test01() throws IOException, InterruptedException {
140138

141139
pdfDocument.close();
142140

143-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder, "diff_"));
141+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename, SOURCE_FOLDER + "cmp_" + filename,
142+
DESTINATION_FOLDER, "diff_"));
144143
}
145144

146145
@Test
@@ -195,7 +194,7 @@ public void barcode417DefaultParamsTest() {
195194
@Test
196195
public void barcode417CreateAWTImageTest() throws IOException, InterruptedException {
197196
String filename = "barcode417CreateAWTImageTest.pdf";
198-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
197+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
199198
PdfDocument document = new PdfDocument(writer);
200199

201200
PdfPage page = document.addNewPage();
@@ -216,14 +215,14 @@ public void barcode417CreateAWTImageTest() throws IOException, InterruptedExcept
216215

217216
document.close();
218217

219-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
220-
sourceFolder + "cmp_" + filename, destinationFolder));
218+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
219+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
221220
}
222221

223222
@Test
224223
public void barcode417XObjectTest() throws IOException, InterruptedException {
225224
String filename = "barcode417XObjectTest.pdf";
226-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
225+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
227226
PdfDocument document = new PdfDocument(writer);
228227

229228
PdfPage page = document.addNewPage();
@@ -242,8 +241,8 @@ public void barcode417XObjectTest() throws IOException, InterruptedException {
242241

243242
document.close();
244243

245-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
246-
sourceFolder + "cmp_" + filename, destinationFolder));
244+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
245+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
247246
}
248247

249248
@Test
@@ -273,7 +272,7 @@ public void barcode417YHeightTest() {
273272
@Test
274273
public void barcode417CodeReuseTest() throws IOException, InterruptedException {
275274
String filename = "barcode417CodeReuseTest.pdf";
276-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
275+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
277276
PdfDocument document = new PdfDocument(writer);
278277

279278
PdfCanvas canvas = new PdfCanvas(document.addNewPage());
@@ -295,14 +294,14 @@ public void barcode417CodeReuseTest() throws IOException, InterruptedException {
295294

296295
document.close();
297296

298-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
299-
sourceFolder + "cmp_" + filename, destinationFolder));
297+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
298+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
300299
}
301300

302301
@Test
303302
public void barcode417NumbersTest() throws IOException, InterruptedException {
304303
String filename = "barcode417NumbersTest.pdf";
305-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
304+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
306305
PdfDocument document = new PdfDocument(writer);
307306

308307
PdfCanvas canvas = new PdfCanvas(document.addNewPage());
@@ -315,14 +314,14 @@ public void barcode417NumbersTest() throws IOException, InterruptedException {
315314

316315
document.close();
317316

318-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
319-
sourceFolder + "cmp_" + filename, destinationFolder));
317+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
318+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
320319
}
321320

322321
@Test
323322
public void barcode417ByteLessThanSixSizeNumbersTest() throws IOException, InterruptedException {
324323
String filename = "barcode417ByteLessThanSixSizeNumbersTest.pdf";
325-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
324+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
326325
PdfDocument document = new PdfDocument(writer);
327326

328327
PdfCanvas canvas = new PdfCanvas(document.addNewPage());
@@ -335,14 +334,14 @@ public void barcode417ByteLessThanSixSizeNumbersTest() throws IOException, Inter
335334

336335
document.close();
337336

338-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
339-
sourceFolder + "cmp_" + filename, destinationFolder));
337+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
338+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
340339
}
341340

342341
@Test
343342
public void barcode417ByteMoreThanSixSizeNumbersTest() throws IOException, InterruptedException {
344343
String filename = "barcode417ByteMoreThanSixSizeNumbersTest.pdf";
345-
PdfWriter writer = new PdfWriter(destinationFolder + filename);
344+
PdfWriter writer = new PdfWriter(DESTINATION_FOLDER + filename);
346345
PdfDocument document = new PdfDocument(writer);
347346

348347
PdfCanvas canvas = new PdfCanvas(document.addNewPage());
@@ -355,8 +354,8 @@ public void barcode417ByteMoreThanSixSizeNumbersTest() throws IOException, Inter
355354

356355
document.close();
357356

358-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + filename,
359-
sourceFolder + "cmp_" + filename, destinationFolder));
357+
Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + filename,
358+
SOURCE_FOLDER + "cmp_" + filename, DESTINATION_FOLDER));
360359
}
361360

362361
@Test
@@ -460,36 +459,33 @@ public void barcode417OptionsWithBarcodeGenerationInvalidSizeTest() {
460459

461460
@Test
462461
public void lenCodewordsIsNotEnoughTest() {
463-
junitExpectedException.expect(PdfException.class);
464-
junitExpectedException.expectMessage(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE);
465-
466462
BarcodePDF417 barcodePDF417 = new BarcodePDF417();
467463
barcodePDF417.setOptions(BarcodePDF417.PDF417_USE_RAW_CODEWORDS);
468-
barcodePDF417.paintCode();
464+
Exception exception = Assert.assertThrows(PdfException.class,
465+
() -> barcodePDF417.paintCode());
466+
Assert.assertEquals(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE, exception.getMessage());
469467
}
470468

471469
@Test
472470
public void lenCodewordsIsTooSmallTest() {
473-
junitExpectedException.expect(PdfException.class);
474-
junitExpectedException.expectMessage(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE);
475-
476471
BarcodePDF417 barcodePDF417 = new BarcodePDF417();
477472
barcodePDF417.setOptions(BarcodePDF417.PDF417_USE_RAW_CODEWORDS);
478473
// lenCodeWords should be bigger than 1
479474
barcodePDF417.setLenCodewords(0);
480-
barcodePDF417.paintCode();
475+
Exception exception = Assert.assertThrows(PdfException.class,
476+
() -> barcodePDF417.paintCode());
477+
Assert.assertEquals(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE, exception.getMessage());
481478
}
482479

483480
@Test
484481
public void lenCodewordsMoreThanMaxDataCodewordsTest() {
485-
junitExpectedException.expect(PdfException.class);
486-
junitExpectedException.expectMessage(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE);
487-
488482
BarcodePDF417 barcodePDF417 = new BarcodePDF417();
489483
barcodePDF417.setOptions(BarcodePDF417.PDF417_USE_RAW_CODEWORDS);
490484
// lenCodeWords should be smaller than MAX_DATA_CODEWORDS
491485
barcodePDF417.setLenCodewords(927);
492-
barcodePDF417.paintCode();
486+
Exception exception = Assert.assertThrows(PdfException.class,
487+
() -> barcodePDF417.paintCode());
488+
Assert.assertEquals(BarcodeExceptionMessageConstant.INVALID_CODEWORD_SIZE, exception.getMessage());
493489
}
494490

495491
private PdfFormXObject createMacroBarcodePart(PdfDocument document, String text, float mh, float mw, int segmentId) {

0 commit comments

Comments
 (0)