1 package org.apache.archiva.transaction;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.io.FileUtils;
25 import java.io.IOException;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
32 public class CopyFileEventTest
33 extends AbstractFileEventTest
35 private File testDir = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/copy-file" );
37 private File testDest = new File( testDir, "test-file.txt" );
39 private File testSource = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/test-file.txt" );
41 private File testDestChecksum;
43 private String source, oldChecksum;
52 testSource.getParentFile().mkdirs();
54 testSource.createNewFile();
56 writeFile( testSource, "source contents" );
58 testDestChecksum = new File( testDest.getPath() + ".sha1" );
60 testDestChecksum.getParentFile().mkdirs();
62 testDestChecksum.createNewFile();
64 writeFile( testDestChecksum, "this is the checksum" );
66 assertTrue( "Test if the source exists", testSource.exists() );
68 assertTrue( "Test if the destination checksum exists", testDestChecksum.exists() );
70 source = readFile( testSource );
72 oldChecksum = readFile( testDestChecksum );
76 public void testCopyCommitRollback()
79 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
81 assertFalse( "Test that the destination is not yet created", testDest.exists() );
85 assertTrue( "Test that the destination is created", testDest.exists() );
87 assertChecksumCommit( testDest );
89 String target = readFile( testDest );
91 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
95 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
97 assertChecksumRollback( testDest );
101 public void testCopyCommitRollbackWithBackup()
104 testDest.getParentFile().mkdirs();
106 testDest.createNewFile();
108 writeFile( testDest, "overwritten contents" );
110 assertTrue( "Test that the destination exists", testDest.exists() );
112 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
114 String target = readFile( testDest );
116 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
120 target = readFile( testDest );
122 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
124 assertChecksumCommit( testDest );
128 target = readFile( testDest );
130 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
132 assertChecksumRollback( testDest );
136 public void testCreateRollbackCommit()
139 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
141 assertFalse( "Test that the destination is not yet created", testDest.exists() );
145 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
149 assertTrue( "Test that the destination is created", testDest.exists() );
151 assertChecksumCommit( testDest );
153 String target = readFile( testDest );
155 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
160 public void tearDown()
165 FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
169 protected void assertChecksumCommit( File file )
172 super.assertChecksumCommit( file );
174 String target = readFile( testDestChecksum );
176 assertFalse( "Test that the destination checksum contents are created correctly", oldChecksum.equals( target ) );
180 protected void assertChecksumRollback( File file )
183 assertChecksumDoesNotExist( file, "md5" );
184 assertChecksumExists( file, "sha1" );
186 String target = readFile( testDestChecksum );
188 assertEquals( "Test that the destination checksum contents are reverted correctly", oldChecksum, target );