You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/09/30 14:04:32 UTC

[GitHub] [incubator-shardingsphere] sluk3r commented on a change in pull request #3158: testcase code added for package of org.apache.shardingsphere.core.optimize.segment.insert

sluk3r commented on a change in pull request #3158: testcase code added for package of org.apache.shardingsphere.core.optimize.segment.insert
URL: https://github.com/apache/incubator-shardingsphere/pull/3158#discussion_r329594879
 
 

 ##########
 File path: sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValueContextTest.java
 ##########
 @@ -0,0 +1,155 @@
+/*
+ * 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.shardingsphere.core.optimize.segment.insert;
+
+import com.google.common.collect.Lists;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.core.optimize.segment.insert.expression.DerivedLiteralExpressionSegment;
+import org.apache.shardingsphere.core.optimize.segment.insert.expression.DerivedParameterMarkerExpressionSegment;
+import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
+import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import org.junit.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+public class InsertValueContextTest {
+
+    @Test
+    public void assertInstanceConstructedOk() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
+        Collection<ExpressionSegment> assignments = Lists.newArrayList();
+        List<Object> parameters = Lists.newArrayList();
+        int parametersOffset = 0;
+
+        InsertValueContext insertValueContext = new InsertValueContext(assignments,parameters, parametersOffset);
+
+        Method calculateParametersCountMethod = InsertValueContext.class.getDeclaredMethod("calculateParametersCount", Collection.class);
+        calculateParametersCountMethod.setAccessible(true);
+        int calculateParametersCountResult = (int) calculateParametersCountMethod.invoke(insertValueContext, new Object[] {assignments});
+        assertThat(insertValueContext.getParametersCount(), is(calculateParametersCountResult));
+
+        Method getValueExpressionsMethod = InsertValueContext.class.getDeclaredMethod("getValueExpressions", Collection.class);
+        getValueExpressionsMethod.setAccessible(true);
+        List<ExpressionSegment> getValueExpressionsResult = (List<ExpressionSegment>) getValueExpressionsMethod.invoke(insertValueContext, new Object[] {assignments});
+        assertThat(insertValueContext.getValueExpressions(), is(getValueExpressionsResult));
+
+        Method getParametersMethod = InsertValueContext.class.getDeclaredMethod("getParameters", new Class[]{List.class, int.class});
+        getParametersMethod.setAccessible(true);
+        List<Object> getParametersResult = (List<Object>) getParametersMethod.invoke(insertValueContext, new Object[] {parameters, parametersOffset});
+        assertThat(insertValueContext.getParameters(), is(getParametersResult));
+    }
+
+    @Test
+    public void assertGetValueWhenParameterMarker() {
+        Collection assignments = makeParameterMarkerExpresstionSegment();
+        String parameterValue = "test";
+        List parameters = Lists.newArrayList(parameterValue);
 
 Review comment:
   To avoid this problem, it seams that the following constructor should been defined : 
   
   ```java 
   public InsertValueContext(final Collection<? extends ExpressionSegment> assignments, final List<? extends Object> parameters, final int parametersOffset) {
   ```
   
   that is, assignments and parameters should use generic subtypes with the format of "? extends XXX". 
   
   If there is another solution, please let know. 
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services