1 package org.apache.velocity.test; 2 3 18 19 import java.io.BufferedWriter ; 20 import java.io.FileOutputStream ; 21 import java.io.OutputStreamWriter ; 22 import java.io.Writer ; 23 24 import java.util.Vector ; 25 26 import org.apache.velocity.VelocityContext; 27 28 import org.apache.velocity.Template; 29 import org.apache.velocity.app.Velocity; 30 import org.apache.velocity.test.provider.TestProvider; 31 import org.apache.velocity.util.StringUtils; 32 33 import junit.framework.TestCase; 34 35 45 public class EncodingTestCase extends BaseTestCase implements TemplateTestBase 46 { 47 public EncodingTestCase() 48 { 49 super("EncodingTestCase"); 50 51 try 52 { 53 Velocity.setProperty( 54 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH); 55 56 Velocity.setProperty( Velocity.INPUT_ENCODING, "UTF-8" ); 57 58 Velocity.init(); 59 } 60 catch (Exception e) 61 { 62 System.err.println("Cannot setup EncodingTestCase!"); 63 e.printStackTrace(); 64 System.exit(1); 65 } 66 } 67 68 public static junit.framework.Test suite() 69 { 70 return new EncodingTestCase(); 71 } 72 73 76 public void runTest () 77 { 78 79 VelocityContext context = new VelocityContext(); 80 81 try 82 { 83 assureResultsDirectoryExists(RESULT_DIR); 84 85 88 89 92 93 Template template = Velocity.getTemplate( 94 getFileName(null, "encodingtest", TMPL_FILE_EXT), "UTF-8"); 95 96 FileOutputStream fos = 97 new FileOutputStream ( 98 getFileName(RESULT_DIR, "encodingtest", RESULT_FILE_EXT)); 99 100 Writer writer = new BufferedWriter (new OutputStreamWriter (fos, "UTF-8")); 101 102 template.merge(context, writer); 103 writer.flush(); 104 writer.close(); 105 106 if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest", 107 RESULT_FILE_EXT,CMP_FILE_EXT) ) 108 { 109 fail("Output 1 incorrect."); 110 } 111 112 115 116 template = Velocity.getTemplate( 117 getFileName( null, "encodingtest2", TMPL_FILE_EXT), "UTF-8"); 118 119 fos = 120 new FileOutputStream ( 121 getFileName(RESULT_DIR, "encodingtest2", RESULT_FILE_EXT)); 122 123 writer = new BufferedWriter (new OutputStreamWriter (fos, "UTF-8")); 124 125 template.merge(context, writer); 126 writer.flush(); 127 writer.close(); 128 129 if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest2", 130 RESULT_FILE_EXT,CMP_FILE_EXT) ) 131 { 132 fail("Output 2 incorrect."); 133 } 134 135 138 139 template = Velocity.getTemplate( 140 getFileName( null, "encodingtest3", TMPL_FILE_EXT), "GBK"); 141 142 fos = 143 new FileOutputStream ( 144 getFileName(RESULT_DIR, "encodingtest3", RESULT_FILE_EXT)); 145 146 writer = new BufferedWriter (new OutputStreamWriter (fos, "GBK")); 147 148 template.merge(context, writer); 149 writer.flush(); 150 writer.close(); 151 152 if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest3", 153 RESULT_FILE_EXT,CMP_FILE_EXT) ) 154 { 155 fail("Output 3 incorrect."); 156 } 157 158 161 162 template = Velocity.getTemplate( 163 getFileName( null, "encodingtest_KOI8-R", TMPL_FILE_EXT), "KOI8-R"); 164 165 fos = 166 new FileOutputStream ( 167 getFileName(RESULT_DIR, "encodingtest_KOI8-R", RESULT_FILE_EXT)); 168 169 writer = new BufferedWriter (new OutputStreamWriter (fos, "KOI8-R")); 170 171 template.merge(context, writer); 172 writer.flush(); 173 writer.close(); 174 175 if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest_KOI8-R", 176 RESULT_FILE_EXT,CMP_FILE_EXT) ) 177 { 178 fail("Output 4 incorrect."); 179 } 180 } 181 catch (Exception e) 182 { 183 fail(e.getMessage()); 184 } 185 } 186 } 187 188 189 190 | Popular Tags |