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.eclipse.aether.internal.test.util;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.*;
29  
30  public class NodeDefinitionTest {
31  
32      private void assertMatch(String text, String regex, String... groups) {
33          Pattern pattern = Pattern.compile(regex);
34          Matcher matcher = pattern.matcher(text);
35          assertTrue(matcher.matches());
36          assertTrue(groups.length + " vs " + matcher.groupCount(), groups.length <= matcher.groupCount());
37          for (int i = 1; i <= groups.length; i++) {
38              assertEquals("Mismatch for group " + i, groups[i - 1], matcher.group(i));
39          }
40      }
41  
42      private void assertNoMatch(String text, String regex) {
43          Pattern pattern = Pattern.compile(regex);
44          Matcher matcher = pattern.matcher(text);
45          assertFalse(matcher.matches());
46      }
47  
48      @Test
49      public void testPatterns() {
50          assertMatch("(Example-ID_0123456789)", NodeDefinition.ID, "Example-ID_0123456789");
51          assertMatch("^Example-ID_0123456789", NodeDefinition.IDREF, "Example-ID_0123456789");
52  
53          assertMatch("gid:aid:1", NodeDefinition.COORDS, "gid", "aid", null, null, "1");
54          assertMatch("gid:aid:jar:1", NodeDefinition.COORDS, "gid", "aid", "jar", null, "1");
55          assertMatch("gid:aid:jar:cls:1", NodeDefinition.COORDS, "gid", "aid", "jar", "cls", "1");
56  
57          assertMatch("[1]", NodeDefinition.RANGE, "[1]");
58          assertMatch("[1,)", NodeDefinition.RANGE, "[1,)");
59          assertMatch("(1,2)", NodeDefinition.RANGE, "(1,2)");
60  
61          assertMatch("scope  =  compile", NodeDefinition.SCOPE, "compile", null);
62          assertMatch("scope=compile<runtime", NodeDefinition.SCOPE, "compile", "runtime");
63          assertMatch("compile<runtime", NodeDefinition.SCOPE, "compile", "runtime");
64          assertNoMatch("optional", NodeDefinition.SCOPE);
65          assertNoMatch("!optional", NodeDefinition.SCOPE);
66  
67          assertMatch("optional", NodeDefinition.OPTIONAL, "optional");
68          assertMatch("!optional", NodeDefinition.OPTIONAL, "!optional");
69  
70          assertMatch("relocations  =  g:a:1", NodeDefinition.RELOCATIONS, "g:a:1");
71          assertMatch("relocations=g:a:1 , g:a:2", NodeDefinition.RELOCATIONS, "g:a:1 , g:a:2");
72  
73          assertMatch("props  =  Key:Value", NodeDefinition.PROPS, "Key:Value");
74          assertMatch("props=k:1 , k_2:v_2", NodeDefinition.PROPS, "k:1 , k_2:v_2");
75  
76          assertMatch("gid:aid:1", NodeDefinition.COORDSX, "gid:aid:1", null, null);
77          assertMatch("gid:aid:1[1,2)", NodeDefinition.COORDSX, "gid:aid:1", "[1,2)", null);
78          assertMatch("gid:aid:1<2", NodeDefinition.COORDSX, "gid:aid:1", null, "2");
79          assertMatch("gid:aid:1(, 2)<[1, 3]", NodeDefinition.COORDSX, "gid:aid:1", "(, 2)", "[1, 3]");
80  
81          assertMatch(
82                  "gid:aid:1(, 2)<[1, 3] props=k:v scope=c<r optional relocations=g:a:v (id)",
83                  NodeDefinition.NODE,
84                  "gid:aid:1",
85                  "(, 2)",
86                  "[1, 3]",
87                  "k:v",
88                  "c",
89                  "r",
90                  "optional",
91                  "g:a:v",
92                  "id");
93  
94          assertMatch(
95                  "gid:aid:1(, 2)<[1, 3] props=k:v c<r optional relocations=g:a:v (id)",
96                  NodeDefinition.LINE,
97                  null,
98                  "gid:aid:1",
99                  "(, 2)",
100                 "[1, 3]",
101                 "k:v",
102                 "c",
103                 "r",
104                 "optional",
105                 "g:a:v",
106                 "id");
107         assertMatch("^id", NodeDefinition.LINE, "id", null, null, null);
108     }
109 
110     @Test
111     public void testParsing_Reference() {
112         NodeDefinition desc = new NodeDefinition("^id");
113         assertEquals("id", desc.reference);
114     }
115 
116     @Test
117     public void testParsing_Node() {
118         NodeDefinition desc = new NodeDefinition("g:a:1");
119         assertNull(desc.reference);
120         assertEquals("g:a:1", desc.coords);
121         assertNull(desc.range);
122         assertNull(desc.premanagedVersion);
123         assertNull(desc.scope);
124         assertNull(desc.premanagedScope);
125         assertEquals(false, desc.optional);
126         assertNull(desc.properties);
127         assertNull(desc.relocations);
128         assertNull(desc.id);
129 
130         desc = new NodeDefinition("gid1:aid1:ext1:ver1 scope1 !optional");
131         assertNull(desc.reference);
132         assertEquals("gid1:aid1:ext1:ver1", desc.coords);
133         assertNull(desc.range);
134         assertNull(desc.premanagedVersion);
135         assertEquals("scope1", desc.scope);
136         assertNull(desc.premanagedScope);
137         assertEquals(false, desc.optional);
138         assertNull(desc.properties);
139         assertNull(desc.relocations);
140         assertNull(desc.id);
141 
142         desc = new NodeDefinition("g:a:1 optional");
143         assertNull(desc.reference);
144         assertEquals("g:a:1", desc.coords);
145         assertNull(desc.range);
146         assertNull(desc.premanagedVersion);
147         assertNull(desc.scope);
148         assertNull(desc.premanagedScope);
149         assertEquals(true, desc.optional);
150         assertNull(desc.properties);
151         assertNull(desc.relocations);
152         assertNull(desc.id);
153 
154         desc = new NodeDefinition("gid:aid:1(, 2)<[1, 3]" + " props = k:v" + " scope=c<r" + " optional"
155                 + " relocations = g:a:v , g:a:1" + " (id)");
156         assertNull(desc.reference);
157         assertEquals("gid:aid:1", desc.coords);
158         assertEquals("(, 2)", desc.range);
159         assertEquals("[1, 3]", desc.premanagedVersion);
160         assertEquals("c", desc.scope);
161         assertEquals("r", desc.premanagedScope);
162         assertEquals(true, desc.optional);
163         assertEquals(Collections.singletonMap("k", "v"), desc.properties);
164         assertEquals(Arrays.asList("g:a:v", "g:a:1"), desc.relocations);
165         assertEquals("id", desc.id);
166     }
167 }