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 1:31:47
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.FkToIndex;
12  import org.asyrinx.joey.gen.model.rdb.Column;
13  import org.asyrinx.joey.gen.model.rdb.Database;
14  import org.asyrinx.joey.gen.model.rdb.ForeignKey;
15  import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
16  import org.asyrinx.joey.gen.model.rdb.Index;
17  import org.asyrinx.joey.gen.model.rdb.IndexEntry;
18  import org.asyrinx.joey.gen.model.rdb.Table;
19  import org.asyrinx.joey.gen.model.rdb.visitor.RdbVisitorAdapter;
20  import org.asyrinx.joey.gen.model.rdb.visitor.RdbTopDownVisitor;
21  
22  /***
23   * @author akima
24   */
25  public class FkToIndexTest extends TestCase {
26  
27      public static void main(String[] args) {
28          junit.swingui.TestRunner.run(FkToIndexTest.class);
29      }
30  
31      public void testExtend() {
32          final Database database = new Database();
33          final Table table1 = new Table(database, "table1");
34          new Column(table1, "col1-1", "int");
35          new Column(table1, "col1-2", "int");
36          new Column(table1, "col1-3", "int");
37          final Index index1_1 = new Index(table1, "named_index_1");
38          index1_1.add(new IndexEntry("col1-2"));
39          final Index index1_2 = new Index(table1);
40          index1_2.add(new IndexEntry("col1-3"));
41          index1_2.add(new IndexEntry("col1-1"));
42          //
43          final Table table2 = new Table(database, "table2");
44          new Column(table2, "col2-1", "int");
45          new Column(table2, "col2-2", "int");
46          new Column(table2, "col2-3", "int");
47          final ForeignKey foreignKey2_1 = new ForeignKey(table2, table1.getName());
48          foreignKey2_1.add(new ForeignKeyEntry("col2-1", "col1-1"));
49          foreignKey2_1.setIndexed(false);
50          final ForeignKey foreignKey2_2 = new ForeignKey(table2, "named_fk", table1.getName());
51          foreignKey2_2.setIndexed(true);
52          foreignKey2_2.add(new ForeignKeyEntry("col2-2", "col1-2"));
53          foreignKey2_2.add(new ForeignKeyEntry("col2-3", "col1-3"));
54          //
55          new FkToIndex().execute(database);
56          //database.accept(new RdbVisitorAdapter(new RdbTopDownVisitor(new
57          // FkToIndex())));
58          //
59          assertEquals(1, table2.getIndexes().size());
60          final Index index = table2.getIndexes().getIndex(0);
61          assertEquals(null, foreignKey2_1.getIndex());
62          assertEquals(index, foreignKey2_2.getIndex());
63          assertEquals(foreignKey2_2, table2.findForeignKey(index));
64          //
65          assertEquals(2, index.size());
66          final IndexEntry entry0 = index.getEntry(0);
67          final IndexEntry entry1 = index.getEntry(1);
68          assertEquals("col2-2", entry0.getName());
69          assertEquals("col2-3", entry1.getName());
70      }
71  
72  }