1 package org.apache.maven.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.maven.archiva.transaction.CopyFileEvent;
23 import org.codehaus.plexus.PlexusTestCase;
24 import org.codehaus.plexus.util.FileUtils;
27 import java.io.IOException;
30 * @author Edwin Punzalan
32 public class CopyFileEventTest
33 extends AbstractFileEventTest
35 private File testDir = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/copy-file" );
37 private File testDest = new File( testDir, "test-file.txt" );
39 private File testSource = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/test-file.txt" );
41 private File testDestChecksum;
43 private String source, oldChecksum;
50 testSource.getParentFile().mkdirs();
52 testSource.createNewFile();
54 writeFile( testSource, "source contents" );
56 testDestChecksum = new File( testDest.getPath() + ".sha1" );
58 testDestChecksum.getParentFile().mkdirs();
60 testDestChecksum.createNewFile();
62 writeFile( testDestChecksum, "this is the checksum" );
64 assertTrue( "Test if the source exists", testSource.exists() );
66 assertTrue( "Test if the destination checksum exists", testDestChecksum.exists() );
68 source = readFile( testSource );
70 oldChecksum = readFile( testDestChecksum );
73 public void testCopyCommitRollback()
76 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
78 assertFalse( "Test that the destination is not yet created", testDest.exists() );
82 assertTrue( "Test that the destination is created", testDest.exists() );
84 assertChecksumCommit( testDest );
86 String target = readFile( testDest );
88 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
92 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
94 assertChecksumRollback( testDest );
97 public void testCopyCommitRollbackWithBackup()
100 testDest.getParentFile().mkdirs();
102 testDest.createNewFile();
104 writeFile( testDest, "overwritten contents" );
106 assertTrue( "Test that the destination exists", testDest.exists() );
108 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
110 String target = readFile( testDest );
112 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
116 target = readFile( testDest );
118 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
120 assertChecksumCommit( testDest );
124 target = readFile( testDest );
126 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
128 assertChecksumRollback( testDest );
131 public void testCreateRollbackCommit()
134 CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
136 assertFalse( "Test that the destination is not yet created", testDest.exists() );
140 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
144 assertTrue( "Test that the destination is created", testDest.exists() );
146 assertChecksumCommit( testDest );
148 String target = readFile( testDest );
150 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
153 protected void tearDown()
158 FileUtils.deleteDirectory( new File( PlexusTestCase.getBasedir(), "target/transaction-tests" ) );
161 protected void assertChecksumCommit( File file )
164 super.assertChecksumCommit( file );
166 String target = readFile( testDestChecksum );
168 assertFalse( "Test that the destination checksum contents are created correctly", oldChecksum.equals( target ) );
171 protected void assertChecksumRollback( File file )
174 assertChecksumDoesNotExist( file, "md5" );
175 assertChecksumExists( file, "sha1" );
177 String target = readFile( testDestChecksum );
179 assertEquals( "Test that the destination checksum contents are reverted correctly", oldChecksum, target );