001package org.apache.maven.scm.provider.svn; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import junit.framework.Assert; 023import org.apache.maven.scm.ScmTestCase; 024import org.codehaus.plexus.util.FileUtils; 025import org.codehaus.plexus.util.Os; 026import org.codehaus.plexus.util.StringUtils; 027import org.codehaus.plexus.util.cli.CommandLineException; 028import org.codehaus.plexus.util.cli.CommandLineUtils; 029import org.codehaus.plexus.util.cli.Commandline; 030 031import java.io.File; 032import java.io.FileInputStream; 033import java.io.InputStream; 034 035/** 036 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 037 * 038 */ 039public final class SvnScmTestUtils 040{ 041 /** 'svn' command line */ 042 public static final String SVN_COMMAND_LINE = "svn"; 043 044 /** 'svnadmin' command line */ 045 public static final String SVNADMIN_COMMAND_LINE = "svnadmin"; 046 047 private SvnScmTestUtils() 048 { 049 } 050 051 public static void initializeRepository( File repositoryRoot ) 052 throws Exception 053 { 054 if ( repositoryRoot.exists() ) 055 { 056 FileUtils.deleteDirectory( repositoryRoot ); 057 } 058 059 Assert.assertFalse( "repositoryRoot still exists", repositoryRoot.exists() ); 060 061 Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(), 062 repositoryRoot.mkdirs() ); 063 064 ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, 065 "create " + repositoryRoot.getName() ); 066 067 loadSvnDump( repositoryRoot, 068 new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) ); 069 } 070 071 public static void initializeRepository( File repositoryRoot, File dump ) 072 throws Exception 073 { 074 if ( repositoryRoot.exists() ) 075 { 076 FileUtils.deleteDirectory( repositoryRoot ); 077 } 078 079 Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(), 080 repositoryRoot.mkdirs() ); 081 082 ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, 083 "create " + repositoryRoot.getName() ); 084 085 Assert.assertTrue( "The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists() ); 086 087 loadSvnDump( repositoryRoot, new FileInputStream( dump ) ); 088 } 089 090 private static void loadSvnDump( File repositoryRoot, InputStream dumpStream ) 091 throws Exception 092 { 093 Commandline cl = new Commandline(); 094 095 cl.setExecutable( SVNADMIN_COMMAND_LINE ); 096 097 cl.setWorkingDirectory( repositoryRoot.getParentFile().getAbsolutePath() ); 098 099 cl.createArg().setValue( "load" ); 100 101 cl.createArg().setValue( repositoryRoot.getAbsolutePath() ); 102 103 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 104 105 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 106 107 int exitValue = CommandLineUtils.executeCommandLine( cl, dumpStream, stdout, stderr ); 108 109 if ( exitValue != 0 ) 110 { 111 System.err.println( "-----------------------------------------" ); 112 System.err.println( "Command line: " + cl ); 113 System.err.println( "Working directory: " + cl.getWorkingDirectory() ); 114 System.err.println( "-----------------------------------------" ); 115 System.err.println( "Standard output: " ); 116 System.err.println( "-----------------------------------------" ); 117 System.err.println( stdout.getOutput() ); 118 System.err.println( "-----------------------------------------" ); 119 120 System.err.println( "Standard error: " ); 121 System.err.println( "-----------------------------------------" ); 122 System.err.println( stderr.getOutput() ); 123 System.err.println( "-----------------------------------------" ); 124 } 125 126 if ( exitValue != 0 ) 127 { 128 Assert.fail( "Exit value wasn't 0, was:" + exitValue ); 129 } 130 } 131 132 public static String getScmUrl( File repositoryRootFile ) 133 throws CommandLineException 134 { 135 String repositoryRoot = repositoryRootFile.getAbsolutePath(); 136 137 // TODO: it'd be great to build this into CommandLineUtils somehow 138 // TODO: some way without a custom cygwin sys property? 139 if ( "true".equals( System.getProperty( "cygwin" ) ) ) 140 { 141 Commandline cl = new Commandline(); 142 143 cl.setExecutable( "cygpath" ); 144 145 cl.createArg().setValue( "--unix" ); 146 147 cl.createArg().setValue( repositoryRoot ); 148 149 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 150 151 int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null ); 152 153 if ( exitValue != 0 ) 154 { 155 throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue ); 156 } 157 158 repositoryRoot = stdout.getOutput().trim(); 159 } 160 else if ( Os.isFamily( "windows" ) ) 161 { 162 repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" ); 163 } 164 165 return "scm:svn:file://" + repositoryRoot; 166 } 167}