1   package org.apache.maven.surefire.its;
2   
3   import org.apache.maven.settings.Profile;
4   import org.apache.maven.settings.Repository;
5   import org.apache.maven.settings.RepositoryPolicy;
6   import org.apache.maven.settings.Settings;
7   import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
8   import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
9   import org.codehaus.plexus.util.xml.XmlStreamReader;
10  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
11  
12  import java.io.File;
13  import java.io.FileWriter;
14  import java.io.IOException;
15  import java.util.Random;
16  /*
17  * Licensed to the Apache Software Foundation (ASF) under one
18  * or more contributor license agreements.  See the NOTICE file
19  * distributed with this work for additional information
20  * regarding copyright ownership.  The ASF licenses this file
21  * to you under the Apache License, Version 2.0 (the
22  * "License"); you may not use this file except in compliance
23  * with the License.  You may obtain a copy of the License at
24  *
25  *  http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing,
28  * software distributed under the License is distributed on an
29  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
30  * KIND, either express or implied.  See the License for the
31  * specific language governing permissions and limitations
32  * under the License.
33  */
34  
35  /**
36   * Helper class to assist in using verifier with a staged local repository.
37   *
38   * @author Stephen Connolly
39   * @since 05-Jan-2010 07:36:22
40   */
41  public final class StagedLocalRepoHelper
42  {
43      private StagedLocalRepoHelper()
44      {
45          throw new IllegalAccessError( "Helper class" );
46      }
47  
48      private static String toUrl( String filename )
49      {
50          /*
51          * NOTE: Maven fails to properly handle percent-encoded "file:" URLs (WAGON-111) so don't use File.toURI() here
52          * as-is but use the decoded path component in the URL.
53          */
54          String url = "file://" + new File( filename ).toURI().getPath();
55          if ( url.endsWith( "/" ) )
56          {
57              url = url.substring( 0, url.length() - 1 );
58          }
59          return url;
60      }
61  
62  
63      public static void createStagedSettingsXml( File originalSettingsXml, File stagedLocalRepo, File stagedSettingsXml )
64          throws IOException
65      {
66          Random entropy = new Random();
67          SettingsXpp3Reader reader = new SettingsXpp3Reader();
68          SettingsXpp3Writer writer = new SettingsXpp3Writer();
69          try
70          {
71              Settings settings = reader.read( new XmlStreamReader( originalSettingsXml ) );
72  
73              String localRepo = System.getProperty( "maven.repo.local" );
74  
75              if ( localRepo == null )
76              {
77                  localRepo = settings.getLocalRepository();
78              }
79  
80              if ( localRepo == null )
81              {
82                  localRepo = System.getProperty( "user.home" ) + "/.m2/repository";
83              }
84  
85              File repoDir = new File( localRepo );
86  
87              if ( !repoDir.exists() )
88              {
89                  repoDir.mkdirs();
90              }
91  
92              // normalize path
93              localRepo = repoDir.getAbsolutePath();
94  
95              Profile profile = new Profile();
96              do
97              {
98                  profile.setId( "stagedLocalRepo" + entropy.nextLong() );
99              }
100             while ( settings.getProfilesAsMap().containsKey( profile.getId() ) );
101             Repository repository = new Repository();
102             repository.setId( profile.getId() + entropy.nextLong() );
103             RepositoryPolicy policy = new RepositoryPolicy();
104             policy.setEnabled( true );
105             policy.setChecksumPolicy( "ignore" );
106             policy.setUpdatePolicy( "never" );
107             repository.setReleases( policy );
108             repository.setSnapshots( policy );
109             repository.setLayout( "default" );
110             repository.setName( "Original Local Repository" );
111             repository.setUrl( toUrl( localRepo ) );
112             profile.addPluginRepository( repository );
113             profile.addRepository( repository );
114             settings.addProfile( profile );
115             settings.addActiveProfile( profile.getId() );
116             settings.setLocalRepository( stagedLocalRepo.getAbsolutePath() );
117             writer.write( new FileWriter( stagedSettingsXml ), settings );
118         }
119         catch ( XmlPullParserException e )
120         {
121             IOException ioe = new IOException( e.getMessage() );
122             ioe.initCause( e );
123             throw ioe;
124         }
125     }
126 }