View Javadoc
1   package org.apache.archiva.transaction;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }