1 16 17 package com.j2biz.pencil.test; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.PrintStream ; 25 import java.io.PrintWriter ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 29 import junit.framework.AssertionFailedError; 30 import junit.framework.TestCase; 31 32 import com.j2biz.pencil.test.scenario.Casting; 33 import com.j2biz.pencil.test.scenario.ClassWithoutStaticBlock; 34 import com.j2biz.pencil.test.scenario.DeepEnclosingClasses; 35 import com.j2biz.pencil.test.scenario.FieldScopes; 36 import com.j2biz.pencil.test.scenario.LoggerExists; 37 import com.j2biz.pencil.test.scenario.MethodWithoutLocalVariable; 38 import com.j2biz.pencil.test.scenario.ThisStory; 39 import com.j2biz.pencil.test.scenario.TypeCheck; 40 import com.j2biz.pencil.test.scenario.classFunctions.ClassLineNumberFunctionScene; 41 import com.j2biz.pencil.test.scenario.classFunctions.ClassNameFunctionScene; 42 import com.j2biz.pencil.test.scenario.npe.SimpleNullPointerTest; 43 import com.j2biz.pencil.test.scenario.refCall.DotCall; 44 import com.j2biz.pencil.test.scenario.refCall.DotCall2SuperClass; 45 import com.j2biz.pencil.test.scenario.refCall.LocalClassTest; 46 47 50 public class EnhancedClassesTestCase extends TestCase { 51 52 private static Properties ENHANCED_VALUES = new Properties (); 53 54 private static Properties ORIGINAL_VALUES = new Properties (); 55 56 public static final String OUTPUT_FILE_NAME = "enhanced.out"; 57 58 public static final String ORIG_OUTPUT_FILE_NAME = "original.out"; 59 60 public static final void executeEnhancedApplication( ) { 61 try { 62 System.out 63 .println("[EnhancedClassesTestCase] Begin with test of the TestApplication"); 64 final PrintStream oldOut = System.err; 65 final FileOutputStream dropedOut = new FileOutputStream ( 66 OUTPUT_FILE_NAME); 67 final PrintStream newOut = new PrintStream (dropedOut); 68 System.setErr(newOut); 69 70 try { 71 final String [] NO_ARGS = new String [0]; 72 FieldScopes.main(NO_ARGS); 73 ClassWithoutStaticBlock.main(NO_ARGS); 74 DeepEnclosingClasses.main(NO_ARGS); 75 TypeCheck.main(NO_ARGS); 76 DotCall.main(NO_ARGS); 77 DotCall2SuperClass.main(NO_ARGS); 78 LoggerExists.main(NO_ARGS); 79 ThisStory.main(NO_ARGS); 80 Casting.main(NO_ARGS); 81 LocalClassTest.main(NO_ARGS); 82 SimpleNullPointerTest.main(NO_ARGS); 83 MethodWithoutLocalVariable.main(NO_ARGS); 84 ClassNameFunctionScene.main(NO_ARGS); 85 ClassLineNumberFunctionScene.main(NO_ARGS); 86 } catch ( final Throwable x ) { 87 System.out 88 .println("[EnhancedClassesTestCase:ERROR] can't execute TestApplication, cause: " 89 + x.getLocalizedMessage()); 90 x.printStackTrace(); 91 } 92 System.setErr(oldOut); 93 newOut.flush(); 94 newOut.close(); 95 96 System.out.println("[EnhancedClassesTestCase] load properties"); 97 ENHANCED_VALUES.load(new FileInputStream (OUTPUT_FILE_NAME)); 98 99 System.out 100 .println("[EnhancedClassesTestCase] properties loaded, ready to test ..."); 101 } catch ( final IOException x ) { 102 System.out 103 .println("[EnhancedClassesTestCase] Failure occured during initialization of the TestCase. cause: " 104 + x.getLocalizedMessage()); 105 x.printStackTrace(); 106 } 107 } 108 109 public EnhancedClassesTestCase( ) throws IOException { 110 ; 111 } 112 113 116 public static void executeOriginalApplikation( ) { 117 try { 118 System.out 119 .println("[EnhancedClassesTestCase] Begin with test of the TestApplication"); 120 PrintStream oldOut = System.err; 121 FileOutputStream dropedOut = new FileOutputStream ( 122 ORIG_OUTPUT_FILE_NAME); 123 PrintStream newOut = new PrintStream (dropedOut); 124 System.setErr(newOut); 125 126 try { 127 final String [] NO_ARGS = new String [0]; 128 com.j2biz.pencil.test.scenario.impl.FieldScopes.main(NO_ARGS); 129 com.j2biz.pencil.test.scenario.impl.ClassWithoutStaticBlock 130 .main(NO_ARGS); 131 com.j2biz.pencil.test.scenario.impl.DeepEnclosingClasses 132 .main(NO_ARGS); 133 com.j2biz.pencil.test.scenario.impl.TypeCheck.main(NO_ARGS); 134 com.j2biz.pencil.test.scenario.impl.refCall.DotCall 135 .main(NO_ARGS); 136 com.j2biz.pencil.test.scenario.impl.refCall.DotCall2SuperClass 137 .main(NO_ARGS); 138 com.j2biz.pencil.test.scenario.impl.LoggerExists.main(NO_ARGS); 139 com.j2biz.pencil.test.scenario.impl.ThisStory.main(NO_ARGS); 140 com.j2biz.pencil.test.scenario.impl.Casting.main(NO_ARGS); 141 com.j2biz.pencil.test.scenario.impl.refCall.LocalClassTest 142 .main(NO_ARGS); 143 com.j2biz.pencil.test.scenario.impl.npe.SimpleNullPointerTest 144 .main(NO_ARGS); 145 com.j2biz.pencil.test.scenario.impl.MethodWithoutLocalVariable 146 .main(NO_ARGS); 147 com.j2biz.pencil.test.scenario.impl.classFunctions 148 .ClassNameFunctionScene.main(NO_ARGS); 149 com.j2biz.pencil.test.scenario.impl.classFunctions.ClassLineNumberFunctionScene.main(NO_ARGS); 150 } catch ( final Throwable x ) { 151 System.out 152 .println("[EnhancedClassesTestCase:ERROR] can't execute TestApplication, cause: " 153 + x.getLocalizedMessage()); 154 x.printStackTrace(); 155 } 156 157 System.setErr(oldOut); 158 newOut.flush(); 159 newOut.close(); 160 161 System.out.println("[EnhancedClassesTestCase] load properties"); 162 163 ORIGINAL_VALUES.load(new FileInputStream (ORIG_OUTPUT_FILE_NAME)); 164 165 System.out 166 .println("[EnhancedClassesTestCase] properties loaded, ready to test ..."); 167 } catch ( IOException x ) { 168 System.out 169 .println("[EnhancedClassesTestCase] Failure occured during initialization of the TestCase. cause: " 170 + x.getLocalizedMessage()); 171 x.printStackTrace(); 172 } 173 } 174 175 public void testCreatedFiles( ) { 176 File enhanced = new File (OUTPUT_FILE_NAME); 177 assertTrue("enhanced output. does not exist.", enhanced.exists()); 178 assertTrue("enhanced output is empty.", enhanced.length() > 0); 179 180 File original = new File (ORIG_OUTPUT_FILE_NAME); 181 assertTrue("original output. does not exist.", original.exists()); 182 assertTrue("original output is empty.", original.length() > 0); 183 } 184 185 public void testOriginalValues( ) throws FileNotFoundException { 186 final PrintWriter fout = new PrintWriter (new FileOutputStream ("error_" 187 + ORIG_OUTPUT_FILE_NAME)); 188 189 int counter = 0; 190 191 for ( Enumeration i = ORIGINAL_VALUES.keys(); i.hasMoreElements(); ) { 192 try { 193 String key = (String ) i.nextElement(); 194 String originalValue = ORIGINAL_VALUES.getProperty(key); 195 String enhancedValue = ENHANCED_VALUES.getProperty(key); 196 assertNotNull("original value is NULL: " + key, originalValue); 197 assertNotNull("enhanced value is NULL:" + key, enhancedValue); 198 assertEquals("original " + key, enhancedValue.trim(), 199 originalValue.trim()); 200 201 } catch ( Throwable t ) { 202 counter++; 203 fout.println(counter + ". error : "); 204 fout.println(">>> "); 205 t.printStackTrace(fout); 206 fout.println("<<< "); 207 fout.println(); 208 } 209 } 210 211 if ( counter > 0 ) { 212 fout.println("\t\t Number of errors: " + counter); 213 } 214 215 fout.flush(); 216 fout.close(); 217 218 if ( counter > 0 ) { 219 throw new AssertionFailedError( 220 "found " 221 + counter 222 + " values of an original class, which doesn't exist in the output of the enhanced class."); 223 } 224 } 225 226 public void testEnhancedValues( ) throws IOException { 227 final PrintWriter fout = new PrintWriter (new FileOutputStream ("error_" 228 + OUTPUT_FILE_NAME)); 229 230 int counter = 0; 231 232 for ( Enumeration i = ENHANCED_VALUES.keys(); i.hasMoreElements(); ) { 233 try { 234 String key = (String ) i.nextElement(); 235 String originalValue = ORIGINAL_VALUES.getProperty(key); 236 String enhancedValue = ENHANCED_VALUES.getProperty(key); 237 assertNotNull("original value is NULL:" + key, originalValue); 238 assertNotNull("enhanced value is NULL:" + key, enhancedValue); 239 assertEquals("enhanced " + key, enhancedValue.trim(), 240 originalValue.trim()); 241 } catch ( Throwable t ) { 242 counter++; 243 fout.println(counter + ". error : "); 244 fout.println(">>> "); 245 t.printStackTrace(fout); 246 fout.println("<<< "); 247 fout.println(); 248 } 249 } 250 251 if ( counter > 0 ) { 252 fout.println("\t\t Number of errors: " + counter); 253 } 254 255 fout.flush(); 256 fout.close(); 257 258 if ( counter > 0 ) { 259 throw new AssertionFailedError( 260 "found " 261 + counter 262 + " values of an enhanced class, which doesn't exist in the output of the original class."); 263 } 264 } 265 } | Popular Tags |