1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package eu.simuline.m2latex.core;
20
21 import eu.simuline.m2latex.mojo.MavenLogWrapper;
22 import eu.simuline.m2latex.mojo.PdfMojo;
23
24 import java.io.File;
25 import java.io.FileFilter;
26
27
28 import java.io.IOException;
29 import java.util.Optional;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.RETURNS_SMART_NULLS;
32 import static org.mockito.Mockito.when;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.doCallRealMethod;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.spy;
38 import static org.mockito.Mockito.verifyNoMoreInteractions;
39 import static org.mockito.Mockito.atLeastOnce;
40 import static org.mockito.Mockito.inOrder;
41
42
43 import static org.mockito.ArgumentMatchers.anyString;
44 import static org.mockito.ArgumentMatchers.isNull;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.AdditionalMatchers.aryEq;
47
48 import org.mockito.InOrder;
49
50
51
52
53 import org.junit.Test;
54
55 import org.junit.Before;
56 import org.junit.After;
57
58
59
60 import static org.junit.Assert.assertEquals;
61
62
63
64
65
66 public class LatexProcessorTest {
67
68 private final static File WORKING_DIR =
69 new File(System.getProperty("unitTestResourcesDir"));
70
71
72
73
74 private final static FileFilter FILTER_FALSE = new FileFilter() {
75 public boolean accept(File pathname) {
76 return false;
77 }
78 };
79
80 private final CommandExecutor executor =
81 mock(CommandExecutor.class, RETURNS_SMART_NULLS);
82
83
84
85 private final TexFileUtils fileUtils =
86 mock(TexFileUtils.class, RETURNS_SMART_NULLS);
87
88
89
90
91 private final InOrder inOrder = inOrder(this.executor, this.fileUtils);
92
93 private final Settings settings = new Settings();
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 private final LogWrapper log =
124 spy(new MavenLogWrapper(this.getClass()));
125
126 private LatexProcessor processor = new LatexProcessor(this.settings,
127 this.executor, this.log, this.fileUtils, new PdfMojo());
128
129
130
131
132 private File texFile = new File(WORKING_DIR, "test.tex");
133 LatexMainDesc desc = new LatexMainDesc(texFile, null);
134
135
136 private File dviPdfFile = new File(WORKING_DIR,
137 "test." + settings.getPdfViaDvi().getLatexOutputFormat());
138 private File htmlFile = new File(WORKING_DIR, "test.html");
139 private File auxFile = new File(WORKING_DIR, "test.aux");
140 private File logFile = new File(WORKING_DIR, "test.log");
141
142 private File bblFile = new File(WORKING_DIR, "test.bbl");
143 private File blgFile = new File(WORKING_DIR, "test.blg");
144
145 private File idxFile = new File(WORKING_DIR, "test.idx");
146 private File indFile = new File(WORKING_DIR, "test.ind");
147 private File ilgFile = new File(WORKING_DIR, "test.ilg");
148
149 private File glsFile = new File(WORKING_DIR, "test.gls");
150 private File glgFile = new File(WORKING_DIR, "test.glg");
151
152 private File xxxFile = new File(WORKING_DIR, "test");
153
154
155 private File tocFile = new File(WORKING_DIR, "test.toc");
156 private File lofFile = new File(WORKING_DIR, "test.lof");
157 private File lotFile = new File(WORKING_DIR, "test.lot");
158
159 public LatexProcessorTest() {
160
161
162
163
164 doThrow(new AssertionError("Found error. ")).when(this.log)
165 .error(anyString());
166 doThrow(new AssertionError("Found warning. ")).when(this.log)
167 .warn(anyString());
168 doCallRealMethod().when(this.log).info(anyString());
169 doNothing().when(this.log).debug(anyString());
170 }
171
172
173
174 private static void cleanWorkingDir() {
175 assert WORKING_DIR.isDirectory() : "Expected directory. ";
176 File[] files = WORKING_DIR.listFiles();
177 assert files != null : "Working directory is not readable. ";
178 for (File file : files) {
179 if (!file.isHidden()) {
180 file.delete();
181 }
182 }
183 }
184
185 @Before
186 public void setUp() throws IOException {
187 cleanWorkingDir();
188
189
190 this.logFile.createNewFile();
191 this.blgFile.createNewFile();
192
193 }
194
195 @After
196 public void tearDown() throws IOException {
197 cleanWorkingDir();
198 }
199
200
201
202 @Test
203 public void testProcessLatexSimple() throws BuildFailureException {
204
205 mockProcessLatex2pdf(false, false, false);
206
207 this.processor.processLatex2pdf(this.desc);
208 verifyProcessLatex2pdf(false, false, false);
209 verifyNoMoreInteractions(this.executor);
210 verifyNoMoreInteractions(this.fileUtils);
211
212 }
213
214
215 @Test
216 public void testProcessLatexWithBibtex() throws BuildFailureException {
217
218 mockProcessLatex2pdf(true, false, false);
219
220 this.processor.processLatex2pdf(this.desc);
221 verifyProcessLatex2pdf(true, false, false);
222 verifyNoMoreInteractions(this.executor);
223 verifyNoMoreInteractions(this.fileUtils);
224
225 }
226
227
228 @Test
229 public void testProcessLatex2html() throws BuildFailureException {
230
231 mockProcessLatex2html(false, false, false);
232 this.processor.processLatex2html(this.desc);
233
234 verifyProcessLatex2html(false, false, false);
235 verifyNoMoreInteractions(this.executor);
236 verifyNoMoreInteractions(this.fileUtils);
237
238 }
239
240 private void mockProcessLatex2pdf(boolean needBibtex, boolean needMakeIndex,
241 boolean needMakeGlossaries) throws BuildFailureException {
242
243 mockConstrLatexMainDesc();
244 assert !this.settings.getPdfViaDvi().isViaDvi();
245
246 mockProcessLatex2devCore(needBibtex, needMakeIndex, needMakeGlossaries);
247
248 when(this.fileUtils.matchInFile(this.logFile,
249 LatexProcessor.PATTERN_OUFULL_HVBOX)).thenReturn(Boolean.FALSE);
250
251 when(this.fileUtils.matchInFile(this.logFile,
252 this.settings.getPatternWarnLatex())).thenReturn(Boolean.FALSE);
253 }
254
255 private void verifyProcessLatex2pdf(boolean needBibtex, boolean needMakeIndex,
256 boolean needMakeGlossary) throws BuildFailureException {
257
258
259 assert !this.settings.getPdfViaDvi().isViaDvi();
260
261 verifyProcessLatex2devCore(needBibtex, needMakeIndex, needMakeGlossary);
262
263 this.inOrder.verify(this.fileUtils).matchInFile(this.logFile,
264 LatexProcessor.PATTERN_OUFULL_HVBOX);
265 this.inOrder.verify(this.fileUtils).matchInFile(this.logFile,
266 this.settings.getPatternWarnLatex());
267 }
268
269 private void mockProcessLatex2html(boolean needBibtex, boolean needMakeIndex,
270 boolean needMakeGlossaries) throws BuildFailureException {
271
272 mockConstrLatexMainDesc();
273 mockPreProcessLatex2dev(needBibtex, needMakeIndex, needMakeGlossaries);
274 mockRunLatex2html();
275 }
276
277 private void verifyProcessLatex2html(boolean needBibtex,
278 boolean needMakeIndex, boolean needMakeGlossary)
279 throws BuildFailureException {
280
281
282 verifyPreProcessLatex2dev(needBibtex, needMakeIndex, needMakeGlossary);
283 verifyRunLatex2html();
284 }
285
286 private void mockConstrLatexMainDesc() {
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 private void mockPreProcessLatex2dev(boolean needBibtex,
334 boolean needMakeIndex, boolean needMakeGlossaries)
335 throws BuildFailureException {
336 assert !needMakeIndex && !needMakeGlossaries;
337
338
339 mockRunLatex();
340
341
342
343
344
345 mockRunMakeIndexByNeed(needMakeIndex);
346 mockRunMakeGlossaryByNeed(needMakeGlossaries);
347
348 if (needBibtex) {
349 return;
350 }
351
352
353
354 assert !this.tocFile.exists() && !this.lofFile.exists()
355 && !this.lotFile.exists();
356
357
358
359
360
361
362 }
363
364 private void verifyPreProcessLatex2dev(boolean needBibtex,
365 boolean needMakeIndex, boolean needMakeGlossaries)
366 throws BuildFailureException {
367 assert !needMakeIndex && !needMakeGlossaries;
368
369
370 verifyRunLatex();
371
372
373
374 verifyRunMakeIndexByNeed(needMakeIndex);
375 verifyRunMakeGlossaryByNeed(needMakeGlossaries);
376
377 if (needBibtex) {
378 return;
379 }
380
381
382
383 assert !this.tocFile.exists() && !this.lofFile.exists()
384 && !this.lotFile.exists();
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 }
400
401
402 private void mockProcessLatex2devCore(boolean needBibtex,
403 boolean needMakeIndex, boolean needMakeGlossaries)
404 throws BuildFailureException {
405
406 assert !needMakeIndex && !needMakeGlossaries;
407 assert !this.tocFile.exists() && !this.lofFile.exists()
408 && !this.lotFile.exists();
409
410 assert this.settings.getMaxNumReRunsLatex() == 5;
411
412 mockPreProcessLatex2dev(needBibtex, needMakeIndex, needMakeGlossaries);
413
414
415
416
417
418
419 if (needBibtex) {
420
421 mockRunLatex();
422
423
424
425 mockRunLatex();
426 mockNeedRun(false, this.settings.getPatternReRunLatex());
427
428
429
430 } else {
431 mockNeedRun(false, this.settings.getPatternReRunLatex());
432
433
434
435
436 }
437 }
438
439
440 private void verifyProcessLatex2devCore(boolean needBibtex,
441 boolean needMakeIndex, boolean needMakeGlossary)
442 throws BuildFailureException {
443
444 assert !needMakeIndex && !needMakeGlossary;
445 assert !this.tocFile.exists() && !this.lofFile.exists()
446 && !this.lotFile.exists();
447
448 assert this.settings.getMaxNumReRunsLatex() == 5;
449
450 verifyPreProcessLatex2dev(needBibtex, needMakeIndex, needMakeGlossary);
451
452
453
454
455
456
457 if (needBibtex) {
458
459 verifyRunLatex();
460
461
462
463 verifyRunLatex();
464 verifyNeedRun(this.logFile, this.settings.getPatternReRunLatex());
465
466
467
468 } else {
469 verifyNeedRun(this.logFile, this.settings.getPatternReRunLatex());
470
471
472
473
474 }
475 }
476
477 private void mockNeedRun(Boolean retVal, String pattern)
478 throws BuildFailureException {
479
480 when(this.fileUtils.matchInFile(this.logFile, pattern))
481 .thenReturn(retVal);
482 }
483
484 private void verifyNeedRun(File file, String pattern)
485 throws BuildFailureException {
486
487
488 verify(this.fileUtils, atLeastOnce()).matchInFile(file, pattern);
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565 private void mockRunMakeIndexByNeed(boolean runMakeIndex)
566 throws BuildFailureException {
567
568 when(this.fileUtils.getFileFilterReplace(this.idxFile, "-.+"))
569 .thenReturn(FILTER_FALSE);
570
571 when(this.fileUtils.listFilesOrWarn(WORKING_DIR, FILTER_FALSE))
572 .thenReturn(new File[0]);
573
574 assert !runMakeIndex;
575
576 if (!runMakeIndex) {
577 return;
578 }
579 mockRunMakeIndex();
580 }
581
582 private void verifyRunMakeIndexByNeed(boolean runMakeIndex)
583 throws BuildFailureException {
584
585 this.inOrder.verify(this.fileUtils).getFileFilterReplace(this.idxFile,
586 "-.+");
587 this.inOrder.verify(this.fileUtils).listFilesOrWarn(WORKING_DIR,
588 FILTER_FALSE);
589
590 assert !runMakeIndex;
591
592 if (!runMakeIndex) {
593 return;
594 }
595 verifyRunMakeIndex();
596 }
597
598 private void mockRunMakeIndex() throws BuildFailureException {
599 assert false;
600
601
602
603
604
605
606
607
608
609
610
611 when(this.fileUtils.matchInFile(this.ilgFile,
612 this.settings.getPatternErrMakeIndex())).thenReturn(Boolean.FALSE);
613 }
614
615 private void verifyRunMakeIndex() throws BuildFailureException {
616 assert false;
617
618 this.inOrder.verify(this.executor)
619 .executeEnvR0(eq(WORKING_DIR), isNull(),
620 eq(this.settings.getCommand(ConverterCategory.MakeIndex)),
621 aryEq(LatexProcessor.buildArguments(
622 this.settings.getMakeIndexOptions(), this.idxFile)),
623 eq(this.indFile));
624 this.inOrder.verify(this.fileUtils).matchInFile(this.ilgFile,
625 this.settings.getPatternErrMakeIndex());
626 }
627
628 private void mockRunMakeGlossaryByNeed(boolean runMakeGlossaries)
629 throws BuildFailureException {
630
631 assert !runMakeGlossaries;
632 if (!runMakeGlossaries) {
633 return;
634 }
635 assert false;
636
637
638
639
640
641
642
643
644
645
646
647
648
649 }
650
651 private void verifyRunMakeGlossaryByNeed(boolean runMakeGlossaries)
652 throws BuildFailureException {
653
654 assert !runMakeGlossaries;
655 if (!runMakeGlossaries) {
656 return;
657 }
658 assert false;
659
660 this.inOrder.verify(this.executor).executeEnvR0(eq(WORKING_DIR), isNull(),
661 eq(this.settings.getCommand(ConverterCategory.MakeGlossaries)),
662 aryEq(LatexProcessor.buildArguments(
663 this.settings.getMakeGlossariesOptions(), this.xxxFile)),
664 eq(this.glsFile));
665
666
667 }
668
669 private void mockRunLatex() throws BuildFailureException {
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 when(this.fileUtils.matchInFile(this.logFile,
685 this.settings.getPatternErrLatex()))
686 .thenReturn(Boolean.FALSE);
687 }
688
689 private void verifyRunLatex() throws BuildFailureException {
690
691
692
693 verify(this.executor, atLeastOnce()).executeEnvR0(eq(WORKING_DIR), isNull(),
694 eq(this.settings.getCommand(ConverterCategory.LaTeX)),
695 aryEq(LatexProcessor.buildLatexArguments(this.settings,
696 this.settings.getPdfViaDvi(), this.texFile, false)),
697 eq(this.dviPdfFile));
698
699
700
701 verify(this.fileUtils, atLeastOnce()).matchInFile(this.logFile,
702 this.settings.getPatternErrLatex());
703 }
704
705 private void mockRunLatex2html() throws BuildFailureException {
706
707 assertEquals(this.htmlFile,
708 TexFileUtils.replaceSuffix(this.texFile, LatexProcessor.SUFFIX_HTML));
709
710
711
712
713
714
715
716
717
718
719
720 when(this.fileUtils.matchInFile(this.logFile,
721 this.settings.getPatternErrLatex())).thenReturn(Boolean.FALSE);
722 when(this.fileUtils.matchInFile(this.logFile,
723 LatexProcessor.PATTERN_OUFULL_HVBOX)).thenReturn(Boolean.FALSE);
724 when(this.fileUtils.matchInFile(this.logFile,
725 this.settings.getPatternWarnLatex())).thenReturn(Boolean.FALSE);
726 }
727
728 private void verifyRunLatex2html() throws BuildFailureException {
729
730
731
732 this.inOrder.verify(this.executor).executeEnvR0(eq(WORKING_DIR), isNull(),
733 eq(this.settings.getTex4htCommand()),
734 aryEq(
735 LatexProcessor.buildHtlatexArguments(this.settings, this.texFile)),
736 eq(this.htmlFile));
737 String[] patterns = new String[] {this.settings.getPatternErrLatex(),
738 LatexProcessor.PATTERN_OUFULL_HVBOX,
739 this.settings.getPatternWarnLatex()};
740 for (int idx = 0; idx < patterns.length; idx++) {
741 this.inOrder.verify(this.fileUtils).matchInFile(this.logFile,
742 patterns[idx]);
743 }
744 }
745 }