Repository: apache/flink
File: flink-metrics/flink-metrics-core/src/test/java/org/apache/flink/metrics/MetricConfigTest.java (Java)

Code before:
 * 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.metrics;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link MetricConfig}. */
class MetricConfigTest {

    // -- String values via setProperty (legacy behavior) --

    @Test
    void testGetIntegerFromString() {
        final MetricConfig config = new MetricConfig();
        config.setProperty("key", "42");
        assertThat(config.getInteger("key", 0)).isEqualTo(42);
    }

    @Test
    void testGetLongFromString() {
        final MetricConfig config = new MetricConfig();
        config.setProperty("key", "123456789012345");
        assertThat(config.getLong("key", 0L)).isEqualTo(123456789012345L);
    }

    @Test
    void testGetFloatFromString() {
        final MetricConfig config = new MetricConfig();
        config.setProperty("key", "3.14");
        assertThat(config.getFloat("key", 0.0f)).isEqualTo(3.14f);
    }

    @Test
    void testGetDoubleFromString() {
        final MetricConfig config = new MetricConfig();
        config.setProperty("key", "2.718281828");
        assertThat(config.getDouble("key", 0.0)).isEqualTo(2.718281828);
    }

Review comment (suggestion):
I am wondering whether we could have a parameterized test for this. As the tests are basically the same for each native type.  

Code after:
 * 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.metrics;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link MetricConfig}. */
class MetricConfigTest {

    @ParameterizedTest
    @MethodSource("fromStringCases")
    void testGetFromString(
            final String storedValue, final TypedGetter getter, final Object expected) {
        final MetricConfig config = new MetricConfig();
        config.setProperty("key", storedValue);
        assertThat(getter.get(config, "key")).isEqualTo(expected);
    }

    @ParameterizedTest
    @MethodSource("nativeTypeCases")
    void testGetFromNativeType(
            final Object storedValue, final TypedGetter getter, final Object expected) {
        final MetricConfig config = new MetricConfig();
        config.put("key", storedValue);
        assertThat(getter.get(config, "key")).isEqualTo(expected);
    }

    @ParameterizedTest
    @MethodSource("crossTypeCases")
    void testGetCrossType(
            final Object storedValue, final TypedGetter getter, final Object expected) {
        final MetricConfig config = new MetricConfig();
        config.put("key", storedValue);
        assertThat(getter.get(config, "key")).isEqualTo(expected);