View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.bsf;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import org.apache.bsf.BSFException;
23  import org.apache.bsf.BSFManager;
24  import org.jdiagnose.DiagnosticUnit;
25  
26  /***
27   * @author jmccrindle
28   */
29  public class BsfDiagnostic extends DiagnosticUnit {
30  
31  	private String lang = null;
32  	private String source = null;
33  	private int lineNo = 0;
34  	private int columnNo = 0;
35  	private String expr = null;
36  	
37  	private Map beans = new HashMap();
38  	
39  	public void diagnoseScript() throws BSFException {
40  		if(source == null) source = this.getName();
41  		assertNotNull("lang property should not be null", lang);
42  		assertNotNull("expr property should not be null", expr);
43  		BSFManager manager = new BSFManager();
44  		for (Iterator beanIterator = beans.entrySet().iterator(); beanIterator.hasNext();) {
45  			Map.Entry entry = (Map.Entry) beanIterator.next();
46  			Object value = entry.getValue();
47  			if(value != null) {
48  				manager.declareBean((String) entry.getKey(), value, value.getClass());
49  			}
50  		}
51  		Object result = manager.eval(lang, source, lineNo, columnNo, expr);
52  		assertTrue("Expression must return a Boolean", result instanceof Boolean);
53  		assertTrue(expr + " returned false", ((Boolean) result).booleanValue());
54  	}
55  	
56  	public String getExpr() {
57  		return expr;
58  	}
59  	
60  	public void setExpr(String expr) {
61  		this.expr = expr;
62  	}
63  	
64  	public String getLang() {
65  		return lang;
66  	}
67  	
68  	public void setLang(String lang) {
69  		this.lang = lang;
70  	}
71  	
72  	public String getSource() {
73  		return source;
74  	}
75  	
76  	public void setSource(String source) {
77  		this.source = source;
78  	}
79  	public Map getBeans() {
80  		return beans;
81  	}
82  }