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.test;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
018    import com.liferay.portal.kernel.template.TemplateManagerUtil;
019    import com.liferay.portal.kernel.test.BaseTestRule;
020    import com.liferay.portal.kernel.test.ReflectionTestUtil;
021    import com.liferay.portal.model.ModelListener;
022    import com.liferay.portal.model.ModelListenerRegistrationUtil;
023    import com.liferay.portal.tools.DBUpgrader;
024    
025    import java.util.List;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    import org.junit.runner.Description;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class PersistenceTestRule extends BaseTestRule<Object, Object> {
034    
035            public static final PersistenceTestRule INSTANCE =
036                    new PersistenceTestRule();
037    
038            @Override
039            protected void afterMethod(Description description, Object modelListeners) {
040                    Object instance = ReflectionTestUtil.getFieldValue(
041                            ModelListenerRegistrationUtil.class, "_instance");
042    
043                    CacheRegistryUtil.setActive(true);
044    
045                    ReflectionTestUtil.setFieldValue(
046                            instance, "_modelListeners", modelListeners);
047            }
048    
049            @Override
050            protected Object beforeMethod(Description description) {
051                    Object instance = ReflectionTestUtil.getFieldValue(
052                            ModelListenerRegistrationUtil.class, "_instance");
053    
054                    Object modelListeners = ReflectionTestUtil.getFieldValue(
055                            instance, "_modelListeners");
056    
057                    ReflectionTestUtil.setFieldValue(
058                            instance, "_modelListeners",
059                            new ConcurrentHashMap<Class<?>, List<ModelListener<?>>>());
060    
061                    CacheRegistryUtil.setActive(false);
062    
063                    return modelListeners;
064            }
065    
066            private PersistenceTestRule() {
067            }
068    
069            static {
070                    try {
071                            DBUpgrader.upgrade();
072    
073                            TemplateManagerUtil.init();
074                    }
075                    catch (Throwable t) {
076                            throw new ExceptionInInitializerError(t);
077                    }
078                    finally {
079                            CacheRegistryUtil.setActive(true);
080                    }
081            }
082    
083    }