1   /*
2    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
3    *
4    * By using this Software, You acknowledge that the Software is a valuable asset
5    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
6    * third party supplier of IDSI and constitutes confidential and proprietary
7    * information.
8    *
9    * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
10   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
11   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
12   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
13   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
14   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
15   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
16   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
17   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
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(); // MessageFormat is zero-based so start here
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  }