1 package org.apache.maven.archiva.transaction;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.commons.io.FileUtils;
25
26
27
28 public class CreateFileEventTest
29 extends AbstractFileEventTest
30 {
31 private File testDir = new File( getBasedir(), "target/transaction-tests/create-file" );
32
33 public void testCreateCommitRollback()
34 throws Exception
35 {
36 File testFile = new File( testDir, "test-file.txt" );
37
38 CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
39
40 assertFalse( "Test file is not yet created", testFile.exists() );
41
42 event.commit();
43
44 assertTrue( "Test file has been created", testFile.exists() );
45
46 assertChecksumCommit( testFile );
47
48 event.rollback();
49
50 assertFalse( "Test file is has been deleted after rollback", testFile.exists() );
51
52 assertChecksumRollback( testFile );
53
54 assertFalse( "Test file parent directories has been rolledback too", testDir.exists() );
55 assertTrue( "target directory still exists", new File( getBasedir(), "target" ).exists() );
56 }
57
58 public void testCreateCommitRollbackWithBackup()
59 throws Exception
60 {
61 File testFile = new File( testDir, "test-file.txt" );
62
63 testFile.getParentFile().mkdirs();
64
65 testFile.createNewFile();
66
67 writeFile( testFile, "original contents" );
68
69 CreateFileEvent event = new CreateFileEvent( "modified contents", testFile, digesters );
70
71 String contents = readFile( testFile );
72
73 assertEquals( "Test contents have not changed", "original contents", contents );
74
75 event.commit();
76
77 contents = readFile( testFile );
78
79 assertEquals( "Test contents have not changed", "modified contents", contents );
80
81 assertChecksumCommit( testFile );
82
83 event.rollback();
84
85 contents = readFile( testFile );
86
87 assertEquals( "Test contents have not changed", "original contents", contents );
88
89 assertChecksumRollback( testFile );
90 }
91
92 public void testCreateRollbackCommit()
93 throws Exception
94 {
95 File testFile = new File( testDir, "test-file.txt" );
96
97 CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
98
99 assertFalse( "Test file is not yet created", testFile.exists() );
100
101 event.rollback();
102
103 assertFalse( "Test file is not yet created", testFile.exists() );
104
105 event.commit();
106
107 assertTrue( "Test file is not yet created", testFile.exists() );
108
109 assertChecksumCommit( testFile );
110 }
111
112 protected void tearDown()
113 throws Exception
114 {
115 super.tearDown();
116
117 FileUtils.deleteDirectory( new File( getBasedir(), "target/transaction-tests" ) );
118 }
119 }