1 package org.apache.maven.repository.converter.transaction;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.codehaus.plexus.PlexusTestCase;
20 import org.codehaus.plexus.util.FileUtils;
25 * @author Edwin Punzalan
27 public class CopyFileEventTest
28 extends PlexusTestCase
30 private File testDir = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/copy-file" );
32 private File testDest = new File( testDir, "test-file.txt" );
34 private File testSource = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/test-file.txt" );
41 testSource.getParentFile().mkdirs();
43 testSource.createNewFile();
45 FileUtils.fileWrite( testSource.getAbsolutePath(), "source contents" );
48 public void testCopyCommitRollback()
51 assertTrue( "Test if the source exists", testSource.exists() );
53 String source = FileUtils.fileRead( testSource.getAbsolutePath() );
55 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
57 assertFalse( "Test that the destination is not yet created", testDest.exists() );
61 assertTrue( "Test that the destination is created", testDest.exists() );
63 String target = FileUtils.fileRead( testDest.getAbsolutePath() );
65 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
69 assertFalse( "Test that the destination file has been deleted", testDest.exists() );
72 public void testCopyCommitRollbackWithBackup()
75 assertTrue( "Test if the source exists", testSource.exists() );
77 String source = FileUtils.fileRead( testSource.getAbsolutePath() );
79 testDest.getParentFile().mkdirs();
81 testDest.createNewFile();
83 FileUtils.fileWrite( testDest.getAbsolutePath(), "overwritten contents" );
85 assertTrue( "Test that the destination exists", testDest.exists() );
87 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
89 String target = FileUtils.fileRead( testDest.getAbsolutePath() );
91 assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) );
95 target = FileUtils.fileRead( testDest.getAbsolutePath() );
97 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
101 target = FileUtils.fileRead( testDest.getAbsolutePath() );
103 assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) );
106 public void testCreateRollbackCommit()
109 assertTrue( "Test if the source exists", testSource.exists() );
111 String source = FileUtils.fileRead( testSource.getAbsolutePath() );
113 CopyFileEvent event = new CopyFileEvent( testSource, testDest );
115 assertFalse( "Test that the destination is not yet created", testDest.exists() );
119 assertFalse( "Test that the destination file is not yet created", testDest.exists() );
123 assertTrue( "Test that the destination is created", testDest.exists() );
125 String target = FileUtils.fileRead( testDest.getAbsolutePath() );
127 assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) );
130 protected void tearDown()
135 FileUtils.deleteDirectory( new File( PlexusTestCase.getBasedir(),
136 "target/transaction-tests" ).getAbsolutePath() );