View Javadoc
1   package org.apache.maven.settings;
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.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.util.Random;
29  
30  import org.apache.maven.api.settings.Activation;
31  import org.apache.maven.api.settings.ActivationFile;
32  import org.apache.maven.api.settings.ActivationOS;
33  import org.apache.maven.api.settings.ActivationProperty;
34  import org.apache.maven.api.settings.Profile;
35  import org.apache.maven.api.settings.Repository;
36  import org.apache.maven.api.settings.Settings;
37  import org.junit.jupiter.api.Test;
38  
39  import static org.junit.jupiter.api.Assertions.assertEquals;
40  import static org.junit.jupiter.api.Assertions.assertNotNull;
41  
42  public class SettingsUtilsTest
43  {
44  
45      @Test
46      public void testShouldAppendRecessivePluginGroupIds()
47      {
48          Settings dominant = Settings.newBuilder()
49                  .pluginGroups( Arrays.asList( "org.apache.maven.plugins", "org.codehaus.modello" ) )
50                  .build();
51  
52          Settings recessive = Settings.newBuilder()
53                  .pluginGroups( Arrays.asList( "org.codehaus.plexus" ) )
54                  .build();
55  
56          Settings merged = SettingsUtils.merge( dominant, recessive, Settings.GLOBAL_LEVEL );
57  
58          List<String> pluginGroups = merged.getPluginGroups();
59  
60          assertNotNull( pluginGroups );
61          assertEquals( 3, pluginGroups.size() );
62          assertEquals( "org.apache.maven.plugins", pluginGroups.get( 0 ) );
63          assertEquals( "org.codehaus.modello", pluginGroups.get( 1 ) );
64          assertEquals( "org.codehaus.plexus", pluginGroups.get( 2 ) );
65      }
66  
67      @Test
68      public void testRoundTripProfiles()
69      {
70          Random entropy = new Random();
71          ActivationFile af = ActivationFile.newBuilder()
72                  .exists( "exists" + Long.toHexString( entropy.nextLong() ) )
73                  .missing( "missing" + Long.toHexString( entropy.nextLong() ) )
74                  .build();
75          ActivationProperty ap = ActivationProperty.newBuilder()
76                  .name( "name" + Long.toHexString( entropy.nextLong() ) )
77                  .value( "value" + Long.toHexString( entropy.nextLong() ) )
78                  .build();
79          ActivationOS ao = ActivationOS.newBuilder()
80                  .arch( "arch" + Long.toHexString( entropy.nextLong() ) )
81                  .family( "family" + Long.toHexString( entropy.nextLong() ) )
82                  .name( "name" + Long.toHexString( entropy.nextLong() ) )
83                  .version( "version" + Long.toHexString( entropy.nextLong() ) )
84                  .build();
85          Activation a = Activation.newBuilder()
86                  .activeByDefault( entropy.nextBoolean() )
87                  .jdk( "jdk" + Long.toHexString( entropy.nextLong() ) )
88                  .file( af )
89                  .property( ap )
90                  .os( ao )
91                  .build();
92          Map<String, String> props = new HashMap<>();
93          int count = entropy.nextInt( 10 );
94          for ( int i = 0; i < count; i++ )
95          {
96              props.put( "name" + Long.toHexString( entropy.nextLong() ),
97                      "value" + Long.toHexString( entropy.nextLong() ) );
98          }
99          count = entropy.nextInt( 3 );
100         List<Repository> repos = new ArrayList<>();
101         for ( int i = 0; i < count; i++ )
102         {
103             Repository r = Repository.newBuilder()
104                     .id( "id" + Long.toHexString( entropy.nextLong() ) )
105                     .name( "name" + Long.toHexString( entropy.nextLong() ) )
106                     .url(  "url" + Long.toHexString( entropy.nextLong() ) )
107                     .build();
108             repos.add( r );
109         }
110         count = entropy.nextInt( 3 );
111         List<Repository> pluginRepos = new ArrayList<>();
112         for ( int i = 0; i < count; i++ )
113         {
114             Repository r = Repository.newBuilder()
115                     .id( "id" + Long.toHexString( entropy.nextLong() ) )
116                     .name( "name" + Long.toHexString( entropy.nextLong() ) )
117                     .url(  "url" + Long.toHexString( entropy.nextLong() ) )
118                     .build();
119             pluginRepos.add( r );
120         }
121         Profile p = Profile.newBuilder()
122                 .id( "id" + Long.toHexString( entropy.nextLong() ) )
123                 .activation( a )
124                 .properties( props )
125                 .repositories( repos )
126                 .pluginRepositories( pluginRepos )
127                 .build();
128 
129         Profile clone = SettingsUtils.convertToSettingsProfile( SettingsUtils.convertFromSettingsProfile( p ) );
130 
131         assertEquals( p.getId(), clone.getId() );
132         assertEquals( p.getActivation().getJdk(), clone.getActivation().getJdk() );
133         assertEquals( p.getActivation().getFile().getExists(), clone.getActivation().getFile().getExists() );
134         assertEquals( p.getActivation().getFile().getMissing(), clone.getActivation().getFile().getMissing() );
135         assertEquals( p.getActivation().getProperty().getName(), clone.getActivation().getProperty().getName() );
136         assertEquals( p.getActivation().getProperty().getValue(), clone.getActivation().getProperty().getValue() );
137         assertEquals( p.getActivation().getOs().getArch(), clone.getActivation().getOs().getArch() );
138         assertEquals( p.getActivation().getOs().getFamily(), clone.getActivation().getOs().getFamily() );
139         assertEquals( p.getActivation().getOs().getName(), clone.getActivation().getOs().getName() );
140         assertEquals( p.getActivation().getOs().getVersion(), clone.getActivation().getOs().getVersion() );
141         assertEquals( p.getProperties(), clone.getProperties() );
142         assertEquals( p.getRepositories().size(), clone.getRepositories().size() );
143         // TODO deep compare the lists
144         assertEquals( p.getPluginRepositories().size(), clone.getPluginRepositories().size() );
145         // TODO deep compare the lists
146     }
147 
148 }