View Javadoc
1   /**
2    * 
3    */
4   package org.melati.poem.dbms.test;
5   
6   import org.melati.poem.BigDecimalPoemType;
7   import org.melati.poem.BinaryPoemType;
8   import org.melati.poem.BooleanPoemType;
9   import org.melati.poem.DatePoemType;
10  import org.melati.poem.DoublePoemType;
11  import org.melati.poem.IntegerPoemType;
12  import org.melati.poem.LongPoemType;
13  import org.melati.poem.StringPoemType;
14  import org.melati.poem.TimestampPoemType;
15  import org.melati.poem.dbms.DbmsFactory;
16  
17  /**
18   * @author timp
19   * @since 23 Jan 2007
20   *
21   */
22  public class MySQLTest extends DbmsSpec {
23  
24    public MySQLTest(String name) {
25      super(name);
26    }
27  
28    protected void setUp() throws Exception {
29      super.setUp();
30    }
31  
32    protected void tearDown() throws Exception {
33      super.tearDown();
34    }
35  
36    protected void setObjectUnderTest() {
37      it = DbmsFactory.getDbms("org.melati.poem.dbms.MySQL");
38    }
39  
40    /**
41     * Test method for {@link org.melati.poem.dbms.Dbms#createTableOptionsSql()}.
42     */
43    public void testCreateTableOptionsSql() {
44      assertEquals(" ENGINE='InnoDB' ", it.createTableOptionsSql());
45    }
46    
47    /**
48     * Test method for {@link org.melati.poem.dbms.Dbms#
49     * getStringSqlDefinition(java.lang.String)}.
50     */
51    public void testGetStringSqlDefinition() throws Exception {
52      assertEquals("VARCHAR(0)", it.getStringSqlDefinition(0));    
53      assertEquals("text",  it.getStringSqlDefinition(-1));
54    }
55    /**
56     * Test method for {@link org.melati.poem.dbms.Dbms#
57     * getSqlDefinition(java.lang.String)}.
58     * @throws Exception 
59     */
60    public void testGetSqlDefinition() throws Exception {
61      assertEquals("bool", it.getSqlDefinition("BOOLEAN"));
62      assertEquals("DOUBLE PRECISION", it.getSqlDefinition("DOUBLE PRECISION"));
63      assertEquals("INT8", it.getSqlDefinition("INT8"));
64      assertEquals("Big Decimal", it.getSqlDefinition("Big Decimal"));
65    }
66  
67  
68    /**
69     * Test method for {@link org.melati.poem.dbms.Dbms#
70     * getBinarySqlDefinition(int)}.
71     */
72    public void testGetBinarySqlDefinition() throws Exception {
73      assertEquals("BLOB", it.getBinarySqlDefinition(0));        
74    }
75  
76    /**
77     * Test method for {@link org.melati.poem.dbms.Dbms#
78     * getIndexLength(org.melati.poem.Column)}.
79     * @throws Exception 
80     */
81    public void testGetIndexLength() throws Exception {
82      assertEquals("", it.getIndexLength(getDb().getUserTable().troidColumn()));
83      assertEquals("(30)", it.getIndexLength(getDb().getTableInfoTable().getDescriptionColumn()));
84    }
85  
86    /**
87     * Test method for {@link org.melati.poem.dbms.Dbms#
88     * canRepresent(org.melati.poem.PoemType, org.melati.poem.PoemType)}.
89     */
90    public void testCanRepresent() {
91      assertNull(it.canRepresent(StringPoemType.nullableInstance, IntegerPoemType.nullableInstance));
92      assertNull(it.canRepresent(IntegerPoemType.nullableInstance,StringPoemType.nullableInstance));
93  
94      assertNull(it.canRepresent(new BigDecimalPoemType(false),new BigDecimalPoemType(true)));
95      assertTrue(it.canRepresent(new BigDecimalPoemType(true),new BigDecimalPoemType(false))
96                 instanceof BigDecimalPoemType);
97  
98      assertNull(it.canRepresent(new StringPoemType(true, 255), new StringPoemType(true, -1)));
99  
100     assertTrue(it.canRepresent(
101             new StringPoemType(true, -1), new StringPoemType(true, -1)) 
102             instanceof StringPoemType);
103 
104     assertNull(it.canRepresent(new TimestampPoemType(true), new DatePoemType(true))); 
105 
106     assertTrue(it.canRepresent(
107             new BooleanPoemType(true), new BooleanPoemType(false)) 
108             instanceof BooleanPoemType);
109 
110     assertTrue(it.canRepresent(
111             new IntegerPoemType(true), new BooleanPoemType(false)) 
112             instanceof BooleanPoemType);
113 
114     assertTrue(it.canRepresent(
115             new IntegerPoemType(false), new BooleanPoemType(false)) 
116             instanceof BooleanPoemType);
117 
118     assertNull(it.canRepresent(new DoublePoemType(false), new BigDecimalPoemType(true)));
119 
120     assertNull(it.canRepresent(new DoublePoemType(true), new BigDecimalPoemType(false))); 
121 
122     assertNull(it.canRepresent(new IntegerPoemType(false), new LongPoemType(true)));
123 
124     
125     assertNull(it.canRepresent(new BinaryPoemType(false,10), new BinaryPoemType(true,10))); 
126     assertNull(it.canRepresent(new BinaryPoemType(true,10), new BinaryPoemType(true,11))); 
127     assertTrue(it.canRepresent(
128             new BinaryPoemType(true,-1), 
129             new BinaryPoemType(true,-1)) instanceof BinaryPoemType); 
130     assertTrue(it.canRepresent(
131             new BinaryPoemType(true,2500), 
132             new BinaryPoemType(true,10)) instanceof BinaryPoemType); 
133  
134   
135   }
136   
137   /**
138    * Test method for {@link org.melati.poem.dbms.Dbms#
139    * getForeignKeyDefinition(java.lang.String, java.lang.String, 
140    * java.lang.String, java.lang.String, java.lang.String)}.
141    */
142   public void testGetForeignKeyDefinition() {
143     assertEquals(" ADD FOREIGN KEY (user) REFERENCES user(id) ON DELETE CASCADE",
144             it.getForeignKeyDefinition("test", "user", "user", "id", "delete"));
145   }
146   
147   /**
148    * Test method for {@link org.melati.poem.dbms.Dbms#
149    * getPrimaryKeyDefinition(java.lang.String)}.
150    */
151   public void testGetPrimaryKeyDefinition() {
152     assertEquals(" ADD PRIMARY KEY (name)", it.getPrimaryKeyDefinition("name"));
153   }
154   
155   /**
156    * Test method for {@link org.melati.poem.dbms.Dbms#
157    * givesCapabilitySQL(java.lang.Integer, java.lang.String)}.
158    */
159   public void testGivesCapabilitySQL() {
160     String actual = it.givesCapabilitySQL(new Integer(42), "hello");
161     String expected = "SELECT " + it.getQuotedName("groupMembership") + ".* "
162             + "FROM " + it.getQuotedName("groupMembership") + " LEFT JOIN "
163             + it.getQuotedName("groupCapability") + " ON "
164             + it.getQuotedName("groupMembership") + "."
165             + it.getQuotedName("group") + " =  "
166             + it.getQuotedName("groupCapability") + "."
167             + it.getQuotedName("group") + " WHERE " + it.getQuotedName("user")
168             + " = 42" + " " + "AND " + it.getQuotedName("groupCapability")
169             + "." + it.getQuotedName("group") + " IS NOT NULL " + "AND "
170             + it.getQuotedName("capability") + " = hello";
171 
172     assertEquals(expected, actual);
173 
174   }
175   /**
176    * Test method for {@link org.melati.poem.dbms.Dbms#
177    * caseInsensitiveRegExpSQL(java.lang.String, java.lang.String)}.
178    */
179   public void testCaseInsensitiveRegExpSQL() {
180     String expected = "a LIKE '%b%'";
181     String actual = it.caseInsensitiveRegExpSQL("a", "b");
182     assertEquals(expected, actual);
183   }
184 
185   public void testCaseInsensitiveRegExpSQLQuoted() {
186     String expected = "a LIKE '%b%'";
187     String actual = it.caseInsensitiveRegExpSQL("a", "\"b\"");
188     assertEquals(expected, actual);
189   }
190 
191   public void testCaseInsensitiveRegExpSQLBlank() {
192     String expected = " LIKE '%%'";
193     String actual = it.caseInsensitiveRegExpSQL("", "");
194     assertEquals(expected, actual);
195   }
196 
197   /**
198    * Test method for {@link org.melati.poem.dbms.Dbms#createTableSql}.
199    */
200   public void testCreateTableSql() {
201     if (getDb().getDbms() == it)
202       assertEquals("CREATE TABLE user (id INT NOT NULL, " + 
203               "name VARCHAR(60) NOT NULL, " + 
204               "login VARCHAR(255) NOT NULL, " + 
205               "password VARCHAR(20) NOT NULL) TYPE='InnoDB' ", 
206               it.createTableSql(getDb().getUserTable()));
207   }
208   
209   
210 
211 
212 }