001package org.apache.maven.doxia.macro;
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.io.File;
023
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.Map;
027
028import org.apache.maven.doxia.sink.impl.SinkEventElement;
029import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
030
031import junit.framework.TestCase;
032
033/**
034 * Test swf macro.
035 *
036 * @author ltheussl
037 */
038public class SwfMacroTest
039        extends TestCase
040{
041
042    /**
043     * Test of execute method, of class SwfMacro.
044     *
045     * @throws MacroExecutionException if a macro fails during testing.
046     */
047    public void testExecute()
048            throws MacroExecutionException
049    {
050
051        Map<String, Object> macroParameters = new HashMap<String, Object>();
052        macroParameters.put( "src", "src.swf" );
053        macroParameters.put( "id", "Movie" );
054        macroParameters.put( "width", "50" );
055        macroParameters.put( "height", "60" );
056        macroParameters.put( "quality", "best" );
057        macroParameters.put( "menu", "true" );
058        macroParameters.put( "loop", "3" );
059        macroParameters.put( "play", "false" );
060        macroParameters.put( "version", "6" );
061        macroParameters.put( "allowScript", "always" );
062
063
064        SinkEventTestingSink sink = new SinkEventTestingSink();
065        MacroRequest request = new MacroRequest( macroParameters, new File( "." ) );
066        SwfMacro macro = new SwfMacro();
067        macro.required( "src", "value" );
068
069        try
070        {
071            macro.required( "src", "" );
072            fail();
073        }
074        catch ( IllegalArgumentException e )
075        {
076            assertNotNull( e );
077        }
078
079        try
080        {
081            macro.required( "src", null );
082            fail();
083        }
084        catch ( IllegalArgumentException e )
085        {
086            assertNotNull( e );
087        }
088
089        macro.execute( sink, request );
090
091        Iterator<SinkEventElement> it = sink.getEventList().iterator();
092        SinkEventElement event = it.next();
093
094        assertEquals( "rawText", event.getName() );
095        assertFalse( it.hasNext() );
096
097        request = new MacroRequest( new HashMap<String, Object>(), new File( "." ) );
098        sink.reset();
099
100        macro.execute( sink, request );
101
102        it = sink.getEventList().iterator();
103        event = it.next();
104
105        assertEquals( "rawText", event.getName() );
106        assertFalse( it.hasNext() );
107    }
108
109    /**
110     * Test that SwfMacro does not crash if other things then Strings are provided.
111     *
112     * @throws MacroExecutionException if a macro fails during testing.
113     */
114    public void testOthersThenStringParameters()
115            throws MacroExecutionException
116    {
117
118        Map<String, Object> macroParameters = new HashMap<String, Object>();
119        macroParameters.put( "src", "src.swf" );
120        macroParameters.put( "id", "Movie" );
121        macroParameters.put( "width", "50" );
122        macroParameters.put( "height", "60" );
123        macroParameters.put( "quality", "best" );
124        macroParameters.put( "menu", "true" );
125        macroParameters.put( "loop", "3" );
126        macroParameters.put( "play", "false" );
127        macroParameters.put( "version", "6" );
128        macroParameters.put( "allowScript", "always" );
129        macroParameters.put( "notAString", new Integer(0) );
130
131
132        SinkEventTestingSink sink = new SinkEventTestingSink();
133        MacroRequest request = new MacroRequest( macroParameters, new File( "." ) );
134        SwfMacro macro = new SwfMacro();
135        macro.required( "src", "value" );
136
137        macro.execute( sink, request );
138
139        Iterator<SinkEventElement> it = sink.getEventList().iterator();
140        SinkEventElement event = it.next();
141
142        assertEquals( "rawText", event.getName() );
143        assertFalse( it.hasNext() );
144
145        request = new MacroRequest( new HashMap<String, Object>(), new File( "." ) );
146        sink.reset();
147
148        macro.execute( sink, request );
149
150        it = sink.getEventList().iterator();
151        event = it.next();
152
153        assertEquals( "rawText", event.getName() );
154        assertFalse( it.hasNext() );
155    }
156
157
158}