<commit_before>/*-
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.maven.config;

import java.util.List;

import org.codehaus.plexus.PlexusTestCase;

public class EffectivePackagingTest
    extends PlexusTestCase
{
    /**
     * Test if multiple rules are correctly aggregated into single effective rule.
     * 
     * @throws Exception
     */
    public void testRuleAggregation()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "foo" );
        glob.setArtifactId( "bar" );
        glob.setVersion( "baz" );

        PackagingRule rule1 = new PackagingRule();
        rule1.setArtifactGlob( glob );
        rule1.addFile( "file1" );
        artifactManagement.add( rule1 );

        PackagingRule rule2 = new PackagingRule();
        rule2.setArtifactGlob( glob );
        rule2.addFile( "file2" );
        artifactManagement.add( rule2 );

        PackagingRule effectiveRule = configuration.createEffectivePackagingRule( "foo", "bar", "baz" );
        assertTrue( effectiveRule.getFiles().get( 0 ).equals( "file1" ) );
        assertTrue( effectiveRule.getFiles().get( 1 ).equals( "file2" ) );
        assertEquals( effectiveRule.getFiles().size(), 2 );

        artifactManagement.clear();
    }

    /**
     * Test if wildcard matching works as expected.
     * 
     * @throws Exception
     */
    public void testWildcards()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "foo*bar" );
        glob.setArtifactId( "{lorem,ipsum}-dolor" );
        glob.setVersion( "1.2.3" );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( glob );
        rule.setTargetPackage( "pkgX" );
        artifactManagement.add( rule );

        PackagingRule effRule1 = configuration.createEffectivePackagingRule( "foo-test-bar", "ipsum-dolor", "1.2.3" );
        assertNotNull( effRule1.getTargetPackage() );
        assertTrue( effRule1.getTargetPackage().equals( "pkgX" ) );

        PackagingRule effRule2 = configuration.createEffectivePackagingRule( "foobar", "lorem-dolor", "1.2.3" );
        assertNotNull( effRule2.getTargetPackage() );
        assertTrue( effRule2.getTargetPackage().equals( "pkgX" ) );

        PackagingRule effRule3 = configuration.createEffectivePackagingRule( "foobar", "lorem-dolor", "1.253" );
        assertNull( effRule3.getTargetPackage() );

        artifactManagement.clear();
    }

    /**
     * Test if empty glob *:*:* matches any artifact.
     * 
     * @throws Exception
     */
    public void testEmptyGlob()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( new Artifact() );
        rule.setTargetPackage( "somePackage" );
        artifactManagement.add( rule );

        PackagingRule effRule1 = configuration.createEffectivePackagingRule( "com.example", "some-test", "0.42" );
        assertNotNull( effRule1.getTargetPackage() );
        assertTrue( effRule1.getTargetPackage().equals( "somePackage" ) );

        artifactManagement.clear();
    }

    /**
     * Test if empty pattern matches everything.
     * 
     * @throws Exception
     */
    public void testEmptyPattern()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "" );
        glob.setArtifactId( "" );
        glob.setVersion( "" );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( glob );
        rule.setTargetPackage( "fooBar" );
        artifactManagement.add( rule );

        PackagingRule effectiveRule = configuration.createEffectivePackagingRule( "foo", "bar", "baz" );
        assertNotNull( effectiveRule );
        assertNotNull( effectiveRule.getTargetPackage() );
        assertTrue( effectiveRule.getTargetPackage().equals( "fooBar" ) );

        artifactManagement.clear();
    }

    /**
     * Test if explicit rules correctly override default singleton packaging rule.
     * 
     * @throws Exception
     */
    public void testSingletonAndSpecificRule()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob1 = new Artifact();
        glob1.setGroupId( "" );
        glob1.setArtifactId( "{sisu,guice}-{*}" );
        glob1.setVersion( "" );

        PackagingRule rule1 = new PackagingRule();
        rule1.setArtifactGlob( glob1 );
        rule1.setTargetPackage( "@2" );
        artifactManagement.add( rule1 );

        Artifact glob2 = new Artifact();
        glob2.setArtifactId( "{*}" );

        PackagingRule rule2 = new PackagingRule();
        rule2.setArtifactGlob( glob2 );
        rule2.setTargetPackage( "@1" );
        artifactManagement.add( rule2 );

        PackagingRule effRule =
            configuration.createEffectivePackagingRule( "org.sonatype.sisu", "sisu-parent", "2.3.0" );
        assertNotNull( effRule.getTargetPackage() );
        assertTrue( effRule.getTargetPackage().equals( "parent" ) );

        artifactManagement.clear();
    }<commit_msg>Add test for artifact alias rules<commit_after>/*-
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.maven.config;

import java.util.List;

import org.codehaus.plexus.PlexusTestCase;

public class EffectivePackagingTest
    extends PlexusTestCase
{
    /**
     * Test if multiple rules are correctly aggregated into single effective rule.
     * 
     * @throws Exception
     */
    public void testRuleAggregation()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "foo" );
        glob.setArtifactId( "bar" );
        glob.setVersion( "baz" );

        PackagingRule rule1 = new PackagingRule();
        rule1.setArtifactGlob( glob );
        rule1.addFile( "file1" );
        artifactManagement.add( rule1 );

        PackagingRule rule2 = new PackagingRule();
        rule2.setArtifactGlob( glob );
        rule2.addFile( "file2" );
        artifactManagement.add( rule2 );

        PackagingRule effectiveRule = configuration.createEffectivePackagingRule( "foo", "bar", "baz" );
        assertTrue( effectiveRule.getFiles().get( 0 ).equals( "file1" ) );
        assertTrue( effectiveRule.getFiles().get( 1 ).equals( "file2" ) );
        assertEquals( effectiveRule.getFiles().size(), 2 );

        artifactManagement.clear();
    }

    /**
     * Test if wildcard matching works as expected.
     * 
     * @throws Exception
     */
    public void testWildcards()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "foo*bar" );
        glob.setArtifactId( "{lorem,ipsum}-dolor" );
        glob.setVersion( "1.2.3" );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( glob );
        rule.setTargetPackage( "pkgX" );
        artifactManagement.add( rule );

        PackagingRule effRule1 = configuration.createEffectivePackagingRule( "foo-test-bar", "ipsum-dolor", "1.2.3" );
        assertNotNull( effRule1.getTargetPackage() );
        assertTrue( effRule1.getTargetPackage().equals( "pkgX" ) );

        PackagingRule effRule2 = configuration.createEffectivePackagingRule( "foobar", "lorem-dolor", "1.2.3" );
        assertNotNull( effRule2.getTargetPackage() );
        assertTrue( effRule2.getTargetPackage().equals( "pkgX" ) );

        PackagingRule effRule3 = configuration.createEffectivePackagingRule( "foobar", "lorem-dolor", "1.253" );
        assertNull( effRule3.getTargetPackage() );

        artifactManagement.clear();
    }

    /**
     * Test if empty glob *:*:* matches any artifact.
     * 
     * @throws Exception
     */
    public void testEmptyGlob()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( new Artifact() );
        rule.setTargetPackage( "somePackage" );
        artifactManagement.add( rule );

        PackagingRule effRule1 = configuration.createEffectivePackagingRule( "com.example", "some-test", "0.42" );
        assertNotNull( effRule1.getTargetPackage() );
        assertTrue( effRule1.getTargetPackage().equals( "somePackage" ) );

        artifactManagement.clear();
    }

    /**
     * Test if empty pattern matches everything.
     * 
     * @throws Exception
     */
    public void testEmptyPattern()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "" );
        glob.setArtifactId( "" );
        glob.setVersion( "" );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( glob );
        rule.setTargetPackage( "fooBar" );
        artifactManagement.add( rule );

        PackagingRule effectiveRule = configuration.createEffectivePackagingRule( "foo", "bar", "baz" );
        assertNotNull( effectiveRule );
        assertNotNull( effectiveRule.getTargetPackage() );
        assertTrue( effectiveRule.getTargetPackage().equals( "fooBar" ) );

        artifactManagement.clear();
    }

    /**
     * Test if explicit rules correctly override default singleton packaging rule.
     * 
     * @throws Exception
     */
    public void testSingletonAndSpecificRule()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob1 = new Artifact();
        glob1.setGroupId( "" );
        glob1.setArtifactId( "{sisu,guice}-{*}" );
        glob1.setVersion( "" );

        PackagingRule rule1 = new PackagingRule();
        rule1.setArtifactGlob( glob1 );
        rule1.setTargetPackage( "@2" );
        artifactManagement.add( rule1 );

        Artifact glob2 = new Artifact();
        glob2.setArtifactId( "{*}" );

        PackagingRule rule2 = new PackagingRule();
        rule2.setArtifactGlob( glob2 );
        rule2.setTargetPackage( "@1" );
        artifactManagement.add( rule2 );

        PackagingRule effRule =
            configuration.createEffectivePackagingRule( "org.sonatype.sisu", "sisu-parent", "2.3.0" );
        assertNotNull( effRule.getTargetPackage() );
        assertTrue( effRule.getTargetPackage().equals( "parent" ) );

        artifactManagement.clear();
    }

    /**
     * Test if artifact aliases work as expected.
     * 
     * @throws Exception
     */
    public void testAliases()
        throws Exception
    {
        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();
        List<PackagingRule> artifactManagement = configuration.getArtifactManagement();
        assertTrue( artifactManagement.isEmpty() );

        Artifact glob = new Artifact();
        glob.setGroupId( "" );
        glob.setArtifactId( "{*}" );
        glob.setVersion( "" );

        Artifact alias = new Artifact();
        alias.setGroupId( "" );
        alias.setArtifactId( "@1-test" );
        alias.setVersion( "" );

        PackagingRule rule = new PackagingRule();
        rule.setArtifactGlob( glob );
        rule.addAlias( alias );
        artifactManagement.add( rule );

        PackagingRule effRule = configuration.createEffectivePackagingRule( "foo", "bar", "1.2.3" );

        assertEquals( effRule.getAliases().size(), 1 );
        Artifact effAlias = effRule.getAliases().iterator().next();

        assertEquals( effAlias.getGroupId(), "foo" );
        assertEquals( effAlias.getArtifactId(), "bar-test" );
        assertEquals( effAlias.getVersion(), "1.2.3" );

        artifactManagement.clear();
    }
{'fraction_non_alphanumeric': 0.11263792053069789, 'fraction_numerical': 0.010153658701685508, 'mean_word_length': 2.8756558237145855, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5455361', 'n_tokens_mistral': 4025, 'n_tokens_neox': 3883, 'n_words': 920}