cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [2/2] cayenne git commit: cleaning up OrderingTranslatorIT
Date Sun, 24 Jan 2016 10:07:43 GMT
cleaning up OrderingTranslatorIT


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

Branch: refs/heads/master
Commit: b0be7f77936f8f59aa6c8036d744623b0271d3ec
Parents: bad8d01
Author: aadamchik <aadamchik@apache.org>
Authored: Sun Jan 24 12:58:42 2016 +0300
Committer: aadamchik <aadamchik@apache.org>
Committed: Sun Jan 24 13:05:14 2016 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/TranslationCase.java     |  62 ---------
 .../translator/select/OrderingTranslatorIT.java | 133 ++++++-------------
 .../translator/select/TstQueryAssembler.java    |   3 +-
 .../apache/cayenne/exp/TstExpressionCase.java   |  42 +++++-
 4 files changed, 76 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0be7f77/cayenne-server/src/test/java/org/apache/cayenne/TranslationCase.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/TranslationCase.java b/cayenne-server/src/test/java/org/apache/cayenne/TranslationCase.java
deleted file mode 100644
index 581b7c4..0000000
--- a/cayenne-server/src/test/java/org/apache/cayenne/TranslationCase.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*****************************************************************
- *   Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- ****************************************************************/
-
-package org.apache.cayenne;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-public class TranslationCase {
-
-	protected Object tstObject;
-	protected String sqlExp;
-	protected String rootEntity;
-
-	public TranslationCase(String rootEntity, Object tstObject, String sqlExp) {
-		this.tstObject = tstObject;
-		this.rootEntity = rootEntity;
-		this.sqlExp = sqlExp;
-	}
-
-	@Override
-	public String toString() {
-		StringBuffer buf = new StringBuffer();
-		buf.append(this.getClass().getName()).append(tstObject);
-		return buf.toString();
-	}
-
-	public void assertTranslatedWell(String translated) {
-		if (sqlExp == null) {
-			assertNull(translated);
-			return;
-		}
-
-		assertNotNull(translated);
-		assertEquals("Unexpected translation: " + translated + "....", sqlExp, translated);
-	}
-
-	public String getRootEntity() {
-		return rootEntity;
-	}
-
-	public String getSqlExp() {
-		return sqlExp;
-	}
-}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0be7f77/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/OrderingTranslatorIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/OrderingTranslatorIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/OrderingTranslatorIT.java
index f2784f4..4e92d02 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/OrderingTranslatorIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/OrderingTranslatorIT.java
@@ -19,11 +19,14 @@
 
 package org.apache.cayenne.access.translator.select;
 
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Arrays;
 
-import org.apache.cayenne.TranslationCase;
 import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.query.Ordering;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.query.SortOrder;
 import org.apache.cayenne.testdo.testmap.Artist;
@@ -46,129 +49,67 @@ public class OrderingTranslatorIT extends ServerCase {
 	 * Tests ascending ordering on string attribute.
 	 */
 	@Test
-	public void testDoTranslation1() throws Exception {
-		SelectQuery q = new SelectQuery(Artist.class);
-		q.addOrdering("artistName", SortOrder.ASCENDING);
-
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
-
-		TranslationCase tstCase = new TranslationCase("Artist", null, "ta.ARTIST_NAME");
-
-		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
-
-		assertTrue(out.length() > 0);
-		tstCase.assertTranslatedWell(out.toString());
+	public void testAppendPart1() throws Exception {
+		Ordering o1 = new Ordering("artistName", SortOrder.ASCENDING);
+		doTestAppendPart("ta.ARTIST_NAME", o1);
 	}
 
 	/**
 	 * Tests descending ordering on string attribute.
 	 */
 	@Test
-	public void testDoTranslation2() throws Exception {
-		SelectQuery q = new SelectQuery(Artist.class);
-		q.addOrdering("artistName", SortOrder.DESCENDING);
+	public void testAppendPart2() throws Exception {
+		Ordering o1 = new Ordering("artistName", SortOrder.DESCENDING);
+		doTestAppendPart("ta.ARTIST_NAME DESC", o1);
+	}
 
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
+	@Test
+	public void testAppendPart3() throws Exception {
 
-		TranslationCase tstCase = new TranslationCase("Artist", null, "ta.ARTIST_NAME DESC");
+		Ordering o1 = new Ordering("artistName", SortOrder.DESCENDING);
+		Ordering o2 = new Ordering("paintingArray.estimatedPrice", SortOrder.ASCENDING);
 
-		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
-
-		assertTrue(out.length() > 0);
-		tstCase.assertTranslatedWell(out.toString());
+		doTestAppendPart("ta.ARTIST_NAME DESC, ta.ESTIMATED_PRICE", o1, o2);
 	}
 
 	/**
 	 * Tests ascending case-insensitive ordering on string attribute.
 	 */
 	@Test
-	public void testDoTranslation4() throws Exception {
-		SelectQuery q = new SelectQuery(Artist.class);
-		q.addOrdering("artistName", SortOrder.ASCENDING_INSENSITIVE);
-
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
-
-		TranslationCase tstCase = new TranslationCase("Artist", null, "UPPER(ta.ARTIST_NAME)");
-
-		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
-
-		assertTrue(out.length() > 0);
-		String orderBySql = out.toString();
-		assertTrue(orderBySql.contains("UPPER("));
-		tstCase.assertTranslatedWell(orderBySql);
+	public void testAppendPart4() throws Exception {
+		Ordering o1 = new Ordering("artistName", SortOrder.ASCENDING_INSENSITIVE);
+		doTestAppendPart("UPPER(ta.ARTIST_NAME)", o1);
 	}
 
 	@Test
-	public void testDoTranslation5() throws Exception {
-		SelectQuery q = new SelectQuery(Artist.class);
-		q.addOrdering("artistName", SortOrder.DESCENDING_INSENSITIVE);
-		q.addOrdering("paintingArray.estimatedPrice", SortOrder.ASCENDING);
+	public void testAppendPart5() throws Exception {
 
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
+		Ordering o1 = new Ordering("artistName", SortOrder.DESCENDING_INSENSITIVE);
+		Ordering o2 = new Ordering("paintingArray.estimatedPrice", SortOrder.ASCENDING);
 
-		TranslationCase tstCase = new TranslationCase("Artist", null, "UPPER(ta.ARTIST_NAME) DESC,
ta.ESTIMATED_PRICE");
-
-		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
-
-		assertTrue(out.length() > 0);
-		String orderBySql = out.toString();
-
-		// Check there is an UPPER modifier
-		int indexOfUpper = orderBySql.indexOf("UPPER(");
-		assertTrue(indexOfUpper != -1);
-
-		// and ensure there is only ONE upper modifier
-		assertTrue(orderBySql.indexOf("UPPER(", indexOfUpper + 1) == -1);
-		tstCase.assertTranslatedWell(orderBySql);
+		doTestAppendPart("UPPER(ta.ARTIST_NAME) DESC, ta.ESTIMATED_PRICE", o1, o2);
 	}
 
 	@Test
-	public void testDoTranslation6() throws Exception {
-		SelectQuery q = new SelectQuery(Artist.class);
-		q.addOrdering("artistName", SortOrder.ASCENDING_INSENSITIVE);
-		q.addOrdering("paintingArray.estimatedPrice", SortOrder.ASCENDING_INSENSITIVE);
-
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
-
-		TranslationCase tstCase = new TranslationCase("Artist", null,
-				"UPPER(ta.ARTIST_NAME), UPPER(ta.ESTIMATED_PRICE)");
-
-		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
-
-		assertTrue(out.length() > 0);
-		String orderBySql = out.toString();
+	public void testAppendPart6() throws Exception {
+		Ordering o1 = new Ordering("artistName", SortOrder.ASCENDING_INSENSITIVE);
+		Ordering o2 = new Ordering("paintingArray.estimatedPrice", SortOrder.ASCENDING_INSENSITIVE);
 
-		// Check there is at least one UPPER modifier
-		int indexOfUpper = orderBySql.indexOf("UPPER(");
-		assertTrue(indexOfUpper != -1);
-
-		// and ensure there is another after it
-		assertTrue(orderBySql.indexOf("UPPER(", indexOfUpper + 1) != -1);
-
-		tstCase.assertTranslatedWell(orderBySql);
+		doTestAppendPart("UPPER(ta.ARTIST_NAME), UPPER(ta.ESTIMATED_PRICE)", o1, o2);
 	}
 
-	@Test
-	public void testDoTranslation3() throws Exception {
-		SelectQuery<Artist> q = SelectQuery.query(Artist.class);
-
-		q.addOrdering("artistName", SortOrder.DESCENDING);
-		q.addOrdering("paintingArray.estimatedPrice", SortOrder.ASCENDING);
-
-		TstQueryAssembler qa = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
+	private void doTestAppendPart(String expectedSQL, Ordering... orderings) {
 
-		TranslationCase tstCase = new TranslationCase("Artist", null, "ta.ARTIST_NAME DESC, ta.ESTIMATED_PRICE");
+		SelectQuery<Artist> q = SelectQuery.query(Artist.class);
+		q.addOrderings(Arrays.asList(orderings));
 
+		TstQueryAssembler assembler = new TstQueryAssembler(q, node.getAdapter(), node.getEntityResolver());
 		StringBuilder out = new StringBuilder();
-		new OrderingTranslator(qa).appendPart(out);
+		new OrderingTranslator(assembler).appendPart(out);
+
+		String translated = out.toString();
 
-		assertTrue(out.length() > 0);
-		String orderBySql = out.toString();
-		tstCase.assertTranslatedWell(orderBySql);
+		assertNotNull(translated);
+		assertEquals("Unexpected translation: " + translated + "....", expectedSQL, translated);
 	}
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0be7f77/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/TstQueryAssembler.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/TstQueryAssembler.java
b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/TstQueryAssembler.java
index 1ea09cb..dfc7495 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/TstQueryAssembler.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/TstQueryAssembler.java
@@ -19,7 +19,6 @@
 
 package org.apache.cayenne.access.translator.select;
 
-import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -33,7 +32,7 @@ public class TstQueryAssembler extends QueryAssembler {
 
 	protected List<DbRelationship> dbRels;
 
-	public TstQueryAssembler(Query q, DbAdapter adapter, EntityResolver resolver) throws SQLException
{
+	public TstQueryAssembler(Query q, DbAdapter adapter, EntityResolver resolver) {
 		super(q, adapter, resolver);
 		dbRels = new ArrayList<DbRelationship>();
 	}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0be7f77/cayenne-server/src/test/java/org/apache/cayenne/exp/TstExpressionCase.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/exp/TstExpressionCase.java b/cayenne-server/src/test/java/org/apache/cayenne/exp/TstExpressionCase.java
index 173e1a0..b3bcd92 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/exp/TstExpressionCase.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/exp/TstExpressionCase.java
@@ -19,14 +19,22 @@
 
 package org.apache.cayenne.exp;
 
-import org.apache.cayenne.TranslationCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TstExpressionCase {
 
-public class TstExpressionCase extends TranslationCase {
 	protected int totalNodes;
 	protected int totalLeaves;
+	protected Expression cayenneExp;
+	protected String sqlExp;
+	protected String rootEntity;
 
 	public TstExpressionCase(String rootEntity, Expression cayenneExp, String sqlExp, int totalNodes,
int totalLeaves) {
-		super(rootEntity, cayenneExp, sqlExp);
+		this.cayenneExp = cayenneExp;
+		this.rootEntity = rootEntity;
+		this.sqlExp = sqlExp;
 		this.totalNodes = totalNodes;
 		this.totalLeaves = totalLeaves;
 	}
@@ -40,6 +48,32 @@ public class TstExpressionCase extends TranslationCase {
 	}
 
 	public Expression getCayenneExp() {
-		return (Expression) tstObject;
+		return cayenneExp;
+	}
+
+	@Override
+	public String toString() {
+		StringBuffer buf = new StringBuffer();
+		buf.append(this.getClass().getName()).append(cayenneExp);
+		return buf.toString();
+	}
+
+	public void assertTranslatedWell(String translated) {
+		if (sqlExp == null) {
+			assertNull(translated);
+			return;
+		}
+
+		assertNotNull(translated);
+		assertEquals("Unexpected translation: " + translated + "....", sqlExp, translated);
 	}
+
+	public String getRootEntity() {
+		return rootEntity;
+	}
+
+	public String getSqlExp() {
+		return sqlExp;
+	}
+
 }


Mime
View raw message