ParagraphTextParser { public static ParseResult parse(String text) { Map<String, String> localProperties = new HashMap<>(); String intpText = ""; String scriptText = null; Matcher matcher = REPL_PATTERN.matcher(text); if (matcher.find()) { String headingSpace = matcher.group(1); intpText = matcher.group(2); int startPos = headingSpace.length() + intpText.length() + 1; if (startPos < text.length() && text.charAt(startPos) == '(') { startPos = parseLocalProperties(text, startPos, localProperties); } scriptText = text.substring(startPos).trim(); } else { intpText = ""; scriptText = text.trim(); } return new ParseResult(intpText, scriptText, localProperties); }  static ParseResult parse(String text);  }
@Test public void testJupyter() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%jupyter(kernel=ir)"); assertEquals("jupyter", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("ir", parseResult.getLocalProperties().get("kernel")); assertEquals("", parseResult.getScriptText()); }
@Test public void testCassandra() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse( "%cassandra(locale=ru_RU, timeFormat=\"E, d MMM yy\", floatPrecision = 5, output=cql)\n" + "select * from system_auth.roles;"); assertEquals("cassandra", parseResult.getIntpText()); assertEquals(4, parseResult.getLocalProperties().size()); assertEquals("E, d MMM yy", parseResult.getLocalProperties().get("timeFormat")); assertEquals("select * from system_auth.roles;", parseResult.getScriptText()); }
@Test public void testParagraphTextLocalPropertiesAndText() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%spark.pyspark(pool=pool_1) sc.version"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("pool_1", parseResult.getLocalProperties().get("pool")); assertEquals("sc.version", parseResult.getScriptText()); }
@Test public void testParagraphTextLocalPropertiesNoText() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%spark.pyspark(pool=pool_1)"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("pool_1", parseResult.getLocalProperties().get("pool")); assertEquals("", parseResult.getScriptText()); }
@Test public void testParagraphTextLocalPropertyNoValueNoText() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%spark.pyspark(pool)"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("pool", parseResult.getLocalProperties().get("pool")); assertEquals("", parseResult.getScriptText()); }
@Test public void testParagraphTextNoLocalProperties() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%spark.pyspark sc.version"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(0, parseResult.getLocalProperties().size()); assertEquals("sc.version", parseResult.getScriptText()); }
@Test public void testParagraphNoInterpreter() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("sc.version"); assertEquals("", parseResult.getIntpText()); assertEquals(0, parseResult.getLocalProperties().size()); assertEquals("sc.version", parseResult.getScriptText()); }
@Test public void testParagraphInterpreterWithoutProperties() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse("%spark() sc.version"); assertEquals("spark", parseResult.getIntpText()); assertEquals(0, parseResult.getLocalProperties().size()); assertEquals("sc.version", parseResult.getScriptText()); }
@Test public void testParagraphTextQuotedPropertyValue1() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse( "%spark.pyspark(pool=\"value with = inside\")"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("value with = inside", parseResult.getLocalProperties().get("pool")); assertEquals("", parseResult.getScriptText()); }
@Test public void testParagraphTextQuotedPropertyValue2() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse( "%spark.pyspark(pool=\"value with \\\" inside\", p=\"eol\\ninside\" )"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(2, parseResult.getLocalProperties().size()); assertEquals("value with \" inside", parseResult.getLocalProperties().get("pool")); assertEquals("eol\ninside", parseResult.getLocalProperties().get("p")); assertEquals("", parseResult.getScriptText()); }
@Test public void testParagraphTextQuotedPropertyKeyAndValue() { ParagraphTextParser.ParseResult parseResult = ParagraphTextParser.parse( "%spark.pyspark(\"po ol\"=\"value with \\\" inside\")"); assertEquals("spark.pyspark", parseResult.getIntpText()); assertEquals(1, parseResult.getLocalProperties().size()); assertEquals("value with \" inside", parseResult.getLocalProperties().get("po ol")); assertEquals("", parseResult.getScriptText()); }
@Test public void testParagraphTextUnfinishedConfig() { exceptionRule.expect(RuntimeException.class); exceptionRule.expectMessage( "Problems by parsing paragraph. Not finished interpreter configuration"); ParagraphTextParser.parse("%spark.pyspark(pool="); }
@Test public void testParagraphTextUnfinishedQuote() { exceptionRule.expect(RuntimeException.class); exceptionRule.expectMessage( "Problems by parsing paragraph. Not finished interpreter configuration"); ParagraphTextParser.parse("%spark.pyspark(pool=\"2314234) sc.version"); }
@Test public void testParagraphTextUnclosedBackslash() { exceptionRule.expect(RuntimeException.class); exceptionRule.expectMessage( "Problems by parsing paragraph. Unfinished escape sequence"); ParagraphTextParser.parse("%spark.pyspark(pool=\\"); }
@Test public void testParagraphTextEmptyKey() { exceptionRule.expect(RuntimeException.class); exceptionRule.expectMessage( "Problems by parsing paragraph. Local property key is empty"); ParagraphTextParser.parse("%spark.pyspark(pool=123, ,)"); }