TableIdParser { public static List<String> parse(String identifier) { TokenStream stream = new TokenStream(identifier, new TableIdTokenizer(identifier), true); stream.start(); List<String> parts = new ArrayList<>(); while (stream.hasNext()) { parts.add(stream.consume().replaceAll("''", "'").replaceAll("\"\"", "\"").replaceAll("``", "`")); } return parts; }  static List<String> parse(String identifier);  }
@Test public void canParseValidIdentifiers() { assertThat(TableIdParser.parse("s.a")).containsExactly("s", "a"); assertThat(TableIdParser.parse("s2.a")).containsExactly("s2", "a"); assertThat(TableIdParser.parse("table")).containsExactly("table"); assertThat(TableIdParser.parse(" table ")).containsExactly("table"); assertThat(TableIdParser.parse("schema.table")).containsExactly("schema", "table"); assertThat(TableIdParser.parse(" schema . table ")).containsExactly("schema", "table"); assertThat(TableIdParser.parse("catalog.schema.table")).containsExactly("catalog", "schema", "table"); assertThat(TableIdParser.parse("catalog . schema . table")).containsExactly("catalog", "schema", "table"); assertThat(TableIdParser.parse("\"table\"")).containsExactly("table"); assertThat(TableIdParser.parse("\"ta.ble\"")).containsExactly("ta.ble"); assertThat(TableIdParser.parse("\"ta ble\"")).containsExactly("ta ble"); assertThat(TableIdParser.parse("\"schema\".\"table\"")).containsExactly("schema", "table"); assertThat(TableIdParser.parse("\"cata . log\" . \"sche . ma\" . \"ta . ble\"")).containsExactly("cata . log", "sche . ma", "ta . ble"); assertThat(TableIdParser.parse("\"tab\"\"le\"")).containsExactly("tab\"le"); assertThat(TableIdParser.parse("\"tab\"\"\"\"le\"")).containsExactly("tab\"\"le"); assertThat(TableIdParser.parse("\"\"\"s\"\"\".\"\"\"a\"\"\"")).containsExactly("\"s\"", "\"a\""); }
@Test(expected = IllegalArgumentException.class) public void leadingSeparatorIsInvalid() { TableIdParser.parse(".table"); }
@Test(expected = IllegalArgumentException.class) public void trailingSeparatorIsInvalid() { TableIdParser.parse("table."); }
@Test(expected = IllegalArgumentException.class) public void unclosedQuotingCharIsInvalid() { TableIdParser.parse("\"table"); }
@Test(expected = IllegalArgumentException.class) public void escapedQuoteDoesntCloseQuotedIdentifier() { TableIdParser.parse("\"table\"\""); }