Quantcast

Working in a Unit Test, not in Application

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Working in a Unit Test, not in Application

Peter Harrison
I've written a wrapper around Freemarker for use in my application. My
unit test runs the following template:

<#assign hello="Goodbye cruel world">
${hello}

And you get "Goodbye cruel world" as you would expect.

But in my application, which runs inside Tomcat, the same template
results in nothing. Nada. Exactly the same code, two different outcomes.
Obviously something environmental, but I have no idea what.

public class TemplatePlugin implements Plugin {

     @Autowired
     private ResourceController resourceController;

     @Override
     public Map<String,Object> process( Action action,
Map<String,Object> context ) throws Exception{
         String resource =
getResourceController().getResource(action.getResource());

         Configuration configuration = new Configuration();
         configuration.setDefaultEncoding("UTF-8");

         Template template = new Template(action.getResource(), new
StringReader(resource), configuration);
         Writer output = new StringWriter();
         template.process(context, output);
         String result = output.toString();
         context.put(action.getResponse(), result);
         return context;
     }

}

------------------------------------------------------------------------------
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Lee
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Working in a Unit Test, not in Application

Lee
Are there any errors in the Tomcat log? 

On Monday, August 31, 2015, Peter Harrison <[hidden email]> wrote:
I've written a wrapper around Freemarker for use in my application. My
unit test runs the following template:

<#assign hello="Goodbye cruel world">
${hello}

And you get "Goodbye cruel world" as you would expect.

But in my application, which runs inside Tomcat, the same template
results in nothing. Nada. Exactly the same code, two different outcomes.
Obviously something environmental, but I have no idea what.

public class TemplatePlugin implements Plugin {

     @Autowired
     private ResourceController resourceController;

     @Override
     public Map<String,Object> process( Action action,
Map<String,Object> context ) throws Exception{
         String resource =
getResourceController().getResource(action.getResource());

         Configuration configuration = new Configuration();
         configuration.setDefaultEncoding("UTF-8");

         Template template = new Template(action.getResource(), new
StringReader(resource), configuration);
         Writer output = new StringWriter();
         template.process(context, output);
         String result = output.toString();
         context.put(action.getResponse(), result);
         return context;
     }

}

------------------------------------------------------------------------------
_______________________________________________
FreeMarker-user mailing list
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;FreeMarker-user@lists.sourceforge.net&#39;)">FreeMarker-user@...
https://lists.sourceforge.net/lists/listinfo/freemarker-user

------------------------------------------------------------------------------

_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Working in a Unit Test, not in Application

Daniel Dekany
In reply to this post by Peter Harrison
Have you checked if the `result` variable is indeed empty (like with
logging it or something direct)?

Have you checked what's in the `template` variable, if you toString
it?

Otherwise, JavaCC has this terrible habit of silently ending parsing
on I/O errors (which is also a problem in Velocity for example, not
just in FreeMarker). In 2.3.24 I have worked that around. So as a last
resort, try if with
http://freemarker.org/builds/2.3.24-nightly/freemarker.jar you get an
exception.

Tuesday, September 1, 2015, 4:28:27 AM, Peter Harrison wrote:

> I've written a wrapper around Freemarker for use in my application. My
> unit test runs the following template:
>
> <#assign hello="Goodbye cruel world">
> ${hello}
>
> And you get "Goodbye cruel world" as you would expect.
>
> But in my application, which runs inside Tomcat, the same template
> results in nothing. Nada. Exactly the same code, two different outcomes.
> Obviously something environmental, but I have no idea what.
>
> public class TemplatePlugin implements Plugin {
>
>      @Autowired
>      private ResourceController resourceController;
>
>      @Override
>      public Map<String,Object> process( Action action,
> Map<String,Object> context ) throws Exception{
>          String resource =
> getResourceController().getResource(action.getResource());
>
>          Configuration configuration = new Configuration();
>          configuration.setDefaultEncoding("UTF-8");
>
>          Template template = new Template(action.getResource(), new
> StringReader(resource), configuration);
>          Writer output = new StringWriter();
>          template.process(context, output);
>          String result = output.toString();
>          context.put(action.getResponse(), result);
>          return context;
>      }
>
> }
>
> ------------------------------------------------------------------------------
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Loading...