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