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