/
CommonsTransactionPlatformTransactionManagerIntegrationTest.java
110 lines (93 loc) · 3.15 KB
/
CommonsTransactionPlatformTransactionManagerIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.davidkarlsen.commonstransaction.spring;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.transaction.file.ResourceManagerException;
import org.junit.Assert;
import org.junit.Before;
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;
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration
public class CommonsTransactionPlatformTransactionManagerIntegrationTest
{
@Autowired
private SomeService someService;
private File storeDir;
@Before
public void before()
throws IOException
{
storeDir = new File( SystemUtils.JAVA_IO_TMPDIR, "storeDir" );
FileUtils.deleteDirectory( storeDir );
}
private void assertFileExists( String resourceId )
{
File file = new File( storeDir, resourceId );
Assert.assertTrue( file.exists() );
}
private void assertFileNotExists( String resourceId )
{
File file = new File( storeDir, resourceId );
Assert.assertFalse( file.exists() );
}
@Test
public void testCreateDelete()
throws ResourceManagerException
{
final String resourceId = "someId";
someService.create( resourceId );
assertFileExists( resourceId );
someService.delete( resourceId );
assertFileNotExists( resourceId );
}
@Test
public void testRollback()
throws IOException, ResourceManagerException
{
final String fileName = "someFileName";
final RuntimeException runtimeException = new RuntimeException();
try
{
someService.writeStringToFile( "someString", fileName, runtimeException );
}
catch ( RuntimeException e )
{
Assert.assertEquals( runtimeException, e );
assertFileNotExists( fileName );
}
}
@Test
public void testWriteRead()
throws Exception
{
final String fileName = "someExistFile";
final String fileContents = "someString";
someService.writeStringToFile( fileContents, fileName, null );
assertFileExists( fileName );
String compareString = someService.readFile( fileName );
Assert.assertEquals( fileContents, compareString );
}
@Test( expected = ResourceManagerException.class )
public void testInvalidFileName()
throws IOException, ResourceManagerException
{
someService.writeStringToFile( "", "", null );
}
@Test( expected = ResourceManagerException.class )
public void testNullFileName()
throws IOException, ResourceManagerException
{
someService.writeStringToFile( "", null, null );
}
@Test
public void testDeleteNonExisting()
throws ResourceManagerException
{
someService.delete( "notExisting" );
}
}