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