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