KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > test > TestRuby


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
15  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  * Copyright (C) 2006 Aslak Hellesoy <rinkrank@codehaus.org>
19  * Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com>
20  * Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * either of the GNU General Public License Version 2 or later (the "GPL"),
24  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25  * in which case the provisions of the GPL or the LGPL are applicable instead
26  * of those above. If you wish to allow use of your version of this file only
27  * under the terms of either the GPL or the LGPL, and not to allow others to
28  * use your version of this file under the terms of the CPL, indicate your
29  * decision by deleting the provisions above and replace them with the notice
30  * and other provisions required by the GPL or the LGPL. If you do not delete
31  * the provisions above, a recipient may use your version of this file under
32  * the terms of any one of the CPL, the GPL or the LGPL.
33  ***** END LICENSE BLOCK *****/

34 package org.jruby.test;
35
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.PrintStream JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Arrays JavaDoc;
40
41 import org.jruby.Ruby;
42 import org.jruby.RubyArray;
43 import org.jruby.RubyException;
44 import org.jruby.RubyInstanceConfig;
45 import org.jruby.RubyIO;
46 import org.jruby.RubyString;
47 import org.jruby.exceptions.RaiseException;
48
49 /**
50  * Unit test for the ruby class.
51  *
52  * @author Benoit
53 */

54 public class TestRuby extends TestRubyBase {
55
56     public TestRuby(String JavaDoc name) {
57         super(name);
58     }
59
60     public void setUp() {
61         runtime = Ruby.getDefaultInstance();
62     }
63     
64     public void testVarAndMet() throws Exception JavaDoc {
65         runtime.getLoadService().init(new ArrayList JavaDoc());
66         eval("load 'test/testVariableAndMethod.rb'");
67         assertEquals("Hello World", eval("puts($a)"));
68         assertEquals("dlroW olleH", eval("puts $b"));
69         assertEquals("Hello World", eval("puts $d.reverse, $c, $e.reverse"));
70         assertEquals("135 20 3", eval("puts $f, \" \", $g, \" \", $h"));
71     }
72     
73     public void testPrintErrorWithNilBacktrace() throws Exception JavaDoc {
74         testPrintErrorWithBacktrace("nil");
75     }
76
77     public void testPrintErrorWithStringBacktrace() throws Exception JavaDoc {
78         testPrintErrorWithBacktrace("\"abc\"");
79     }
80     
81     private void testPrintErrorWithBacktrace(String JavaDoc backtrace) throws Exception JavaDoc {
82         RubyIO oldStderr = (RubyIO)runtime.getGlobalVariables().get("$stderr");
83         try {
84             ByteArrayOutputStream JavaDoc stderrOutput = new ByteArrayOutputStream JavaDoc();
85             RubyIO newStderr = new RubyIO(runtime, stderrOutput);
86             runtime.getGlobalVariables().set("$stderr", newStderr);
87             
88             try {
89                 eval("class MyError < StandardError ; def backtrace ; " + backtrace + " ; end ; end ; raise MyError.new ");
90                 fail("Expected MyError to be raised");
91             } catch (RaiseException re) {
92                 //No ClassCastException!
93
runtime.printError(re.getException());
94             }
95         } finally {
96             runtime.getGlobalVariables().set("$stderr", oldStderr);
97         }
98     }
99     
100     public void testPrintErrorShouldPrintErrorMessageAndStacktraceWhenBacktraceIsPresent() {
101         final ByteArrayOutputStream JavaDoc err = new ByteArrayOutputStream JavaDoc();
102         RubyInstanceConfig config = new RubyInstanceConfig() {{
103             setInput(System.in); setOutput(System.out); setError(new PrintStream JavaDoc(err)); setObjectSpaceEnabled(false);
104         }};
105         Ruby ruby = Ruby.newInstance(config);
106         RubyException exception = new RubyException(ruby, ruby.getClass("NameError"), "A message");
107         RubyString[] lines = new RubyString[]{
108             RubyString.newString(ruby, "Line 1"),
109             RubyString.newString(ruby, "Line 2"),
110         };
111         RubyArray backtrace = RubyArray.newArray(ruby, Arrays.asList(lines));
112         exception.set_backtrace(backtrace);
113         ruby.printError(exception);
114         assertEquals("Line 1: A message (NameError)\n\tfrom Line 2\n", err.toString());
115     }
116     
117     public void testPrintErrorShouldOnlyPrintErrorMessageWhenBacktraceIsNil() {
118         final ByteArrayOutputStream JavaDoc err = new ByteArrayOutputStream JavaDoc();
119         RubyInstanceConfig config = new RubyInstanceConfig() {{
120             setInput(System.in); setOutput(System.out); setError(new PrintStream JavaDoc(err)); setObjectSpaceEnabled(false);
121         }};
122         Ruby ruby = Ruby.newInstance(config);
123         RubyException exception = new RubyException(ruby, ruby.getClass("NameError"), "A message");
124         ruby.printError(exception);
125         // assertEquals(":[0,0]:[0,7]: A message (NameError)\n", err.toString());
126
}
127 }
128
Popular Tags