001package org.eclipse.aether.internal.test.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.junit.Test;
030
031public class NodeDefinitionTest
032{
033
034    private void assertMatch( String text, String regex, String... groups )
035    {
036        Pattern pattern = Pattern.compile( regex );
037        Matcher matcher = pattern.matcher( text );
038        assertTrue( matcher.matches() );
039        assertTrue( groups.length + " vs " + matcher.groupCount(), groups.length <= matcher.groupCount() );
040        for ( int i = 1; i <= groups.length; i++ )
041        {
042            assertEquals( "Mismatch for group " + i, groups[i - 1], matcher.group( i ) );
043        }
044    }
045
046    private void assertNoMatch( String text, String regex )
047    {
048        Pattern pattern = Pattern.compile( regex );
049        Matcher matcher = pattern.matcher( text );
050        assertFalse( matcher.matches() );
051    }
052
053    @Test
054    public void testPatterns()
055    {
056        assertMatch( "(Example-ID_0123456789)", NodeDefinition.ID, "Example-ID_0123456789" );
057        assertMatch( "^Example-ID_0123456789", NodeDefinition.IDREF, "Example-ID_0123456789" );
058
059        assertMatch( "gid:aid:1", NodeDefinition.COORDS, "gid", "aid", null, null, "1" );
060        assertMatch( "gid:aid:jar:1", NodeDefinition.COORDS, "gid", "aid", "jar", null, "1" );
061        assertMatch( "gid:aid:jar:cls:1", NodeDefinition.COORDS, "gid", "aid", "jar", "cls", "1" );
062
063        assertMatch( "[1]", NodeDefinition.RANGE, "[1]" );
064        assertMatch( "[1,)", NodeDefinition.RANGE, "[1,)" );
065        assertMatch( "(1,2)", NodeDefinition.RANGE, "(1,2)" );
066
067        assertMatch( "scope  =  compile", NodeDefinition.SCOPE, "compile", null );
068        assertMatch( "scope=compile<runtime", NodeDefinition.SCOPE, "compile", "runtime" );
069        assertMatch( "compile<runtime", NodeDefinition.SCOPE, "compile", "runtime" );
070        assertNoMatch( "optional", NodeDefinition.SCOPE );
071        assertNoMatch( "!optional", NodeDefinition.SCOPE );
072
073        assertMatch( "optional", NodeDefinition.OPTIONAL, "optional" );
074        assertMatch( "!optional", NodeDefinition.OPTIONAL, "!optional" );
075
076        assertMatch( "relocations  =  g:a:1", NodeDefinition.RELOCATIONS, "g:a:1" );
077        assertMatch( "relocations=g:a:1 , g:a:2", NodeDefinition.RELOCATIONS, "g:a:1 , g:a:2" );
078
079        assertMatch( "props  =  Key:Value", NodeDefinition.PROPS, "Key:Value" );
080        assertMatch( "props=k:1 , k_2:v_2", NodeDefinition.PROPS, "k:1 , k_2:v_2" );
081
082        assertMatch( "gid:aid:1", NodeDefinition.COORDSX, "gid:aid:1", null, null );
083        assertMatch( "gid:aid:1[1,2)", NodeDefinition.COORDSX, "gid:aid:1", "[1,2)", null );
084        assertMatch( "gid:aid:1<2", NodeDefinition.COORDSX, "gid:aid:1", null, "2" );
085        assertMatch( "gid:aid:1(, 2)<[1, 3]", NodeDefinition.COORDSX, "gid:aid:1", "(, 2)", "[1, 3]" );
086
087        assertMatch( "gid:aid:1(, 2)<[1, 3] props=k:v scope=c<r optional relocations=g:a:v (id)", NodeDefinition.NODE,
088                     "gid:aid:1", "(, 2)", "[1, 3]", "k:v", "c", "r", "optional", "g:a:v", "id" );
089
090        assertMatch( "gid:aid:1(, 2)<[1, 3] props=k:v c<r optional relocations=g:a:v (id)", NodeDefinition.LINE, null,
091                     "gid:aid:1", "(, 2)", "[1, 3]", "k:v", "c", "r", "optional", "g:a:v", "id" );
092        assertMatch( "^id", NodeDefinition.LINE, "id", null, null, null );
093    }
094
095    @Test
096    public void testParsing_Reference()
097    {
098        NodeDefinition desc = new NodeDefinition( "^id" );
099        assertEquals( "id", desc.reference );
100    }
101
102    @Test
103    public void testParsing_Node()
104    {
105        NodeDefinition desc = new NodeDefinition( "g:a:1" );
106        assertNull( desc.reference );
107        assertEquals( "g:a:1", desc.coords );
108        assertNull( desc.range );
109        assertNull( desc.premanagedVersion );
110        assertNull( desc.scope );
111        assertNull( desc.premanagedScope );
112        assertEquals( false, desc.optional );
113        assertNull( desc.properties );
114        assertNull( desc.relocations );
115        assertNull( desc.id );
116
117        desc = new NodeDefinition( "gid1:aid1:ext1:ver1 scope1 !optional" );
118        assertNull( desc.reference );
119        assertEquals( "gid1:aid1:ext1:ver1", desc.coords );
120        assertNull( desc.range );
121        assertNull( desc.premanagedVersion );
122        assertEquals( "scope1", desc.scope );
123        assertNull( desc.premanagedScope );
124        assertEquals( false, desc.optional );
125        assertNull( desc.properties );
126        assertNull( desc.relocations );
127        assertNull( desc.id );
128
129        desc = new NodeDefinition( "g:a:1 optional" );
130        assertNull( desc.reference );
131        assertEquals( "g:a:1", desc.coords );
132        assertNull( desc.range );
133        assertNull( desc.premanagedVersion );
134        assertNull( desc.scope );
135        assertNull( desc.premanagedScope );
136        assertEquals( true, desc.optional );
137        assertNull( desc.properties );
138        assertNull( desc.relocations );
139        assertNull( 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        assertNull( 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}