View Javadoc
1   package org.apache.maven.doxia.macro.snippet;
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  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.maven.doxia.macro.MacroExecutionException;
28  import org.apache.maven.doxia.macro.MacroRequest;
29  import org.apache.maven.doxia.sink.impl.SinkEventElement;
30  import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
31  import org.codehaus.plexus.PlexusTestCase;
32  import org.hamcrest.CoreMatchers;
33  import org.junit.Assert;
34  
35  /**
36   * Test snippet macro.
37   *
38   * @author ltheussl
39   */
40  public class SnippetMacroTest
41      extends PlexusTestCase
42  {
43      /**
44       * Test of execute method, of class SnippetMacro.
45       *
46       * @throws MacroExecutionException if a macro fails during testing.
47       */
48      public void testExecute()
49          throws MacroExecutionException
50      {
51          Map<String, Object> macroParameters = new HashMap<String, Object>();
52          macroParameters.put( "file", "src/test/resources/macro/snippet/testSnippet.txt" );
53          macroParameters.put( "encoding", "UTF-8" );
54  
55          SinkEventTestingSink sink = executeSnippetMacro( macroParameters );
56  
57          Iterator<SinkEventElement> it = sink.getEventList().iterator();
58          assertEquals( "verbatim", it.next().getName() );
59          SinkEventElement event = it.next();
60          assertEquals( "text", event.getName() );
61          String snippet = (String) event.getArgs()[0];
62          assertEquals( "verbatim_", it.next().getName() );
63          assertFalse( it.hasNext() );
64  
65          assertTrue( snippet.contains( "preamble" ) );
66          assertTrue( snippet.contains( "first snippet" ) );
67          assertTrue( snippet.contains( "interlude" ) );
68          assertTrue( snippet.contains( "second snippet" ) );
69          assertTrue( snippet.contains( "conclusion" ) );
70  
71          // again
72  
73          macroParameters.put( "id", "firstId" );
74          macroParameters.put( "verbatim", "" );
75          sink = executeSnippetMacro( macroParameters );
76  
77          it = sink.getEventList().iterator();
78          assertEquals( "verbatim", it.next().getName() );
79          event = it.next();
80          assertEquals( "text", event.getName() );
81          snippet = (String) event.getArgs()[0];
82          assertEquals( "verbatim_", it.next().getName() );
83          assertFalse( it.hasNext() );
84  
85          assertFalse( snippet.contains( "preamble" ) );
86          assertTrue( snippet.contains( "first snippet" ) );
87          assertFalse( snippet.contains( "interlude" ) );
88          assertFalse( snippet.contains( "second snippet" ) );
89          assertFalse( snippet.contains( "conclusion" ) );
90  
91          // again
92  
93          macroParameters.put( "id", "secondId" );
94          macroParameters.put( "verbatim", "false" );
95          sink = executeSnippetMacro( macroParameters );
96  
97          it = sink.getEventList().iterator();
98          event = it.next();
99          assertEquals( "rawText", event.getName() );
100         snippet = (String) event.getArgs()[0];
101         assertFalse( it.hasNext() );
102 
103         assertFalse( snippet.contains( "preamble" ) );
104         assertFalse( snippet.contains( "first snippet" ) );
105         assertFalse( snippet.contains( "interlude" ) );
106         assertTrue( snippet.contains( "second snippet" ) );
107         assertFalse( snippet.contains( "conclusion" ) );
108 
109         // again
110 
111         macroParameters.put( "id", "thirdId" );
112         macroParameters.put( "verbatim", "false" );
113         sink = executeSnippetMacro( macroParameters );
114 
115         it = sink.getEventList().iterator();
116         event = it.next();
117         assertEquals( "rawText", event.getName() );
118         snippet = (String) event.getArgs()[0];
119         assertFalse( it.hasNext() );
120 
121         // no need to verify the absence of the first and second snippets if tests above were successful
122         Assert.assertThat( snippet, CoreMatchers.containsString( "Этот сниппет в формате Unicode (UTF-8)" ) );
123         
124         // again
125         // Shouldn't work because no snippet called "first" exists, only "firstId"
126         macroParameters.put( "id", "first" );
127         macroParameters.put( "verbatim", "" );
128         macroParameters.put( "ignoreDownloadError", "false" );
129         try
130         {
131             executeSnippetMacro( macroParameters );
132             fail();
133         }
134         catch ( Exception e )
135         {
136             // good
137         }
138     }
139 
140     public void testIgnoreDownloadError()
141         throws Exception
142     {
143         Map<String, Object> macroParameters = new HashMap<String, Object>();
144         macroParameters.put( "debug", "true" );
145         macroParameters.put( "ignoreDownloadError", "true" );
146         macroParameters.put( "url", "http://foo.bar.com/wine.txt" );
147 
148         SinkEventTestingSink sink = executeSnippetMacro( macroParameters );
149 
150         Iterator<SinkEventElement> it = sink.getEventList().iterator();
151         assertEquals( "verbatim", it.next().getName() );
152         SinkEventElement event = it.next();
153         assertEquals( "text", event.getName() );
154         String snippet = (String) event.getArgs()[0];
155         Assert.assertThat( snippet, CoreMatchers.containsString( "Error during retrieving content" ) );
156     }
157 
158     private SinkEventTestingSink executeSnippetMacro( Map<String, Object> macroParameters )
159         throws MacroExecutionException
160     {
161         File basedir = new File( getBasedir() );
162 
163         SinkEventTestingSink sink = new SinkEventTestingSink();
164 
165         MacroRequest request = new MacroRequest( macroParameters, basedir );
166         SnippetMacro macro = new SnippetMacro();
167         macro.execute( sink, request );
168 
169         return sink;
170     }
171 }