1+ package com .itextpdf .text .pdf ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .FileReader ;
5+ import java .text .MessageFormat ;
6+ import org .junit .Assert ;
7+ import org .junit .Test ;
8+
9+ public class DefaultSplitCharacterProfilingTest {
10+
11+ private static final String INPUT_DIR = "./src/test/resources/com/itextpdf/text/pdf/DefaultSplitCharacterProfilingTest/" ;
12+
13+ private static final String CHECK_DATE_PATTERN_FAIL_MESSAGE =
14+ "The test verifies the optimization of the checkDatePattern method. This failure indicates that the optimization was broken." ;
15+
16+ private static final String READ_FILE_FAIL_MESSAGE = "Failed to read test file {0}. The test could not be completed." ;
17+
18+ private static final int TIME_LIMIT = 20000 ;
19+
20+ @ Test (timeout = 30000 )
21+ public void checkDatePatternProfilingTest () {
22+ String testFile = INPUT_DIR + "profilingText.txt" ;
23+ String str = readFile (testFile );
24+ if (str == null ) {
25+ Assert .fail (MessageFormat .format (READ_FILE_FAIL_MESSAGE , testFile ));
26+ }
27+ long startTime = System .currentTimeMillis ();
28+ for (int i = 0 ; i < 70000 ; i ++) {
29+ isSplitCharacter (str );
30+ }
31+ long time = System .currentTimeMillis () - startTime ;
32+ System .out .println ("Test run time: " + time );
33+ Assert .assertTrue (CHECK_DATE_PATTERN_FAIL_MESSAGE , time < TIME_LIMIT );
34+ }
35+
36+ private static void isSplitCharacter (String text ) {
37+ new DefaultSplitCharacter ().isSplitCharacter (0 , 0 , text .length () + 1 , text .toCharArray (), null );
38+ }
39+
40+ private static String readFile (String fileName ) {
41+ StringBuilder stringBuilder = new StringBuilder ();
42+ try {
43+ BufferedReader reader = new BufferedReader (new FileReader (fileName ));
44+ String line ;
45+ while ((line = reader .readLine ()) != null ) {
46+ stringBuilder .append (line );
47+ }
48+ reader .close ();
49+ return stringBuilder .toString ();
50+ } catch (Exception e ) {
51+ return null ;
52+ }
53+ }
54+ }
0 commit comments