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