cz.muni.fi.editor.test.service.support.configs.TestConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.test.service.support.configs.TestConfiguration.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.editor.test.service.support.configs;

import cz.muni.fi.editor.database.helpers.DataSourceConfiguration;
import cz.muni.fi.editor.database.helpers.HibernateConfiguration;
import cz.muni.fi.editor.services.commons.ServiceConfiguration;
import cz.muni.fi.editor.test.service.support.other.NotificationCheckService;
import cz.muni.fi.editor.test.service.support.other.NotificationCheckServiceImpl;
import cz.muni.fi.editor.test.service.support.other.EnhancedRandomFactory;
import lombok.extern.log4j.Log4j2;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;

import java.util.concurrent.Executor;

/**
 * @author Dominik Szalai - emptulik at gmail.com on 23.04.2016.
 */
@Configuration
@TestExecutionListeners({ DirtiesContextTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@Import({ DataSourceConfiguration.class, HibernateConfiguration.class, ServiceConfiguration.class })
@PropertySources({ @PropertySource("classpath:config/hibernate-test.properties"),
        @PropertySource("classpath:config/jdbc-test.properties"),
        @PropertySource("classpath:config/database.properties"),
        @PropertySource("classpath:config/cmis.properties"),
        @PropertySource("classpath:config/service-test.properties") })
@ComponentScan({ "cz.muni.fi.editor.database", "cz.muni.fi.editor.services" })
@ActiveProfiles("test")
@Log4j2
@EnableGlobalMethodSecurity(prePostEnabled = true, order = 2147483646)
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class TestConfiguration implements AsyncConfigurer {
    //    @Autowired
    //    private void Configure(HttpSecurity http) throws Exception
    //    {
    //        http.sessionManagement()
    //                .maximumSessions(1)
    //                .sessionRegistry(sessionRegistry());
    //    }
    //    private DataSource dataSource;

    //    @Autowired
    //    public TestConfiguration(DataSource dataSource)
    //    {
    //        this.dataSource = dataSource;
    //    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public EnhancedRandomFactory enhancedRandomFactory() {
        return new EnhancedRandomFactory();
    }

    @Bean
    public NotificationCheckService bypassService() {
        return new NotificationCheckServiceImpl();
    }

    @Bean
    public SessionRegistry sessionRegistry() {
        return new SessionRegistryImpl();
    }

    //    @Bean
    //    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
    //        return (ThreadPoolTaskExecutor) getAsyncExecutor();
    //    }

    @Override
    public Executor getAsyncExecutor() {
        // move to configuration
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(42);
        executor.setQueueCapacity(11);
        executor.setThreadNamePrefix("EditorExecutor-");
        executor.initialize();

        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}