BigQueryStrings { public static TableReference parseTableReference(String tableRefString) { TableReference tableRef = new TableReference(); int projectIdEnd = tableRefString.lastIndexOf(':'); String datasetAndTableString = tableRefString; if (projectIdEnd != -1) { tableRef.setProjectId(tableRefString.substring(0, projectIdEnd)); datasetAndTableString = tableRefString.substring(projectIdEnd + 1); } Preconditions.checkArgument(datasetAndTableString.matches(DATASET_AND_TABLE_REGEX), "Invalid datasetAndTableString '%s'; must match regex '%s'.", datasetAndTableString, DATASET_AND_TABLE_REGEX); List<String> idParts = DOT_SPLITTER.splitToList(datasetAndTableString); tableRef.setDatasetId(idParts.get(0)); tableRef.setTableId(idParts.get(1)); return tableRef; }  static String toString(TableReference tableRef); static TableReference parseTableReference(String tableRefString); static final String DATASET_AND_TABLE_REGEX; }
@Test public void testParseTableReferenceNoProject() { TableReference tableRef = BigQueryStrings.parseTableReference("fooA1_.2bar"); assertThat(tableRef.getProjectId()).isNull(); assertThat(tableRef.getDatasetId()).isEqualTo("fooA1_"); assertThat(tableRef.getTableId()).isEqualTo("2bar"); }
@Test public void testParseTableReferenceWithProject() { TableReference tableRef = BigQueryStrings.parseTableReference("google.com:foo-proj:foo.bar"); assertThat(tableRef.getProjectId()).isEqualTo("google.com:foo-proj"); assertThat(tableRef.getDatasetId()).isEqualTo("foo"); assertThat(tableRef.getTableId()).isEqualTo("bar"); }
@Test public void testParseTableReferenceThrowsWhenDashesArePresent() { assertThrows( IllegalArgumentException.class, () -> BigQueryStrings.parseTableReference("foo-o.bar")); }
@Test public void testParseTableReferenceThrowsWhenNoDotsPresent() { assertThrows(IllegalArgumentException.class, () -> BigQueryStrings.parseTableReference("foo")); }
@Test public void testParseTableReferenceThrowsWhenOnlyOneDotPresent() { assertThrows( IllegalArgumentException.class, () -> BigQueryStrings.parseTableReference("p.foo:bar")); }
@Test public void testParseTableReferenceThrowsWhenDatasetIsEmpty() { assertThrows(IllegalArgumentException.class, () -> BigQueryStrings.parseTableReference("foo.")); }
@Test public void testParseTableReferenceThrowsWhenTableIsEmpty() { assertThrows(IllegalArgumentException.class, () -> BigQueryStrings.parseTableReference(".bar")); }