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.util.PrefsPropsUtil;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.portlet.PortletPreferences;
023    import javax.portlet.ReadOnlyException;
024    
025    /**
026     * @author Adolfo P??rez
027     */
028    public class PrefsPropsTemporarySwapper implements AutoCloseable {
029    
030            public PrefsPropsTemporarySwapper(
031                            String firstKey, Object firstValue, Object... keysAndValues)
032                    throws Exception {
033    
034                    PortletPreferences portletPreferences = PrefsPropsUtil.getPreferences(
035                            0, false);
036    
037                    _setTemporaryValue(
038                            portletPreferences, firstKey, String.valueOf(firstValue));
039    
040                    for (int i = 0; i < keysAndValues.length; i += 2) {
041                            String key = String.valueOf(keysAndValues[i]);
042                            String value = String.valueOf(keysAndValues[i + 1]);
043    
044                            _setTemporaryValue(portletPreferences, key, value);
045                    }
046    
047                    portletPreferences.store();
048            }
049    
050            @Override
051            public void close() throws Exception {
052                    PortletPreferences portletPreferences = PrefsPropsUtil.getPreferences(
053                            0, false);
054    
055                    for (Map.Entry<String, String> entry : _oldValues.entrySet()) {
056                            portletPreferences.setValue(entry.getKey(), entry.getValue());
057                    }
058    
059                    portletPreferences.store();
060            }
061    
062            private void _setTemporaryValue(
063                            PortletPreferences portletPreferences, String key, String value)
064                    throws ReadOnlyException {
065    
066                    _oldValues.put(key, PrefsPropsUtil.getString(key));
067    
068                    portletPreferences.setValue(key, value);
069            }
070    
071            private final Map<String, String> _oldValues = new HashMap<>();
072    
073    }