KickJava   Java API By Example, From Geeks To Geeks.

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


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) 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) 2005 Kiel Hodges <jruby-devel@selfsosoft.com>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby.test;
33
34 import java.util.ArrayList JavaDoc;
35
36 import org.jruby.Ruby;
37 import org.jruby.RubyException;
38 import org.jruby.RubyFixnum;
39 import org.jruby.RubyObject;
40 import org.jruby.exceptions.RaiseException;
41
42 /**
43  * Unit test for the kernel class.
44  **/

45 public class TestKernel extends TestRubyBase {
46
47     public TestKernel(String JavaDoc name) {
48         super(name);
49     }
50
51     public void setUp() {
52         runtime = Ruby.getDefaultInstance();
53         runtime.getLoadService().init(new ArrayList JavaDoc());
54     }
55
56     public void testLoad() throws Exception JavaDoc {
57         //load should work several times in a row
58
assertEquals("0", eval("load 'test/loadTest.rb'"));
59         assertEquals("load did not load the same file several times", "1", eval("load 'test/loadTest.rb'"));
60     }
61
62     public void testRequire() throws Exception JavaDoc {
63         //reset the $loadTestvar
64
eval("$loadTest = nil");
65         assertEquals("failed to load the file test/loadTest", "0", eval("require 'test/loadTest'"));
66         assertEquals("incorrectly reloaded the file test/loadTest", "", eval("require 'test/loadTest'"));
67
68         assertEquals("incorrect value for $\" variable", "test/loadTest.rb", eval("print $\".sort"));
69     }
70
71     public void testPrintf() throws Exception JavaDoc {
72         assertEquals("hello", eval("printf(\"%s\", \"hello\")"));
73         assertEquals("", eval("printf(\"%s\", nil)"));
74     }
75
76     public void testExit() throws Exception JavaDoc {
77         verifyExit(RubyFixnum.zero(runtime), "true");
78         verifyExit(RubyFixnum.one(runtime), "false");
79         verifyExit(RubyFixnum.one(runtime), "");
80         verifyExit(new RubyFixnum(runtime, 7), "7");
81     }
82         
83     private void verifyExit(RubyObject expectedStatus, String JavaDoc argument) throws Exception JavaDoc {
84         try {
85             eval("exit " + argument);
86             fail("Expected a SystemExit to be thrown by calling exit.");
87         } catch (RaiseException re) {
88             RubyException raisedException = re.getException();
89             if (raisedException.isKindOf(runtime.getClass("SystemExit"))) {
90                 RubyObject status = (RubyObject)raisedException.getInstanceVariable("status");
91                 assertEquals(expectedStatus, status);
92             } else {
93                 throw re;
94             }
95         }
96     }
97         
98     public void tearDown() {
99         super.tearDown();
100     }
101
102 }
103
Popular Tags