View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.impl.model.profile;
20  
21  import java.util.List;
22  
23  import org.apache.maven.api.model.InputLocation;
24  import org.apache.maven.api.model.Model;
25  import org.apache.maven.api.services.BuilderProblem;
26  import org.apache.maven.api.services.ModelBuilderException;
27  import org.apache.maven.api.services.ModelProblem;
28  import org.apache.maven.api.services.ModelProblemCollector;
29  import org.apache.maven.api.services.ProblemCollector;
30  import org.apache.maven.impl.model.DefaultModelProblem;
31  
32  /**
33   * A simple model problem collector for testing the model building components.
34   */
35  public class SimpleProblemCollector implements ModelProblemCollector {
36  
37      final ProblemCollector<ModelProblem> problemCollector = ProblemCollector.create(100);
38  
39      @Override
40      public ProblemCollector<ModelProblem> getProblemCollector() {
41          return problemCollector;
42      }
43  
44      @Override
45      public void add(
46              BuilderProblem.Severity severity,
47              ModelProblem.Version version,
48              String message,
49              InputLocation location,
50              Exception exception) {
51          add(new DefaultModelProblem(
52                  message,
53                  severity,
54                  version,
55                  null,
56                  location != null ? location.getLineNumber() : -1,
57                  location != null ? location.getColumnNumber() : -1,
58                  exception));
59      }
60  
61      @Override
62      public ModelBuilderException newModelBuilderException() {
63          throw new UnsupportedOperationException();
64      }
65  
66      @Override
67      public void setSource(String location) {
68          throw new UnsupportedOperationException();
69      }
70  
71      @Override
72      public void setSource(Model model) {
73          throw new UnsupportedOperationException();
74      }
75  
76      @Override
77      public String getSource() {
78          throw new UnsupportedOperationException();
79      }
80  
81      @Override
82      public void setRootModel(Model model) {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      public Model getRootModel() {
88          throw new UnsupportedOperationException();
89      }
90  
91      public List<String> getFatals() {
92          return getForLevel(ModelProblem.Severity.FATAL);
93      }
94  
95      public List<String> getErrors() {
96          return getForLevel(ModelProblem.Severity.ERROR);
97      }
98  
99      public List<String> getWarnings() {
100         return getForLevel(ModelProblem.Severity.WARNING);
101     }
102 
103     private List<String> getForLevel(BuilderProblem.Severity severity) {
104         return problemCollector
105                 .problems(severity)
106                 .map(BuilderProblem::getMessage)
107                 .toList();
108     }
109 }