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.kernel.test.util;
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                            Thread currentThread = Thread.currentThread();
050    
051                            ClassLoader classLoader = currentThread.getContextClassLoader();
052    
053                            InputStream is = classLoader.getResourceAsStream(
054                                    "test-portal-impl.properties");
055    
056                            _props.load(is);
057    
058                            is = classLoader.getResourceAsStream(
059                                    "test-portal-impl-ext.properties");
060    
061                            if (is != null) {
062                                    _props.load(is);
063                            }
064    
065                            _printProperties(false);
066                    }
067                    catch (Exception e) {
068                            e.printStackTrace();
069                    }
070            }
071    
072            private String _get(String key) {
073                    return _props.getProperty(key);
074            }
075    
076            private void _printProperties(boolean update) {
077                    List<String> keys = Collections.list(
078                            (Enumeration<String>)_props.propertyNames());
079    
080                    keys = ListUtil.sort(keys);
081    
082                    if (update) {
083                            System.out.println("-- updated properties --");
084                    }
085                    else {
086                            System.out.println("-- listing properties --");
087                    }
088    
089                    for (String key : keys) {
090                            System.out.println(key + "=" + _props.getProperty(key));
091                    }
092    
093                    System.out.println("");
094            }
095    
096            private void _set(String key, String value) {
097                    _props.setProperty(key, value);
098            }
099    
100            private static final TestPropsUtil _instance = new TestPropsUtil();
101    
102            private final Properties _props = new Properties();
103    
104    }