View Javadoc
1   package org.apache.maven.model.building;
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.api.model.Dependency;
23  import org.apache.maven.api.model.Parent;
24  import org.apache.maven.api.model.Repository;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.resolution.InvalidRepositoryException;
27  import org.apache.maven.model.resolution.ModelResolver;
28  import org.apache.maven.model.resolution.UnresolvableModelException;
29  import org.junit.jupiter.api.Test;
30  import java.io.File;
31  import java.util.concurrent.atomic.AtomicReference;
32  
33  import static org.junit.jupiter.api.Assertions.assertNotNull;
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  
36  /**
37   * @author Guillaume Nodet
38   */
39  public class DefaultModelBuilderTest
40  {
41  
42      private static final String BASE1_ID = "thegroup:base1:pom";
43  
44      private static final String BASE1 = "<project>\n" +
45              "  <modelVersion>4.0.0</modelVersion>\n" +
46              "  <groupId>thegroup</groupId>\n" +
47              "  <artifactId>base1</artifactId>\n" +
48              "  <version>1</version>\n" +
49              "  <packaging>pom</packaging>\n" +
50              "  <dependencyManagement>\n" +
51              "    <dependencies>\n" +
52              "      <dependency>\n" +
53              "        <groupId>thegroup</groupId>\n" +
54              "        <artifactId>base2</artifactId>\n" +
55              "        <version>1</version>\n" +
56              "        <type>pom</type>\n" +
57              "        <scope>import</scope>\n" +
58              "      </dependency>\n" +
59              "    </dependencies>\n" +
60              "  </dependencyManagement>\n" +
61              "</project>\n";
62  
63      private static final String BASE2_ID = "thegroup:base2:pom";
64  
65      private static final String BASE2 = "<project>\n" +
66              "  <modelVersion>4.0.0</modelVersion>\n" +
67              "  <groupId>thegroup</groupId>\n" +
68              "  <artifactId>base2</artifactId>\n" +
69              "  <version>1</version>\n" +
70              "  <packaging>pom</packaging>\n" +
71              "  <dependencyManagement>\n" +
72              "    <dependencies>\n" +
73              "      <dependency>\n" +
74              "        <groupId>thegroup</groupId>\n" +
75              "        <artifactId>base1</artifactId>\n" +
76              "        <version>1</version>\n" +
77              "        <type>pom</type>\n" +
78              "        <scope>import</scope>\n" +
79              "      </dependency>\n" +
80              "    </dependencies>\n" +
81              "  </dependencyManagement>\n" +
82              "</project>\n";
83  
84      @Test
85      public void testCycleInImports()
86              throws Exception
87      {
88          ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
89          assertNotNull( builder );
90  
91          DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
92          request.setModelSource( new StringModelSource( BASE1 ) );
93          request.setModelResolver( new CycleInImportsResolver() );
94  
95          assertThrows( ModelBuildingException.class, () -> builder.build( request ) );
96      }
97  
98      static class CycleInImportsResolver extends BaseModelResolver
99      {
100         @Override
101         public ModelSource resolveModel( org.apache.maven.model.Dependency dependency ) throws UnresolvableModelException
102         {
103             switch ( dependency.getManagementKey() )
104             {
105                 case BASE1_ID: return new StringModelSource( BASE1 );
106                 case BASE2_ID: return new StringModelSource( BASE2 );
107             }
108             return null;
109         }
110     }
111 
112     static class BaseModelResolver implements ModelResolver
113     {
114         @Override
115         public ModelSource resolveModel( String groupId, String artifactId, String version )
116                 throws UnresolvableModelException
117         {
118             return null;
119         }
120 
121         @Override
122         public ModelSource resolveModel( Parent parent, AtomicReference<Parent> modified ) throws UnresolvableModelException
123         {
124             return null;
125         }
126 
127         @Override
128         public ModelSource resolveModel( Dependency dependency, AtomicReference<Dependency> modified ) throws UnresolvableModelException
129         {
130             return null;
131         }
132 
133         @Override
134         public void addRepository( Repository repository ) throws InvalidRepositoryException
135         {
136         }
137 
138         @Override
139         public void addRepository(Repository repository, boolean replace) throws InvalidRepositoryException
140         {
141         }
142 
143         @Override
144         public ModelResolver newCopy()
145         {
146             return this;
147         }
148 
149         @Override
150         public ModelSource resolveModel( org.apache.maven.model.Parent parent ) throws UnresolvableModelException
151         {
152             return null;
153         }
154 
155         @Override
156         public ModelSource resolveModel( org.apache.maven.model.Dependency dependency )
157                 throws UnresolvableModelException
158         {
159             return null;
160         }
161 
162         @Override
163         public void addRepository( org.apache.maven.model.Repository repository ) throws InvalidRepositoryException
164         {
165 
166         }
167 
168         @Override
169         public void addRepository( org.apache.maven.model.Repository repository, boolean replace )
170                 throws InvalidRepositoryException
171         {
172 
173         }
174     }
175 
176     @Test
177     public void testBuildRawModel()
178             throws Exception
179     {
180         ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
181         assertNotNull( builder );
182 
183         Result<? extends Model> res = builder.buildRawModel( new File( getClass().getResource("/poms/factory/simple.xml" ).getFile() ), ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, false);
184         assertNotNull( res.get() );
185     }
186 }