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 junit.framework.TestCase;
23 import org.apache.commons.io.FileUtils;
24 import org.codehaus.plexus.digest.Digester;
25 import org.codehaus.plexus.digest.Md5Digester;
26 import org.codehaus.plexus.digest.Sha1Digester;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
33 import org.junit.Before;
34 import org.junit.runner.RunWith;
35
36
37
38
39 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
40 public abstract class AbstractFileEventTest
41 extends TestCase
42 {
43 protected List<Digester> digesters;
44
45 @SuppressWarnings( "unchecked" )
46 @Before
47 @Override
48 public void setUp()
49 throws Exception
50 {
51 super.setUp();
52
53 digesters = Arrays.asList( (Digester) new Md5Digester(), (Digester) new Sha1Digester() );
54 }
55
56 protected void assertChecksumExists( File file, String algorithm )
57 {
58 assertChecksum( file, algorithm, true );
59 }
60
61 protected void assertChecksumDoesNotExist( File file, String algorithm )
62 {
63 assertChecksum( file, algorithm, false );
64 }
65
66 private void assertChecksum( File file, String algorithm, boolean exist )
67 {
68 String msg = exist ? "exists" : "does not exist";
69 File checksumFile = new File( file.getPath() + "." + algorithm );
70 assertEquals( "Test file " + algorithm + " checksum " + msg, exist, checksumFile.exists() );
71 }
72
73 protected void assertChecksumCommit( File file )
74 throws IOException
75 {
76 assertChecksumExists( file, "md5" );
77 assertChecksumExists( file, "sha1" );
78 }
79
80 protected void assertChecksumRollback( File file )
81 throws IOException
82 {
83 assertChecksumDoesNotExist( file, "md5" );
84 assertChecksumDoesNotExist( file, "sha1" );
85 }
86
87 protected String readFile( File file )
88 throws IOException
89 {
90 return FileUtils.readFileToString( file );
91 }
92
93 protected void writeFile( File file, String content )
94 throws IOException
95 {
96 FileUtils.writeStringToFile( file, content );
97 }
98 }