1 15 package org.apache.tapestry.junit.mock; 16 17 import java.io.BufferedOutputStream ; 18 import java.io.File ; 19 import java.io.FileOutputStream ; 20 import java.io.PrintStream ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.hivemind.util.PropertyUtils; 26 import org.apache.tapestry.junit.TapestryTestCase; 27 28 34 35 public class TestMocks extends TapestryTestCase 36 { 37 public static final String LOGS_DIR = "/target/logs"; 38 39 public static final String DEFAULT_BASE_DIR = "."; 40 41 public static final String SCRIPTS_DIR = "/src/scripts"; 42 43 private PrintStream _savedOut; 44 45 private PrintStream _savedErr; 46 47 private static String _baseDir; 48 49 public static String getBaseDirectory() 50 { 51 if (_baseDir == null) 52 _baseDir = System.getProperty("BASEDIR", DEFAULT_BASE_DIR); 53 54 return _baseDir; 55 } 56 57 protected void runTest() throws Throwable 58 { 59 String path = getBaseDirectory() + SCRIPTS_DIR + "/" + getName(); 60 61 MockTester tester = new MockTester(getBaseDirectory() + "/src/test-data/", path); 62 63 tester.execute(); 64 65 PropertyUtils.clearCache(); 66 } 67 68 public static Test suite() 69 { 70 TestSuite suite = new TestSuite("Mock Unit Test Suite"); 71 72 if (Boolean.getBoolean("skip-mock-tests")) 73 { 74 System.out.println("*** Skipping Mock Unit Test Suite"); 75 } 76 else 77 { 78 addScripts(suite); 79 80 deleteDir(getBaseDirectory() + "/target/.private"); 82 } 83 84 return suite; 85 } 86 87 private static void addScripts(TestSuite suite) 88 { 89 File scriptsDir = new File (getBaseDirectory() + SCRIPTS_DIR); 90 91 String [] names = scriptsDir.list(); 92 93 for (int i = 0; i < names.length; i++) 94 { 95 String name = names[i]; 96 97 if (name.endsWith(".xml")) 98 { 99 TestMocks test = new TestMocks(); 100 101 test.setName(name); 102 103 suite.addTest(test); 104 } 105 } 106 } 107 108 private static void deleteDir(String path) 109 { 110 File file = new File (path); 111 112 if (!file.exists()) 113 return; 114 115 deleteRecursive(file); 116 } 117 118 private static void deleteRecursive(File file) 119 { 120 if (file.isFile()) 121 { 122 file.delete(); 123 return; 124 } 125 126 String [] names = file.list(); 127 128 for (int i = 0; i < names.length; i++) 129 { 130 File f = new File (file, names[i]); 131 deleteRecursive(f); 132 } 133 134 file.delete(); 135 } 136 137 141 protected void setUp() throws Exception 142 { 143 File outDir = new File (getBaseDirectory() + LOGS_DIR); 144 145 if (!outDir.isDirectory()) 146 outDir.mkdirs(); 147 148 _savedOut = System.out; 149 _savedErr = System.err; 150 151 System.setOut(createPrintStream(outDir, "out")); 152 System.setErr(createPrintStream(outDir, "err")); 153 } 154 155 protected PrintStream createPrintStream(File directory, String extension) throws Exception 156 { 157 String name = getName() + "." + extension; 158 159 File file = new File (directory, name); 160 161 163 FileOutputStream fos = new FileOutputStream (file); 164 165 BufferedOutputStream bos = new BufferedOutputStream (fos); 166 167 return new PrintStream (bos, true); 168 } 169 170 173 protected void tearDown() throws Exception 174 { 175 System.err.close(); 176 System.setErr(_savedErr); 177 178 System.out.close(); 179 System.setOut(_savedOut); 180 } 181 } | Popular Tags |