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