001/*
002 * Copyright 2011 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.tests;
017
018import javax.sql.DataSource;
019
020import org.aopalliance.intercept.MethodInterceptor;
021import org.aopalliance.intercept.MethodInvocation;
022import org.atteo.moonshine.liquibase.LiquibaseFacade;
023
024import com.google.common.base.Strings;
025import com.google.inject.ConfigurationException;
026import com.google.inject.Inject;
027import com.google.inject.Injector;
028import com.google.inject.Key;
029import com.google.inject.name.Names;
030
031public class FixtureInterceptor implements MethodInterceptor {
032
033    @Inject
034    private Injector injector;
035
036    @Override
037    public Object invoke(final MethodInvocation invocation) throws Throwable {
038        final Fixture annotation = invocation.getMethod().getAnnotation(Fixture.class);
039        String[] fixtureNames = annotation.value();
040        String databaseName = annotation.database();
041        Class<? extends ChangelogParametersProvider> providerClass = annotation.parametersProvider();
042
043        ChangelogParametersProvider provider = providerClass.newInstance();
044
045        DataSource dataSource;
046        try {
047            if (Strings.isNullOrEmpty(databaseName)) {
048                dataSource = injector.getInstance(DataSource.class);
049            } else {
050                dataSource = injector.getInstance(Key.get(DataSource.class, Names.named(databaseName)));
051            }
052        } catch (ConfigurationException e) {
053            throw new RuntimeException("Cannot find database for annotation " + annotation, e);
054        }
055
056        for (int i = 0; i < fixtureNames.length; i++) {
057            if (!fixtureNames[i].startsWith("/")) {
058                fixtureNames[i] = "/" + invocation.getMethod().getDeclaringClass().getPackage().getName()
059                    .replace('.', '/')
060                    + "/" + fixtureNames[i];
061            }
062        }
063
064        LiquibaseFacade liquibase = new LiquibaseFacade(dataSource);
065
066        for (String fixtureName : fixtureNames) {
067            liquibase.migrate(fixtureName, null, provider.getChangelogParameters());
068        }
069
070        Object o = null;
071        try {
072            o = invocation.proceed();
073        } finally {
074            for (int i = fixtureNames.length - 1; i >= 0; i--) {
075                liquibase.rollbackLastUpdate(fixtureNames[i], null, provider.getChangelogParameters());
076            }
077        }
078        return o;
079    }
080}