1 package org.apache.maven.scm.provider.bazaar;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmFileSet;
23 import org.apache.maven.scm.ScmResult;
24 import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
25 import org.codehaus.plexus.PlexusTestCase;
26 import org.codehaus.plexus.util.FileUtils;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36
37
38 public class BazaarRepoUtils
39 extends PlexusTestCase
40 {
41
42 public static final String[] filesInTestBranch =
43 new String[] { "pom.xml", "readme.txt", "src/main/java/Application.java", "src/test/java/Test.java" };
44
45 public static final String TCK_FILE_CONSTANT = "/";
46
47 public static final String BRANCH_NAME = "target" + File.separator + "test-branch";
48
49 public static final File WORKING_DIR = new File( getBasedir(), BRANCH_NAME );
50
51 public static final String COMMIT_MESSAGE = "Add files to test branch";
52
53 public static String getScmUrl()
54 throws Exception
55 {
56 return "scm:bazaar:file://" + WORKING_DIR.getAbsolutePath();
57 }
58
59 public static void initRepo()
60 throws Exception
61 {
62
63 if ( WORKING_DIR.exists() )
64 {
65 FileUtils.deleteDirectory( WORKING_DIR );
66
67 if ( WORKING_DIR.exists() )
68 {
69 throw new IOException( WORKING_DIR.getAbsolutePath() + " wasn't deleted." );
70 }
71 }
72
73 boolean workingDirReady = WORKING_DIR.mkdirs();
74
75 if ( !workingDirReady )
76 {
77 throw new IOException( "Could not initiate test branch at: " + WORKING_DIR );
78 }
79
80
81 String[] init_cmd = new String[]{BazaarConstants.INIT_CMD};
82 BazaarUtils.execute( WORKING_DIR, init_cmd );
83
84
85 List<File> files = new ArrayList<File>();
86 for ( int i = 0; i < filesInTestBranch.length; i++ )
87 {
88 File file = new File( WORKING_DIR.getAbsolutePath(), filesInTestBranch[i] );
89 if ( file.getParentFile() != null && !file.getParentFile().exists() )
90 {
91 boolean success = file.getParentFile().mkdirs();
92 if ( !success )
93 {
94 throw new IOException( "Could not create directories in branch for: " + file );
95 }
96 }
97 file.createNewFile();
98
99 FileUtils.fileWrite( file.getAbsolutePath(), TCK_FILE_CONSTANT + filesInTestBranch[i] );
100
101 files.add( file );
102 }
103
104
105 String[] add_cmd = new String[] { BazaarConstants.ADD_CMD };
106 ScmFileSet filesToAdd = new ScmFileSet( new File( "" ), files );
107 add_cmd = BazaarUtils.expandCommandLine( add_cmd, filesToAdd );
108 ScmResult result = BazaarUtils.execute( WORKING_DIR, add_cmd );
109 if ( !result.isSuccess() )
110 {
111 String message =
112 "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
113 throw new Exception( message );
114 }
115
116
117 String[] commit_cmd = new String[]{BazaarConstants.COMMIT_CMD, BazaarConstants.MESSAGE_OPTION, COMMIT_MESSAGE};
118 result = BazaarUtils.execute( WORKING_DIR, commit_cmd );
119 if ( !result.isSuccess() )
120 {
121 String message =
122 "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
123 throw new Exception( message );
124 }
125 }
126 }