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.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
33 public class CopyFileEventTest
34 extends AbstractFileEventTest
36 private Path testDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/copy-file");
38 private Path testDest = testDir.resolve( "test-file.txt" );
40 private Path testSource = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/test-file.txt" );
42 private Path testDestChecksum;
44 private String source, oldChecksum;
53 Files.createDirectories(testSource.getParent());
55 Files.createFile(testSource);
57 writeFile( testSource, "source contents" );
59 testDestChecksum = Paths.get( testDest.toAbsolutePath() + ".sha1" );
61 Files.createDirectories(testDestChecksum.getParent());
63 Files.createFile(testDestChecksum);
65 writeFile( testDestChecksum, "this is the checksum" );
67 assertTrue( "Test if the source exists", Files.exists(testSource) );
69 assertTrue( "Test if the destination checksum exists", Files.exists(testDestChecksum) );
71 source = readFile( testSource );
73 oldChecksum = readFile( testDestChecksum );
77 public void testCopyCommitRollback()
80 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
82 assertFalse( "Test that the destination is not yet created", Files.exists(testDest) );
86 assertTrue( "Test that the destination is created", Files.exists(testDest) );
88 assertChecksumCommit( testDest );
90 String target = readFile( testDest );
92 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
96 assertFalse( "Test that the destination file has been deleted", Files.exists(testDest) );
98 assertChecksumRollback( testDest );
102 public void testCopyCommitRollbackWithBackup()
105 Files.createDirectories(testDest.getParent());
107 Files.createFile(testDest);
109 writeFile( testDest, "overwritten contents" );
111 assertTrue( "Test that the destination exists", Files.exists(testDest) );
113 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
115 String target = readFile( testDest );
117 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
121 target = readFile( testDest );
123 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
125 assertChecksumCommit( testDest );
129 target = readFile( testDest );
131 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
133 assertChecksumRollback( testDest );
137 public void testCreateRollbackCommit()
140 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
142 assertFalse( "Test that the destination is not yet created", Files.exists(testDest) );
146 assertFalse( "Test that the destination file is not yet created", Files.exists(testDest) );
150 assertTrue( "Test that the destination is created", Files.exists(testDest) );
152 assertChecksumCommit( testDest );
154 String target = readFile( testDest );
156 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
161 public void tearDown()
166 org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
170 protected void assertChecksumCommit( Path file )
173 super.assertChecksumCommit( file );
175 String target = readFile( testDestChecksum );
177 assertFalse( "Test that the destination checksum contents are created correctly", oldChecksum.equals( target ) );
181 protected void assertChecksumRollback( Path file )
184 assertChecksumDoesNotExist( file, "md5" );
185 assertChecksumExists( file, "sha1" );
187 String target = readFile( testDestChecksum );
189 assertEquals( "Test that the destination checksum contents are reverted correctly", oldChecksum, target );