/*
* Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
*
* This file is part of TransferCM.
*
* TransferCM is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
* Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.methodhead.shim;
import java.io.*;
import java.util.*;
import java.sql.*;
import junit.framework.*;
import org.apache.log4j.*;
import com.methodhead.persistable.*;
import com.methodhead.test.*;
import com.methodhead.sitecontext.*;
import com.methodhead.*;
import com.methodhead.util.*;
import org.apache.cactus.*;
public class TemplateTest extends ServletTestCase {
static {
TestUtils.initLogger();
TestUtils.initDb();
}
public TemplateTest( String name ) {
super( name );
}
File templateBase_ = null;
File template_ = null;
SiteContext siteContext1_ = null;
SiteContext siteContext2_ = null;
protected void setUp() {
//setLogLevel( Level.DEBUG );
try {
ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
siteContext1_ = new SiteContext();
siteContext1_.setString( "path", "foo" );
siteContext1_.saveNew();
siteContext2_ = new SiteContext();
siteContext2_.setString( "path", "foo" );
siteContext2_.saveNew();
}
catch ( Exception e ) {
fail( e.getMessage() );
}
}
protected void tearDown() {
}
public void testGetSiteContext() {
try {
Template template = null;
SiteContext siteContext = null;
template = new Template();
siteContext = SiteContext.getDefaultContext();
//
// get site context
//
try {
template.getSiteContext();
fail( "Template.getSiteContext() did not throw an exception." );
}
catch ( ShimException e ) {
// success
}
template.setSiteContext( siteContext );
assertNotNull( template.getSiteContext() );
assertEquals( siteContext, template.getSiteContext() );
}
catch ( Exception e ) {
e.printStackTrace();
fail();
}
}
public void testGetTemplateList() throws Exception {
Template t = null;
List l = null;
//
// get the list for the default context
//
t = new Template();
t.setSiteContext( SiteContext.getDefaultContext() );
l = t.getTemplateList( request );
assertNotNull( l );
assertEquals( 4, l.size() );
assertEquals( "template.jsp", l.get( 0 ) );
assertEquals( "Template2.jsp", l.get( 1 ) );
assertEquals( "template3.jsp", l.get( 2 ) );
assertEquals( "template4.jsp", l.get( 3 ) );
//
// get the list for a specific context (should be id=1)
//
t = new Template();
t.setSiteContext( siteContext1_ );
l = t.getTemplateList( request );
assertNotNull( l );
assertEquals( 2, l.size() );
assertEquals( "template.jsp", l.get( 0 ) );
//
// get the list for a specific context with no templates (should be id=2)
//
t = new Template();
t.setSiteContext( siteContext2_ );
l = t.getTemplateList( request );
assertNotNull( l );
assertEquals( 0, l.size() );
}
public void testLoad() {
try {
Template t = null;
PanelConfig panelConfig = null;
Map map = null;
TestData.createWebappFiles( ServletUtils.getRealFile( request, "" ) );
//
// try to load with an invalid template
//
try {
t = new Template();
t.setSiteContext( SiteContext.getDefaultContext() );
t.load( request, null );
fail( "load() did not throw an exception." );
}
catch ( ShimException e ) {
// success
}
//
// try to load with an invalid template
//
try {
t = new Template();
t.setSiteContext( SiteContext.getDefaultContext() );
t.load( request, "" );
fail( "load() did not throw an exception." );
}
catch ( ShimException e ) {
// success
}
//
// load for the default context
//
t = new Template();
t.setSiteContext( SiteContext.getDefaultContext() );
t.load( request, "template.jsp" );
assertEquals( "template.jsp", t.getFileName() );
assertNull( request.getAttribute( ShimGlobals.PANELMAP_KEY) );
map = t.getPanelConfigs();
assertNotNull( map );
assertEquals( 1, map.keySet().size() );
panelConfig = ( PanelConfig )map.get( "body" );
assertNotNull( panelConfig );
assertEquals( ShimGlobals.DEFAULT_MODULE, panelConfig.getDefaultModule() );
//
// load for a particular context
//
t = new Template();
t.setSiteContext( siteContext1_ );
t.load( request, "template.jsp" );
assertEquals( "template.jsp", t.getFileName() );
assertNull( request.getAttribute( ShimGlobals.PANELMAP_KEY) );
map = t.getPanelConfigs();
assertNotNull( map );
assertEquals( 2, map.keySet().size() );
panelConfig = ( PanelConfig )map.get( "body" );
assertNotNull( panelConfig );
assertEquals( ShimGlobals.DEFAULT_MODULE, panelConfig.getDefaultModule() );
panelConfig = ( PanelConfig )map.get( "footer" );
assertNotNull( panelConfig );
assertEquals( "some.Module", panelConfig.getDefaultModule() );
}
catch ( Exception e ) {
e.printStackTrace();
fail();
}
}
public void testInclude() {
try {
Template template = null;
SiteContext siteContext = null;
//
// include a template from the default site context
//
template = new Template();
template.setSiteContext( SiteContext.getDefaultContext() );
template.include( request, response, "template3.jsp" );
//
// include a template from a specific site context
//
siteContext = new SiteContext();
siteContext.setInt( "id", 1 );
template.setSiteContext( siteContext );
template.include( request, response, "template2.jsp" );
}
catch ( Exception e ) {
e.printStackTrace();
fail();
}
}
public void endInclude( WebResponse response ) {
assertEquals( "This is a template.\nThis is also a template.\n", response.getText() );
}
}
|