|
| 1 | +package com.davidkarlsen.commonstransaction.spring.issue1; |
| 2 | + |
| 3 | +import com.davidkarlsen.commonstransaction.spring.CommonsTransactionPlatformTransactionManager; |
| 4 | +import com.davidkarlsen.commonstransaction.spring.FileResourceManagerFactory; |
| 5 | +import com.davidkarlsen.commonstransaction.spring.TransactionAwareFileResourceManager; |
| 6 | +import org.apache.commons.transaction.file.FileResourceManager; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.ClassRule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.rules.TemporaryFolder; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author et2448 |
| 25 | + * @since 7/7/15 |
| 26 | + */ |
| 27 | +@RunWith( SpringJUnit4ClassRunner.class ) |
| 28 | +@ContextConfiguration( classes = Issue1Test.TestConfig.class ) |
| 29 | +public class Issue1Test |
| 30 | + extends Assert |
| 31 | +{ |
| 32 | + @ClassRule |
| 33 | + public static TemporaryFolder temporaryFolder = new TemporaryFolder( new File( "target" ) ); |
| 34 | + static File workDir; |
| 35 | + static File storeDir; |
| 36 | + |
| 37 | + //@Autowired |
| 38 | + //private FileServiceTwo fileServiceTwo; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private FileService fileService; |
| 42 | + |
| 43 | + @BeforeClass |
| 44 | + public static void before() throws IOException { |
| 45 | + workDir = temporaryFolder.newFolder( "workDir" ); |
| 46 | + storeDir = temporaryFolder.newFolder( "storeDir" ); |
| 47 | + } |
| 48 | + |
| 49 | + @Configuration |
| 50 | + @EnableTransactionManagement( proxyTargetClass = true ) |
| 51 | + public static class TestConfig |
| 52 | + { |
| 53 | + @Bean |
| 54 | + public FileService fileService() |
| 55 | + { |
| 56 | + return new FileService(); |
| 57 | + } |
| 58 | + |
| 59 | + @Bean |
| 60 | + public FileServiceTwo fileServiceTwo() |
| 61 | + { |
| 62 | + return new FileServiceTwo(); |
| 63 | + } |
| 64 | + |
| 65 | + @Bean |
| 66 | + public FileResourceManagerFactory fileResourceManagerFactory() |
| 67 | + throws IOException |
| 68 | + { |
| 69 | + FileResourceManagerFactory fileResourceManagerFactory = new FileResourceManagerFactory(); |
| 70 | + fileResourceManagerFactory.setStoreDir( storeDir ); |
| 71 | + fileResourceManagerFactory.setWorkDir( workDir ); |
| 72 | + |
| 73 | + return fileResourceManagerFactory; |
| 74 | + } |
| 75 | + |
| 76 | + @Bean |
| 77 | + public TransactionAwareFileResourceManager transactionAwareFileResourceManager( |
| 78 | + FileResourceManager fileResourceManager ) |
| 79 | + { |
| 80 | + TransactionAwareFileResourceManager transactionAwareFileResourceManager = |
| 81 | + new TransactionAwareFileResourceManager(); |
| 82 | + transactionAwareFileResourceManager.setFileResourceManager( fileResourceManager ); |
| 83 | + |
| 84 | + return transactionAwareFileResourceManager; |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + public CommonsTransactionPlatformTransactionManager fileTransactionManager( |
| 89 | + FileResourceManager fileResourceManager ) |
| 90 | + throws Exception |
| 91 | + { |
| 92 | + CommonsTransactionPlatformTransactionManager commonsTransactionPlatformTransactionManager = |
| 93 | + new CommonsTransactionPlatformTransactionManager(); |
| 94 | + commonsTransactionPlatformTransactionManager.setFileResourceManager( fileResourceManager ); |
| 95 | + |
| 96 | + return commonsTransactionPlatformTransactionManager; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testIssue1() |
| 102 | + throws Exception |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + fileService.test(); |
| 107 | + fail(); |
| 108 | + } |
| 109 | + catch ( RuntimeException e ) { |
| 110 | + assertEmptyDirectory( workDir ); |
| 111 | + assertEmptyDirectory( storeDir ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + private void assertEmptyDirectory( File file ) { |
| 118 | + assertTrue( file.isDirectory() && file.list().length == 0 ); |
| 119 | + } |
| 120 | +} |
0 commit comments