1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.migrator.helper;
26
27 import org.slf4j.migrator.helper.Abbreviator;
28
29 import junit.framework.TestCase;
30
31 public class AbbreviatorTest extends TestCase {
32
33 static final char FS = '/';
34 static final String INPUT_0 = "/abc/123456/ABC";
35 static final String INPUT_1 = "/abc/123456/xxxxx/ABC";
36
37 RandomHelper rh = new RandomHelper(FS);
38
39 public AbbreviatorTest(String arg0) {
40 super(arg0);
41 }
42
43 protected void setUp() throws Exception {
44 super.setUp();
45 }
46
47 protected void tearDown() throws Exception {
48 super.tearDown();
49 }
50
51 public void testSmoke() {
52 {
53 Abbreviator abb = new Abbreviator(2, 100, FS);
54 String r = abb.abbreviate(INPUT_0);
55 assertEquals(INPUT_0, r);
56 }
57
58 {
59 Abbreviator abb = new Abbreviator(3, 8, FS);
60 String r = abb.abbreviate(INPUT_0);
61 assertEquals("/abc/.../ABC", r);
62 }
63 {
64 Abbreviator abb = new Abbreviator(3, 8, FS);
65 String r = abb.abbreviate(INPUT_0);
66 assertEquals("/abc/.../ABC", r);
67 }
68 }
69
70 public void testImpossibleToAbbreviate() {
71 Abbreviator abb = new Abbreviator(2, 20, FS);
72 String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
73 String r = abb.abbreviate(in);
74 assertEquals(in, r);
75 }
76
77 public void testNoFS() {
78 Abbreviator abb = new Abbreviator(2, 100, FS);
79 String r = abb.abbreviate("hello");
80 assertEquals("hello", r);
81
82 }
83
84 public void testZeroPrefix() {
85 {
86 Abbreviator abb = new Abbreviator(0, 100, FS);
87 String r = abb.abbreviate(INPUT_0);
88 assertEquals(INPUT_0, r);
89 }
90 }
91
92 public void testTheories() {
93 int MAX_RANDOM_FIXED_LEN = 20;
94 int MAX_RANDOM_AVG_LEN = 20;
95 int MAX_RANDOM_MAX_LEN = 100;
96 for (int i = 0; i < 10000; i++) {
97
98
99
100
101 int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
102
103 int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
104
105
106 int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
107 if (maxLen <= 1) {
108 continue;
109 }
110
111 int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;
112
113 if (targetLen > maxLen) {
114 targetLen = maxLen;
115 }
116 String filename = rh.buildRandomFileName(averageLen, maxLen);
117
118 Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
119 String result = abb.abbreviate(filename);
120 assertTheory0(averageLen, filename, result, fixedLen, targetLen);
121 assertUsefulness(averageLen, filename, result, fixedLen, targetLen);
122 assertTheory1(filename, result, fixedLen, targetLen);
123 assertTheory2(filename, result, fixedLen, targetLen);
124 }
125 }
126
127
128 void assertTheory0(int averageLen, String filename, String result,
129 int fixedLen, int targetLength) {
130 assertTrue("filename=[" + filename + "] result=[" + result + "]", result
131 .length() <= filename.length());
132 }
133
134
135 void assertUsefulness(int averageLen, String filename, String result,
136 int fixedLen, int targetLength) {
137 int resLen = result.length();
138
139 int margin = averageLen * 4;
140 if (targetLength > fixedLen + margin) {
141 assertTrue("filename=[" + filename + "], result=[" + result
142 + "] resultLength=" + resLen + " fixedLength=" + fixedLen
143 + ", targetLength=" + targetLength + ", avgLen=" + averageLen, result
144 .length() <= targetLength + averageLen);
145 }
146 }
147
148
149 void assertTheory1(String filename, String result, int fixedLen,
150 int targetLength) {
151 String prefix = filename.substring(0, fixedLen);
152 assertTrue(result.startsWith(prefix));
153 }
154
155
156
157 void assertTheory2(String filename, String result, int fixedLen,
158 int targetLength) {
159 if (filename == result) {
160 return;
161 }
162 int fillerIndex = result.indexOf(Abbreviator.FILLER);
163 assertTrue(fillerIndex >= fixedLen);
164 }
165 }