| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package com.netscape.javascript.qa.drivers; |
| 6 |
|
| 7 |
import java.io.*; |
| 8 |
import com.netscape.javascript.*; |
| 9 |
import org.mozilla.javascript.*; |
| 10 |
import org.mozilla.javascript.tools.shell.*; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class RhinoEnv implements TestEnvironment { |
| 31 |
public Global global; |
| 32 |
Object cx; |
| 33 |
Object result; |
| 34 |
TestDriver driver; |
| 35 |
TestSuite suite; |
| 36 |
TestFile file; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public RhinoEnv( TestFile f, TestSuite s, TestDriver d) { |
| 46 |
this.file = f; |
| 47 |
this.suite = s; |
| 48 |
this.driver = d; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public synchronized void runTest() { |
| 60 |
this.driver.p( file.name ); |
| 61 |
try { |
| 62 |
cx = createContext(); |
| 63 |
((Context) cx).setOptimizationLevel( driver.OPT_LEVEL ); |
| 64 |
((Context) cx).setDebugLevel( driver.DEBUG_LEVEL ); |
| 65 |
Object loadFn = executeTestFile(driver.HELPER_FUNCTIONS.getAbsolutePath()); |
| 66 |
|
| 67 |
file.startTime = driver.getCurrentTime(); |
| 68 |
result = executeTestFile( file.filePath ); |
| 69 |
file.endTime = driver.getCurrentTime(); |
| 70 |
parseResult(); |
| 71 |
|
| 72 |
} catch ( Exception e ) { |
| 73 |
suite.passed = false; |
| 74 |
file.passed = false; |
| 75 |
file.exception += "file failed with exception: " + e ; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Object createContext() { |
| 85 |
|
| 86 |
cx = new Context(); |
| 87 |
((Context) cx).enter(); |
| 88 |
|
| 89 |
global = new Global(); |
| 90 |
((Context) cx).initStandardObjects(global); |
| 91 |
|
| 92 |
String[] names = { "print", "quit", "version", "load", "help", |
| 93 |
"loadClass" }; |
| 94 |
try { |
| 95 |
global.defineFunctionProperties(names, Main.class, |
| 96 |
ScriptableObject.DONTENUM); |
| 97 |
} catch (PropertyException e) { |
| 98 |
|
| 99 |
throw new Error(e.getMessage()); |
| 100 |
} |
| 101 |
|
| 102 |
return cx; |
| 103 |
} |
| 104 |
public Object executeTestFile() { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Object executeTestFile( String s ) { |
| 121 |
|
| 122 |
FileReader in = null; |
| 123 |
try { |
| 124 |
in = new FileReader( s ); |
| 125 |
} catch (FileNotFoundException ex) { |
| 126 |
driver.p("couldn't open file " + s); |
| 127 |
} |
| 128 |
|
| 129 |
Object result = null; |
| 130 |
|
| 131 |
try { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
result = ((Scriptable) (((Context) cx).evaluateReader( |
| 138 |
(Scriptable) global, (Reader) in, s, 1, null))); |
| 139 |
|
| 140 |
} catch (WrappedException we) { |
| 141 |
driver.p("Wrapped Exception: "+ |
| 142 |
we.getWrappedException().toString()); |
| 143 |
result = we.getWrappedException().toString(); |
| 144 |
} catch (Exception jse) { |
| 145 |
driver.p("JavaScriptException: " + jse.getMessage()); |
| 146 |
result = jse.getMessage(); |
| 147 |
} |
| 148 |
|
| 149 |
return ( result ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public boolean parseResult() { |
| 168 |
FlattenedObject fo = null; |
| 169 |
|
| 170 |
if ( result instanceof Scriptable ) { |
| 171 |
fo = new FlattenedObject( (Scriptable) result ); |
| 172 |
|
| 173 |
try { |
| 174 |
file.totalCases = ((Number) fo.getProperty("length")).intValue(); |
| 175 |
for ( int i = 0; i < file.totalCases; i++ ) { |
| 176 |
Scriptable tc = (Scriptable) ((Scriptable) result).get( i, |
| 177 |
(Scriptable) result); |
| 178 |
|
| 179 |
TestCase rt = new TestCase( |
| 180 |
getString(tc.get( "passed", tc )), |
| 181 |
getString(tc.get( "name", tc )), |
| 182 |
getString(tc.get( "description", tc )), |
| 183 |
getString(tc.get( "expect", tc )), |
| 184 |
getString(tc.get( "actual", tc )), |
| 185 |
getString(tc.get( "reason", tc )) |
| 186 |
); |
| 187 |
|
| 188 |
file.bugnumber= |
| 189 |
(getString(tc.get("bugnumber", tc))).startsWith |
| 190 |
("com.netscape.javascript") |
| 191 |
? file.bugnumber |
| 192 |
: getString(tc.get("bugnumber", tc)); |
| 193 |
|
| 194 |
file.caseVector.addElement( rt ); |
| 195 |
if ( rt.passed.equals("false") ) { |
| 196 |
this.file.passed = false; |
| 197 |
this.suite.passed = false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( file.totalCases == 0 ) { |
| 202 |
if ( file.name.endsWith( "-n.js" ) ) { |
| 203 |
this.file.passed = true; |
| 204 |
} else { |
| 205 |
this.file.reason = "File contains no testcases. " + this.file.reason; |
| 206 |
this.file.passed = false; |
| 207 |
this.suite.passed = false; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
} catch ( Exception e ) { |
| 212 |
this.file.exception = "Got a Scriptable result, but failed "+ |
| 213 |
"parsing its arguments. Exception: " + e.toString() + |
| 214 |
" Flattened Object is: " + fo.toString(); |
| 215 |
|
| 216 |
this.file.passed = false; |
| 217 |
this.suite.passed = false; |
| 218 |
|
| 219 |
return false; |
| 220 |
} |
| 221 |
} else { |
| 222 |
|
| 223 |
|
| 224 |
this.file.exception = result.toString(); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if ( file.name.endsWith( "-n.js" ) ) { |
| 229 |
this.file.passed = true; |
| 230 |
} else { |
| 231 |
this.file.passed = false; |
| 232 |
this.suite.passed = false; |
| 233 |
return false; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public void close() { |
| 243 |
try { |
| 244 |
((Context) cx).exit(); |
| 245 |
} catch ( Exception e ) { |
| 246 |
suite.passed = false; |
| 247 |
file.passed = false; |
| 248 |
file.exception = "file failed with exception: " + e ; |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public String getString( Object object ) { |
| 259 |
return (((Context) cx).toString( object )); |
| 260 |
} |
| 261 |
} |