1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.addam.ddlrun;
20
21 import junit.framework.TestCase;
22
23 import java.util.regex.Pattern;
24 import java.util.regex.Matcher;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.text.MessageFormat;
28
29 /**
30 * @author TIM3
31 * @since May 18, 2005 12:19:24 PM
32 */
33 public class ServletContainerUpgraderUTEST extends TestCase {
34
35 public void testGetLogFileViaMessageFormat() {
36 Pattern p = Pattern.compile("\\{([^\\{\\}\\,]*)[\\}|\\,]");
37 Matcher m = p.matcher("/{user.name}/{java.home}/{baz}/{current.time,date,yyyyMMdd.hhmm}");
38 StringBuffer buf = new StringBuffer();
39 ArrayList args = new ArrayList();
40 while (m.find()) {
41 int location = args.size();
42 String match = m.group(0);
43 String variable = m.group(1);
44 String replacement = "{" + location + (match.endsWith(",") ? "," : "}");
45 args.add(getReplacement(variable));
46 m.appendReplacement(buf, replacement);
47 }
48 m.appendTail(buf);
49 System.out.println(MessageFormat.format(buf.toString(),args.toArray()));
50 }
51
52 private Object getReplacement(String variable) {
53 Object replacement = System.getProperty(variable);
54 if (replacement == null) {
55 if (variable.equals("current.time")) {
56 replacement = new Date();
57 } else {
58 replacement = "{" + variable + "}";
59 }
60 }
61 return replacement;
62 }
63 }