Coverage Report - org.apache.maven.shared.runtime.PropertiesMavenRuntimeVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesMavenRuntimeVisitor
90%
20/22
N/A
1.4
 
 1  
 package org.apache.maven.shared.runtime;
 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.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.net.URL;
 25  
 import java.net.URLConnection;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collections;
 28  
 import java.util.List;
 29  
 import java.util.Properties;
 30  
 
 31  
 import org.codehaus.plexus.util.IOUtil;
 32  
 
 33  
 /**
 34  
  * A visitor that parses and collects Maven project property files.
 35  
  * 
 36  
  * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
 37  
  * @version $Id: PropertiesMavenRuntimeVisitor.java 831910 2009-11-02 15:05:33Z markh $
 38  
  */
 39  
 class PropertiesMavenRuntimeVisitor implements MavenRuntimeVisitor
 40  
 {
 41  
     // constants --------------------------------------------------------------
 42  
 
 43  
     /**
 44  
      * The group id property name in a Maven project property file.
 45  
      */
 46  
     private static final String GROUP_ID_PROPERTY = "groupId";
 47  
 
 48  
     /**
 49  
      * The artifact id property name in a Maven project property file.
 50  
      */
 51  
     private static final String ARTIFACT_ID_PROPERTY = "artifactId";
 52  
 
 53  
     /**
 54  
      * The version property name in a Maven project property file.
 55  
      */
 56  
     private static final String VERSION_PROPERTY = "version";
 57  
 
 58  
     // fields -----------------------------------------------------------------
 59  
 
 60  
     /**
 61  
      * A list of the collected project properties.
 62  
      */
 63  
     private final List<MavenProjectProperties> projects;
 64  
 
 65  
     // constructors -----------------------------------------------------------
 66  
 
 67  
     /**
 68  
      * Creates a new {@code PropertiesMavenRuntimeVisitor}.
 69  
      */
 70  
     public PropertiesMavenRuntimeVisitor()
 71  48
     {
 72  48
         projects = new ArrayList<MavenProjectProperties>();
 73  48
     }
 74  
 
 75  
     // MavenRuntimeVisitor methods --------------------------------------------
 76  
 
 77  
     /**
 78  
      * {@inheritDoc}
 79  
      */
 80  
     public void visitProjectProperties( URL url ) throws MavenRuntimeException
 81  
     {
 82  54
         MavenProjectProperties project = parseProjectProperties( url );
 83  
 
 84  54
         projects.add( project );
 85  54
     }
 86  
 
 87  
     /**
 88  
      * {@inheritDoc}
 89  
      */
 90  
     public void visitProjectXML( URL url ) throws MavenRuntimeException
 91  
     {
 92  
         // no-op
 93  54
     }
 94  
 
 95  
     // public methods ---------------------------------------------------------
 96  
 
 97  
     /**
 98  
      * Gets the collected Maven project properties.
 99  
      * 
 100  
      * @return a list of the collected project properties
 101  
      */
 102  
     public List<MavenProjectProperties> getProjects()
 103  
     {
 104  48
         return Collections.unmodifiableList( projects );
 105  
     }
 106  
 
 107  
     // private methods --------------------------------------------------------
 108  
 
 109  
     /**
 110  
      * Parses the specified Maven project properties into a {@code MavenProjectProperties} object.
 111  
      * 
 112  
      * @param url
 113  
      *            a URL to the Maven project properties
 114  
      * @return a {@code MavenProjectProperties} object that represents the properties
 115  
      * @throws MavenRuntimeException
 116  
      *             if an error occurs parsing the properties
 117  
      */
 118  
     private MavenProjectProperties parseProjectProperties( URL url ) throws MavenRuntimeException
 119  
     {
 120  54
         Properties properties = new Properties();
 121  
 
 122  54
         InputStream in = null;
 123  
 
 124  
         try
 125  
         {
 126  54
             URLConnection connection = url.openConnection();
 127  54
             connection.setUseCaches( false );
 128  
 
 129  54
             in = connection.getInputStream();
 130  
 
 131  54
             properties.load( in );
 132  
         }
 133  0
         catch ( IOException exception )
 134  
         {
 135  0
             throw new MavenRuntimeException( "Cannot read project properties: " + url, exception );
 136  
         }
 137  
         finally
 138  
         {
 139  54
             IOUtil.close( in );
 140  54
         }
 141  
 
 142  54
         String groupId = properties.getProperty( GROUP_ID_PROPERTY );
 143  54
         String artifactId = properties.getProperty( ARTIFACT_ID_PROPERTY );
 144  54
         String version = properties.getProperty( VERSION_PROPERTY );
 145  
 
 146  54
         return new MavenProjectProperties( groupId, artifactId, version );
 147  
     }
 148  
 }