1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/08/16 3:17:22
6    */
7   package test.org.asyrinx.joey.gen.command.rdb;
8   
9   import junit.framework.TestCase;
10  
11  import org.asyrinx.joey.gen.command.rdb.CheckForeignKey;
12  import org.asyrinx.joey.gen.model.command.ValidationError;
13  import org.asyrinx.joey.gen.model.rdb.Column;
14  import org.asyrinx.joey.gen.model.rdb.Database;
15  import org.asyrinx.joey.gen.model.rdb.ForeignKey;
16  import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
17  import org.asyrinx.joey.gen.model.rdb.Table;
18  
19  /***
20   * @author akima
21   */
22  public class CheckForeignKeyTest extends TestCase {
23  
24      public static void main(String[] args) {
25          junit.swingui.TestRunner.run(CheckForeignKeyTest.class);
26      }
27  
28      public void testNormal() {
29          final Database database = new Database();
30          final Table table1 = new Table(database, "table1");
31          new Column(table1, "col1-1", "int");
32          new Column(table1, "col1-2", "int");
33          new Column(table1, "col1-3", "int");
34          //
35          final Table table2 = new Table(database, "table2");
36          new Column(table2, "col2-1", "int");
37          new Column(table2, "col2-2", "int");
38          new Column(table2, "col2-3", "int");
39          final ForeignKey foreignKey2_1 = new ForeignKey(table2, "");
40          final ForeignKeyEntry entry2_1_1 = new ForeignKeyEntry(foreignKey2_1, "col2", "col1");
41          foreignKey2_1.setIndexed(false);
42          //
43          try {
44              new CheckForeignKey().execute(database);
45              //database.accept(new RdbVisitorAdapter(new RdbTopDownVisitor(new
46              // CheckForeignKey())));
47              fail();
48          } catch (ValidationError e) {
49              assertEquals(foreignKey2_1, e.getElement());
50          } catch (Throwable e) {
51              e.printStackTrace();
52              fail();
53          }
54          //
55          foreignKey2_1.setForeign("t1");
56          try {
57              new CheckForeignKey().execute(database);
58              fail();
59          } catch (ValidationError e) {
60              assertEquals(foreignKey2_1, e.getElement());
61          } catch (Throwable e) {
62              e.printStackTrace();
63              fail();
64          }
65          //
66          foreignKey2_1.setForeign("table1");
67          try {
68              new CheckForeignKey().execute(database);
69              fail();
70          } catch (ValidationError e) {
71              assertEquals(entry2_1_1, e.getElement());
72          } catch (Throwable e) {
73              e.printStackTrace();
74              fail();
75          }
76          //
77          entry2_1_1.setLocal("col2-1");
78          try {
79              new CheckForeignKey().execute(database);
80              fail();
81          } catch (ValidationError e) {
82              assertEquals(entry2_1_1, e.getElement());
83          } catch (Throwable e) {
84              e.printStackTrace();
85              fail();
86          }
87          //
88          entry2_1_1.setForeign("col1-1");
89          try {
90              new CheckForeignKey().execute(database);
91          } catch (Throwable e) {
92              e.printStackTrace();
93              fail();
94          }
95  
96      }
97  
98  }