001    package liquibase.maven;
002    
003    import java.io.IOException;
004    import java.io.File;
005    import org.apache.maven.it.VerificationException;
006    
007    import org.apache.maven.it.Verifier;
008    import org.apache.maven.it.util.ResourceExtractor;
009    import org.junit.Test;
010    import liquibase.database.Database;
011    import liquibase.database.DatabaseConnection;
012    import liquibase.database.DatabaseFactory;
013    import liquibase.test.DatabaseTestContext;
014    import org.junit.Before;
015    import static org.junit.Assert.*;
016    
017    /**
018     * Maven integration test. Run an update executing maven as if it was ran by the user
019     * @author lujop
020     */
021    public class MavenIntegrationTest {
022        private static final String URL="jdbc:hsqldb:file:target/test-classes/maven/liquibase;shutdown=true";
023    
024        @Before
025        public void cleanDatabase() throws Exception {
026             DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(URL);
027             assertNotNull(connection);
028             Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
029             database.dropDatabaseObjects(null);
030             database.close();
031             DatabaseFactory.reset();
032        }
033    
034        @Test
035        public void testUpdate() throws Exception{        
036            Verifier verifier=createVerifier();
037    
038            verifier.executeGoal( "clean" );
039            verifier.executeGoal( "install" );
040    
041            //Verify everithing has gone well.
042            verifier.verifyErrorFreeLog();
043    
044            //Reset the streams before executing the verifier
045            verifier.resetStreams();
046        }
047        
048        @Test
049        public void testRollbackTag() throws Exception {
050            Verifier verifier= createVerifier();
051    
052    
053            verifier.executeGoal("clean");
054            verifier.executeGoal("liquibase:tag");
055            verifier.executeGoal("package"); //runs update that is bound to test phase
056            verifier.executeGoal("liquibase:rollback");
057            //If we can reupdate rollback has succeded
058            verifier.executeGoal("liquibase:update");
059    
060            //Verify everithing has gone well.
061            verifier.verifyErrorFreeLog();
062    
063            //Reset the streams before executing the verifier
064            verifier.resetStreams();
065        }
066    
067       private Verifier createVerifier() throws IOException, VerificationException {
068            File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/maven" );
069    
070            //Clear any artifact created by the test project to avoid unstable test results
071            Verifier verifier = new Verifier(testDir.getAbsolutePath());
072            verifier.setAutoclean(false); //Don't do clean automatically in each executeGoal
073            verifier.deleteArtifact("org.liquibase", "liquibase-maven-integration-tests", "1.0-SNAPSHOT", "jar");
074            return verifier;
075        }
076        
077    }