cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject cayenne git commit: cleaning up flaky unit tests
Date Sun, 10 May 2015 18:18:22 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 05e55337b -> beb07ceb5


cleaning up flaky unit tests


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/beb07ceb
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/beb07ceb
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/beb07ceb

Branch: refs/heads/master
Commit: beb07ceb5d293e864221061692f3cd543fdb5682
Parents: 05e5533
Author: aadamchik <aadamchik@apache.org>
Authored: Sun May 10 14:01:44 2015 -0400
Committer: aadamchik <aadamchik@apache.org>
Committed: Sun May 10 14:18:13 2015 -0400

----------------------------------------------------------------------
 .../cayenne/velocity/BindDirectiveIT.java       | 421 +++++++++----------
 1 file changed, 197 insertions(+), 224 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/beb07ceb/cayenne-server/src/test/java/org/apache/cayenne/velocity/BindDirectiveIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/velocity/BindDirectiveIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/velocity/BindDirectiveIT.java
index 69461c8..4f71bad 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/velocity/BindDirectiveIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/velocity/BindDirectiveIT.java
@@ -18,249 +18,222 @@
  ****************************************************************/
 package org.apache.cayenne.velocity;
 
-import org.apache.cayenne.DataRow;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.sql.Timestamp;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.access.MockOperationObserver;
-import org.apache.cayenne.access.jdbc.SQLTemplateAction;
 import org.apache.cayenne.dba.JdbcAdapter;
 import org.apache.cayenne.dba.oracle.OracleAdapter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.log.JdbcEventLogger;
 import org.apache.cayenne.query.CapsStrategy;
+import org.apache.cayenne.query.ObjectSelect;
 import org.apache.cayenne.query.SQLTemplate;
-import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.unit.di.server.CayenneProjects;
 import org.apache.cayenne.unit.di.server.ServerCase;
-import org.apache.cayenne.unit.di.server.ServerCaseDataSourceFactory;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
 import org.junit.Test;
 
-import java.sql.Connection;
-import java.sql.Timestamp;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
 /**
  * Tests BindDirective for passed null parameters and for not passed parameters
  */
 @UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
 public class BindDirectiveIT extends ServerCase {
 
-    @Inject
-    private ServerCaseDataSourceFactory dataSourceFactory;
-
-    @Inject
-    private JdbcAdapter adapter;
-
-    @Inject
-    private ObjectContext context;
-
-    @Inject
-    private JdbcEventLogger logger;
-    
-    @Inject
-    private DataNode node;
-
-    @Test
-    public void testBindTimestamp() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Integer(1));
-        parameters.put("name", "ArtistWithDOB");
-        Calendar cal = Calendar.getInstance();
-        cal.clear();
-        cal.set(2010, 2, 8);
-        parameters.put("dob", new Timestamp(cal.getTime().getTime()));
-
-        // without JDBC usage
-        Map<String, ?> row = performInsertForParameters(parameters, false, 1);
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        assertEquals(cal.getTime(), row.get("DATE_OF_BIRTH"));
-        assertNotNull(row.get("DATE_OF_BIRTH"));
-        assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
-    }
-
-    @Test
-    public void testBindSQLDate() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Integer(1));
-        parameters.put("name", "ArtistWithDOB");
-        Calendar cal = Calendar.getInstance();
-        cal.clear();
-        cal.set(2010, 2, 8);
-        parameters.put("dob", new java.sql.Date(cal.getTime().getTime()));
-
-        // without JDBC usage
-        Map<String, ?> row = performInsertForParameters(parameters, false, 1);
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
-        assertNotNull(row.get("DATE_OF_BIRTH"));
-        assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
-    }
-
-    @Test
-    public void testBindUtilDate() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Integer(1));
-        parameters.put("name", "ArtistWithDOB");
-        Calendar cal = Calendar.getInstance();
-        cal.clear();
-        cal.set(2010, 2, 8);
-        parameters.put("dob", cal.getTime());
-
-        // without JDBC usage
-        Map<String, ?> row = performInsertForParameters(parameters, false, 1);
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
-        assertNotNull(row.get("DATE_OF_BIRTH"));
-        assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
-    }
-
-    @Test
-    public void testBindingForCollection() throws Exception {
-
-        // insert 3 artists
-        for (int i = 1; i < 4; i++) {
-            Map<String, Object> parameters = new HashMap<String, Object>();
-            parameters.put("id", new Long(i));
-            parameters.put("name", "Artist" + i);
-            performInsertForParameters(parameters, true, i);
-        }
-
-        // now select only with names: Artist1 and Artist3
-        Set<String> artistNames = new HashSet<String>();
-        artistNames.add("Artist1");
-        artistNames.add("Artist3");
-        String sql = "SELECT * FROM ARTIST WHERE ARTIST_NAME in (#bind($ARTISTNAMES))";
-        SQLTemplate query = new SQLTemplate(Artist.class, sql);
-        
-        // customize for DB's that require trimming CHAR spaces 
-        query.setTemplate(OracleAdapter.class.getName(), "SELECT * FROM ARTIST WHERE RTRIM(ARTIST_NAME)
in (#bind($ARTISTNAMES))");
-        
-        query.setColumnNamesCapitalization(CapsStrategy.UPPER);
-        query.setParams(Collections.singletonMap("ARTISTNAMES", artistNames));
-        List<DataRow> result = context.performQuery(query);
-        assertEquals(2, result.size());
-    }
-
-    @Test
-    public void testBindForPassedNullParam() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Long(1));
-        parameters.put("name", "ArtistWithoutDOB");
-        // passing null in parameter
-        parameters.put("dob", null);
-
-        // without JDBC usage
-        Map<String, ?> row = performInsertForParameters(parameters, false, 1);
-        assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
-        assertNull(row.get("DATE_OF_BIRTH"));
-    }
-
-    @Test
-    public void testBindWithJDBCForPassedNullParam() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Long(1));
-        parameters.put("name", "ArtistWithoutDOB");
-        // passing null in parameter
-        parameters.put("dob", null);
-
-        // use JDBC
-        Map<String, ?> row = performInsertForParameters(parameters, true, 1);
-        assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
-        assertNull(row.get("DATE_OF_BIRTH"));
-    }
-
-    @Test
-    public void testBindForNotPassedParam() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Long(1));
-        parameters.put("name", "ArtistWithoutDOB");
-        // not passing parameter parameters.put("dob", not passed!);
-
-        // without JDBC usage
-        Map<String, ?> row = performInsertForParameters(parameters, false, 1);
-        assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        // parameter should be passed as null
-        assertNull(row.get("DATE_OF_BIRTH"));
-    }
-
-    @Test
-    public void testBindWithJDBCForNotPassedParam() throws Exception {
-        Map<String, Object> parameters = new HashMap<String, Object>();
-        parameters.put("id", new Long(1));
-        parameters.put("name", "ArtistWithoutDOB");
-        // not passing parameter parameters.put("dob", not passed!);
-
-        // use JDBC
-        Map<String, ?> row = performInsertForParameters(parameters, true, 1);
-        assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
-        assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
-        // parameter should be passed as null
-        assertNull(row.get("DATE_OF_BIRTH"));
-    }
-
-    /**
-     * Inserts row for given parameters
-     * 
-     * @return inserted row
-     */
-    private Map<String, ?> performInsertForParameters(
-            Map<String, Object> parameters,
-            boolean useJDBCType,
-            int expectedRowCount) throws Exception {
-
-        String templateString;
-        if (useJDBCType) {
-            templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH)
"
-                    + "VALUES (#bind($id), #bind($name), #bind($dob 'DATE'))";
-        }
-        else {
-            templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH)
"
-                    + "VALUES (#bind($id), #bind($name), #bind($dob))";
-        }
-        SQLTemplate template = new SQLTemplate(Object.class, templateString);
-
-        template.setParams(parameters);
-
-    
-        SQLTemplateAction action = new SQLTemplateAction(template, node);
-
-        Connection c = dataSourceFactory.getSharedDataSource().getConnection();
-        try {
-            MockOperationObserver observer = new MockOperationObserver();
-            action.performAction(c, observer);
-
-            int[] batches = observer.countsForQuery(template);
-            assertNotNull(batches);
-            assertEquals(1, batches.length);
-            assertEquals(1, batches[0]);
-        }
-        finally {
-            c.close();
-        }
-
-        SelectQuery query = new SelectQuery(Artist.class);
-        query.setFetchingDataRows(true);
-
-        List<DataRow> data = context.performQuery(query);
-        assertEquals(expectedRowCount, data.size());
-        return data.get(0);
-    }
+	private static String INSERT_TEMPLATE = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH)
"
+			+ "VALUES (#bind($id), #bind($name), #bind($dob))";
+	private static String INSERT_TEMPLATE_WITH_TYPES = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME,
DATE_OF_BIRTH) "
+			+ "VALUES (#bind($id), #bind($name), #bind($dob 'DATE'))";
+
+	@Inject
+	private JdbcAdapter adapter;
+
+	@Inject
+	private ObjectContext context;
+
+	@Inject
+	private JdbcEventLogger logger;
+
+	@Inject
+	private DataNode node;
+
+	@Inject
+	private DBHelper dbHelper;
+
+	@Test
+	public void testBind_Timestamp() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Integer(1));
+		parameters.put("name", "ArtistWithDOB");
+		Calendar cal = Calendar.getInstance();
+		cal.clear();
+		cal.set(2010, 2, 8);
+		parameters.put("dob", new Timestamp(cal.getTime().getTime()));
+
+		// without JDBC usage
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE);
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		assertEquals(cal.getTime(), row.get("DATE_OF_BIRTH"));
+		assertNotNull(row.get("DATE_OF_BIRTH"));
+		assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
+	}
+
+	@Test
+	public void testBind_SQLDate() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Integer(1));
+		parameters.put("name", "ArtistWithDOB");
+		Calendar cal = Calendar.getInstance();
+		cal.clear();
+		cal.set(2010, 2, 8);
+		parameters.put("dob", new java.sql.Date(cal.getTime().getTime()));
+
+		// without JDBC usage
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE);
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
+		assertNotNull(row.get("DATE_OF_BIRTH"));
+		assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
+	}
+
+	@Test
+	public void testBind_UtilDate() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Integer(1));
+		parameters.put("name", "ArtistWithDOB");
+		Calendar cal = Calendar.getInstance();
+		cal.clear();
+		cal.set(2010, 2, 8);
+		parameters.put("dob", cal.getTime());
+
+		// without JDBC usage
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE);
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
+		assertNotNull(row.get("DATE_OF_BIRTH"));
+		assertEquals(Date.class, row.get("DATE_OF_BIRTH").getClass());
+	}
+
+	@Test
+	public void testBind_Collection() throws Exception {
+
+		TableHelper tArtist = new TableHelper(dbHelper, "ARTIST").setColumns("ARTIST_ID", "ARTIST_NAME");
+
+		// insert 3 artists
+		for (int i = 1; i < 4; i++) {
+			tArtist.insert(new Long(i), "Artist" + i);
+		}
+
+		// now select only with names: Artist1 and Artist3
+		Set<String> artistNames = new HashSet<String>();
+		artistNames.add("Artist1");
+		artistNames.add("Artist3");
+		String sql = "SELECT * FROM ARTIST WHERE ARTIST_NAME in (#bind($ARTISTNAMES))";
+		SQLTemplate query = new SQLTemplate(Artist.class, sql);
+
+		// customize for DB's that require trimming CHAR spaces
+		query.setTemplate(OracleAdapter.class.getName(),
+				"SELECT * FROM ARTIST WHERE RTRIM(ARTIST_NAME) in (#bind($ARTISTNAMES))");
+
+		query.setColumnNamesCapitalization(CapsStrategy.UPPER);
+		query.setParams(Collections.singletonMap("ARTISTNAMES", artistNames));
+		List<?> result = context.performQuery(query);
+		assertEquals(2, result.size());
+	}
+
+	@Test
+	public void testBind_NullParam() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Long(1));
+		parameters.put("name", "ArtistWithoutDOB");
+		// passing null in parameter
+		parameters.put("dob", null);
+
+		// without JDBC usage
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE);
+		assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
+		assertNull(row.get("DATE_OF_BIRTH"));
+	}
+
+	@Test
+	public void testBind_NullParam_JDBCTypes() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Long(1));
+		parameters.put("name", "ArtistWithoutDOB");
+		// passing null in parameter
+		parameters.put("dob", null);
+
+		// use JDBC
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE_WITH_TYPES);
+		assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		assertEquals(parameters.get("dob"), row.get("DATE_OF_BIRTH"));
+		assertNull(row.get("DATE_OF_BIRTH"));
+	}
+
+	@Test
+	public void testBind_SkippedParam() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Long(1));
+		parameters.put("name", "ArtistWithoutDOB");
+		// skipping "dob"
+
+		// without JDBC usage
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE);
+		assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		// parameter should be passed as null
+		assertNull(row.get("DATE_OF_BIRTH"));
+	}
+
+	@Test
+	public void testBind_SkippedParam_JDBCTypes() throws Exception {
+		Map<String, Object> parameters = new HashMap<String, Object>();
+		parameters.put("id", new Long(1));
+		parameters.put("name", "ArtistWithoutDOB");
+		// skipping "dob"
+
+		// use JDBC
+		Map<String, ?> row = performInsertForParameters(parameters, INSERT_TEMPLATE_WITH_TYPES);
+		assertEquals(parameters.get("id"), row.get("ARTIST_ID"));
+		assertEquals(parameters.get("name"), row.get("ARTIST_NAME"));
+		// parameter should be passed as null
+		assertNull(row.get("DATE_OF_BIRTH"));
+	}
+
+	/**
+	 * Inserts row for given parameters
+	 * 
+	 * @return inserted row
+	 */
+	private Map<String, ?> performInsertForParameters(Map<String, Object> parameters,
String templateString)
+			throws Exception {
+
+		// TODO: do we really care if an inserting SQLTemplate is executed via
+		// ObjectContext?
+		SQLTemplate template = new SQLTemplate(Object.class, templateString);
+		template.setParams(parameters);
+		MockOperationObserver observer = new MockOperationObserver();
+		node.performQueries(Collections.singletonList(template), observer);
+
+		return ObjectSelect.dataRowQuery(Artist.class).selectOne(context);
+	}
 }


Mime
View raw message