View Javadoc
1   package org.apache.maven.scm.provider.hg;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmFileSet;
23  import org.apache.maven.scm.ScmResult;
24  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
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   * Common code used in all tests.
35   *
36   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
37   */
38  public class HgRepoUtils
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:hg:" + WORKING_DIR.getAbsolutePath();
57      }
58  
59      public static void initRepo()
60          throws Exception
61      {
62          // Prepare tmp directory
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          // Init repository
81          String[] init_cmd = new String[]{HgCommandConstants.INIT_CMD};
82          HgUtils.execute( WORKING_DIR, init_cmd );
83  
84          // Create and add files to repository
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         //Add to repository
105         String[] add_cmd = new String[]{HgCommandConstants.ADD_CMD};
106         ScmFileSet filesToAdd = new ScmFileSet( new File( "" ), files );
107         add_cmd = HgUtils.expandCommandLine( add_cmd, filesToAdd );
108         ScmResult result = HgUtils.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         // Commit the initial repository
117         String[] commit_cmd = new String[]{HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, COMMIT_MESSAGE};
118         result = HgUtils.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 }