diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java index 9106cf5f17c..de4ea4059e5 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java @@ -30,7 +30,6 @@ import java.util.concurrent.atomic.AtomicReference; import com.google.gson.Gson; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,6 +55,11 @@ import org.apache.zeppelin.user.AuthenticationInfo; import org.apache.zeppelin.user.Credentials; +import static org.apache.commons.lang.StringUtils.EMPTY; +import static org.apache.commons.lang.StringUtils.isEmpty; +import static org.apache.commons.lang.StringUtils.isNotEmpty; +import static org.apache.commons.lang.StringUtils.isBlank; + /** * Binded interpreters for a note */ @@ -76,7 +80,7 @@ public class Note implements Serializable, ParagraphJobListener { private String name = ""; private String id; - private AtomicReference lastReplName = new AtomicReference<>(StringUtils.EMPTY); + private AtomicReference lastReplName = new AtomicReference<>(EMPTY); private transient ZeppelinConfiguration conf = ZeppelinConfiguration.create(); private Map> angularObjects = new HashMap<>(); @@ -122,7 +126,7 @@ private void generateId() { private String getDefaultInterpreterName() { InterpreterSetting setting = factory.getDefaultInterpreterSetting(getId()); - return null != setting ? setting.getName() : StringUtils.EMPTY; + return null != setting ? setting.getName() : EMPTY; } void putDefaultReplName() { @@ -275,13 +279,17 @@ public Paragraph insertParagraph(int index) { */ private void addLastReplNameIfEmptyText(Paragraph p) { String replName = lastReplName.get(); - if (StringUtils.isEmpty(p.getText()) && StringUtils.isNotEmpty(replName)) { + if (isEmpty(p.getText()) && isNotEmpty(replName) && isBinding(replName)) { p.setText(getInterpreterName(replName) + " "); } } + public boolean isBinding(String replName) { + return factory.getInterpreter(this.getId(), replName) != null; + } + private String getInterpreterName(String replName) { - return StringUtils.isBlank(replName) ? StringUtils.EMPTY : "%" + replName; + return isBlank(replName) ? EMPTY : "%" + replName; } /** @@ -569,7 +577,7 @@ public void persist(AuthenticationInfo subject) throws IOException { } private void setLastReplName(Paragraph lastParagraphStarted) { - if (StringUtils.isNotEmpty(lastParagraphStarted.getRequiredReplName())) { + if (isNotEmpty(lastParagraphStarted.getRequiredReplName())) { lastReplName.set(lastParagraphStarted.getRequiredReplName()); } } diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NoteTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NoteTest.java index cff66adc78d..fda4e4e3249 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NoteTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NoteTest.java @@ -23,6 +23,7 @@ import org.apache.zeppelin.interpreter.Interpreter; import org.apache.zeppelin.interpreter.InterpreterFactory; import org.apache.zeppelin.interpreter.InterpreterSetting; +import org.apache.zeppelin.interpreter.mock.MockInterpreter2; import org.apache.zeppelin.notebook.repo.NotebookRepo; import org.apache.zeppelin.scheduler.Scheduler; import org.apache.zeppelin.search.SearchService; @@ -138,6 +139,7 @@ public void addParagraphWithLastReplName() { Note note = new Note(repo, interpreterFactory, jobListenerFactory, index, credentials, noteEventListener); note.putDefaultReplName(); //set lastReplName + when(interpreterFactory.getInterpreter(note.getId(), "spark")).thenReturn(new MockInterpreter2(null)); Paragraph p = note.addParagraph(); @@ -153,6 +155,7 @@ public void insertParagraphWithLastReplName() { Note note = new Note(repo, interpreterFactory, jobListenerFactory, index, credentials, noteEventListener); note.putDefaultReplName(); //set lastReplName + when(interpreterFactory.getInterpreter(note.getId(), "spark")).thenReturn(new MockInterpreter2(null)); Paragraph p = note.insertParagraph(note.getParagraphs().size()); @@ -171,4 +174,20 @@ public void setLastReplName() { assertEquals("spark", note.getLastReplName()); } + + @Test + public void isBindingTest() { + Note note = spy(new Note()); + when(note.getId()).thenReturn("test1"); + InterpreterFactory mockInterpreterFactory = mock(InterpreterFactory.class); + note.setInterpreterFactory(mockInterpreterFactory); + + //when is not binding + assertFalse(note.isBinding("spark")); + + //when is binding + when(mockInterpreterFactory.getInterpreter("test1", "spark")). + thenReturn(new MockInterpreter2(null)); + assertTrue(note.isBinding("spark")); + } }