/// *
// * Copyright 1999-2012 Alibaba Group.
// *
// * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
/// except
// * in compliance with the License. You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software distributed under the
/// License
// * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
/// express
// * or implied. See the License for the specific language governing permissions and limitations
/// under
// * the License.
// */
/// **
// * (created at 2012-5-30)
// */
// package fm.liu.timo.route;
//
// import java.sql.SQLSyntaxErrorException;
// import java.util.ArrayList;
// import java.util.Collection;
// import java.util.HashMap;
// import java.util.HashSet;
// import java.util.List;
// import java.util.Map;
// import java.util.NoSuchElementException;
// import java.util.Set;
//
// import fm.liu.timo.config.loader.SchemaLoader;
// import fm.liu.timo.config.loader.xml.XMLSchemaLoader;
// import fm.liu.timo.config.model.SchemaConfig;
// import fm.liu.timo.config.util.ConfigException;
// import fm.liu.timo.parser.ast.expression.primary.RowExpression;
// import fm.liu.timo.parser.ast.stmt.SQLStatement;
// import fm.liu.timo.parser.ast.stmt.dml.DMLInsertStatement;
// import fm.liu.timo.parser.recognizer.SQLParserDelegate;
// import fm.liu.timo.route.RouteResultset;
// import fm.liu.timo.route.RouteResultsetNode;
// import fm.liu.timo.route.ServerRouter;
// import fm.liu.timo.route.config.RouteRuleInitializer;
// import fm.liu.timo.route.util.PermutationUtil.PermutationGenerator;
// import junit.framework.Assert;
//
/// **
// * @author <a href="mailto:shuo.qius@alibaba-inc.com">QIU Shuo</a>
// */
// public class ServerRouteTest extends AbstractAliasConvert {
//
// protected Map<String, SchemaConfig> schemaMap;
//
// public ServerRouteTest() {
// String schemaFile = "/route/schema.xml";
// String ruleFile = "/route/rule.xml";
// SchemaLoader schemaLoader = new XMLSchemaLoader(schemaFile, ruleFile);
// try {
// RouteRuleInitializer.initRouteRule(schemaLoader);
// } catch (SQLSyntaxErrorException e) {
// throw new ConfigException(e);
// } catch (RuntimeException ee) {
// throw ee;
// }
// schemaMap = schemaLoader.getSchemas();
// }
//
// protected void setUp() throws Exception {
// // super.setUp();
// // schemaMap = TimoServer.getInstance().getConfig().getSchemas();
// }
//
// public void testRouteInsertShort() throws Exception {
// String sql = "inSErt into offer_detail (`offer_id`, gmt) values (123,now())";
// SchemaConfig schema = schemaMap.get("cndb");
// RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("detail_dn[15]", rrs.getNodes()[0].getName());
// Assert.assertEquals("inSErt into offer_detail (`offer_id`, gmt) values (123,now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "inSErt into offer_detail ( gmt) values (now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(128, rrs.getNodes().length);
//
// sql = "inSErt into offer_detail (offer_id, gmt) values (123,now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("detail_dn[15]", rrs.getNodes()[0].getName());
// Assert.assertEquals("inSErt into offer_detail (offer_id, gmt) values (123,now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert into offer(group_id,offer_id,member_id)values(234,123,'abc')";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[12]", rrs.getNodes()[0].getName());
// Assert.assertEquals("insert into offer(group_id,offer_id,member_id)values(234,123,'abc')",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert into offer (group_id, offer_id, gmt) values (234,123,now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
// Assert.assertEquals("insert into offer (group_id, offer_id, gmt) values (234,123,now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert into offer (offer_id, group_id, gmt) values (123,234,now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
// Assert.assertEquals("insert into offer (offer_id, group_id, gmt) values (123,234,now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert into offer (offer_id, group_id, gmt) values (234,123,now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
// Assert.assertEquals("insert into offer (offer_id, group_id, gmt) values (234,123,now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert into wp_image (member_id,gmt) values ('pavarotti17',now())";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
// Assert.assertEquals("insert into wp_image (member_id,gmt) values ('pavarotti17',now())",
// rrs.getNodes()[0].getStatement());
//
// sql = "insert low_priority into offer set offer_id=123, group_id=234,gmt=now() on duplicate key
/// update `dual`=1";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
// Assert.assertEquals(
// "insert low_priority into offer set offer_id=123, group_id=234,gmt=now() on duplicate key update
/// `dual`=1",
// rrs.getNodes()[0].getStatement());
//
// sql = "update ignore wp_image set name='abc',gmt=now()where `select`='abc'";
// schema = schemaMap.get("cndb");
// rrs = ServerRouter.route(schema, sql, null, null);
// Assert.assertEquals(1, rrs.getNodes().length);
// Assert.assertEquals(-1l, rrs.getLimitSize());
// Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX,
// rrs.getNodes()[0].getReplicaIndex());
// Assert.assertEquals("offer_dn[12]", rrs.getNodes()[0].getName());
// Assert.assertEqu