org.flywaydb.core.internal.util.plus.MysqlToH2ConverterTests.java Source code

Java tutorial

Introduction

Here is the source code for org.flywaydb.core.internal.util.plus.MysqlToH2ConverterTests.java

Source

/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed 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.flywaydb.core.internal.util.plus;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

/**
 * {@link MysqlToH2Converter} tests
 */
public class MysqlToH2ConverterTests {

    /**
     * {@link MysqlToH2Converter#convert(String)}
     */
    @Test
    public void convert() {

        assertThat(new MysqlToH2Converter().convert(this.readFileInClassPath("data/converter/before.sql", "\r\n")),
                equalTo(this.readFileInClassPath("data/converter/after.sql", "\n")));
    }

    /**
     * Read file in class path
     * 
     * @param path path
     * @param joiner joiner
     * @return string in file
     */
    private String readFileInClassPath(String path, String joiner) {

        try {

            return Files.lines(Paths.get(new ClassPathResource(path).getURI()), StandardCharsets.UTF_8)
                    .collect(Collectors.joining(joiner));
        } catch (IOException e) {

            throw new IllegalStateException("Failed to read file", e);
        }
    }
}