dawidwys commented on a change in pull request #10942: [FLINK-15487][table] Allow registering FLIP-65 functions in TableEnvironment URL: https://github.com/apache/flink/pull/10942#discussion_r371863785
########## File path: flink-table/flink-table-api-java/src/test/java/org/apache/flink/table/functions/UserDefinedFunctionHelperTest.java ########## @@ -0,0 +1,293 @@ +/* + * 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.flink.table.functions; + +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.util.Collector; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; + +import javax.annotation.Nullable; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link UserDefinedFunctionHelper}. + */ +@RunWith(Parameterized.class) +@SuppressWarnings("unused") +public class UserDefinedFunctionHelperTest { + + @Parameterized.Parameters + public static List<TestSpec> testData() { + return Arrays.asList( + TestSpec + .forClass(ValidScalarFunction.class) + .expectSuccess(), + + TestSpec + .forInstance(new ValidScalarFunction()) + .expectSuccess(), + + TestSpec + .forClass(PrivateScalarFunction.class) + .expectErrorMessage( + "Function class '" + PrivateScalarFunction.class.getName() + "' is not public."), + + TestSpec + .forClass(MissingImplementationScalarFunction.class) + .expectErrorMessage( + "Function class '" + MissingImplementationScalarFunction.class.getName() + + "' does not implement a method named 'eval'."), + + TestSpec + .forClass(PrivateMethodScalarFunction.class) + .expectErrorMessage( + "Method 'eval' of function class '" + PrivateMethodScalarFunction.class.getName() + + "' is not public."), + + TestSpec + .forInstance(new ValidTableAggregateFunction()) + .expectSuccess(), + + TestSpec + .forInstance(new MissingEmitTableAggregateFunction()) + .expectErrorMessage( + "Function class '" + MissingEmitTableAggregateFunction.class.getName() + + "' does not implement a method named 'emitUpdateWithRetract' or 'emitValue'."), + + TestSpec + .forInstance(new ValidTableFunction()) + .expectSuccess(), + + TestSpec + .forInstance(new ParameterizedTableFunction(12)) + .expectSuccess(), + + TestSpec + .forClass(ParameterizedTableFunction.class) + .expectErrorMessage( + "Function class '" + ParameterizedTableFunction.class.getName() + + "' must have a public default constructor."), + + TestSpec + .forClass(HierarchicalTableAggregateFunction.class) + .expectSuccess() + ); + } + + @Parameter + public TestSpec testSpec; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testInstantiation() { + if (testSpec.functionClass != null) { + if (testSpec.expectedErrorMessage != null) { + thrown.expect(ValidationException.class); + thrown.expectMessage(testSpec.expectedErrorMessage); + } + assertThat( + UserDefinedFunctionHelper.instantiateFunction(testSpec.functionClass), + notNullValue()); + } + } + + @Test + public void testValidation() { + if (testSpec.expectedErrorMessage != null) { + thrown.expect(ValidationException.class); + thrown.expectMessage(testSpec.expectedErrorMessage); + } + if (testSpec.functionClass != null) { + UserDefinedFunctionHelper.validateClass(testSpec.functionClass); + } else if (testSpec.functionInstance != null) { + UserDefinedFunctionHelper.prepareInstance(new TableConfig(), testSpec.functionInstance); + } + } + + // -------------------------------------------------------------------------------------------- + // Test utilities + // -------------------------------------------------------------------------------------------- + + /** + * Test specification shared with the Scala tests. Review comment: nit: Is this comment valid? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
