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