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;
019import javax.xml.bind.annotation.XmlElement;
020import javax.xml.bind.annotation.XmlIDREF;
021import javax.xml.bind.annotation.XmlRootElement;
022
023import org.atteo.moonshine.TopLevelService;
024import org.atteo.moonshine.database.DatabaseService;
025import org.atteo.moonshine.services.ImportService;
026
027import com.google.inject.AbstractModule;
028import com.google.inject.Inject;
029import com.google.inject.Module;
030import com.google.inject.Provider;
031import com.google.inject.Scopes;
032import com.google.inject.matcher.Matchers;
033
034@XmlRootElement(name = "database-tests")
035public class DatabaseTestUtilities extends TopLevelService {
036    @ImportService
037    @XmlIDREF
038    @XmlElement(name = "database")
039    private DatabaseService database;
040
041    @Override
042    public Module configure() {
043        return new AbstractModule() {
044            @Override
045            protected void configure() {
046                bind(DatabaseCleaner.class).toProvider(new Provider<DatabaseCleaner>() {
047                    @Inject
048                    private DataSource dataSource;
049
050                    @Override
051                    public DatabaseCleaner get() {
052                        return new DatabaseCleaner(dataSource, database);
053                    }
054                }).in(Scopes.SINGLETON);
055
056                FixtureInterceptor interceptor = new FixtureInterceptor();
057                requestInjection(interceptor);
058                bindInterceptor(Matchers.any(), Matchers.annotatedWith(Fixture.class), interceptor);
059            }
060        };
061    }
062}