Search in sources :

Example 1 with PathToJar

use of com.twosigma.beakerx.kernel.PathToJar in project beakerx by twosigma.

the class ClasspathMagicCommandTest method showClasspath.

@Test
public void showClasspath() {
    // given
    kernel.addJarsToClasspath(asList(new PathToJar(CLASSPATH_TO_JAR_PATH)));
    Code code = CodeFactory.create("%classpath", new Message(), kernel);
    // when
    code.execute(kernel, 1);
    // then
    List<Message> std = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
    String text = (String) std.get(0).getContent().get("text");
    assertThat(text).contains(FOO_JAR);
}
Also used : Message(com.twosigma.beakerx.message.Message) PathToJar(com.twosigma.beakerx.kernel.PathToJar) PlainCode(com.twosigma.beakerx.kernel.PlainCode) Code(com.twosigma.beakerx.kernel.Code) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest)

Example 2 with PathToJar

use of com.twosigma.beakerx.kernel.PathToJar in project beakerx by twosigma.

the class SQLEvaluator method configureSqlEvaluator.

protected void configureSqlEvaluator(EvaluatorParameters kernelParameters) {
    SQLKernelParameters params = new SQLKernelParameters(kernelParameters);
    Optional<Collection<String>> cp = params.getClassPath();
    if (cp.isPresent()) {
        if (cp.get() == null || cp.get().isEmpty()) {
            classPath = new Classpath();
        } else {
            for (String line : cp.get()) {
                if (!line.trim().isEmpty()) {
                    classPath.add(new PathToJar(line));
                }
            }
        }
        jdbcClient.loadDrivers(classPath.getPathsAsStrings());
    }
    if (params.defaultDatasource().isPresent()) {
        this.defaultConnectionString = new ConnectionStringHolder(params.defaultDatasource().orElse(""), jdbcClient);
    }
    if (params.datasources().isPresent()) {
        this.namedConnectionString = new HashMap<>();
        Scanner sc = new Scanner(params.datasources().orElse(""));
        while (sc.hasNext()) {
            String line = sc.nextLine();
            int i = line.indexOf('=');
            if (i < 1 || i == line.length() - 1) {
                logger.warn("Error in datasource line, this line will be ignored: {}.", line);
                continue;
            }
            String name = line.substring(0, i).trim();
            String value = line.substring(i + 1).trim();
            if (value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            }
            namedConnectionString.put(name, new ConnectionStringHolder(value, jdbcClient));
        }
    }
}
Also used : Scanner(java.util.Scanner) ClasspathScanner(com.twosigma.beakerx.autocomplete.ClasspathScanner) ConnectionStringHolder(com.twosigma.beakerx.sql.ConnectionStringHolder) Classpath(com.twosigma.beakerx.kernel.Classpath) SQLKernelParameters(com.twosigma.beakerx.sql.kernel.SQLKernelParameters) Collection(java.util.Collection) PathToJar(com.twosigma.beakerx.kernel.PathToJar)

Example 3 with PathToJar

use of com.twosigma.beakerx.kernel.PathToJar in project beakerx by twosigma.

the class ClasspathMagicCommandTest method showClasspathShouldNotContainDuplication.

@Test
public void showClasspathShouldNotContainDuplication() throws Exception {
    // given
    kernel.addJarsToClasspath(asList(new PathToJar(CLASSPATH_TO_JAR_PATH)));
    kernel.clearMessages();
    // when
    kernel.addJarsToClasspath(asList(new PathToJar(CLASSPATH_TO_JAR_PATH)));
    Code code = CodeFactory.create("%classpath", new Message(), kernel);
    code.execute(kernel, 1);
    // then
    List<Message> std = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
    String text = (String) std.get(0).getContent().get("text");
    assertThat(text).contains(FOO_JAR);
}
Also used : Message(com.twosigma.beakerx.message.Message) PathToJar(com.twosigma.beakerx.kernel.PathToJar) PlainCode(com.twosigma.beakerx.kernel.PlainCode) Code(com.twosigma.beakerx.kernel.Code) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest)

Example 4 with PathToJar

use of com.twosigma.beakerx.kernel.PathToJar in project beakerx by twosigma.

the class ClasspathMagicCommand method handlePath.

private Collection<String> handlePath(String path) {
    List<String> addedJarsName = Lists.newLinkedList();
    Path currentPath = Paths.get(path);
    List<Path> paths = this.kernel.addJarsToClasspath(singletonList(new PathToJar(path)));
    if (!paths.isEmpty()) {
        addedJarsName.add(currentPath.getFileName().toString());
    }
    return addedJarsName;
}
Also used : Path(java.nio.file.Path) PathToJar(com.twosigma.beakerx.kernel.PathToJar)

Example 5 with PathToJar

use of com.twosigma.beakerx.kernel.PathToJar in project beakerx by twosigma.

the class GroovyEvaluatorMagicCommandsTest method addJarToClasspath.

@Test
public void addJarToClasspath() throws Exception {
    // given
    String code = "" + "import com.example.Demo;\n" + "Demo demo = new Demo();\n" + "demo.getObjectTest()\n";
    TryResult seo = runCode(code);
    assertThat(seo.error()).contains("unable to resolve class");
    // when
    PathToJar path = new PathToJar(SRC_TEST_RESOURCES + "demo.jar");
    groovyEvaluator.addJarsToClasspath(singletonList(path));
    // then
    TryResult seo2 = runCode(code);
    assertThat(seo2.result()).isEqualTo("Demo_test_123");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) PathToJar(com.twosigma.beakerx.kernel.PathToJar) Test(org.junit.Test)

Aggregations

PathToJar (com.twosigma.beakerx.kernel.PathToJar)5 Test (org.junit.Test)3 KernelTest (com.twosigma.beakerx.KernelTest)2 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)2 Code (com.twosigma.beakerx.kernel.Code)2 PlainCode (com.twosigma.beakerx.kernel.PlainCode)2 Message (com.twosigma.beakerx.message.Message)2 TryResult (com.twosigma.beakerx.TryResult)1 ClasspathScanner (com.twosigma.beakerx.autocomplete.ClasspathScanner)1 Classpath (com.twosigma.beakerx.kernel.Classpath)1 ConnectionStringHolder (com.twosigma.beakerx.sql.ConnectionStringHolder)1 SQLKernelParameters (com.twosigma.beakerx.sql.kernel.SQLKernelParameters)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 Scanner (java.util.Scanner)1