/*
* Name: GeneratorTest
* Authors: Richard Rodger
*
* Copyright (c) 2003-2006 Richard Rodger
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
// package
package org.jostraca.test;
// import
import org.jostraca.Generator;
import org.jostraca.Property;
import org.jostraca.Template;
import org.jostraca.Jostraca;
import org.jostraca.util.PropertySet;
import org.jostraca.util.FileUtil;
import org.jostraca.util.Standard;
import org.jostraca.util.UserMessageHandler;
import org.jostraca.util.RecordingUserMessageHandler;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import java.util.Vector;
import java.util.ArrayList;
import java.io.File;
/** Test cases for org.jostraca.Generator
*/
public class GeneratorTest extends TestCase {
public GeneratorTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( GeneratorTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void testSimple() throws Exception {
File simple_txt = FileUtil.findFile( "org/jostraca/test/simple.txt" );
if( simple_txt.exists() ) {
simple_txt.delete();
}
Generator g = new Generator();
PropertySet ps = new PropertySet();
ps.set( Property.main_MakeBackup, Standard.NO );
ps.set( Property.main_CompileCodeWriter, Standard.YES );
ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
g.setCmdPropertySet( ps );
Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/simple.jtm" ) );
RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
simple_txt = FileUtil.findFile( "org/jostraca/test/simple.txt" );
assertTrue( simple_txt.exists() );
assertEquals( "simple: simple", FileUtil.readFile( simple_txt ).trim() );
assertEquals( "simple: simple", ((String)tm.getResult()).trim() );
}
public void testParse() throws Exception {
File org_jostraca_test = FileUtil.findFile( "org/jostraca/test" );
File system_conf = new File( FileUtil.findFile( "org/jostraca" ), "../../../conf/system.conf" );
FileUtil.copyFile( system_conf, org_jostraca_test );
File parse_txt = FileUtil.findFile( "org/jostraca/test/parse.txt" );
if( parse_txt.exists() ) {
parse_txt.delete();
}
Generator g = new Generator( org_jostraca_test );
PropertySet ps = new PropertySet();
ps.set( Property.main_MakeBackup, Standard.NO );
ps.set( Property.parse_OpenInnerChar, Standard.COLON );
ps.set( Property.parse_CloseInnerChar, Standard.COLON );
ps.set( Property.main_CompileCodeWriter, Standard.YES );
ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
g.setCmdPropertySet( ps );
g.generate( FileUtil.findFile( "org/jostraca/test/parse.jtm" ) );
RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
parse_txt = FileUtil.findFile( "org/jostraca/test/parse.txt" );
assertTrue( parse_txt.exists() );
assertEquals( "parse: parse", FileUtil.readFile( parse_txt ).trim() );
}
public void testInternal() throws Exception {
File internal_txt = FileUtil.findFile( "org/jostraca/test/internal.txt" );
if( internal_txt.exists() ) {
internal_txt.delete();
}
// just compile first
RecordingUserMessageHandler rumh = null;
Generator g = new Generator();
PropertySet ps = new PropertySet();
ps.set( Property.main_MakeBackup, Standard.NO );
ps.set( Property.main_CompileCodeWriter, Standard.YES );
ps.set( Property.main_ExecuteCodeWriter, Standard.NO );
g.setCmdPropertySet( ps );
g.generate( FileUtil.findFile( "org/jostraca/test/internal.jtm" ) );
rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
internal_txt = FileUtil.findFile( "org/jostraca/test/internal.txt" );
assertTrue( !internal_txt.exists() );
// and now execute
ps.set( Property.main_CompileCodeWriter, Standard.EMPTY );
ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
g.setCmdPropertySet( ps );
g.generate( FileUtil.findFile( "org/jostraca/test/internal.jtm" ), new RecordingUserMessageHandler() );
rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
assertTrue( internal_txt.exists() );
assertEquals( "internal: internal", FileUtil.readFile( internal_txt ).trim() );
}
public void testContext() throws Exception {
File context_txt = FileUtil.findFile( "org/jostraca/test/context.txt" );
if( context_txt.exists() ) {
context_txt.delete();
}
// just compile first
RecordingUserMessageHandler rumh = null;
Generator g = new Generator();
PropertySet ps = new PropertySet();
ps.set( Property.main_MakeBackup, Standard.NO );
ps.set( Property.main_CompileCodeWriter, Standard.YES );
ps.set( Property.main_ExecuteCodeWriter, Standard.NO );
g.setCmdPropertySet( ps );
String c = "context";
g.setContext( c );
g.generate( FileUtil.findFile( "org/jostraca/test/context.jtm" ) );
rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
context_txt = FileUtil.findFile( "org/jostraca/test/context.txt" );
assertTrue( !context_txt.exists() );
// and now execute
ps.set( Property.main_CompileCodeWriter, Standard.EMPTY );
ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
g.setCmdPropertySet( ps );
g.generate( FileUtil.findFile( "org/jostraca/test/context.jtm" ), new RecordingUserMessageHandler() );
rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
assertTrue( context_txt.exists() );
assertEquals( "context: context", FileUtil.readFile( context_txt ).trim() );
}
public void testCmdArgs() throws Exception {
File cmdargs_txt = FileUtil.findFile( "org/jostraca/test/cmdargs.txt" );
if( cmdargs_txt.exists() ) {
cmdargs_txt.delete();
}
Generator g = new Generator();
PropertySet ps = Jostraca.parseCommandLine( "-Bcg -Dfoo=bar", new ArrayList(), new ArrayList() );
g.setCmdPropertySet( ps );
Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/cmdargs.jtm" ) );
RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
assertTrue( null != rumh );
//System.out.println( rumh );
assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );
cmdargs_txt = FileUtil.findFile( "org/jostraca/test/cmdargs.txt" );
assertTrue( cmdargs_txt.exists() );
assertEquals( "cmdargs: cmdargs, foo: bar", FileUtil.readFile( cmdargs_txt ).trim() );
assertEquals( "cmdargs: cmdargs, foo: bar", ((String)tm.getResult()).trim() );
}
// REVIEW: need a test for uptodate checks
}