From fe82d5541ee6f3dac66c359b75d55c33158b45b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B0=91?= Date: Sun, 6 May 2012 22:22:28 +0800 Subject: [PATCH] add test --- .../bvt/mapping/MySqlMappingShardingTest.java | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java index bea73396ce..c7d1c519c0 100644 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java +++ b/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java @@ -1,5 +1,8 @@ package com.alibaba.druid.bvt.mapping; +import java.util.Collections; + +import junit.framework.Assert; import junit.framework.TestCase; import com.alibaba.druid.mapping.Entity; @@ -11,8 +14,8 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; import com.alibaba.druid.sql.ast.statement.SQLTableSource; - public class MySqlMappingShardingTest extends TestCase { + MappingEngine engine = new ShardingMappingEngine(); protected void setUp() throws Exception { @@ -25,23 +28,52 @@ public class MySqlMappingShardingTest extends TestCase { engine.addEntity(entity); } - - public void test_0 () throws Exception { + + public void test_0() throws Exception { String oql = "select * from 用户 u where u.名称 = 'a'"; - + String sql = engine.explainToSelectSQL(oql); + + String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + // + "FROM user_a u\n" + // + "WHERE u.uid = 'a'"; - System.out.println(sql); + Assert.assertEquals(expected, sql); } + public void test_1() throws Exception { + String oql = "select * from 用户 u where u.名称 = ?"; + + String sql = engine.explainToSelectSQL(oql, Collections.singletonList("a")); + + String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + // + "FROM user_a u\n" + // + "WHERE u.uid = ?"; + + Assert.assertEquals(expected, sql); + } + + public void test_2() throws Exception { + String oql = "select * from 用户 u where u.名称 = ?"; + + String sql = engine.explainToSelectSQL(oql, Collections.singletonList("b")); + + String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + // + "FROM user_x u\n" + // + "WHERE u.uid = ?"; + + Assert.assertEquals(expected, sql); + } + public static class ShardingMappingEngine extends MappingEngine { + public void afterResole(MappingVisitor visitor) { String shardingTableName = null; for (PropertyValue entry : visitor.getPropertyValues()) { Entity entity = entry.getEntity(); Property property = entry.getProperty(); Object value = entry.getValue(); - + if ("用户".equals(entity.getName()) && "名称".equals(property.getName())) { if ("a".equals(value)) { shardingTableName = "user_a"; @@ -50,7 +82,7 @@ public class MySqlMappingShardingTest extends TestCase { } } } - + for (SQLTableSource tableSource : visitor.getTableSources().values()) { Entity entity = (Entity) tableSource.getAttribute("mapping.entity"); if (entity != null && "用户".equals(entity.getName())) {