1 package toolsTest;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.Iterator;
11 import java.util.Map;
12
13 import junit.framework.TestCase;
14
15 import org.apache.commons.lang3.ArrayUtils;
16 import org.apache.commons.lang3.CharSet;
17 import org.apache.commons.lang3.CharSetUtils;
18 import org.apache.commons.lang3.ObjectUtils;
19 import org.apache.commons.lang3.RandomStringUtils;
20 import org.apache.commons.lang3.SerializationUtils;
21 import org.apache.commons.lang3.StringEscapeUtils;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.commons.lang3.SystemUtils;
24 import org.apache.commons.lang3.Validate;
25 import org.apache.commons.lang3.math.NumberUtils;
26 import org.apache.commons.lang3.text.WordUtils;
27 import org.apache.commons.lang3.time.DateFormatUtils;
28 import org.apache.commons.lang3.time.DateUtils;
29 import org.apache.commons.lang3.time.StopWatch;
30 import org.junit.Test;
31
32 /**
33 * common-lang工具包测试
34 * @author Administrator
35 *
36 */
37 public class Tests extends TestCase {
38
39 /**
40 * 数组测试:
41 * ArrayUtils.toString(Object array):将数组转换成字符串
42 * ArrayUtils.toString(Object array,stringIfNull):将数组转换成字符串,如果这个字符串为空时以stringIfNull这个自定义字符串替代
43 * ArrayUtils.contains(Object array, objectToFind):数组array中是否含有objectToFind,含有返回true,否则返回false
44 * ArrayUtils.indexOf(Object array, objectToFind):数组array中第一次出现objectToFind的位置,有返回下标,没有返回-1
45 * ArrayUtils.lastIndexOf(Object array, objectToFind):数组array中最后一个objectToFind的位置,有返回下标,没有返回-1
46 * ArrayUtils.clone(Object array):克隆数组,如果是直接赋值的话,因为引用地址是一样的,一个数组改变也会影响另外的那个数组
47 * ArrayUtils.toMap(Object array):将二维数组转换成Map
48 */
49 @SuppressWarnings("unchecked")
50 @Test
51 public void test(){
52 int a[]=null;
53 System.out.println(ArrayUtils.toString(a, "这个为空时的值"));//这个为空时的值
54 String array1[]={"苹果","桔子"};
55 System.out.println(array1);//[Ljava.lang.String;@6ee7e2ec
56 System.out.println(ArrayUtils.toString(array1));//{苹果,桔子}
57
58 String array2[][]={{"name","桔子"},{"age","25"}};
59 System.out.println(array2);//[[Ljava.lang.String;@49ada86
60 System.out.println(ArrayUtils.toString(array2));//{{name,桔子},{age,25}}
61
62 int array3[]={1,2,3,4,5,6,7,8,9,0,7};
63 System.out.println("array3中是否有7:"+ArrayUtils.contains(array3, 7));//array3中是否有7:true
64 System.out.println("array3中7的位置:"+ArrayUtils.indexOf(array3, 7));//array3中7的位置:6
65 System.out.println("array3中最后一个7的位置:"+ArrayUtils.lastIndexOf(array3, 7));//array3中最后一个7的位置:10
66
67 int array4[]=array3;
68 array4[0]=11;
69 System.out.println(ArrayUtils.toString(array3));//{11,2,3,4,5,6,7,8,9,0,7};可以看出array4改变会影响array3,因为他们的引用地址一样
70 System.out.println(ArrayUtils.toString(array4));//{11,2,3,4,5,6,7,8,9,0,7}
71 array3[0]=1;
72 int array5[]=ArrayUtils.clone(array3);
73 array5[1]=22;
74 System.out.println(ArrayUtils.toString(array3));//{1,2,3,4,5,6,7,8,9,0,7}
75 System.out.println(ArrayUtils.toString(array5));//{1,22,3,4,5,6,7,8,9,0,7}
76
77 ArrayUtils.reverse(array3);//反转数组
78 System.out.println(ArrayUtils.toString(array3));//{7,0,9,8,7,6,5,4,3,2,1}
79
80 int array6[]={1,2,3};
81 Integer array7[]=ArrayUtils.toObject(array6);
82 System.out.println(ArrayUtils.toString(array7));//{1,2,3}
83
84 Map arrayToMap=ArrayUtils.toMap(array2);
85 System.out.println(arrayToMap.get("name"));//桔子
86 }
87
88 /**
89 * 字符串测试:
90 * StringUtils.isBlank(String str):判断字符串是否为空字符串
91 * StringUtils.isNumeric(String str);判断字符串是否是数字
92 * StringUtils.chomp(String str):去除换行符
93 * StringUtils.reverseDelimited(String str,char separatorChar):字符串str以separatorChar这个字符进行反转
94 */
95 @Test
96 public void test2(){
97 String str1 = "";
98 String str2 = " ";
99 String str3 = "\t";
100 String str4 = null;
101 String str5 = "123";
102 String str6 = "ABCDEFG";
103 String str7 = "It feels good to use Jakarta Commons.\r\n";
104 System.out.println(StringUtils.isBlank(str1));//true
105 System.out.println(StringUtils.isBlank(str2));//true
106 System.out.println(StringUtils.isBlank(str3));//true
107 System.out.println(StringUtils.isBlank(str4));//true
108 System.out.println("Is str5 numeric? " + StringUtils.isNumeric(str5));//Is str5 numeric? true
109 System.out.println("Is str6 numeric? " + StringUtils.isNumeric(str6));//Is str5 numeric? true
110 System.out.println("str7: " + str7);//str7: It feels good to use Jakarta Commons.
111 System.out.println("================================");
112 String str8 = StringUtils.chomp(str7);
113 str8 = StringUtils.reverseDelimited(str8, ' ');
114 System.out.println("str7 reversed whole words : \r\n" + str8);//str7 reversed whole words : Commons. Jakarta use to good feels It
115 System.out.println("================================");
116 }
117
118 @Test
119 public void test3(){
120 CharSet charSet = CharSet.getInstance("aeiou");
121 System.out.println(charSet.contains('a'));//true
122 System.out.println(charSet.contains('n'));//false
123 }
124
125 /**
126 * CharSetUtils测试:
127 * CharSetUtils.count(String arg0, String... arg1):计算字符串arg0中包含arg1中某字符数
128 * 注:String... arg1可变长参数
129 * CharSetUtils.delete(String arg0, String... set):删除arg0字符串中某字符
130 * CharSetUtils.keep(String arg0, String... set):保留arg0字符串中某字符
131 * CharSetUtils.squeeze(String arg0,String... arg1):合并arg0中重复的字符
132 * CharSetUtils.containsAny(String arg0,String... arg1):字符串中arg0只要包含arg1中某个字符就返回true,否则返回false
133 *
134 */
135 @Test
136 public void test4(){
137 System.out.println("计算字符串中包含某字符数.");
138 System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.","a","e"));//4
139 System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.","ae"));//4
140
141 System.out.println("删除字符串中某字符.");
142 System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.", "ae"));//Th quick brown fox jumps ovr th lzy dog.
143
144 System.out.println("保留字符串中某字符.");
145 System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.", "ae"));//eeea
146
147 System.out.println("合并重复的字符.");
148 System.out.println(CharSetUtils.squeeze("a bbbbbb c dd eeeeee", "b d","e"));//a b c d e
149 System.out.println("字符串中只要包含某个字符就返回true,否则返回false");
150 System.out.println( CharSetUtils.containsAny("abcde", "ahpoiu"));//true
151 }
152 /**
153 * ObjectUtils测试:
154 * ObjectUtils.defaultIfNull(Object object, Object defaultValue):如果object为空则返回defaultValue,不为空返回自身即object
155 */
156 @Test
157 public void test5(){
158 System.out.println("Object为null时,默认打印某字符.");
159 Object obj = null;
160 System.out.println(ObjectUtils.defaultIfNull(obj, "空")); //空
161 }
162
163 /**
164 * SerializationUtils测试:
165 * SerializationUtils.serialize(Serializable obj):序列化对象
166 * SerializationUtils.deserialize(byte[] objectData):反序列化
167 */
168 @Test
169 public void test6(){
170 Date date = new Date();
171 byte[] bytes = SerializationUtils.serialize(date);
172 System.out.println(ArrayUtils.toString(bytes));
173 System.out.println(date);
174
175 Date reDate = (Date) SerializationUtils.deserialize(bytes);
176 System.out.println(reDate);
177 System.out.println(date.equals(reDate));//true
178 System.out.println(date == reDate); //false
179
180 FileOutputStream fos = null;
181 FileInputStream fis = null;
182 try {
183 fos = new FileOutputStream(new File("d:/test.txt"));
184 fis = new FileInputStream(new File("d:/test.txt"));
185 SerializationUtils.serialize(date, fos);
186 Date reDate2 = (Date) SerializationUtils.deserialize(fis);
187
188 System.out.println(date.equals(reDate2));//true
189
190 } catch (FileNotFoundException e) {
191 e.printStackTrace();
192 } finally {
193 try {
194 fos.close();
195 fis.close();
196 } catch (IOException e) {
197 e.printStackTrace();
198 }
199 }
200
201 }
202
203 /**
204 * RandomStringUtils测试:
205 * RandomStringUtils.random(int count,String chars):在指定字符串中生成长度为n的随机字符串
206 * RandomStringUtils.random(int count, boolean letters, boolean numbers):指定从字符或数字中生成随机字符串
207 * letters:字母 ;numbers;数字
208 */
209 @Test
210 public void test7(){
211 System.out.println(RandomStringUtils.random(5,"abcdefghijk"));
212 System.out.println(RandomStringUtils.random(5, true, false));
213 System.out.println(RandomStringUtils.random(5, false, true));
214 System.out.println(RandomStringUtils.random(5, true, true));
215 }
216
217 /**
218 * StringUtils测试:
219 * StringUtils.repeat(String arg0,int arg1):将字符串arg0重复arg1次
220 * StringUtils.center(String str, int size, String padStr):将字符串str按某size居中,长度不足以padStr填充
221 * StringUtils.join(Object[] array, String separator):将字符串数组array用字符串separator连接
222 * StringUtils.abbreviate(String str, int maxWidth):缩短到某长度,用...结尾
223 * StringUtils.abbreviate(String str,int offset,int maxWidth):缩短到某长度,用...结尾
224 * StringUtils.indexOfDifference(CharSequence cs1, CharSequence cs2):返回两字符串第一次不同处索引号(从0开始)
225 * StringUtils.difference(String str1,String str2):返回两字符串不同处开始至结束
226 * StringUtils.containsOnly(CharSequence cs, String validChars):检查一字符串是否为另一字符串的子集
227 * StringUtils.containsNone(CharSequence cs, String validChars):检查一字符串是否不是另一字符串的子集
228 * StringUtils.contains(CharSequence seq, CharSequence searchSeq):检查一字符串是否包含另一字符串
229 * StringUtils.defaultString(String str, String defaultStr):如果str为null时返回defaultStr
230 * StringUtils.split(String str, String separatorChars):将str字符串以separatorChars分割成数组
231 * StringUtils.deleteWhitespace(String arg0):去除字符中的空格
232 * StringUtils.isAlpha(CharSequence arg0):判断arg0是否全是字母和汉字
233 * StringUtils.isNumeric(CharSequence arg0):判断arg0是否全是数字
234 * StringUtils.isAlphanumeric(CharSequence arg0):判断arg0是否全是数字和数字和汉字
235 * StringUtils.isBlank(CharSequence arg0):判断arg0是否为空
236 */
237 @Test
238 public void test8(){
239 System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
240 String[] header = new String[3];
241 header[0] = StringUtils.repeat("*", 50);
242 header[1] = StringUtils.center(" StringUtilsDemo ", 50, "^O^");
243 header[2] = header[0];
244 String head = StringUtils.join(header, "\n");
245 System.out.println(head);
246
247 System.out.println("缩短到某长度,用...结尾.");
248 System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10));//The quick...
249 System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 15, 10));//... fox...
250
251 System.out.println("返回两字符串不同处索引号.");
252 System.out.println(StringUtils.indexOfDifference("aaabc", "aaacc"));//3
253
254 System.out.println("返回两字符串不同处开始至结束.");
255 System.out.println(StringUtils.difference("aaabcde", "aaaccde")); //ccde
256
257 System.out.println("检查一字符串是否为另一字符串的子集.");
258 System.out.println(StringUtils.containsOnly("aad", "aadd")); //true
259
260 System.out.println("检查一字符串是否不是另一字符串的子集.");
261 System.out.println(StringUtils.containsNone("defg", "aadd")); //true
262
263 System.out.println("检查一字符串是否包含另一字符串.");
264 System.out.println(StringUtils.contains("defg", "ef")); //true
265
266 System.out.println("返回可以处理null的toString().");
267 System.out.println(StringUtils.defaultString(null, "空")); //空
268
269 System.out.println("去除字符中的空格.");
270 System.out.println(StringUtils.deleteWhitespace(" aa bb cc ")); //aabbcc
271
272 System.out.println("分隔符处理成数组.");
273 String[] strArray = StringUtils.split("a,b,,c,d,null,e", ",");
274 System.out.println(ArrayUtils.toString(strArray)); //{a,b,c,d,null,e}
275
276 System.out.println("判断是否是某类字符.");
277 System.out.println(StringUtils.isAlpha("ab")); //true
278 System.out.println(StringUtils.isAlphanumeric("12aa")); //true
279 System.out.println(StringUtils.isBlank("")); //true
280 System.out.println(StringUtils.isNumeric("123")); //true
281 }
282
283 /**
284 * SystemUtils测试:
285 * SystemUtils.FILE_SEPARATOR:获得系统文件分隔符
286 */
287 @Test
288 public void test9(){
289 System.out.println("获得系统文件分隔符.");
290 System.out.println(SystemUtils.FILE_SEPARATOR);
291
292 System.out.println("获得源文件编码.");
293 System.out.println(SystemUtils.FILE_ENCODING);
294
295 System.out.println("获得ext目录.");
296 System.out.println(SystemUtils.JAVA_EXT_DIRS);
297
298 System.out.println("获得java版本.");
299 System.out.println(SystemUtils.JAVA_VM_VERSION);
300
301 System.out.println("获得java厂商.");
302 System.out.println(SystemUtils.JAVA_VENDOR);
303 }
304
305 @Test
306 public void test10(){
307 System.out.println("转换特殊字符.");
308 System.out.println("html:" + StringEscapeUtils.escapeHtml3("<"));//html:<
309 System.out.println("html:" + StringEscapeUtils.escapeHtml4(">"));//html:>
310 System.out.println("html:" + StringEscapeUtils.unescapeHtml3("<p>"));//html:<p>
311 System.out.println("html:" + StringEscapeUtils.unescapeHtml4("<p>"));//html:<p>
312 }
313
314 /**
315 * NumberUtils测试:
316 * NumberUtils.toInt(String arg0, int arg1):字符串arg0转换成数字,转换失败择返回arg1
317 * NumberUtils.max(int...arg0):从数组中选出最大值
318 * NumberUtils.isDigits(String arg0):判断字符串是否全是整数
319 * NumberUtils.isNumber(String arg0):判断字符串是否是有效数字
320 *
321 */
322 @Test
323 public void test11(){
324 System.out.println("字符串转为数字");
325 System.out.println(NumberUtils.toInt("33", 0));//33
326
327 System.out.println("从数组中选出最大值.");
328 System.out.println(NumberUtils.max(new int[] {1, 2, 3, 4 }));
329 System.out.println("判断字符串是否全是整数.");
330 System.out.println(NumberUtils.isDigits("123.1"));
331
332 System.out.println("判断字符串是否是有效数字.");
333 System.out.println(NumberUtils.isNumber("0123.1"));
334 }
335
336 @SuppressWarnings("unchecked")
337 @Test
338 public void test12(){
339 System.out.println("格式化日期输出.");
340 System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
341
342 System.out.println("秒表.");
343 StopWatch sw = new StopWatch();
344 sw.start();
345
346 for (Iterator iterator = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
347 Calendar cal = (Calendar) iterator.next();
348 System.out.println(DateFormatUtils.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss"));
349 }
350
351 sw.stop();
352 System.out.println("秒表计时:" + sw.getTime());
353 }
354
355 @Test
356 public void test13(){
357 String[] strarray = { "a", "b", "c" };
358 System.out.println("验证功能");
359 System.out.println(Validate.notEmpty(strarray));
360 }
361
362 @Test
363 public void test14(){
364 System.out.println("单词处理功能");
365 String str1 = "wOrD";
366 String str2 = "ghj\nui\tpo";
367 System.out.println(WordUtils.capitalize(str1)); // 首字母大写
368 System.out.println(WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写
369 char[] ctrg = { '.' };
370 System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换
371 System.out.println(WordUtils.initials(str1)); // 获取首字母
372 System.out.println(WordUtils.initials("Ben John Lee", null)); // 取每个单词的首字母
373 char[] ctr = { ' ', '.' };
374 System.out.println(WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母
375 System.out.println(WordUtils.swapCase(str1)); // 大小写逆转
376 System.out.println(WordUtils.wrap(str2, 1)); // 解析\n和\t等字符
377 }
378 }