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  
53          SinkEventTestingSink sink = new SinkEventTestingSink();
54  
55          MacroRequest request = new MacroRequest( macroParameters, basedir );
56          SnippetMacro macro = new SnippetMacro();
57          macro.execute( sink, request );
58  
59          Iterator<SinkEventElement> it = sink.getEventList().iterator();
60          assertEquals( "verbatim", ( it.next() ).getName() );
61          SinkEventElement event = it.next();
62          assertEquals( "text", event.getName() );
63          String snippet = (String) event.getArgs()[0];
64          assertEquals( "verbatim_", ( it.next() ).getName() );
65          assertFalse( it.hasNext() );
66  
67          assertTrue( snippet.contains( "preamble" ) );
68          assertTrue( snippet.contains( "first snippet" ) );
69          assertTrue( snippet.contains( "interlude" ) );
70          assertTrue( snippet.contains( "second snippet" ) );
71          assertTrue( snippet.contains( "conclusion" ) );
72  
73          // again
74  
75          macroParameters.put( "id", "firstId" );
76          macroParameters.put( "verbatim", "" );
77          sink.reset();
78          request = new MacroRequest( macroParameters, basedir );
79          macro.execute( sink, request );
80  
81          it = sink.getEventList().iterator();
82          assertEquals( "verbatim", ( it.next() ).getName() );
83          event = it.next();
84          assertEquals( "text", event.getName() );
85          snippet = (String) event.getArgs()[0];
86          assertEquals( "verbatim_", ( it.next() ).getName() );
87          assertFalse( it.hasNext() );
88  
89          assertFalse( snippet.contains( "preamble" ) );
90          assertTrue( snippet.contains( "first snippet" ) );
91          assertFalse( snippet.contains( "interlude" ) );
92          assertFalse( snippet.contains( "second snippet" ) );
93          assertFalse( snippet.contains( "conclusion" ) );
94  
95          // again
96  
97          macroParameters.put( "id", "secondId" );
98          macroParameters.put( "verbatim", "false" );
99          sink.reset();
100         request = new MacroRequest( macroParameters, basedir );
101         macro.execute( sink, request );
102 
103         it = sink.getEventList().iterator();
104         event = it.next();
105         assertEquals( "rawText", event.getName() );
106         snippet = (String) event.getArgs()[0];
107         assertFalse( it.hasNext() );
108 
109         assertFalse( snippet.contains( "preamble" ) );
110         assertFalse( snippet.contains( "first snippet" ) );
111         assertFalse( snippet.contains( "interlude" ) );
112         assertTrue( snippet.contains( "second snippet" ) );
113         assertFalse( snippet.contains( "conclusion" ) );
114     }
115 
116     public void testIgnoreDownloadError()
117         throws Exception
118     {
119         Map<String, Object> macroParameters = new HashMap<String, Object>();
120         macroParameters.put( "debug", "true" );
121         macroParameters.put( "ignoreDownloadError", "true" );
122 
123         macroParameters.put( "url", "http://foo.bar.com/wine.txt" );
124 
125         File basedir = new File( getBasedir() );
126 
127         SinkEventTestingSink sink = new SinkEventTestingSink();
128 
129         MacroRequest request = new MacroRequest( macroParameters, basedir );
130         SnippetMacro macro = new SnippetMacro();
131         macro.execute( sink, request );
132         Iterator<SinkEventElement> it = sink.getEventList().iterator();
133         assertEquals( "verbatim", ( it.next() ).getName() );
134         SinkEventElement event = it.next();
135         assertEquals( "text", event.getName() );
136         String snippet = (String) event.getArgs()[0];
137         assertTrue( snippet.contains( "Error during retrieving content" ) );
138 
139     }
140 }