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 org.apache.commons.io.FileUtils;
23
24 import java.io.File;
25 import java.io.IOException;
26
27
28
29 public class CopyFileEventTest
30 extends AbstractFileEventTest
31 {
32 private File testDir = new File( getBasedir(), "target/transaction-tests/copy-file" );
33
34 private File testDest = new File( testDir, "test-file.txt" );
35
36 private File testSource = new File( getBasedir(), "target/transaction-tests/test-file.txt" );
37
38 private File testDestChecksum;
39
40 private String source, oldChecksum;
41
42 public void setUp()
43 throws Exception
44 {
45 super.setUp();
46
47 testSource.getParentFile().mkdirs();
48
49 testSource.createNewFile();
50
51 writeFile( testSource, "source contents" );
52
53 testDestChecksum = new File( testDest.getPath() + ".sha1" );
54
55 testDestChecksum.getParentFile().mkdirs();
56
57 testDestChecksum.createNewFile();
58
59 writeFile( testDestChecksum, "this is the checksum" );
60
61 assertTrue( "Test if the source exists", testSource.exists() );
62
63 assertTrue( "Test if the destination checksum exists", testDestChecksum.exists() );
64
65 source = readFile( testSource );
66
67 oldChecksum = readFile( testDestChecksum );
68 }
69
70 public void testCopyCommitRollback()
71 throws Exception
72 {
73 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
74
75 assertFalse( "Test that the destination is not yet created", testDest.exists() );
76
77 event.commit();
78
79 assertTrue( "Test that the destination is created", testDest.exists() );
80
81 assertChecksumCommit( testDest );
82
83 String target = readFile( testDest );
84
85 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
86
87 event.rollback();
88
89 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
90
91 assertChecksumRollback( testDest );
92 }
93
94 public void testCopyCommitRollbackWithBackup()
95 throws Exception
96 {
97 testDest.getParentFile().mkdirs();
98
99 testDest.createNewFile();
100
101 writeFile( testDest, "overwritten contents" );
102
103 assertTrue( "Test that the destination exists", testDest.exists() );
104
105 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
106
107 String target = readFile( testDest );
108
109 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
110
111 event.commit();
112
113 target = readFile( testDest );
114
115 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
116
117 assertChecksumCommit( testDest );
118
119 event.rollback();
120
121 target = readFile( testDest );
122
123 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
124
125 assertChecksumRollback( testDest );
126 }
127
128 public void testCreateRollbackCommit()
129 throws Exception
130 {
131 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
132
133 assertFalse( "Test that the destination is not yet created", testDest.exists() );
134
135 event.rollback();
136
137 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
138
139 event.commit();
140
141 assertTrue( "Test that the destination is created", testDest.exists() );
142
143 assertChecksumCommit( testDest );
144
145 String target = readFile( testDest );
146
147 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
148 }
149
150 protected void tearDown()
151 throws Exception
152 {
153 super.tearDown();
154
155 FileUtils.deleteDirectory( new File( getBasedir(), "target/transaction-tests" ) );
156 }
157
158 protected void assertChecksumCommit( File file )
159 throws IOException
160 {
161 super.assertChecksumCommit( file );
162
163 String target = readFile( testDestChecksum );
164
165 assertFalse( "Test that the destination checksum contents are created correctly", oldChecksum.equals( target ) );
166 }
167
168 protected void assertChecksumRollback( File file )
169 throws IOException
170 {
171 assertChecksumDoesNotExist( file, "md5" );
172 assertChecksumExists( file, "sha1" );
173
174 String target = readFile( testDestChecksum );
175
176 assertEquals( "Test that the destination checksum contents are reverted correctly", oldChecksum, target );
177 }
178 }