View Javadoc
1   package org.eclipse.aether.internal.test.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.junit.Test;
30  
31  public class NodeDefinitionTest
32  {
33  
34      private void assertMatch( String text, String regex, String... groups )
35      {
36          Pattern pattern = Pattern.compile( regex );
37          Matcher matcher = pattern.matcher( text );
38          assertEquals( true, matcher.matches() );
39          assertTrue( groups.length + " vs " + matcher.groupCount(), groups.length <= matcher.groupCount() );
40          for ( int i = 1; i <= groups.length; i++ )
41          {
42              assertEquals( "Mismatch for group " + i, groups[i - 1], matcher.group( i ) );
43          }
44      }
45  
46      private void assertNoMatch( String text, String regex )
47      {
48          Pattern pattern = Pattern.compile( regex );
49          Matcher matcher = pattern.matcher( text );
50          assertEquals( false, matcher.matches() );
51      }
52  
53      @Test
54      public void testPatterns()
55      {
56          assertMatch( "(Example-ID_0123456789)", NodeDefinition.ID, "Example-ID_0123456789" );
57          assertMatch( "^Example-ID_0123456789", NodeDefinition.IDREF, "Example-ID_0123456789" );
58  
59          assertMatch( "gid:aid:1", NodeDefinition.COORDS, "gid", "aid", null, null, "1" );
60          assertMatch( "gid:aid:jar:1", NodeDefinition.COORDS, "gid", "aid", "jar", null, "1" );
61          assertMatch( "gid:aid:jar:cls:1", NodeDefinition.COORDS, "gid", "aid", "jar", "cls", "1" );
62  
63          assertMatch( "[1]", NodeDefinition.RANGE, "[1]" );
64          assertMatch( "[1,)", NodeDefinition.RANGE, "[1,)" );
65          assertMatch( "(1,2)", NodeDefinition.RANGE, "(1,2)" );
66  
67          assertMatch( "scope  =  compile", NodeDefinition.SCOPE, "compile", null );
68          assertMatch( "scope=compile<runtime", NodeDefinition.SCOPE, "compile", "runtime" );
69          assertMatch( "compile<runtime", NodeDefinition.SCOPE, "compile", "runtime" );
70          assertNoMatch( "optional", NodeDefinition.SCOPE );
71          assertNoMatch( "!optional", NodeDefinition.SCOPE );
72  
73          assertMatch( "optional", NodeDefinition.OPTIONAL, "optional" );
74          assertMatch( "!optional", NodeDefinition.OPTIONAL, "!optional" );
75  
76          assertMatch( "relocations  =  g:a:1", NodeDefinition.RELOCATIONS, "g:a:1" );
77          assertMatch( "relocations=g:a:1 , g:a:2", NodeDefinition.RELOCATIONS, "g:a:1 , g:a:2" );
78  
79          assertMatch( "props  =  Key:Value", NodeDefinition.PROPS, "Key:Value" );
80          assertMatch( "props=k:1 , k_2:v_2", NodeDefinition.PROPS, "k:1 , k_2:v_2" );
81  
82          assertMatch( "gid:aid:1", NodeDefinition.COORDSX, "gid:aid:1", null, null );
83          assertMatch( "gid:aid:1[1,2)", NodeDefinition.COORDSX, "gid:aid:1", "[1,2)", null );
84          assertMatch( "gid:aid:1<2", NodeDefinition.COORDSX, "gid:aid:1", null, "2" );
85          assertMatch( "gid:aid:1(, 2)<[1, 3]", NodeDefinition.COORDSX, "gid:aid:1", "(, 2)", "[1, 3]" );
86  
87          assertMatch( "gid:aid:1(, 2)<[1, 3] props=k:v scope=c<r optional relocations=g:a:v (id)", NodeDefinition.NODE,
88                       "gid:aid:1", "(, 2)", "[1, 3]", "k:v", "c", "r", "optional", "g:a:v", "id" );
89  
90          assertMatch( "gid:aid:1(, 2)<[1, 3] props=k:v c<r optional relocations=g:a:v (id)", NodeDefinition.LINE, null,
91                       "gid:aid:1", "(, 2)", "[1, 3]", "k:v", "c", "r", "optional", "g:a:v", "id" );
92          assertMatch( "^id", NodeDefinition.LINE, "id", null, null, null );
93      }
94  
95      @Test
96      public void testParsing_Reference()
97      {
98          NodeDefinition desc = new NodeDefinition( "^id" );
99          assertEquals( "id", desc.reference );
100     }
101 
102     @Test
103     public void testParsing_Node()
104     {
105         NodeDefinition desc = new NodeDefinition( "g:a:1" );
106         assertEquals( null, desc.reference );
107         assertEquals( "g:a:1", desc.coords );
108         assertEquals( null, desc.range );
109         assertEquals( null, desc.premanagedVersion );
110         assertEquals( null, desc.scope );
111         assertEquals( null, desc.premanagedScope );
112         assertEquals( false, desc.optional );
113         assertEquals( null, desc.properties );
114         assertEquals( null, desc.relocations );
115         assertEquals( null, desc.id );
116 
117         desc = new NodeDefinition( "gid1:aid1:ext1:ver1 scope1 !optional" );
118         assertEquals( null, desc.reference );
119         assertEquals( "gid1:aid1:ext1:ver1", desc.coords );
120         assertEquals( null, desc.range );
121         assertEquals( null, desc.premanagedVersion );
122         assertEquals( "scope1", desc.scope );
123         assertEquals( null, desc.premanagedScope );
124         assertEquals( false, desc.optional );
125         assertEquals( null, desc.properties );
126         assertEquals( null, desc.relocations );
127         assertEquals( null, desc.id );
128 
129         desc = new NodeDefinition( "g:a:1 optional" );
130         assertEquals( null, desc.reference );
131         assertEquals( "g:a:1", desc.coords );
132         assertEquals( null, desc.range );
133         assertEquals( null, desc.premanagedVersion );
134         assertEquals( null, desc.scope );
135         assertEquals( null, desc.premanagedScope );
136         assertEquals( true, desc.optional );
137         assertEquals( null, desc.properties );
138         assertEquals( null, desc.relocations );
139         assertEquals( null, desc.id );
140 
141         desc =
142             new NodeDefinition( "gid:aid:1(, 2)<[1, 3]" + " props = k:v" + " scope=c<r" + " optional"
143                 + " relocations = g:a:v , g:a:1" + " (id)" );
144         assertEquals( null, desc.reference );
145         assertEquals( "gid:aid:1", desc.coords );
146         assertEquals( "(, 2)", desc.range );
147         assertEquals( "[1, 3]", desc.premanagedVersion );
148         assertEquals( "c", desc.scope );
149         assertEquals( "r", desc.premanagedScope );
150         assertEquals( true, desc.optional );
151         assertEquals( Collections.singletonMap( "k", "v" ), desc.properties );
152         assertEquals( Arrays.asList( "g:a:v", "g:a:1" ), desc.relocations );
153         assertEquals( "id", desc.id );
154     }
155 
156 }