View Javadoc

1   package org.apache.maven.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 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 }