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.verify.test;
016    
017    import com.liferay.portal.kernel.settings.CompanyServiceSettingsLocator;
018    import com.liferay.portal.kernel.settings.ModifiableSettings;
019    import com.liferay.portal.kernel.settings.Settings;
020    import com.liferay.portal.kernel.settings.SettingsException;
021    import com.liferay.portal.kernel.settings.SettingsFactory;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.PrefsProps;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.service.CompanyLocalService;
027    import com.liferay.portal.verify.VerifyException;
028    import com.liferay.portal.verify.VerifyProcess;
029    import com.liferay.registry.Registry;
030    import com.liferay.registry.RegistryUtil;
031    import com.liferay.registry.ServiceReference;
032    
033    import java.util.List;
034    
035    import javax.portlet.PortletPreferences;
036    
037    import org.junit.After;
038    import org.junit.Assert;
039    import org.junit.Before;
040    
041    /**
042     * @author Michael C. Han
043     */
044    public abstract class BaseCompanySettingsVerifyProcessTestCase
045            extends BaseVerifyProcessTestCase {
046    
047            @Before
048            @Override
049            public void setUp() throws Exception {
050                    super.setUp();
051    
052                    Registry registry = RegistryUtil.getRegistry();
053    
054                    ServiceReference<CompanyLocalService> companyLocalServiceReference =
055                            registry.getServiceReference(CompanyLocalService.class);
056    
057                    companyLocalService = registry.getService(companyLocalServiceReference);
058    
059                    ServiceReference<PrefsProps> prefsPropsServiceReference =
060                            registry.getServiceReference(PrefsProps.class);
061    
062                    prefsProps = registry.getService(prefsPropsServiceReference);
063    
064                    ServiceReference<SettingsFactory> configurationAdminServiceReference =
065                            registry.getServiceReference(SettingsFactory.class);
066    
067                    settingsFactory = registry.getService(
068                            configurationAdminServiceReference);
069    
070                    UnicodeProperties properties = new UnicodeProperties();
071    
072                    populateLegacyProperties(properties);
073    
074                    List<Company> companies = companyLocalService.getCompanies(false);
075    
076                    for (Company company : companies) {
077                            companyLocalService.updatePreferences(
078                                    company.getCompanyId(), properties);
079                    }
080            }
081    
082            @After
083            @Override
084            public void tearDown() throws Exception {
085                    List<Company> companies = companyLocalService.getCompanies(false);
086    
087                    for (Company company : companies) {
088                            Settings settings = getSettings(company.getCompanyId());
089    
090                            ModifiableSettings modifiableSettings =
091                                    settings.getModifiableSettings();
092    
093                            modifiableSettings.reset();
094    
095                            modifiableSettings.store();
096                    }
097    
098                    super.tearDown();
099            }
100    
101            @Override
102            protected void doVerify() throws VerifyException {
103                    super.doVerify();
104    
105                    List<Company> companies = companyLocalService.getCompanies(false);
106    
107                    for (Company company : companies) {
108                            PortletPreferences portletPreferences = prefsProps.getPreferences(
109                                    company.getCompanyId(), true);
110    
111                            Settings settings = getSettings(company.getCompanyId());
112    
113                            Assert.assertNotNull(settings);
114    
115                            doVerify(portletPreferences, settings);
116                    }
117            }
118    
119            protected abstract void doVerify(
120                    PortletPreferences portletPreferences, Settings settings);
121    
122            protected Settings getSettings(long companyId) {
123                    try {
124                            Settings settings = settingsFactory.getSettings(
125                                    new CompanyServiceSettingsLocator(companyId, getSettingsId()));
126    
127                            return settings;
128                    }
129                    catch (SettingsException e) {
130                            throw new IllegalStateException(e);
131                    }
132            }
133    
134            protected abstract String getSettingsId();
135    
136            @Override
137            protected VerifyProcess getVerifyProcess() {
138                    try {
139                            Registry registry = RegistryUtil.getRegistry();
140    
141                            ServiceReference<?>[] serviceReferences =
142                                    registry.getServiceReferences(
143                                            VerifyProcess.class.getName(),
144                                            "(&(objectClass=" + VerifyProcess.class.getName() +
145                                                    ")(verify.process.name=" + getVerifyProcessName() +
146                                                            "))");
147    
148                            if (ArrayUtil.isEmpty(serviceReferences)) {
149                                    throw new IllegalStateException("Unable to get verify process");
150                            }
151    
152                            return (VerifyProcess)registry.getService(serviceReferences[0]);
153                    }
154                    catch (Exception ise) {
155                            throw new IllegalStateException("Unable to get verify process");
156                    }
157            }
158    
159            protected abstract String getVerifyProcessName();
160    
161            protected abstract void populateLegacyProperties(
162                    UnicodeProperties properties);
163    
164            protected CompanyLocalService companyLocalService;
165            protected PrefsProps prefsProps;
166            protected SettingsFactory settingsFactory;
167    
168    }