Coverage Report - org.melati.test.TemplateServletTest
 
Classes in this File Line Coverage Branch Coverage Complexity
TemplateServletTest
100%
30/30
100%
12/12
4.333
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 Tim Joyce
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     Tim Joyce <timj@paneris.org>
 42  
  *     http://paneris.org/
 43  
  *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
 44  
  */
 45  
 
 46  
 package org.melati.test;
 47  
 
 48  
 import java.io.OutputStream;
 49  
 
 50  
 import org.melati.servlet.Form;
 51  
 import org.melati.servlet.TemplateServlet;
 52  
 import org.melati.servlet.TemporaryFileFormDataAdaptorFactory;
 53  
 import org.melati.Melati;
 54  
 import org.melati.MelatiConfig;
 55  
 import org.melati.servlet.PathInfoException;
 56  
 import org.melati.PoemContext;
 57  
 import org.melati.servlet.MultipartFormField;
 58  
 import org.melati.template.ServletTemplateContext;
 59  
 import org.melati.util.MelatiBugMelatiException;
 60  
 
 61  
 /**
 62  
  * Test a Melati configuration with a Template Engine and a 
 63  
  * temporary file form data adaptor.
 64  
  *
 65  
  * @author Tim Joyce
 66  
  * $Revision$
 67  
  */
 68  5
 public class TemplateServletTest extends TemplateServlet {
 69  
   private static final long serialVersionUID = 1L;
 70  
 
 71  
   protected String doTemplateRequest(Melati melati,
 72  
                                      ServletTemplateContext templateContext) 
 73  
       throws Exception {
 74  
     
 75  31
     String method = melati.getMethod();
 76  31
     templateContext.put("RestrictedAccessObject", 
 77  
             new RestrictedAccessObject());
 78  31
     templateContext.put("servletName", this.getClass().getName());
 79  31
     if (method != null) {
 80  
 
 81  10
       if (method.equals("Upload")) {
 82  4
         MultipartFormField field = templateContext.getMultipartFormField("file");
 83  4
         byte[] data = field.getData();
 84  4
         melati.getResponse().setContentType(field.getContentType());
 85  4
         OutputStream output = melati.getResponse().getOutputStream();
 86  4
         output.write(data);
 87  4
         output.close();
 88  4
         return null;
 89  
       }
 90  
 
 91  6
       if (method.equals("Redirect")) {
 92  2
         melati.getResponse().sendRedirect("http://www.melati.org");
 93  2
         return null;
 94  
       }
 95  4
       if (method.equals("Exception")) {  
 96  
 
 97  2
         throw new MelatiBugMelatiException("It got caught!");
 98  
       }
 99  
     }
 100  23
     if (Form.getFieldNulled(melati.getServletTemplateContext(), "passback") != null)
 101  3
       melati.setPassbackExceptionHandling();
 102  
     else
 103  
       // No need to explicitly do this as it is the default, other than test coverage
 104  20
       melati.setPropagateExceptionHandling(); 
 105  
 
 106  23
     return("org/melati/test/TemplateServletTest");
 107  
   }
 108  
 
 109  
 /**
 110  
  * Set up the POEM context so we don't have to specify the 
 111  
  * logicaldatabase on the pathinfo.  
 112  
  *
 113  
  * Useful when writing appications where you are typically only accessing
 114  
  * a single database.
 115  
  */
 116  
   protected PoemContext poemContext(Melati melati)
 117  
   throws PathInfoException {
 118  31
     String[] parts = melati.getPathInfoParts();
 119  31
     if (parts.length == 0) 
 120  3
       return poemContextWithLDB(melati,"melatitest");
 121  
     else 
 122  28
       return super.poemContext(melati);
 123  
   }
 124  
 
 125  
   /** 
 126  
    * Set the form data adaptor.
 127  
    * 
 128  
    * @see org.melati.servlet.ConfigServlet#melatiConfig()
 129  
    */
 130  
   protected MelatiConfig melatiConfig() {
 131  5
     MelatiConfig config = super.melatiConfig();
 132  5
     TemporaryFileFormDataAdaptorFactory factory = 
 133  
         new TemporaryFileFormDataAdaptorFactory();
 134  5
     config.setFormDataAdaptorFactory(factory);
 135  5
     return config;
 136  
   }
 137  
   
 138  
 }
 139  
 
 140  
 
 141  
 
 142  
 
 143  
 
 144