cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1385258 - /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
Date Sun, 16 Sep 2012 13:16:52 GMT
Author: aadamchik
Date: Sun Sep 16 13:16:51 2012
New Revision: 1385258

URL: http://svn.apache.org/viewvc?rev=1385258&view=rev
Log:
better DB cleanup to prevent random test failures

Specifically hoping to clear this Jenkins error:

java.sql.SQLException: Attempt to insert null into a non-nullable column in statement [ALTER
TABLE PAINTING ALTER COLUMN NEWCOL2 SET NOT NULL]
	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
	at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
	at org.hsqldb.jdbc.jdbcStatement.execute(Unknown Source)
	at org.apache.cayenne.merge.AbstractToDbToken.executeSql(AbstractToDbToken.java:57)
	at org.apache.cayenne.merge.AbstractToDbToken.execute(AbstractToDbToken.java:45)
	at org.apache.cayenne.merge.MergeCase.execute(MergeCase.java:131)
	at org.apache.cayenne.merge.MergeCase.assertTokensAndExecute(MergeCase.java:188)
	at org.apache.cayenne.merge.SetAllowNullToDbTest.test(SetAllowNullToDbTest.java:43)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java?rev=1385258&r1=1385257&r2=1385258&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
Sun Sep 16 13:16:51 2012
@@ -33,194 +33,196 @@ import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.test.jdbc.DBHelper;
 import org.apache.cayenne.unit.UnitDbAdapter;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.ServerCaseDataSourceFactory;
 
 public abstract class MergeCase extends ServerCase {
 
-    @Inject
-    private ServerRuntime runtime;
+	@Inject
+	private DBHelper dbHelper;
 
-    @Inject
-    private UnitDbAdapter accessStackAdapter;
+	@Inject
+	private ServerRuntime runtime;
 
-    @Inject
-    private ServerCaseDataSourceFactory dataSourceFactory;
+	@Inject
+	private UnitDbAdapter accessStackAdapter;
 
-    @Inject
-    protected EntityResolver resolver;
-
-    @Inject
-    protected DataNode node;
-
-    protected DataMap map;
-
-    private static List<String> TABLE_NAMES = Arrays.asList(
-            "ARTIST",
-            "PAINTING",
-            "NEW_TABLE",
-            "NEW_TABLE2");
-
-    @Override
-    protected void setUpAfterInjection() throws Exception {
-
-        // this map can't be safely modified in this test, as it is reset by DI container
-        // on every test
-        map = runtime.getDataDomain().getDataMap("testmap");
-
-        filterDataMap();
-
-        List<MergerToken> tokens = createMergeTokens();
-        execute(tokens);
-
-        assertTokensAndExecute(0, 0);
-    }
-
-    protected DbMerger createMerger() {
-        return new DbMerger() {
-
-            @Override
-            public boolean includeTableName(String tableName) {
-                return TABLE_NAMES.contains(tableName.toUpperCase());
-            }
-        };
-    }
-
-    protected List<MergerToken> createMergeTokens() {
-        return createMerger().createMergeTokens(node, map);
-    }
-
-    /**
-     * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting that and so
-     * on.
-     */
-    private void filterDataMap() {
-        // copied from AbstractAccessStack.dbEntitiesInInsertOrder
-        boolean excludeBinPK = accessStackAdapter.supportsBinaryPK();
-
-        if (!excludeBinPK) {
-            return;
-        }
-
-        List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
-
-        for (DbEntity ent : map.getDbEntities()) {
-            for (DbAttribute attr : ent.getAttributes()) {
-                // check for BIN PK or FK to BIN Pk
-                if (attr.getType() == Types.BINARY
-                        || attr.getType() == Types.VARBINARY
-                        || attr.getType() == Types.LONGVARBINARY) {
-
-                    if (attr.isPrimaryKey() || attr.isForeignKey()) {
-                        entitiesToRemove.add(ent);
-                        break;
-                    }
-                }
-            }
-        }
-
-        for (DbEntity e : entitiesToRemove) {
-            map.removeDbEntity(e.getName(), true);
-        }
-    }
-
-    protected void execute(List<MergerToken> tokens) throws Exception {
-        MergerContext mergerContext = new ExecutingMergerContext(map, node);
-        for (MergerToken tok : tokens) {
-            tok.execute(mergerContext);
-        }
-    }
-
-    protected void execute(MergerToken token) throws Exception {
-        MergerContext mergerContext = new ExecutingMergerContext(map, node);
-        token.execute(mergerContext);
-    }
-
-    private void executeSql(String sql) throws Exception {
-        Connection conn = dataSourceFactory.getSharedDataSource().getConnection();
-
-        try {
-            Statement st = conn.createStatement();
-
-            try {
-                st.execute(sql);
-            }
-            finally {
-                st.close();
-            }
-        }
-
-        finally {
-            conn.close();
-        }
-    }
-
-    protected void assertTokens(
-            List<MergerToken> tokens,
-            int expectedToDb,
-            int expectedToModel) {
-        int actualToDb = 0;
-        int actualToModel = 0;
-        for (MergerToken token : tokens) {
-            if (token.getDirection().isToDb()) {
-                actualToDb++;
-            }
-            else if (token.getDirection().isToModel()) {
-                actualToModel++;
-            }
-        }
-
-        assertEquals("tokens to db", expectedToDb, actualToDb);
-        assertEquals("tokens to model", expectedToModel, actualToModel);
-    }
-
-    protected void assertTokensAndExecute(
-            DataNode node,
-            DataMap map,
-            int expectedToDb,
-            int expectedToModel) {
-        List<MergerToken> tokens = createMergeTokens();
-
-        assertTokens(tokens, expectedToDb, expectedToModel);
-        if (!tokens.isEmpty()) {
-            try {
-                execute(tokens);
-            }
-            catch (Exception e) {
-                fail(e.getMessage());
-            }
-        }
-    }
-
-    protected void assertTokensAndExecute(int expectedToDb, int expectedToModel) {
-        List<MergerToken> tokens = createMergeTokens();
-
-        assertTokens(tokens, expectedToDb, expectedToModel);
-        if (!tokens.isEmpty()) {
-            try {
-                execute(tokens);
-            }
-            catch (Exception e) {
-                fail(e.getMessage());
-            }
-        }
-    }
-
-    protected MergerFactory mergerFactory() {
-        return node.getAdapter().mergerFactory();
-    }
-
-    protected void dropTableIfPresent(DataNode node, String tableName) {
-        DbEntity entity = new DbEntity(tableName);
-        AbstractToDbToken t = (AbstractToDbToken) mergerFactory().createDropTableToDb(
-                entity);
-        try {
-            for (String sql : t.createSql(node.getAdapter())) {
-                executeSql(sql);
-            }
-        }
-        catch (Exception e) {
-        }
-    }
+	@Inject
+	private ServerCaseDataSourceFactory dataSourceFactory;
+
+	@Inject
+	protected EntityResolver resolver;
+
+	@Inject
+	protected DataNode node;
+
+	protected DataMap map;
+
+	private static List<String> TABLE_NAMES = Arrays.asList("ARTIST",
+			"PAINTING", "NEW_TABLE", "NEW_TABLE2");
+
+	@Override
+	protected void setUpAfterInjection() throws Exception {
+
+		dbHelper.deleteAll("ARTGROUP");
+		dbHelper.deleteAll("PAINTING_INFO");
+		dbHelper.deleteAll("PAINTING");
+		dbHelper.deleteAll("ARTIST_EXHIBIT");
+		dbHelper.deleteAll("ARTIST_GROUP");
+		dbHelper.deleteAll("ARTIST");
+		dbHelper.deleteAll("EXHIBIT");
+		dbHelper.deleteAll("GALLERY");
+
+		// this map can't be safely modified in this test, as it is reset by DI
+		// container
+		// on every test
+		map = runtime.getDataDomain().getDataMap("testmap");
+
+		filterDataMap();
+
+		List<MergerToken> tokens = createMergeTokens();
+		execute(tokens);
+
+		assertTokensAndExecute(0, 0);
+	}
+
+	protected DbMerger createMerger() {
+		return new DbMerger() {
+
+			@Override
+			public boolean includeTableName(String tableName) {
+				return TABLE_NAMES.contains(tableName.toUpperCase());
+			}
+		};
+	}
+
+	protected List<MergerToken> createMergeTokens() {
+		return createMerger().createMergeTokens(node, map);
+	}
+
+	/**
+	 * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting
+	 * that and so on.
+	 */
+	private void filterDataMap() {
+		// copied from AbstractAccessStack.dbEntitiesInInsertOrder
+		boolean excludeBinPK = accessStackAdapter.supportsBinaryPK();
+
+		if (!excludeBinPK) {
+			return;
+		}
+
+		List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
+
+		for (DbEntity ent : map.getDbEntities()) {
+			for (DbAttribute attr : ent.getAttributes()) {
+				// check for BIN PK or FK to BIN Pk
+				if (attr.getType() == Types.BINARY
+						|| attr.getType() == Types.VARBINARY
+						|| attr.getType() == Types.LONGVARBINARY) {
+
+					if (attr.isPrimaryKey() || attr.isForeignKey()) {
+						entitiesToRemove.add(ent);
+						break;
+					}
+				}
+			}
+		}
+
+		for (DbEntity e : entitiesToRemove) {
+			map.removeDbEntity(e.getName(), true);
+		}
+	}
+
+	protected void execute(List<MergerToken> tokens) throws Exception {
+		MergerContext mergerContext = new ExecutingMergerContext(map, node);
+		for (MergerToken tok : tokens) {
+			tok.execute(mergerContext);
+		}
+	}
+
+	protected void execute(MergerToken token) throws Exception {
+		MergerContext mergerContext = new ExecutingMergerContext(map, node);
+		token.execute(mergerContext);
+	}
+
+	private void executeSql(String sql) throws Exception {
+		Connection conn = dataSourceFactory.getSharedDataSource()
+				.getConnection();
+
+		try {
+			Statement st = conn.createStatement();
+
+			try {
+				st.execute(sql);
+			} finally {
+				st.close();
+			}
+		}
+
+		finally {
+			conn.close();
+		}
+	}
+
+	protected void assertTokens(List<MergerToken> tokens, int expectedToDb,
+			int expectedToModel) {
+		int actualToDb = 0;
+		int actualToModel = 0;
+		for (MergerToken token : tokens) {
+			if (token.getDirection().isToDb()) {
+				actualToDb++;
+			} else if (token.getDirection().isToModel()) {
+				actualToModel++;
+			}
+		}
+
+		assertEquals("tokens to db", expectedToDb, actualToDb);
+		assertEquals("tokens to model", expectedToModel, actualToModel);
+	}
+
+	protected void assertTokensAndExecute(DataNode node, DataMap map,
+			int expectedToDb, int expectedToModel) {
+		List<MergerToken> tokens = createMergeTokens();
+
+		assertTokens(tokens, expectedToDb, expectedToModel);
+		if (!tokens.isEmpty()) {
+			try {
+				execute(tokens);
+			} catch (Exception e) {
+				fail(e.getMessage());
+			}
+		}
+	}
+
+	protected void assertTokensAndExecute(int expectedToDb, int expectedToModel) {
+		List<MergerToken> tokens = createMergeTokens();
+
+		assertTokens(tokens, expectedToDb, expectedToModel);
+		if (!tokens.isEmpty()) {
+			try {
+				execute(tokens);
+			} catch (Exception e) {
+				fail(e.getMessage());
+			}
+		}
+	}
+
+	protected MergerFactory mergerFactory() {
+		return node.getAdapter().mergerFactory();
+	}
+
+	protected void dropTableIfPresent(DataNode node, String tableName) {
+		DbEntity entity = new DbEntity(tableName);
+		AbstractToDbToken t = (AbstractToDbToken) mergerFactory()
+				.createDropTableToDb(entity);
+		try {
+			for (String sql : t.createSql(node.getAdapter())) {
+				executeSql(sql);
+			}
+		} catch (Exception e) {
+		}
+	}
 }



Mime
View raw message