001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util.test;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    
019    import java.io.InputStream;
020    
021    import java.util.Collections;
022    import java.util.Enumeration;
023    import java.util.List;
024    import java.util.Properties;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class TestPropsUtil {
030    
031            public static String get(String key) {
032                    return _instance._get(key);
033            }
034    
035            public static Properties getProperties() {
036                    return _instance._props;
037            }
038    
039            public static void printProperties() {
040                    _instance._printProperties(true);
041            }
042    
043            public static void set(String key, String value) {
044                    _instance._set(key, value);
045            }
046    
047            private TestPropsUtil() {
048                    try {
049                            ClassLoader classLoader = ClassLoader.getSystemClassLoader();
050    
051                            InputStream is = classLoader.getResourceAsStream(
052                                    "test-portal-impl.properties");
053    
054                            _props.load(is);
055    
056                            is = classLoader.getResourceAsStream(
057                                    "test-portal-impl-ext.properties");
058    
059                            if (is != null) {
060                                    _props.load(is);
061                            }
062    
063                            _printProperties(false);
064                    }
065                    catch (Exception e) {
066                            e.printStackTrace();
067                    }
068            }
069    
070            private String _get(String key) {
071                    return _props.getProperty(key);
072            }
073    
074            private void _printProperties(boolean update) {
075                    List<String> keys = Collections.list(
076                            (Enumeration<String>)_props.propertyNames());
077    
078                    keys = ListUtil.sort(keys);
079    
080                    if (update) {
081                            System.out.println("-- updated properties --");
082                    }
083                    else {
084                            System.out.println("-- listing properties --");
085                    }
086    
087                    for (String key : keys) {
088                            System.out.println(key + "=" + _props.getProperty(key));
089                    }
090    
091                    System.out.println("");
092            }
093    
094            private void _set(String key, String value) {
095                    _props.setProperty(key, value);
096            }
097    
098            private static TestPropsUtil _instance = new TestPropsUtil();
099    
100            private Properties _props = new Properties();
101    
102    }