11package net .sf .jsqlparser .statement .alter ;
22
3- import static net .sf .jsqlparser .test .TestUtils .assertSqlCanBeParsedAndDeparsed ;
4- import static net .sf .jsqlparser .test .TestUtils .assertStatementCanBeDeparsedAs ;
5-
63import java .util .Arrays ;
74import java .util .List ;
8-
9- import junit .framework .TestCase ;
105import net .sf .jsqlparser .JSQLParserException ;
116import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
127import net .sf .jsqlparser .statement .Statement ;
138import net .sf .jsqlparser .statement .alter .AlterExpression .ColumnDataType ;
9+ import static net .sf .jsqlparser .test .TestUtils .assertSqlCanBeParsedAndDeparsed ;
10+ import static net .sf .jsqlparser .test .TestUtils .assertStatementCanBeDeparsedAs ;
11+ import static org .junit .Assert .assertEquals ;
12+ import static org .junit .Assert .assertNotNull ;
13+ import static org .junit .Assert .assertNull ;
14+ import static org .junit .Assert .assertTrue ;
15+ import org .junit .Test ;
1416
15- public class AlterTest extends TestCase {
16-
17- public AlterTest (String arg0 ) {
18- super (arg0 );
19- }
17+ public class AlterTest {
2018
19+ @ Test
2120 public void testAlterTableAddColumn () throws JSQLParserException {
2221 Statement stmt = CCJSqlParserUtil .
2322 parse ("ALTER TABLE mytable ADD COLUMN mycolumn varchar (255)" );
@@ -31,6 +30,7 @@ public void testAlterTableAddColumn() throws JSQLParserException {
3130 assertEquals ("varchar (255)" , colDataTypes .get (0 ).getColDataType ().toString ());
3231 }
3332
33+ @ Test
3434 public void testAlterTableAddColumn_ColumnKeyWordImplicit () throws JSQLParserException {
3535 Statement stmt = CCJSqlParserUtil .parse ("ALTER TABLE mytable ADD mycolumn varchar (255)" );
3636 assertTrue (stmt instanceof Alter );
@@ -43,70 +43,87 @@ public void testAlterTableAddColumn_ColumnKeyWordImplicit() throws JSQLParserExc
4343 assertEquals ("varchar (255)" , colDataTypes .get (0 ).getColDataType ().toString ());
4444 }
4545
46+ @ Test
4647 public void testAlterTablePrimaryKey () throws JSQLParserException {
4748 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id)" );
4849 }
4950
51+ @ Test
5052 public void testAlterTablePrimaryKeyDeferrable () throws JSQLParserException {
5153 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) DEFERRABLE" );
5254 }
5355
56+ @ Test
5457 public void testAlterTablePrimaryKeyNotDeferrable () throws JSQLParserException {
5558 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) NOT DEFERRABLE" );
5659 }
5760
61+ @ Test
5862 public void testAlterTablePrimaryKeyValidate () throws JSQLParserException {
5963 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) VALIDATE" );
6064 }
6165
66+ @ Test
6267 public void testAlterTablePrimaryKeyNoValidate () throws JSQLParserException {
6368 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) NOVALIDATE" );
6469 }
6570
71+ @ Test
6672 public void testAlterTablePrimaryKeyDeferrableValidate () throws JSQLParserException {
6773 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) DEFERRABLE VALIDATE" );
6874 }
6975
76+ @ Test
7077 public void testAlterTablePrimaryKeyDeferrableDisableNoValidate () throws JSQLParserException {
7178 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD PRIMARY KEY (id) DEFERRABLE DISABLE NOVALIDATE" );
7279 }
7380
81+ @ Test
7482 public void testAlterTableUniqueKey () throws JSQLParserException {
7583 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE `schema_migrations` ADD UNIQUE KEY `unique_schema_migrations` (`version`)" );
7684 }
7785
86+ @ Test
7887 public void testAlterTableForgeignKey () throws JSQLParserException {
7988 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE CASCADE" );
8089 }
8190
91+ @ Test
8292 public void testAlterTableAddConstraint () throws JSQLParserException {
8393 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE RESOURCELINKTYPE ADD CONSTRAINT FK_RESOURCELINKTYPE_PARENTTYPE_PRIMARYKEY FOREIGN KEY (PARENTTYPE_PRIMARYKEY) REFERENCES RESOURCETYPE(PRIMARYKEY)" );
8494 }
8595
96+ @ Test
8697 public void testAlterTableAddConstraintWithConstraintState () throws JSQLParserException {
8798 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE RESOURCELINKTYPE ADD CONSTRAINT FK_RESOURCELINKTYPE_PARENTTYPE_PRIMARYKEY FOREIGN KEY (PARENTTYPE_PRIMARYKEY) REFERENCES RESOURCETYPE(PRIMARYKEY) DEFERRABLE DISABLE NOVALIDATE" );
8899 }
89-
100+
101+ @ Test
90102 public void testAlterTableAddConstraintWithConstraintState2 () throws JSQLParserException {
91103 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE RESOURCELINKTYPE ADD CONSTRAINT RESOURCELINKTYPE_PRIMARYKEY PRIMARY KEY (PRIMARYKEY) DEFERRABLE NOVALIDATE" );
92104 }
93105
106+ @ Test
94107 public void testAlterTableForgeignKey2 () throws JSQLParserException {
95108 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id)" );
96109 }
97110
111+ @ Test
98112 public void testAlterTableForgeignKey3 () throws JSQLParserException {
99113 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE RESTRICT" );
100114 }
101115
116+ @ Test
102117 public void testAlterTableForgeignKey4 () throws JSQLParserException {
103118 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE SET NULL" );
104119 }
105120
121+ @ Test
106122 public void testAlterTableDropColumn () throws JSQLParserException {
107123 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test DROP COLUMN YYY" );
108124 }
109125
126+ @ Test
110127 public void testAlterTableDropColumn2 () throws JSQLParserException {
111128 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable DROP COLUMN col1, DROP COLUMN col2" );
112129
@@ -120,6 +137,7 @@ public void testAlterTableDropColumn2() throws JSQLParserException {
120137 assertEquals ("col2" , col2Exp .getColumnName ());
121138 }
122139
140+ @ Test
123141 public void testAlterTableDropConstraint () throws JSQLParserException {
124142 final String sql = "ALTER TABLE test DROP CONSTRAINT YYY" ;
125143 Statement stmt = CCJSqlParserUtil .parse (sql );
@@ -128,6 +146,7 @@ public void testAlterTableDropConstraint() throws JSQLParserException {
128146 assertEquals (alterExpression .getConstraintName (), "YYY" );
129147 }
130148
149+ @ Test
131150 public void testAlterTablePK () throws JSQLParserException {
132151 final String sql = "ALTER TABLE `Author` ADD CONSTRAINT `AuthorPK` PRIMARY KEY (`ID`)" ;
133152 Statement stmt = CCJSqlParserUtil .parse (sql );
@@ -138,6 +157,7 @@ public void testAlterTablePK() throws JSQLParserException {
138157 assertEquals (alterExpression .getIndex ().getColumnsNames ().get (0 ), "`ID`" );
139158 }
140159
160+ @ Test
141161 public void testAlterTableFK () throws JSQLParserException {
142162 String sql = "ALTER TABLE `Novels` ADD FOREIGN KEY (AuthorID) REFERENCES Author (ID)" ;
143163 Statement stmt = CCJSqlParserUtil .parse (sql );
@@ -150,18 +170,22 @@ public void testAlterTableFK() throws JSQLParserException {
150170 assertEquals (alterExpression .getFkSourceColumns ().get (0 ), "ID" );
151171 }
152172
173+ @ Test
153174 public void testAlterTableCheckConstraint () throws JSQLParserException {
154175 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE `Author` ADD CONSTRAINT name_not_empty CHECK (`NAME` <> '')" );
155176 }
156177
178+ @ Test
157179 public void testAlterTableAddColumn2 () throws JSQLParserException {
158180 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals ADD (col1 integer, col2 integer)" );
159181 }
160182
183+ @ Test
161184 public void testAlterTableAddColumn3 () throws JSQLParserException {
162185 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN mycolumn varchar (255)" );
163186 }
164187
188+ @ Test
165189 public void testAlterTableAddColumn4 () throws JSQLParserException {
166190 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 varchar (255), ADD COLUMN col2 integer" );
167191
@@ -179,6 +203,7 @@ public void testAlterTableAddColumn4() throws JSQLParserException {
179203 assertEquals ("integer" , col2DataTypes .get (0 ).getColDataType ().toString ());
180204 }
181205
206+ @ Test
182207 public void testAlterTableAddColumn5 () throws JSQLParserException {
183208 Statement stmt = CCJSqlParserUtil .parse ("ALTER TABLE mytable ADD col1 timestamp (3)" );
184209
@@ -193,6 +218,7 @@ public void testAlterTableAddColumn5() throws JSQLParserException {
193218 assertEquals ("timestamp (3)" , col1DataTypes .get (0 ).getColDataType ().toString ());
194219 }
195220
221+ @ Test
196222 public void testAlterTableAddColumn6 () throws JSQLParserException {
197223 final String sql = "ALTER TABLE mytable ADD COLUMN col1 timestamp (3) not null" ;
198224 Statement stmt = CCJSqlParserUtil .parse (sql );
@@ -204,10 +230,12 @@ public void testAlterTableAddColumn6() throws JSQLParserException {
204230 assertEquals ("null" , col1Exp .getColDataTypeList ().get (0 ).getColumnSpecs ().get (1 ));
205231 }
206232
233+ @ Test
207234 public void testAlterTableModifyColumn1 () throws JSQLParserException {
208235 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE animals MODIFY (col1 integer, col2 number (8, 2))" );
209236 }
210237
238+ @ Test
211239 public void testAlterTableModifyColumn2 () throws JSQLParserException {
212240 Statement stmt = CCJSqlParserUtil .parse ("ALTER TABLE mytable modify col1 timestamp (6)" );
213241
@@ -218,6 +246,7 @@ public void testAlterTableModifyColumn2() throws JSQLParserException {
218246 getOperation ());
219247 }
220248
249+ @ Test
221250 public void testAlterTableAddColumnWithZone () throws JSQLParserException {
222251 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone" );
223252 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 timestamp without time zone" );
@@ -233,24 +262,28 @@ public void testAlterTableAddColumnWithZone() throws JSQLParserException {
233262 assertEquals ("timestamp with time zone" , col1DataTypes .get (0 ).getColDataType ().toString ());
234263 }
235264
265+ @ Test
236266 public void testAlterTableAddColumnKeywordTypes () throws JSQLParserException {
237267 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 xml" );
238268 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 interval" );
239269 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE mytable ADD COLUMN col1 bit varying" );
240270 }
241-
271+
272+ @ Test
242273 public void testDropColumnRestrictIssue510 () throws JSQLParserException {
243274 assertSqlCanBeParsedAndDeparsed ("ALTER TABLE TABLE1 DROP COLUMN NewColumn CASCADE" );
244275 }
245-
276+
277+ @ Test
246278 public void testDropColumnRestrictIssue551 () throws JSQLParserException {
247279 Statement stmt = CCJSqlParserUtil .parse ("ALTER TABLE table1 DROP NewColumn" );
248-
280+
249281 // COLUMN keyword appears in deparsed statement, drop becomes all caps
250282 assertStatementCanBeDeparsedAs (stmt , "ALTER TABLE table1 DROP COLUMN NewColumn" );
251-
283+
252284 }
253-
285+
286+ @ Test
254287 public void testAddConstraintKeyIssue320 () throws JSQLParserException {
255288 String tableName = "table1" ;
256289 String columnName1 = "col1" ;
@@ -260,16 +293,21 @@ public void testAddConstraintKeyIssue320() throws JSQLParserException {
260293 String constraintName1 = "table1_constraint_1" ;
261294 String constraintName2 = "table1_constraint_2" ;
262295
263- for (String constraintType : Arrays .asList ("UNIQUE KEY" , "KEY" )) {
264- assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
296+ for (String constraintType : Arrays .asList ("UNIQUE KEY" , "KEY" )) {
297+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
265298 + constraintType + " (" + columnName1 + ")" );
266299
267- assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
300+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
268301 + constraintType + " (" + columnName1 + ", " + columnName2 + ")" );
269302
270- assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
271- + constraintType + " (" + columnName1 + ", " + columnName2 + "), ADD CONSTRAINT "
303+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE " + tableName + " ADD CONSTRAINT " + constraintName1 + " "
304+ + constraintType + " (" + columnName1 + ", " + columnName2 + "), ADD CONSTRAINT "
272305 + constraintName2 + " " + constraintType + " (" + columnName3 + ", " + columnName4 + ")" );
273306 }
274307 }
308+
309+ @ Test
310+ public void testIssue633 () throws JSQLParserException {
311+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE team_phases ADD CONSTRAINT team_phases_id_key UNIQUE (id)" );
312+ }
275313}
0 commit comments