@@ -45,25 +45,41 @@ public class SparkInterpreterTest {
4545 private InterpreterContext context ;
4646 private File tmpDir ;
4747
48+
49+ /**
50+ * Get spark version number as a numerical value.
51+ * eg. 1.1.x => 11, 1.2.x => 12, 1.3.x => 13 ...
52+ */
53+ public static int getSparkVersionNumber () {
54+ if (repl == null ) {
55+ return 0 ;
56+ }
57+
58+ String [] split = repl .getSparkContext ().version ().split ("." );
59+ int version = Integer .parseInt (split [0 ]) + Integer .parseInt (split [1 ]);
60+ return version ;
61+ }
62+
4863 @ Before
4964 public void setUp () throws Exception {
5065 tmpDir = new File (System .getProperty ("java.io.tmpdir" ) + "/ZeppelinLTest_" + System .currentTimeMillis ());
5166 System .setProperty ("zeppelin.dep.localrepo" , tmpDir .getAbsolutePath () + "/local-repo" );
5267
5368 tmpDir .mkdirs ();
5469
55- if (repl == null ) {
56- Properties p = new Properties ();
70+ if (repl == null ) {
71+ Properties p = new Properties ();
5772
58- repl = new SparkInterpreter (p );
59- repl .open ();
60- }
73+ repl = new SparkInterpreter (p );
74+ repl .open ();
75+ }
6176
62- InterpreterGroup intpGroup = new InterpreterGroup ();
63- context = new InterpreterContext ("id" , "title" , "text" , new HashMap <String , Object >(), new GUI (),
64- new AngularObjectRegistry (intpGroup .getId (), null ),
77+ InterpreterGroup intpGroup = new InterpreterGroup ();
78+ context = new InterpreterContext ("id" , "title" , "text" ,
79+ new HashMap <String , Object >(), new GUI (), new AngularObjectRegistry (
80+ intpGroup .getId (), null ),
6581 new LinkedList <InterpreterContextRunner >());
66- }
82+ }
6783
6884 @ After
6985 public void tearDown () throws Exception {
@@ -83,52 +99,55 @@ else if (file.isDirectory()) {
8399 }
84100 }
85101
86- @ Test
87- public void testBasicIntp () {
88- assertEquals (InterpreterResult .Code .SUCCESS , repl .interpret ("val a = 1\n val b = 2" , context ).code ());
89-
90- // when interpret incomplete expression
91- InterpreterResult incomplete = repl .interpret ("val a = \" \" \" " , context );
92- assertEquals (InterpreterResult .Code .INCOMPLETE , incomplete .code ());
93- assertTrue (incomplete .message ().length ()>0 ); // expecting some error message
94- /*
95- assertEquals(1, repl.getValue("a"));
96- assertEquals(2, repl.getValue("b"));
97- repl.interpret("val ver = sc.version");
98- assertNotNull(repl.getValue("ver"));
99- assertEquals("HELLO\n", repl.interpret("println(\"HELLO\")").message());
100- */
101- }
102-
103- @ Test
104- public void testEndWithComment () {
105- assertEquals (InterpreterResult .Code .SUCCESS , repl .interpret ("val c=1\n //comment" , context ).code ());
106- }
107-
108- @ Test
109- public void testSparkSql (){
110- repl .interpret ("case class Person(name:String, age:Int)\n " , context );
111- repl .interpret ("val people = sc.parallelize(Seq(Person(\" moon\" , 33), Person(\" jobs\" , 51), Person(\" gates\" , 51), Person(\" park\" , 34)))\n " , context );
112- assertEquals (Code .SUCCESS , repl .interpret ("people.take(3)" , context ).code ());
113-
114- // create new interpreter
115- Properties p = new Properties ();
116- SparkInterpreter repl2 = new SparkInterpreter (p );
117- repl2 .open ();
118-
119- repl .interpret ("case class Man(name:String, age:Int)" , context );
120- repl .interpret ("val man = sc.parallelize(Seq(Man(\" moon\" , 33), Man(\" jobs\" , 51), Man(\" gates\" , 51), Man(\" park\" , 34)))" , context );
121- assertEquals (Code .SUCCESS , repl .interpret ("man.take(3)" , context ).code ());
122- repl2 .getSparkContext ().stop ();
123- }
124-
125- @ Test
126- public void testReferencingUndefinedVal (){
127- InterpreterResult result = repl .interpret ("def category(min: Int) = {" +
128- " if (0 <= value) \" error\" " +
129- "}" , context );
130- assertEquals (Code .ERROR , result .code ());
131- }
102+ @ Test
103+ public void testBasicIntp () {
104+ assertEquals (InterpreterResult .Code .SUCCESS ,
105+ repl .interpret ("val a = 1\n val b = 2" , context ).code ());
106+
107+ // when interpret incomplete expression
108+ InterpreterResult incomplete = repl .interpret ("val a = \" \" \" " , context );
109+ assertEquals (InterpreterResult .Code .INCOMPLETE , incomplete .code ());
110+ assertTrue (incomplete .message ().length () > 0 ); // expecting some error
111+ // message
112+ /*
113+ * assertEquals(1, repl.getValue("a")); assertEquals(2, repl.getValue("b"));
114+ * repl.interpret("val ver = sc.version");
115+ * assertNotNull(repl.getValue("ver")); assertEquals("HELLO\n",
116+ * repl.interpret("println(\"HELLO\")").message());
117+ */
118+ }
119+
120+ @ Test
121+ public void testEndWithComment () {
122+ assertEquals (InterpreterResult .Code .SUCCESS , repl .interpret ("val c=1\n //comment" , context ).code ());
123+ }
124+
125+ @ Test
126+ public void testSparkSql (){
127+ repl .interpret ("case class Person(name:String, age:Int)\n " , context );
128+ repl .interpret ("val people = sc.parallelize(Seq(Person(\" moon\" , 33), Person(\" jobs\" , 51), Person(\" gates\" , 51), Person(\" park\" , 34)))\n " , context );
129+ assertEquals (Code .SUCCESS , repl .interpret ("people.take(3)" , context ).code ());
130+
131+
132+ if (getSparkVersionNumber () <= 11 ) { // spark 1.2 or later does not allow create multiple SparkContext in the same jvm by default.
133+ // create new interpreter
134+ Properties p = new Properties ();
135+ SparkInterpreter repl2 = new SparkInterpreter (p );
136+ repl2 .open ();
137+
138+ repl .interpret ("case class Man(name:String, age:Int)" , context );
139+ repl .interpret ("val man = sc.parallelize(Seq(Man(\" moon\" , 33), Man(\" jobs\" , 51), Man(\" gates\" , 51), Man(\" park\" , 34)))" , context );
140+ assertEquals (Code .SUCCESS , repl .interpret ("man.take(3)" , context ).code ());
141+ repl2 .getSparkContext ().stop ();
142+ }
143+ }
144+
145+ @ Test
146+ public void testReferencingUndefinedVal () {
147+ InterpreterResult result = repl .interpret ("def category(min: Int) = {"
148+ + " if (0 <= value) \" error\" " + "}" , context );
149+ assertEquals (Code .ERROR , result .code ());
150+ }
132151
133152 @ Test
134153 public void testZContextDependencyLoading () {
0 commit comments