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