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.model.building;
20  
21  import java.lang.reflect.Method;
22  import java.lang.reflect.ParameterizedType;
23  import java.lang.reflect.Type;
24  import java.util.List;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  
28  import org.apache.maven.model.v4.MavenMerger;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.hamcrest.MatcherAssert.assertThat;
32  import static org.hamcrest.Matchers.hasItems;
33  
34  public class FileToRawModelMergerTest {
35  
36      /**
37       * Ensures that all list-merge methods are overridden
38       */
39      @Test
40      public void testOverriddenMergeMethods() {
41          List<String> methodNames = Stream.of(MavenMerger.class.getDeclaredMethods())
42                  .filter(m -> m.getName().startsWith("merge"))
43                  .filter(m -> {
44                      String baseName = m.getName().substring(5 /* merge */);
45                      String entity = baseName.substring(baseName.indexOf('_') + 1);
46                      try {
47                          Type returnType = m.getParameterTypes()[0]
48                                  .getMethod("get" + entity)
49                                  .getGenericReturnType();
50                          if (returnType instanceof ParameterizedType) {
51                              return !((ParameterizedType) returnType).getActualTypeArguments()[0].equals(String.class);
52                          } else {
53                              return false;
54                          }
55                      } catch (ReflectiveOperationException | SecurityException e) {
56                          return false;
57                      }
58                  })
59                  .map(Method::getName)
60                  .collect(Collectors.toList());
61  
62          List<String> overriddenMethods = Stream.of(FileToRawModelMerger.class.getDeclaredMethods())
63                  .map(Method::getName)
64                  .filter(m -> m.startsWith("merge"))
65                  .collect(Collectors.toList());
66  
67          assertThat(overriddenMethods, hasItems(methodNames.toArray(new String[0])));
68      }
69  }