View Javadoc
1   package org.apache.maven.doxia.macro;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import org.apache.maven.doxia.sink.impl.SinkEventElement;
29  import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * Test swf macro.
35   *
36   * @author ltheussl
37   */
38  public class SwfMacroTest
39          extends TestCase
40  {
41  
42      /**
43       * Test of execute method, of class SwfMacro.
44       *
45       * @throws MacroExecutionException if a macro fails during testing.
46       */
47      public void testExecute()
48              throws MacroExecutionException
49      {
50  
51          Map<String, Object> macroParameters = new HashMap<String, Object>();
52          macroParameters.put( "src", "src.swf" );
53          macroParameters.put( "id", "Movie" );
54          macroParameters.put( "width", "50" );
55          macroParameters.put( "height", "60" );
56          macroParameters.put( "quality", "best" );
57          macroParameters.put( "menu", "true" );
58          macroParameters.put( "loop", "3" );
59          macroParameters.put( "play", "false" );
60          macroParameters.put( "version", "6" );
61          macroParameters.put( "allowScript", "always" );
62  
63  
64          SinkEventTestingSink sink = new SinkEventTestingSink();
65          MacroRequest request = new MacroRequest( macroParameters, new File( "." ) );
66          SwfMacro macro = new SwfMacro();
67          macro.required( "src", "value" );
68  
69          try
70          {
71              macro.required( "src", "" );
72              fail();
73          }
74          catch ( IllegalArgumentException e )
75          {
76              assertNotNull( e );
77          }
78  
79          try
80          {
81              macro.required( "src", null );
82              fail();
83          }
84          catch ( IllegalArgumentException e )
85          {
86              assertNotNull( e );
87          }
88  
89          macro.execute( sink, request );
90  
91          Iterator<SinkEventElement> it = sink.getEventList().iterator();
92          SinkEventElement event = it.next();
93  
94          assertEquals( "rawText", event.getName() );
95          assertFalse( it.hasNext() );
96  
97          request = new MacroRequest( new HashMap<String, Object>(), new File( "." ) );
98          sink.reset();
99  
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 }