1 package org.apache.maven.archiva.converter.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.codehaus.plexus.PlexusTestCase;
28 * @author Edwin Punzalan
30 public class CopyFileEventTest
31 extends PlexusTestCase
33 private File testDir = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/copy-file" );
35 private File testDest = new File( testDir, "test-file.txt" );
37 private File testSource = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/test-file.txt" );
44 testSource.getParentFile().mkdirs();
46 testSource.createNewFile();
48 FileUtils.writeStringToFile( testSource, "source contents", null );
51 public void testCopyCommitRollback()
54 assertTrue( "Test if the source exists", testSource.exists() );
56 String source = FileUtils.readFileToString( testSource, null );
58 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
60 assertFalse( "Test that the destination is not yet created", testDest.exists() );
64 assertTrue( "Test that the destination is created", testDest.exists() );
66 String target = FileUtils.readFileToString( testDest, null );
68 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
72 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
75 public void testCopyCommitRollbackWithBackup()
78 assertTrue( "Test if the source exists", testSource.exists() );
80 String source = FileUtils.readFileToString( testSource, null );
82 testDest.getParentFile().mkdirs();
84 testDest.createNewFile();
86 FileUtils.writeStringToFile( testDest, "overwritten contents", null );
88 assertTrue( "Test that the destination exists", testDest.exists() );
90 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
92 String target = FileUtils.readFileToString( testDest, null );
94 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
98 target = FileUtils.readFileToString( testDest, null );
100 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
104 target = FileUtils.readFileToString( testDest, null );
106 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
109 public void testCreateRollbackCommit()
112 assertTrue( "Test if the source exists", testSource.exists() );
114 String source = FileUtils.readFileToString( testSource, null );
116 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
118 assertFalse( "Test that the destination is not yet created", testDest.exists() );
122 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
126 assertTrue( "Test that the destination is created", testDest.exists() );
128 String target = FileUtils.readFileToString( testDest, null );
130 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
133 protected void tearDown()
138 FileUtils.deleteDirectory( new File( PlexusTestCase.getBasedir(), "target/transaction-tests" ) );