From 78de20b9d197cdaffb48226a9ab4a549f88e0202 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Thu, 8 Jun 2006 14:20:19 +0000 Subject: [PATCH] Added more unit tests for the TransactionEvents git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@412760 13f79535-47bb-0310-9956-ffa450edef68 --- .../transaction/CopyFileEventTest.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 maven-repository-converter/src/test/java/org/apache/maven/repository/converter/transaction/CopyFileEventTest.java diff --git a/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/transaction/CopyFileEventTest.java b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/transaction/CopyFileEventTest.java new file mode 100644 index 000000000..627c32cde --- /dev/null +++ b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/transaction/CopyFileEventTest.java @@ -0,0 +1,138 @@ +package org.apache.maven.repository.converter.transaction; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; + +/** + * @author Edwin Punzalan + */ +public class CopyFileEventTest + extends PlexusTestCase +{ + private File testDir = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/copy-file" ); + + private File testDest = new File( testDir, "test-file.txt" ); + + private File testSource = new File( PlexusTestCase.getBasedir(), "target/transaction-tests/test-file.txt" ); + + public void setUp() + throws Exception + { + super.setUp(); + + testSource.getParentFile().mkdirs(); + + testSource.createNewFile(); + + FileUtils.fileWrite( testSource.getAbsolutePath(), "source contents" ); + } + + public void testCopyCommitRollback() + throws Exception + { + assertTrue( "Test if the source exists", testSource.exists() ); + + String source = FileUtils.fileRead( testSource.getAbsolutePath() ); + + CopyFileEvent event = new CopyFileEvent( testSource, testDest ); + + assertFalse( "Test that the destination is not yet created", testDest.exists() ); + + event.commit(); + + assertTrue( "Test that the destination is created", testDest.exists() ); + + String target = FileUtils.fileRead( testDest.getAbsolutePath() ); + + assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) ); + + event.rollback(); + + assertFalse( "Test that the destination file has been deleted", testDest.exists() ); + } + + public void testCopyCommitRollbackWithBackup() + throws Exception + { + assertTrue( "Test if the source exists", testSource.exists() ); + + String source = FileUtils.fileRead( testSource.getAbsolutePath() ); + + testDest.getParentFile().mkdirs(); + + testDest.createNewFile(); + + FileUtils.fileWrite( testDest.getAbsolutePath(), "overwritten contents" ); + + assertTrue( "Test that the destination exists", testDest.exists() ); + + CopyFileEvent event = new CopyFileEvent( testSource, testDest ); + + String target = FileUtils.fileRead( testDest.getAbsolutePath() ); + + assertTrue( "Test that the destination contents have not changed", target.equals( "overwritten contents" ) ); + + event.commit(); + + target = FileUtils.fileRead( testDest.getAbsolutePath() ); + + assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) ); + + event.rollback(); + + target = FileUtils.fileRead( testDest.getAbsolutePath() ); + + assertTrue( "Test the destination file contents have been restored", target.equals( "overwritten contents" ) ); + } + + public void testCreateRollbackCommit() + throws Exception + { + assertTrue( "Test if the source exists", testSource.exists() ); + + String source = FileUtils.fileRead( testSource.getAbsolutePath() ); + + CopyFileEvent event = new CopyFileEvent( testSource, testDest ); + + assertFalse( "Test that the destination is not yet created", testDest.exists() ); + + event.rollback(); + + assertFalse( "Test that the destination file is not yet created", testDest.exists() ); + + event.commit(); + + assertTrue( "Test that the destination is created", testDest.exists() ); + + String target = FileUtils.fileRead( testDest.getAbsolutePath() ); + + assertTrue( "Test that the destination contents are copied correctly", source.equals( target ) ); + } + + protected void tearDown() + throws Exception + { + super.tearDown(); + + FileUtils.deleteDirectory( new File( PlexusTestCase.getBasedir(), + "target/transaction-tests" ).getAbsolutePath() ); + } +} -- 2.39.5