1 package liquibase.sqlgenerator; 2 3 import liquibase.database.Database; 4 import liquibase.database.core.H2Database; 5 import liquibase.exception.ValidationErrors; 6 import liquibase.exception.Warnings; 7 import liquibase.sql.Sql; 8 import liquibase.sqlgenerator.core.AddAutoIncrementGenerator; 9 import liquibase.sqlgenerator.core.AddAutoIncrementGeneratorDB2; 10 import liquibase.sqlgenerator.core.AddAutoIncrementGeneratorHsqlH2; 11 import liquibase.sqlgenerator.core.AddColumnGenerator; 12 import liquibase.statement.SqlStatement; 13 import liquibase.statement.core.AddAutoIncrementStatement; 14 import org.junit.After; 15 import static org.junit.Assert.*; 16 import org.junit.Test; 17 18 import java.util.Collection; 19 import java.util.SortedSet; 20 21 public class SqlGeneratorFactoryTest { 22 23 @After 24 public void teardown() { 25 SqlGeneratorFactory.reset(); 26 } 27 28 @Test 29 public void getInstance() { 30 assertNotNull(SqlGeneratorFactory.getInstance()); 31 32 assertTrue(SqlGeneratorFactory.getInstance() == SqlGeneratorFactory.getInstance()); 33 } 34 35 @Test 36 public void register() { 37 SqlGeneratorFactory.getInstance().getGenerators().clear(); 38 39 assertEquals(0, SqlGeneratorFactory.getInstance().getGenerators().size()); 40 41 SqlGeneratorFactory.getInstance().register(new MockSqlGenerator(1, "A1")); 42 43 assertEquals(1, SqlGeneratorFactory.getInstance().getGenerators().size()); 44 } 45 46 @Test 47 public void unregister_instance() { 48 SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance(); 49 50 factory.getGenerators().clear(); 51 52 assertEquals(0, factory.getGenerators().size()); 53 54 AddAutoIncrementGeneratorHsqlH2 sqlGenerator 55 = new AddAutoIncrementGeneratorHsqlH2(); 56 57 factory.register(new AddAutoIncrementGenerator()); 58 factory.register(sqlGenerator); 59 factory.register(new AddAutoIncrementGeneratorDB2()); 60 61 assertEquals(3, factory.getGenerators().size()); 62 63 factory.unregister(sqlGenerator); 64 assertEquals(2, factory.getGenerators().size()); 65 } 66 67 @Test 68 public void unregister_class() { 69 SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance(); 70 71 factory.getGenerators().clear(); 72 73 assertEquals(0, factory.getGenerators().size()); 74 75 AddAutoIncrementGeneratorHsqlH2 sqlGenerator 76 = new AddAutoIncrementGeneratorHsqlH2(); 77 78 factory.register(new AddAutoIncrementGenerator()); 79 factory.register(sqlGenerator); 80 factory.register(new AddAutoIncrementGeneratorDB2()); 81 82 assertEquals(3, factory.getGenerators().size()); 83 84 factory.unregister(AddAutoIncrementGeneratorHsqlH2.class); 85 assertEquals(2, factory.getGenerators().size()); 86 } 87 88 @Test 89 public void unregister_class_doesNotExist() { 90 SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance(); 91 92 factory.getGenerators().clear(); 93 94 assertEquals(0, factory.getGenerators().size()); 95 96 factory.register(new AddAutoIncrementGenerator()); 97 factory.register(new AddAutoIncrementGeneratorHsqlH2()); 98 factory.register(new AddAutoIncrementGeneratorDB2()); 99 100 assertEquals(3, factory.getGenerators().size()); 101 102 factory.unregister(AddColumnGenerator.class); 103 assertEquals(3, factory.getGenerators().size()); 104 } 105 106 @Test 107 public void reset() { 108 SqlGeneratorFactory instance1 = SqlGeneratorFactory.getInstance(); 109 SqlGeneratorFactory.reset(); 110 assertFalse(instance1 == SqlGeneratorFactory.getInstance()); 111 } 112 113 @SuppressWarnings("unchecked") 114 @Test 115 public void builtInGeneratorsAreFound() { 116 Collection<SqlGenerator> generators = SqlGeneratorFactory.getInstance().getGenerators(); 117 assertTrue(generators.size() > 10); 118 } 119 120 @Test 121 public void getGenerators() { 122 SortedSet<SqlGenerator> allGenerators = SqlGeneratorFactory.getInstance().getGenerators(new AddAutoIncrementStatement(null, "person", "name", "varchar(255)"), new H2Database()); 123 124 assertNotNull(allGenerators); 125 assertEquals(1, allGenerators.size()); 126 } 127 128 private SqlGenerator addGenerator(final Class<? extends SqlStatement> sqlStatementClass, final Class<? extends Database> sqlDatabaseClass, final int level) { 129 130 SqlGenerator generator = new SqlGenerator() { 131 public int getPriority() { 132 return level; 133 } 134 135 public boolean requiresUpdatedDatabaseMetadata(Database database) { 136 return false; 137 } 138 139 public Warnings warn(SqlStatement sqlStatement, Database database, SqlGeneratorChain sqlGeneratorChain) { 140 return new Warnings(); 141 } 142 143 public ValidationErrors validate(SqlStatement sqlStatement, Database database, SqlGeneratorChain sqlGeneratorChain) { 144 return new ValidationErrors(); 145 } 146 147 public boolean supports(SqlStatement statement, Database database) { 148 boolean ret = sqlStatementClass.isAssignableFrom(statement.getClass()) && sqlDatabaseClass.isAssignableFrom(database.getClass()); 149 return ret; 150 } 151 152 public Sql[] generateSql(SqlStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { 153 return new Sql[0]; 154 } 155 }; 156 SqlGeneratorFactory.getInstance().register(generator); 157 158 return generator; 159 } 160 }