/**
* Copyright (C) 2008 rweber <quietgenie@users.sourceforge.net>
*
* This file is part of CsvObjectMapper.
*
* CsvObjectMapper is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CsvObjectMapper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CsvObjectMapper. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*/
package com.projectnine.csvmapper;
import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import com.projectnine.csvmapper.CsvToObjectMapper;
import com.projectnine.spring.SpringLoader;
/**
* @author robweber
*
*/
public class CsvToObjectMapperTest extends TestCase {
public static final String csvFileContent = "\"hello kitty\",\"1.21\"\n"
+ "\"goodbye kitty\",\"3.6\"\n";
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
try {
SpringLoader.loadSpringConfiguration(new ClassPathResource(
"csvObjectMapper-test.xml"));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
SpringLoader.reset();
}
/**
* Test method for
* {@link com.projectnine.csvmapper.CsvToObjectMapper#generateObjectFromCsv(java.lang.String, org.springframework.core.io.Resource, boolean)}
* .
*/
@Test
public final void testGenerateSimpleObjectFromCsv() throws Exception {
try {
CsvToObjectMapper csvToObjectMapper = new CsvToObjectMapper(
new InputStreamResource(new ByteArrayInputStream(
csvFileContent.getBytes())), false,
"simpleCsvMappingDefinition");
// assertTrue(csvToObjectMapper.loadNextRecord());
CsvSimpleTestBean bean = (CsvSimpleTestBean) csvToObjectMapper
.generateNextObjectFromCsv();
assertEquals("hello kitty", bean.getFoo());
assertEquals(new BigDecimal("1.21").toString(), bean.getBar()
.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* Test method for
* {@link com.projectnine.csvmapper.CsvToObjectMapper#generateObjectFromCsv(java.lang.String, org.springframework.core.io.Resource, boolean)}
* .
*/
@Test
public final void testGenerateComplexObjectFromCsv() throws Exception {
try {
CsvToObjectMapper csvToObjectMapper = new CsvToObjectMapper(
new InputStreamResource(new ByteArrayInputStream(
csvFileContent.getBytes())), false,
"complexCsvMappingDefinition");
// assertTrue(csvToObjectMapper.loadNextRecord());
CsvComplexTestBean bean = (CsvComplexTestBean) csvToObjectMapper
.generateNextObjectFromCsv();
assertEquals("hello kitty", bean.getFoo());
assertEquals("hello kitty", bean.getBar().getFoo());
assertEquals(new BigDecimal("1.21").toString(), bean.getBar()
.getBar().toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}