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  public class SettingsUtilsTest {
41  
42      @Test
43      public 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      public 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                  .build();
87          Map<String, String> props = new HashMap<>();
88          int count = entropy.nextInt(10);
89          for (int i = 0; i < count; i++) {
90              props.put("name" + Long.toHexString(entropy.nextLong()), "value" + Long.toHexString(entropy.nextLong()));
91          }
92          count = entropy.nextInt(3);
93          List<Repository> repos = new ArrayList<>();
94          for (int i = 0; i < count; i++) {
95              Repository r = Repository.newBuilder()
96                      .id("id" + Long.toHexString(entropy.nextLong()))
97                      .name("name" + Long.toHexString(entropy.nextLong()))
98                      .url("url" + Long.toHexString(entropy.nextLong()))
99                      .build();
100             repos.add(r);
101         }
102         count = entropy.nextInt(3);
103         List<Repository> pluginRepos = new ArrayList<>();
104         for (int i = 0; i < count; i++) {
105             Repository r = Repository.newBuilder()
106                     .id("id" + Long.toHexString(entropy.nextLong()))
107                     .name("name" + Long.toHexString(entropy.nextLong()))
108                     .url("url" + Long.toHexString(entropy.nextLong()))
109                     .build();
110             pluginRepos.add(r);
111         }
112         Profile p = Profile.newBuilder()
113                 .id("id" + Long.toHexString(entropy.nextLong()))
114                 .activation(a)
115                 .properties(props)
116                 .repositories(repos)
117                 .pluginRepositories(pluginRepos)
118                 .build();
119 
120         Profile clone = SettingsUtilsV4.convertToSettingsProfile(SettingsUtilsV4.convertFromSettingsProfile(p));
121 
122         assertEquals(p.getId(), clone.getId());
123         assertEquals(p.getActivation().getJdk(), clone.getActivation().getJdk());
124         assertEquals(
125                 p.getActivation().getFile().getExists(),
126                 clone.getActivation().getFile().getExists());
127         assertEquals(
128                 p.getActivation().getFile().getMissing(),
129                 clone.getActivation().getFile().getMissing());
130         assertEquals(
131                 p.getActivation().getProperty().getName(),
132                 clone.getActivation().getProperty().getName());
133         assertEquals(
134                 p.getActivation().getProperty().getValue(),
135                 clone.getActivation().getProperty().getValue());
136         assertEquals(
137                 p.getActivation().getOs().getArch(),
138                 clone.getActivation().getOs().getArch());
139         assertEquals(
140                 p.getActivation().getOs().getFamily(),
141                 clone.getActivation().getOs().getFamily());
142         assertEquals(
143                 p.getActivation().getOs().getName(),
144                 clone.getActivation().getOs().getName());
145         assertEquals(
146                 p.getActivation().getOs().getVersion(),
147                 clone.getActivation().getOs().getVersion());
148         assertEquals(p.getProperties(), clone.getProperties());
149         assertEquals(p.getRepositories().size(), clone.getRepositories().size());
150         // TODO deep compare the lists
151         assertEquals(
152                 p.getPluginRepositories().size(), clone.getPluginRepositories().size());
153         // TODO deep compare the lists
154     }
155 }