/*-
 * 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();
    }
}

@@ -4290,32 +4290,1063 @@
 ePackage%22 ) );%0A%0A
+        artifactManagement.clear();%0A    %7D%0A%0A    /**%0A     * Test if empty pattern matches everything.%0A     * %0A     * @throws Exception%0A     */%0A    public void testEmptyPattern()%0A        throws Exception%0A    %7B%0A        Configuration configuration = lookup( Configurator.class ).getDefaultConfiguration();%0A        List%3CPackagingRule%3E artifactManagement = configuration.getArtifactManagement();%0A        assertTrue( artifactManagement.isEmpty() );%0A%0A        Artifact glob = new Artifact();%0A        glob.setGroupId( %22%22 );%0A        glob.setArtifactId( %22%22 );%0A        glob.setVersion( %22%22 );%0A%0A        PackagingRule rule = new PackagingRule();%0A        rule.setArtifactGlob( glob );%0A        rule.setTargetPackage( %22fooBar%22 );%0A        artifactManagement.add( rule );%0A%0A        PackagingRule effectiveRule = configuration.createEffectivePackagingRule( %22foo%22, %22bar%22, %22baz%22 );%0A        assertNotNull( effectiveRule );%0A        assertNotNull( effectiveRule.getTargetPackage() );%0A        assertTrue( effectiveRule.getTargetPackage().equals( %22fooBar%22 ) );%0A%0A
         artifact

Add unit test for empty pattern matching