net.eusashead.hateoas.hal.http.converter.HalTestUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.hal.http.converter.HalTestUtils.java

Source

package net.eusashead.hateoas.hal.http.converter;

/*
 * #[license]
 * spring-halbuilder
 * %%
 * Copyright (C) 2013 Eusa's Head
 * %%
 * 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.
 * %[license]
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory;

public class HalTestUtils {

    private static RepresentationFactory representationFactory = new StandardRepresentationFactory();

    private static Log log = LogFactory.getLog(HalTestUtils.class);

    /**
     * Read a Hal {@link ReadableRepresentation}
     * from the supplied file
     * @param path
     * @return
     */
    public static ReadableRepresentation halFromFile(String path) {
        Reader reader = null;
        try {
            reader = new FileReader(new File(path));
            return representationFactory.readRepresentation(reader);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }

    /**
     * Read the supplied file 
     * into a String for comparison 
     * purposes
     * @param file
     * @return
     * @throws IOException
     */
    public static String stringFromFile(String file) throws IOException {
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;

            String ls = System.getProperty("line.separator");

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }
            stringBuilder.setLength(stringBuilder.length() - 1);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                log.error(e);
            }
        }

        return stringBuilder.toString();
    }
}