com.orangesignal.csv.SpringTest.java Source code

Java tutorial

Introduction

Here is the source code for com.orangesignal.csv.SpringTest.java

Source

/*
 * Copyright 2009 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 com.orangesignal.csv;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.orangesignal.csv.entity.Price;
import com.orangesignal.csv.filters.SimpleBeanFilter;
import com.orangesignal.csv.filters.SimpleCsvNamedValueFilter;
import com.orangesignal.csv.manager.CsvBeanManager;
import com.orangesignal.csv.model.SampleBean;

/**
 * Spring Framework ??
 * 
 * @author Koji Sugisawa
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class SpringTest {

    @Resource
    private CsvConfig cfg;

    @Resource
    private CsvListHandler<Price> handler;

    @Resource
    private CsvBeanManager manager;

    @Test
    public void testCsvConfig() {
        assertThat(cfg.getSeparator(), is(','));
        assertThat(cfg.getQuote(), is('"'));
        assertThat(cfg.getEscape(), is('\\'));
        assertThat(cfg.isQuoteDisabled(), is(false));
        assertThat(cfg.isEscapeDisabled(), is(false));
        assertThat(cfg.getBreakString(), is("\n"));
        assertThat(cfg.getNullString(), is("NULL"));
        assertThat(cfg.isIgnoreCaseNullString(), is(true));
        assertThat(cfg.isIgnoreLeadingWhitespaces(), is(true));
        assertThat(cfg.isIgnoreTrailingWhitespaces(), is(true));
        assertThat(cfg.isIgnoreEmptyLines(), is(true));
        assertThat(cfg.getSkipLines(), is(0));
        assertThat(cfg.getQuotePolicy(), is(QuotePolicy.MINIMAL));
        assertThat(cfg.getLineSeparator(), is("\r\n"));
    }

    @Test
    public void testCsvListHandler() throws Exception {
        final Reader reader = new StringReader(",??,,?,\r\n"
                + "GCQ09,COMEX  200908?,1058.70,10,2008/08/06\r\n"
                + "GCU09,COMEX  200909?,1068.70,10,2008/09/06\r\n"
                + "GCV09,COMEX  200910?,1078.70,11,2008/10/06\r\n"
                + "GCX09,COMEX  200911?,1088.70,12,2008/11/06\r\n"
                + "GCZ09,COMEX  200912?,1098.70,13,2008/12/06\r\n");
        try {
            final List<Price> list = Csv.load(reader, cfg, handler);
            assertThat(list.size(), is(5));
            assertThat(list.get(0).symbol, is("GCQ09"));
            assertThat(list.get(1).symbol, is("GCU09"));
            assertThat(list.get(2).symbol, is("GCV09"));
            assertThat(list.get(3).symbol, is("GCX09"));
            assertThat(list.get(4).symbol, is("GCZ09"));
            assertThat(list.get(0).name, is("COMEX  200908?"));
            assertThat(list.get(1).name, is("COMEX  200909?"));
            assertThat(list.get(2).name, is("COMEX  200910?"));
            assertThat(list.get(3).name, is("COMEX  200911?"));
            assertThat(list.get(4).name, is("COMEX  200912?"));
            assertThat(list.get(0).price.doubleValue(), is(1058.70D));
            assertThat(list.get(1).price.doubleValue(), is(1068.70D));
            assertThat(list.get(2).price.doubleValue(), is(1078.70D));
            assertThat(list.get(3).price.doubleValue(), is(1088.70D));
            assertThat(list.get(4).price.doubleValue(), is(1098.70D));
            assertThat(list.get(0).date, is(new SimpleDateFormat("yyyy/MM/dd").parse("2008/08/06")));
            assertThat(list.get(1).date, is(new SimpleDateFormat("yyyy/MM/dd").parse("2008/09/06")));
            assertThat(list.get(2).date, is(new SimpleDateFormat("yyyy/MM/dd").parse("2008/10/06")));
            assertThat(list.get(3).date, is(new SimpleDateFormat("yyyy/MM/dd").parse("2008/11/06")));
            assertThat(list.get(4).date, is(new SimpleDateFormat("yyyy/MM/dd").parse("2008/12/06")));
        } finally {
            Csv.closeQuietly(reader);
        }
    }

    @Test
    public void testCsvBeanManager() throws Exception {
        assertThat(manager, notNullValue());
        final Reader reader = new StringReader(
                "symbol,name,price,volume,date\r\n" + "GCQ09,COMEX  200908?,1058.70,10,2008/08/06\r\n"
                        + "GCU09,COMEX  200909?,1068.70,10,2008/09/06\r\n"
                        + "GCV09,COMEX  200910?,1078.70,11,2008/10/06\r\n"
                        + "GCX09,COMEX  200911?,1088.70,12,2008/11/06\r\n"
                        + "GCZ09,COMEX  200912?,1098.70,13,2008/12/06\r\n");

        try {
            final DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            final List<SampleBean> list = manager.load(SampleBean.class)
                    .format("date", new SimpleDateFormat("yyyy/MM/dd"))
                    .filter(new SimpleCsvNamedValueFilter().ne("symbol", "gcu09", true))
                    .filter(new SimpleBeanFilter().ne("date", df.parse("2008/11/06"))).offset(1).limit(1)
                    .from(reader);

            assertThat(list.size(), is(1));
            final SampleBean o1 = list.get(0);
            assertThat(o1.symbol, is("GCV09"));
            assertThat(o1.name, is("COMEX  200910?"));
            assertThat(o1.price.doubleValue(), is(1078.70D));
            assertThat(o1.volume.longValue(), is(11L));
            assertThat(o1.date, is(df.parse("2008/10/06")));
        } finally {
            reader.close();
        }
    }

}