001package org.apache.maven.tools.plugin.extractor;
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 java.util.TreeSet;
023
024import org.junit.jupiter.api.Test;
025
026import static org.junit.jupiter.api.Assertions.assertEquals;
027import static org.junit.jupiter.api.Assertions.assertFalse;
028
029/**
030 * UT for {@link GroupKey}.
031 */
032public class GroupKeyTest
033{
034    @Test
035    public void sortOrder()
036    {
037        TreeSet<GroupKey> groupKeys = new TreeSet<>();
038        groupKeys.add( new GroupKey( "ant", 1 ) );
039        groupKeys.add( new GroupKey( "bsh", 1 ) );
040        groupKeys.add( new GroupKey( "foo", 1 ) );
041        groupKeys.add( new GroupKey( "zzz", 1 ) );
042        groupKeys.add( new GroupKey( GroupKey.JAVA_GROUP, 1 ) );
043        groupKeys.add( new GroupKey( "aaa", 2 ) );
044        groupKeys.add( new GroupKey( "bbb", 3 ) );
045        groupKeys.add( new GroupKey( "bsh", 100 ) );
046        groupKeys.add( new GroupKey( "ant", 5 ) );
047        groupKeys.add( new GroupKey( GroupKey.JAVA_GROUP, 2 ) );
048        assertFalse( groupKeys.add( new GroupKey( GroupKey.JAVA_GROUP, 1 ) ) ); // already present
049
050        StringBuilder stringBuilder = new StringBuilder();
051        for ( GroupKey groupKey : groupKeys )
052        {
053            stringBuilder.append( groupKey.getGroup() ).append( ":" ).append( groupKey.getOrder() ).append( " " );
054        }
055        // Sort order:
056        // 'java' group is always first
057        // non-java groups are sorted lexicographically after java
058        // within same named groups, int order defines ordering
059        assertEquals(
060                "java:1 java:2 aaa:2 ant:1 ant:5 bbb:3 bsh:1 bsh:100 foo:1 zzz:1 ",
061                stringBuilder.toString()
062        );
063    }
064}