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.upgrade;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    
019    import java.util.Collections;
020    import java.util.Enumeration;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import javax.portlet.PortletPreferences;
025    
026    /**
027     * @author Iv??n Zaera
028     */
029    public class MockPortletPreferences implements PortletPreferences {
030    
031            @Override
032            public Map<String, String[]> getMap() {
033                    return _map;
034            }
035    
036            @Override
037            public Enumeration<String> getNames() {
038                    return Collections.enumeration(_map.keySet());
039            }
040    
041            @Override
042            public String getValue(String key, String defaultValue) {
043                    String[] values = _map.get(key);
044    
045                    if (ArrayUtil.isNotEmpty(values)) {
046                            return values[0];
047                    }
048    
049                    return defaultValue;
050            }
051    
052            @Override
053            public String[] getValues(String key, String[] defaultValues) {
054                    String[] values = _map.get(key);
055    
056                    if (ArrayUtil.isNotEmpty(values)) {
057                            return values;
058                    }
059    
060                    return defaultValues;
061            }
062    
063            @Override
064            public boolean isReadOnly(String key) {
065                    return false;
066            }
067    
068            @Override
069            public void reset(String key) {
070                    _map.remove(key);
071            }
072    
073            @Override
074            public void setValue(String key, String value) {
075                    _map.put(key, new String[] { value });
076            }
077    
078            @Override
079            public void setValues(String key, String[] values) {
080                    _map.put(key, values);
081            }
082    
083            @Override
084            public void store() {
085            }
086    
087            private final Map<String, String[]> _map = new HashMap<>();
088    
089    }