com.huawei.streaming.cql.CQLTestCommons.java Source code

Java tutorial

Introduction

Here is the source code for com.huawei.streaming.cql.CQLTestCommons.java

Source

/**
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.huawei.streaming.cql;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;

/**
 * 
 *
 */
public class CQLTestCommons {

    /**
     * ???
     */
    public static final String INPUT_POSTFIX = ".cql";

    /**
     * ?
     */
    public static final String OUTPUT_POSTFIX = ".cql.out";

    /**
     * ?
     */
    public static final String RESULT_POSTFIX = ".cql.res";

    /**
     * ??
     * ??CQL?
     */
    public static final String INPUT_DIR_NAME = "input";

    /**
     * ??
     * ??
     */
    public static final String OUTPUT_DIR_NAME = "output";

    /**
     * ??
     * ??output
     */
    public static final String RESULT_DIR_NAME = "result";

    /**
     * Driver
     */
    public static final String DRIVER_TEST_POSTFIX = ".xml";

    /**
     * ?
     */
    public static final Charset CHARSET = Charset.forName("utf-8");

    /**
     * ?? 1000
     */
    public static final int BASICTIMESTAMP = 1000;

    private static final Logger LOG = LoggerFactory.getLogger(CQLTestCommons.class);

    /**
     * 
     *
     * @param f 
     * @param values 
     * @throws IOException ?
     */
    public static void appendToFile(File f, String values) throws IOException {
        if (!f.exists()) {
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            LOG.warn("file " + f.getPath() + " does not exist! will create it.");
            if (!f.createNewFile()) {
                LOG.error("failed to create file " + f.getPath());
                return;
            }
        }
        if (f.isDirectory()) {
            LOG.error("file " + f.getPath() + " is a directory!");
        }
        FileUtils.write(f, values, CHARSET, true);
    }

    /**
     * ??
     *
     * @param f1 1
     * @param f2 2
     * @return ?true
     * @throws IOException ?
     */
    public static boolean compareFileContent(File f1, File f2) throws IOException {
        List<String> f1Contexts = Files.readAllLines(f1.toPath(), CHARSET);
        List<String> f2Contexts = Files.readAllLines(f2.toPath(), CHARSET);

        if (f1Contexts.size() != f2Contexts.size()) {
            return false;
        }
        for (int i = 0; i < f1Contexts.size(); i++) {
            String s1 = f1Contexts.get(i).trim();
            String s2 = f2Contexts.get(i).trim();
            if (Strings.isNullOrEmpty(s1) && Strings.isNullOrEmpty(s2)) {
                continue;
            }

            if (!s1.equals(s2)) {
                return false;
            }
        }
        return true;
    }

    /**
     * ??
     *
     * @param dir 
     * @throws IOException ?
     */
    public static void emptyDir(File dir) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }
        FileUtils.cleanDirectory(dir);
    }

    /**
     * ?sql??
     *
     * @param qf 
     * @return ??sql
     * @throws Exception ?
     */
    public static List<String> addFile(File qf) throws Exception {
        CQLFileReader reader = new CQLFileReader();
        reader.readCQLs(qf.getCanonicalPath());
        return reader.getResult();
    }
}