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    import com.liferay.portal.kernel.util.ReflectionUtil;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    
023    import java.util.Collections;
024    import java.util.Enumeration;
025    import java.util.List;
026    import java.util.Properties;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class TestPropsUtil {
032    
033            public static String get(String key) {
034                    return _instance._get(key);
035            }
036    
037            public static Properties getProperties() {
038                    return _instance._props;
039            }
040    
041            public static void printProperties() {
042                    _instance._printProperties(true);
043            }
044    
045            public static void set(String key, String value) {
046                    _instance._set(key, value);
047            }
048    
049            private TestPropsUtil() {
050                    try (InputStream is = TestPropsUtil.class.getResourceAsStream(
051                                    "/test-portal-impl.properties")) {
052    
053                            _props.load(is);
054                    }
055                    catch (IOException ioe) {
056                            ReflectionUtil.throwException(ioe);
057                    }
058    
059                    try (InputStream is = TestPropsUtil.class.getResourceAsStream(
060                                    "/test-portal-impl-ext.properties")) {
061    
062                            if (is != null) {
063                                    _props.load(is);
064                            }
065                    }
066                    catch (IOException ioe) {
067                            ReflectionUtil.throwException(ioe);
068                    }
069    
070                    _printProperties(false);
071            }
072    
073            private String _get(String key) {
074                    return _props.getProperty(key);
075            }
076    
077            private void _printProperties(boolean update) {
078                    List<String> keys = Collections.list(
079                            (Enumeration<String>)_props.propertyNames());
080    
081                    keys = ListUtil.sort(keys);
082    
083                    if (update) {
084                            System.out.println("-- updated properties --");
085                    }
086                    else {
087                            System.out.println("-- listing properties --");
088                    }
089    
090                    for (String key : keys) {
091                            System.out.println(key + "=" + _props.getProperty(key));
092                    }
093    
094                    System.out.println("");
095            }
096    
097            private void _set(String key, String value) {
098                    _props.setProperty(key, value);
099            }
100    
101            private static final TestPropsUtil _instance = new TestPropsUtil();
102    
103            private final Properties _props = new Properties();
104    
105    }