package org.litesoft.template; import junit.framework.*; import org.litesoft.util.*; // Copyright Status: // // All Software available from LiteSoft.org (including this file) is // hereby released into the public domain. // // It is free! As in, you may use it freely in both commercial and // non-commercial applications, bundle it with your software // distribution, include it on a CD-ROM, list the source code in a book, // mirror the documentation at your own web site, or use it in any other // way you see fit. // // NO Warranty! // // All software is provided "as is". // // There is ABSOLUTELY NO WARRANTY OF ANY KIND: not for the design, fitness // (for a particular purpose), level of errors (or lack thereof), or // applicability of this software. The entire risk as to the quality // and performance of this software is with you. Should this software // prove defective, you assume the cost of all necessary servicing, repair // or correction. // // In no event unless required by applicable law or agreed to in writing // will any party who created or may modify and/or redistribute this // software, be liable to you for damages, including any general, // special, incidental or consequential damages arising out of the use or // inability to use this software (including but not limited to loss of // data or data being rendered inaccurate or losses sustained by you or // third parties or a failure of this software to operate with any // other programs), even if such holder or other party has been advised // of the possibility of such damages. // // NOTE: Should you discover a bug, have a recogmendation for a change, wish // to submit modifications, or wish to add new classes/functionality, // please email them to: // // changes44@litesoft.org // public class TemplateFillerTest extends org.litesoft.Extend4Test { private static final Class THIS = TemplateFillerTest.class; public TemplateFillerTest( String name ) { super( name ); } public static void main( String[] args ) { run( suite() ); } public static Test suite() { return new TestSuite( THIS ); } public void test_Null() throws Exception { try { new TemplateFiller( null ); fail( "'null' excepted?" ); } catch ( NullPointerException expected ) { } } public void test_Empty() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template } ); String[] result = tf.makePopulated( new SimpleResolver() ); assertEquals( "" + // "ABC: 0 String" + // "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_OneEmptyLine() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template "" , } ); String[] result = tf.makePopulated( new SimpleResolver() ); assertEquals( "" + // "ABC: 1 String\n" + // " []\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_TwoLinesNoSubs() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template "XYZ" , " LMN " , } ); String[] result = tf.makePopulated( new SimpleResolver() ); assertEquals( "" + // "ABC: 2 String\n" + // " [XYZ]\n" + " [ LMN ]\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_TwoLinesWithSubsButNoResolvers() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template ".%XYZ%." , " .%LMN%. " , } ); String[] result = tf.makePopulated( new SimpleResolver() ); assertEquals( "" + // "ABC: 3 String\n" + // " [*** NOT FOUND (XYZ): .%XYZ%.]\n" + " [*** NOT FOUND (LMN): .%LMN%. ]\n" + " [ .%LMN%. ]\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_TwoLinesWithSimpleSubs() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template ".%XYZ%." , " .%LMN%. " , } ); String[] result = tf.makePopulated( new SimpleResolver( new Object[] { "XYZ" , new FieldSubrTest( "xyz" ) , "LMN" , new FieldSubrTest( "lmn" ) , } ) ); assertEquals( "" + // "ABC: 4 String\n" + // " [1st:xyz]\n" + " [2:xyz:nd]\n" + " [xyz:3rd]\n" + " [ lmn ]\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_OneLineWithMultipleSubs() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template " .%XYZ%..%LMN%..%XYZ%..%XYZ%..%LMN%. " , } ); String[] result = tf.makePopulated( new SimpleResolver( new Object[] { "XYZ" , new FieldSubrTest( "xyz" ) , "LMN" , new FieldSubrTest( "lmn" ) , } ) ); assertEquals( "" + // "ABC: 1 String\n" + // " [ xyzlmnxyzxyzlmn ]\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } public void test_FourLinesWithSubs() throws Exception { TemplateFiller tf = new TemplateFiller( new String[]{ // Template ".%XYZ%." , " .%LMN%. " , ".%XYZ:p1,p2,p3%." , " .%LMN:p4,p5,p6%. " , } ); String[] result = tf.makePopulated( new SimpleResolver( new Object[] { "XYZ" , new FieldSubrTest( "xyz" ) , "LMN" , new FieldSubrTest( "lmn" ) , } ) ); assertEquals( "" + // "ABC: 8 String\n" + // " [1st:xyz]\n" + " [2:xyz:nd]\n" + " [xyz:3rd]\n" + " [ lmn ]\n" + " [1st:xyz(P1,P2,P3)]\n" + " [2:xyz(P1,P2,P3):nd]\n" + " [xyz(P1,P2,P3):3rd]\n" + " [ lmn(P4,P5,P6) ]\n" + "" , ArrayHelper.toString( result , "ABC" ) ); } private static class FieldSubrTest extends TemplateSubstitutionFieldControl { private String zSubText; private String[] zSubLines; public FieldSubrTest( String pSubText ) { zSubText = pSubText; zSubLines = new String[] { "1st:" + pSubText, "2:" + pSubText + ":nd", pSubText + ":3rd", }; } public TemplateSubstitutionFieldControl getInstance( String pParams ) throws IllegalArgumentException { return new FieldSubrTest( zSubText + "(" + pParams.toUpperCase() + ")" ); } public String getStringSubstitution() { return zSubText; } public String[] getMultiLineSubstitution() { return zSubLines; } } }