aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/io/trygvis/test/spring
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/io/trygvis/test/spring')
-rw-r--r--src/test/java/io/trygvis/test/spring/PlainSpringTest.java59
-rwxr-xr-xsrc/test/java/io/trygvis/test/spring/TestConfig.java172
2 files changed, 231 insertions, 0 deletions
diff --git a/src/test/java/io/trygvis/test/spring/PlainSpringTest.java b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java
new file mode 100644
index 0000000..9a7a436
--- /dev/null
+++ b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java
@@ -0,0 +1,59 @@
+package io.trygvis.test.spring;
+
+import io.trygvis.async.AsyncService;
+import io.trygvis.queue.Queue;
+import io.trygvis.spring.DefaultConfig;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static java.lang.System.getProperty;
+import static java.lang.System.setProperty;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = {TestConfig.class, DefaultConfig.class})
+public class PlainSpringTest {
+
+ @Autowired
+ private AsyncService asyncService;
+
+ static {
+ String username = getProperty("user.name");
+ setProperty("database.url", getProperty("jdbc.url", "jdbc:postgresql://localhost/" + username));
+ setProperty("database.username", username);
+ setProperty("database.password", username);
+ }
+
+ @Test
+ public void testBasic() throws SQLException, InterruptedException {
+ final AtomicReference<List<String>> ref = new AtomicReference<>();
+ Queue test = asyncService.registerQueue("test", 10, new AsyncService.AsyncCallable() {
+ public void run(List<String> arguments) throws Exception {
+ System.out.println("PlainSpringTest.run");
+ ref.set(arguments);
+ synchronized (ref) {
+ ref.notify();
+ }
+ }
+ });
+
+ synchronized (ref) {
+ System.out.println("Scheduling task");
+ asyncService.schedule(test, "hello", "world");
+ System.out.println("Waiting");
+ ref.wait(1000);
+ }
+
+ List<String> args = ref.get();
+ assertNotNull(args);
+ assertThat(args).containsExactly("hello", "world");
+ }
+}
diff --git a/src/test/java/io/trygvis/test/spring/TestConfig.java b/src/test/java/io/trygvis/test/spring/TestConfig.java
new file mode 100755
index 0000000..7853cb5
--- /dev/null
+++ b/src/test/java/io/trygvis/test/spring/TestConfig.java
@@ -0,0 +1,172 @@
+package io.trygvis.test.spring;
+
+import com.jolbox.bonecp.BoneCPDataSource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
+import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.transaction.support.TransactionTemplate;
+
+import javax.sql.DataSource;
+
+@Configuration
+@ComponentScan(basePackages = "io.trygvis")
+@EnableTransactionManagement
+//@EnableJpaRepositories(basePackages = "io.trygvis.data")
+public class TestConfig {
+
+ @Bean
+ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws Exception {
+ return new PropertySourcesPlaceholderConfigurer() {{
+// setLocation(new UrlResource("file:environment.properties"));
+ setProperties(System.getProperties());
+ setLocalOverride(true);
+ }};
+ }
+
+ @Bean
+ public JdbcTemplate jdbcTemplate(DataSource dataSource) {
+ return new JdbcTemplate(dataSource);
+ }
+
+// public SpringBeanJobFactory springBeanJobFactory() {
+// SpringBeanJobFactory factory = new SpringBeanJobFactory();
+// return factory;
+// }
+
+/*
+ @Bean
+ public SchedulerFactoryBean quartz(DataSource dataSource, PlatformTransactionManager transactionManager) {
+ SchedulerFactoryBean bean = new SchedulerFactoryBean();
+ bean.setApplicationContextSchedulerContextKey("applicationContext");
+ bean.setDataSource(dataSource);
+ bean.setTransactionManager(transactionManager);
+// bean.setJobFactory(new JobFactory() {
+// public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
+// Class<? extends Job> klass = bundle.getJobDetail().getJobClass();
+// }
+// });
+
+ Properties quartzProperties = new Properties();
+ quartzProperties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");
+// quartzProperties.setProperty("org.quartz.jobStore.selectWithLockSQL", "false");
+ quartzProperties.setProperty("org.quartz.jobStore.driverDelegateClass", PostgreSQLDelegate.class.getName());
+ quartzProperties.setProperty("org.quartz.scheduler.jmx.export", "true");
+ bean.setQuartzProperties(quartzProperties);
+ return bean;
+ }
+*/
+
+ // This turns out to be fairly useless as Spring won't register them automatically.
+ // It's probably better to use @Scheduled/@Async instead
+ /*
+ @Bean(name = "my-job")
+ public JobDetailFactoryBean myJobDetailBean() {
+ JobDetailFactoryBean bean = new JobDetailFactoryBean();
+ bean.setJobClass(MyJob.class);
+ bean.setDurability(true);
+
+ return bean;
+ }
+
+ @Bean
+ public CronTriggerFactoryBean myJobTrigger(JobDetail jobDetail) {
+ CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
+ bean.setName("my-trigger");
+ bean.setBeanName("my-job");
+ bean.setJobDetail(jobDetail);
+ bean.setCronExpression("0/10 * * * * ?");
+
+ return bean;
+ }
+ */
+
+ @Bean
+ public DataSource dataSource(@Value("${database.url}") String jdbcUrl,
+ @Value("${database.username}") String username,
+ @Value("${database.password}") String password) {
+ BoneCPDataSource ds = new BoneCPDataSource();
+
+ ds.setLogStatementsEnabled(true);
+
+ ds.setJdbcUrl(jdbcUrl);
+ ds.setUsername(username);
+ ds.setPassword(password);
+
+ ds.setIdleConnectionTestPeriodInSeconds(60);
+ ds.setIdleMaxAgeInSeconds(240);
+ ds.setMaxConnectionsPerPartition(40);
+ ds.setMinConnectionsPerPartition(0);
+ ds.setPartitionCount(1);
+ ds.setAcquireIncrement(1);
+ ds.setStatementsCacheSize(1000);
+ ds.setReleaseHelperThreads(3);
+ ds.setStatisticsEnabled(true);
+ return new TransactionAwareDataSourceProxy(new LazyConnectionDataSourceProxy(ds));
+ }
+
+/*
+ @Bean
+ public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
+ @Value("${hibernate.hbm2ddl.auto}") String hbm2ddl,
+ @Value("${hibernate.showSql:false}") boolean showSql,
+ @Value("${hibernate.dialect}") String dialect) {
+ LocalContainerEntityManagerFactoryBean x = new LocalContainerEntityManagerFactoryBean();
+ x.setDataSource(dataSource);
+ x.setJpaPropertyMap(createJpaMap(hbm2ddl, showSql, dialect));
+ x.setPackagesToScan(Article.class.getPackage().getName());
+ HibernatePersistence persistenceProvider = new HibernatePersistence();
+ x.setPersistenceProvider(persistenceProvider);
+ return x;
+ }
+
+ public static Map<String, Object> createJpaMap(String hbm2ddl, boolean showSql, String dialect) {
+ Map<String, Object> map = new HashMap<>();
+ map.put(HBM2DDL_AUTO, hbm2ddl);
+ map.put(FORMAT_SQL, showSql);
+ map.put(SHOW_SQL, showSql);
+ map.put(USE_SQL_COMMENTS, showSql);
+ map.put(GENERATE_STATISTICS, true);
+ map.put(NAMING_STRATEGY, ImprovedNamingStrategy.class.getName());
+
+ map.put(DEFAULT_CACHE_CONCURRENCY_STRATEGY, CacheConcurrencyStrategy.READ_WRITE.toString());
+ map.put(CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
+// map.put(CACHE_REGION_FACTORY, EhCacheRegionFactory.class.getName());
+ map.put(USE_SECOND_LEVEL_CACHE, false);
+ map.put(USE_QUERY_CACHE, false);
+// map.put(SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE.toString());
+
+ map.put(DIALECT, dialect);
+ map.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
+
+ return map;
+ }
+
+ @Bean
+ public SessionFactory sessionFactory(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
+ return ((HibernateEntityManagerFactory) entityManagerFactory.nativeEntityManagerFactory).getSessionFactory();
+ }
+
+ @Bean
+ public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
+ return new JpaTransactionManager(entityManagerFactory);
+ }
+*/
+
+ @Bean
+ public PlatformTransactionManager transactionManager(DataSource dataSource) {
+ return new DataSourceTransactionManager(dataSource);
+ }
+
+ @Bean
+ public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager) {
+ return new TransactionTemplate(platformTransactionManager);
+ }
+}