package org.apache.maven.reporting;

/*
 * Copyright 2001-2007 The Apache Software Foundation.
 *
 * 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.
 */

import java.util.Iterator;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;

public class AbstractMavenReportRendererTest extends TestCase
{
    private static void checkPattern( String pattern, String[] expectedResult )
    {
        //Map result = AbstractMavenReportRenderer.applyPattern( pattern );
        List result = AbstractMavenReportRenderer.applyPattern( pattern );
        Assert.assertEquals( "result size", expectedResult.length, result.size() );
        int i = 0;
        //for ( Iterator it = result.entrySet().iterator(); it.hasNext(); )
        for ( Iterator it = result.iterator(); it.hasNext(); )
        {
            //Map.Entry segment = (Map.Entry) it.next();
            //String name = (String) segment.getKey();
            //String href = (String) segment.getValue();
            String name = (String) it.next();
            String href = (String) it.next();
            Assert.assertEquals( expectedResult[i], name );
            Assert.assertEquals( expectedResult[i + 1], href );
            i += 2;
        }
    }

    private static void checkPatternIllegalArgument( String cause, String pattern )
    {
        try
        {
            AbstractMavenReportRenderer.applyPattern( pattern );
            Assert.fail( cause + " should throw an IllegalArgumentException" );
        }
        catch ( IllegalArgumentException iae )
        {
            // ok
        }
    }

    public void testApplyPattern()
    {
        // the most simple test
        checkPattern( "test {text,url}", new String[] { "test ", null, "text", "url" } );

        // check that link content is trimmed, and no problem if 2 text values are the same
        checkPattern( "test{ text , url }test", new String[] { "test", null, "text", "url", "test", null } );

        // check brace stacking
        checkPattern( "test{ {text} , url }test", new String[] { "test", null, "{text}", "url", "test", null } );

        // check quoting
        checkPatternIllegalArgument( "unmatched brace", "{" );
        checkPattern( "'{'", new String[] { "{", null } );
        checkPattern( " ' { '.", new String[] { "  { .", null } );
        checkPatternIllegalArgument( "unmatched quote", " ' " );
        checkPatternIllegalArgument( "unmatched quote", " '" );

        // check double quoting
        checkPattern( " '' ", new String[] { " ' ", null } );

        // real world cases with quote
        checkPattern( "project''s info", new String[] { "project's info", null } );
        checkPattern( "it''s a question of {chance, http://en.wikipedia.org/wiki/Chance}",
                      new String[] { "it's a question of ", null, "chance", "http://en.wikipedia.org/wiki/Chance" } );
    }

}

