1   package uba.db.table;
2   
3   import java.util.HashSet;
4   import java.util.List;
5   
6   import junit.framework.TestCase;
7   
8   import org.apache.commons.collections.ListUtils;
9   import org.apache.commons.collections.SetUtils;
10  
11  import uba.db.column.IntegerColumnSpecification;
12  import uba.db.testhelpers.TestUtils;
13  
14  /***
15   * Test de unidad para {@link TableSchema}.
16   * 
17   * @version $Revision: 1.2 $
18   */
19  public class TableSchemaTest extends TestCase {
20      private TableSchema tableSchema;
21      private TableSchema sameTableSchema;
22      private TableSchema otherTableSchema;
23  
24      private static final String TABLE_NAME = "table";
25      private List columnSpecifications;
26      private List otherColumnSpecifications;
27  
28      // TODO agregar tests para las validaciones de nombres: no se puede crear un nombre
29      // que sea igual a una tabla del sistema, con caraceteres raros como el enter, etc
30  
31      /***
32       * @see junit.framework.TestCase#setUp()
33       */
34      protected void setUp() throws Exception {
35          super.setUp();
36  
37          columnSpecifications = TestUtils.list(new IntegerColumnSpecification("numeric"));
38          otherColumnSpecifications = TestUtils
39                  .list(new IntegerColumnSpecification("other"));
40  
41          tableSchema = new TableSchema(TABLE_NAME, columnSpecifications);
42          sameTableSchema = new TableSchema(TABLE_NAME, columnSpecifications);
43          otherTableSchema = new TableSchema("otherTable", columnSpecifications);
44      }
45  
46      /***
47       * Test de igualdad entre dos instancias
48       */
49      public void testEquals() throws Exception {
50          TestUtils.assertEqualsImplementation(tableSchema,
51                                               sameTableSchema,
52                                               otherTableSchema);
53      }
54  
55      /***
56       * Test: acceder al nombre de la tabla.
57       */
58      public void testTableName() {
59          assertEquals(TABLE_NAME, tableSchema.tableName());
60      }
61  
62      /***
63       * Test: el nombre de la tabla esta siempre en minusculas (aunque se haya
64       * especificado con mayusculas/minusculas).
65       */
66      public void testTableNameIsAlwaysLowerCase() throws Exception {
67          assertEquals("othertable", otherTableSchema.tableName());
68      }
69  
70      /***
71       * Test: crear una instancia con un nombre no valido.
72       */
73      public void testCreateWithInvalidName() throws Exception {
74          try {
75              new TableSchema(null, ListUtils.EMPTY_LIST, SetUtils.EMPTY_SET);
76              fail();
77          } catch (InvalidTableNameException e) {
78              /* se espera la excepcion, por lo que el test pasa */
79          }
80  
81          try {
82              new TableSchema("", ListUtils.EMPTY_LIST, SetUtils.EMPTY_SET);
83              fail();
84          } catch (InvalidTableNameException e) {
85              /* se espera la excepcion, por lo que el test pasa */
86          }
87      }
88  
89      /***
90       * Test: crear una instancia con un primary keys no validos.
91       */
92      public void testCreateWithInvalidPrimaryKeys() throws Exception {
93          try {
94              new TableSchema("table", columnSpecifications, new HashSet(
95                      otherColumnSpecifications));
96              fail();
97          } catch (InvalidPrimaryKeyColumnsException e) {
98              /* se espera la excepcion, por lo que el test pasa */
99          }
100 
101         try {
102             new TableSchema("table", columnSpecifications, null);
103             fail();
104         } catch (InvalidPrimaryKeyColumnsException e) {
105             /* se espera la excepcion, por lo que el test pasa */
106         }
107     }
108 }