View Javadoc
1   package org.apache.maven.surefire.its;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.Random;
26  import org.apache.maven.settings.Profile;
27  import org.apache.maven.settings.Repository;
28  import org.apache.maven.settings.RepositoryPolicy;
29  import org.apache.maven.settings.Settings;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
31  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
32  import org.apache.maven.shared.utils.ReaderFactory;
33  import org.apache.maven.shared.utils.WriterFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * Helper class to assist in using verifier with a staged local repository.
38   *
39   * @author Stephen Connolly
40   * @since 05-Jan-2010 07:36:22
41   */
42  public final class StagedLocalRepoHelper
43  {
44      private StagedLocalRepoHelper()
45      {
46          throw new IllegalAccessError( "Helper class" );
47      }
48  
49      private static String toUrl( String filename )
50      {
51          /*
52          * NOTE: Maven fails to properly handle percent-encoded "file:" URLs (WAGON-111) so don't use File.toURI() here
53          * as-is but use the decoded path component in the URL.
54          */
55          String url = "file://" + new File( filename ).toURI().getPath();
56          if ( url.endsWith( "/" ) )
57          {
58              url = url.substring( 0, url.length() - 1 );
59          }
60          return url;
61      }
62  
63  
64      public static void createStagedSettingsXml( File originalSettingsXml, File stagedLocalRepo, File stagedSettingsXml )
65          throws IOException
66      {
67          Random entropy = new Random();
68          try
69          {
70              Settings settings = new SettingsXpp3Reader().read( ReaderFactory.newXmlReader( originalSettingsXml ) );
71  
72              String localRepo = System.getProperty( "maven.repo.local" );
73  
74              if ( localRepo == null )
75              {
76                  localRepo = settings.getLocalRepository();
77              }
78  
79              if ( localRepo == null )
80              {
81                  localRepo = System.getProperty( "user.home" ) + "/.m2/repository";
82              }
83  
84              File repoDir = new File( localRepo );
85  
86              if ( !repoDir.exists() )
87              {
88                  repoDir.mkdirs();
89              }
90  
91              // normalize path
92              localRepo = repoDir.getAbsolutePath();
93  
94              Profile profile = new Profile();
95              do
96              {
97                  profile.setId( "stagedLocalRepo" + entropy.nextLong() );
98              }
99              while ( settings.getProfilesAsMap().containsKey( profile.getId() ) );
100 
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 
118             for ( Object o : settings.getProfiles() )
119             {
120                 profile = (Profile) o;
121                 disableUpdates( profile.getRepositories() );
122                 disableUpdates( profile.getPluginRepositories() );
123             }
124 
125             new SettingsXpp3Writer().write( WriterFactory.newXmlWriter( stagedSettingsXml ), settings );
126         }
127         catch ( XmlPullParserException e )
128         {
129             IOException ioe = new IOException( e.getMessage() );
130             ioe.initCause( e );
131             throw ioe;
132         }
133     }
134 
135     private static void disableUpdates( List<Repository> repositories )
136     {
137         if ( repositories != null )
138         {
139             for (Repository repo : repositories) {
140                 repo.setReleases(disableUpdates(repo.getReleases()));
141                 repo.setSnapshots(disableUpdates(repo.getSnapshots()));
142             }
143         }
144     }
145 
146     private static RepositoryPolicy disableUpdates( RepositoryPolicy policy )
147     {
148         if ( policy == null )
149         {
150             policy = new RepositoryPolicy();
151         }
152 
153         policy.setUpdatePolicy( "never" );
154 
155         return policy;
156     }
157 
158 }