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;
23 import org.apache.archiva.common.utils.FileUtil;
26 import java.io.IOException;
30 public class CopyFileEventTest
31 extends AbstractFileEventTest
33 private File testDir = new File( FileUtil.getBasedir(), "target/transaction-tests/copy-file" );
35 private File testDest = new File( testDir, "test-file.txt" );
37 private File testSource = new File( FileUtil.getBasedir(), "target/transaction-tests/test-file.txt" );
39 private File testDestChecksum;
41 private String source, oldChecksum;
48 testSource.getParentFile().mkdirs();
50 testSource.createNewFile();
52 writeFile( testSource, "source contents" );
54 testDestChecksum = new File( testDest.getPath() + ".sha1" );
56 testDestChecksum.getParentFile().mkdirs();
58 testDestChecksum.createNewFile();
60 writeFile( testDestChecksum, "this is the checksum" );
62 assertTrue( "Test if the source exists", testSource.exists() );
64 assertTrue( "Test if the destination checksum exists", testDestChecksum.exists() );
66 source = readFile( testSource );
68 oldChecksum = readFile( testDestChecksum );
71 public void testCopyCommitRollback()
74 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
76 assertFalse( "Test that the destination is not yet created", testDest.exists() );
80 assertTrue( "Test that the destination is created", testDest.exists() );
82 assertChecksumCommit( testDest );
84 String target = readFile( testDest );
86 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
90 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
92 assertChecksumRollback( testDest );
95 public void testCopyCommitRollbackWithBackup()
98 testDest.getParentFile().mkdirs();
100 testDest.createNewFile();
102 writeFile( testDest, "overwritten contents" );
104 assertTrue( "Test that the destination exists", testDest.exists() );
106 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
108 String target = readFile( testDest );
110 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
114 target = readFile( testDest );
116 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
118 assertChecksumCommit( testDest );
122 target = readFile( testDest );
124 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
126 assertChecksumRollback( testDest );
129 public void testCreateRollbackCommit()
132 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
134 assertFalse( "Test that the destination is not yet created", testDest.exists() );
138 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
142 assertTrue( "Test that the destination is created", testDest.exists() );
144 assertChecksumCommit( testDest );
146 String target = readFile( testDest );
148 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
151 protected void tearDown()
156 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "target/transaction-tests" ) );
159 protected void assertChecksumCommit( File file )
162 super.assertChecksumCommit( file );
164 String target = readFile( testDestChecksum );
166 assertFalse( "Test that the destination checksum contents are created correctly", oldChecksum.equals( target ) );
169 protected void assertChecksumRollback( File file )
172 assertChecksumDoesNotExist( file, "md5" );
173 assertChecksumExists( file, "sha1" );
175 String target = readFile( testDestChecksum );
177 assertEquals( "Test that the destination checksum contents are reverted correctly", oldChecksum, target );