/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   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.bodington.util;

import java.io.IOException;
import java.io.StringWriter;

import junit.framework.TestCase;

public class CSVWriterTest extends TestCase
{

    private StringWriter output;
    private CSVWriter writer;
    
    public void setUp()
    {
        output = new StringWriter();
        writer = new CSVWriter(output);
    }
    /*
     * Test method for 'org.bodington.util.CSVWriter.write(String[])'
     */
    public void testWriteSimple() throws IOException
    {
        writer.writeln(new String[]{"hello"});
        assertEquals("hello\n", output.toString());
    }
    
    public void testWriteComma() throws IOException
    {
        writer.writeln(new String[]{"hello, people"});
        assertEquals("\"hello, people\"\n", output.toString());
    }
    
    public void testWriteMultipleColumns() throws IOException
    {
        writer.writeln(new String[]{"first", "second", "third"});
        assertEquals("first,second,third\n", output.toString());
    }
    
    public void testWriteLeadingSpace() throws IOException
    {
        writer.write(" hello");
        assertEquals("\" hello\"", output.toString());
    }
    
    public void testWriteWithNewlines() throws IOException
    {
        writer.write("hello\nworld");
        writer.write("goodbye\nworld");
        assertEquals("\"hello\nworld\",\"goodbye\nworld\"", output.toString());
    }
    
    public void testWriteWithQuotes() throws IOException
    {
        writer.write("hello \"me\"");
        assertEquals("\"hello \"\"me\"\"\"", output.toString());
    }
    
    public void testWriteMultipleLines() throws IOException
    {
        writer.write("hello");
        writer.write("people");
        writer.writeln();
        writer.writeln(new String[]{"goodbye", "people"});
        assertEquals("hello,people\ngoodbye,people\n", output.toString());
    }

}
