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