DALBackendHandlerFactory { public static TextProtocolBackendHandler newInstance(final String sql, final DALStatement dalStatement, final BackendConnection backendConnection) { if (dalStatement instanceof MySQLUseStatement) { return new UseDatabaseBackendHandler((MySQLUseStatement) dalStatement, backendConnection); } if (dalStatement instanceof MySQLShowDatabasesStatement) { return new ShowDatabasesBackendHandler(backendConnection); } if (dalStatement instanceof MySQLShowTablesStatement) { return new ShowTablesBackendHandler(sql, dalStatement, backendConnection); } if (dalStatement instanceof SetStatement) { return new BroadcastBackendHandler(sql, dalStatement, backendConnection); } return new UnicastBackendHandler(sql, dalStatement, backendConnection); }  static TextProtocolBackendHandler newInstance(final String sql, final DALStatement dalStatement, final BackendConnection backendConnection);  }
@Test public void assertShowTablesBackendHandlerReturnedWhenMySQLShowTablesStatement() { MySQLShowTablesStatement mySQLShowTablesStatement = mock(MySQLShowTablesStatement.class); BackendConnection backendConnection = mock(BackendConnection.class); TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mySQLShowTablesStatement, backendConnection); assertThat(textProtocolBackendHandler, instanceOf(ShowTablesBackendHandler.class)); ShowTablesBackendHandler showTablesBackendHandler = (ShowTablesBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(showTablesBackendHandler, "sqlStatement", is(mySQLShowTablesStatement)); assertFieldOfInstance(showTablesBackendHandler, "sql", is("")); assertFieldOfInstance(showTablesBackendHandler, "backendConnection", is(backendConnection)); }
@Test public void assertUseDatabaseBackendHandlerReturnedWhenMySQLUseStatement() { MySQLUseStatement mySQLUseStatement = mock(MySQLUseStatement.class); BackendConnection backendConnection = mock(BackendConnection.class); TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mySQLUseStatement, backendConnection); assertThat(textProtocolBackendHandler, instanceOf(UseDatabaseBackendHandler.class)); UseDatabaseBackendHandler useDatabaseBackendHandler = (UseDatabaseBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(useDatabaseBackendHandler, "useStatement", is(mySQLUseStatement)); assertFieldOfInstance(useDatabaseBackendHandler, "backendConnection", is(backendConnection)); }
@Test public void assertShowDatabasesBackendHandlerReturnedWhenMySQLShowDatabasesStatement() { BackendConnection backendConnection = mock(BackendConnection.class); TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mock(MySQLShowDatabasesStatement.class), backendConnection); assertThat(textProtocolBackendHandler, instanceOf(ShowDatabasesBackendHandler.class)); ShowDatabasesBackendHandler showDatabasesBackendHandler = (ShowDatabasesBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(showDatabasesBackendHandler, "backendConnection", is(backendConnection)); }
@Test public void assertBroadcastBackendHandlerReturnedWhenSetStatement() { SetStatement setStatement = mock(SetStatement.class); BackendConnection backendConnection = mock(BackendConnection.class); TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", setStatement, backendConnection); assertThat(textProtocolBackendHandler, instanceOf(BroadcastBackendHandler.class)); BroadcastBackendHandler broadcastBackendHandler = (BroadcastBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(broadcastBackendHandler, "sqlStatement", is(setStatement)); assertFieldOfInstance(broadcastBackendHandler, "sql", is("")); assertFieldOfInstance(broadcastBackendHandler, "backendConnection", is(backendConnection)); }
@Test public void assertUnicastBackendHandlerReturnedWhenOtherDALStatement() { DALStatement dalStatement = mock(DALStatement.class); BackendConnection backendConnection = mock(BackendConnection.class); TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", dalStatement, backendConnection); assertThat(textProtocolBackendHandler, instanceOf(UnicastBackendHandler.class)); UnicastBackendHandler unicastBackendHandler = (UnicastBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(unicastBackendHandler, "sqlStatement", is(dalStatement)); assertFieldOfInstance(unicastBackendHandler, "sql", is("")); assertFieldOfInstance(unicastBackendHandler, "backendConnection", is(backendConnection)); }