UseDatabaseBackendHandler implements TextProtocolBackendHandler { @Override public BackendResponse execute() { String schema = SQLUtil.getExactlyValue(useStatement.getSchema()); if (ProxyContext.getInstance().schemaExists(schema) && isAuthorizedSchema(schema)) { backendConnection.setCurrentSchema(schema); return new UpdateResponse(); } throw new UnknownDatabaseException(schema); }  @Override BackendResponse execute(); @Override boolean next(); @Override QueryData getQueryData();  }
@Test public void assertExecuteUseStatementBackendHandler() { MySQLUseStatement useStatement = mock(MySQLUseStatement.class); when(useStatement.getSchema()).thenReturn(String.format(SCHEMA_PATTERN, 0)); UseDatabaseBackendHandler useSchemaBackendHandler = new UseDatabaseBackendHandler(useStatement, backendConnection); BackendResponse actual = useSchemaBackendHandler.execute(); verify(backendConnection).setCurrentSchema(anyString()); assertThat(actual, instanceOf(UpdateResponse.class)); }
@Test(expected = UnknownDatabaseException.class) public void assertExecuteUseStatementNotExist() { MySQLUseStatement useStatement = mock(MySQLUseStatement.class); when(useStatement.getSchema()).thenReturn("not_exist"); UseDatabaseBackendHandler useSchemaBackendHandler = new UseDatabaseBackendHandler(useStatement, backendConnection); useSchemaBackendHandler.execute(); }