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 java.io.File;
23  
24  import org.apache.commons.io.FileUtils;
25  import org.apache.archiva.common.utils.FileUtil;
26  import org.junit.After;
27  import org.junit.Test;
28  
29  /**
30   */
31  public class CreateFileEventTest
32      extends AbstractFileEventTest
33  {
34      private File testDir = new File( FileUtil.getBasedir(), "target/transaction-tests/create-file" );
35  
36      @Test
37      public void testCreateCommitRollback()
38          throws Exception
39      {
40          File testFile = new File( testDir, "test-file.txt" );
41  
42          CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
43  
44          assertFalse( "Test file is not yet created", testFile.exists() );
45  
46          event.commit();
47  
48          assertTrue( "Test file has been created", testFile.exists() );
49  
50          assertChecksumCommit( testFile );
51  
52          event.rollback();
53  
54          assertFalse( "Test file is has been deleted after rollback", testFile.exists() );
55  
56          assertChecksumRollback( testFile );
57  
58          assertFalse( "Test file parent directories has been rolledback too", testDir.exists() );
59          assertTrue( "target directory still exists", new File( FileUtil.getBasedir(), "target" ).exists() );
60      }
61  
62      @Test
63      public void testCreateCommitRollbackWithBackup()
64          throws Exception
65      {
66          File testFile = new File( testDir, "test-file.txt" );
67  
68          testFile.getParentFile().mkdirs();
69  
70          testFile.createNewFile();
71  
72          writeFile( testFile, "original contents" );
73  
74          CreateFileEvent event = new CreateFileEvent( "modified contents", testFile, digesters );
75  
76          String contents = readFile( testFile );
77  
78          assertEquals( "Test contents have not changed", "original contents", contents );
79  
80          event.commit();
81  
82          contents = readFile( testFile );
83  
84          assertEquals( "Test contents have not changed", "modified contents", contents );
85  
86          assertChecksumCommit( testFile );
87  
88          event.rollback();
89  
90          contents = readFile( testFile );
91  
92          assertEquals( "Test contents have not changed", "original contents", contents );
93  
94          assertChecksumRollback( testFile );
95      }
96  
97      @Test
98      public void testCreateRollbackCommit()
99          throws Exception
100     {
101         File testFile = new File( testDir, "test-file.txt" );
102 
103         CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
104 
105         assertFalse( "Test file is not yet created", testFile.exists() );
106 
107         event.rollback();
108 
109         assertFalse( "Test file is not yet created", testFile.exists() );
110 
111         event.commit();
112 
113         assertTrue( "Test file is not yet created", testFile.exists() );
114 
115         assertChecksumCommit( testFile );
116     }
117 
118     @Override
119     @After
120     public void tearDown()
121         throws Exception
122     {
123         super.tearDown();
124 
125         FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "target/transaction-tests" ) );
126     }
127 }