LineSetTest.java :  » Database-ORM » csvbeans » org » csvbeans » beans » Java Open Source

Java Open Source » Database ORM » csvbeans 
csvbeans » org » csvbeans » beans » LineSetTest.java
/*
 * Copyright 2004-2005 Fouad HAMDI.
 *
 * 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.csvbeans.beans;

import org.csvbeans.LineSet;
import org.csvbeans.parsers.LinesReadingException;
import org.csvbeans.utils.MessageContainer;
import org.csvbeans.utils.MessagesBundle;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import org.jmock.core.Constraint;

/**
 * Contains tests for the <code>LineSet</code> class.
 * 
 * @author Fouad Hamdi
 * @since 0.7
 */
public class LineSetTest extends MockObjectTestCase {
    private LineSet lineSet;
    private Mock messageContainer;

    protected void setUp() throws Exception {
        lineSet = new LineSet();
        lineSet.addLine("Line 1");
        lineSet.addLine("Line 2");
        messageContainer = new Mock(MessageContainer.class);
        MessagesBundle.setInstance(new MessagesBundle((MessageContainer) messageContainer.proxy()));
    }
    
    /**
     * Test the constructor.
     */
    public void testConstructor() throws Exception {
        LineSet ls = new LineSet("Line");
        assertEquals("Line", ls.getFirstLine());
    }
    
    /**
     * Test that an exception is thrown when asking for
     * a too big position.
     */
    public void testGetLineError() throws Exception {
        try {
          messageContainer.expects(once()).method("getMessage")
        .with(new Constraint[] {eq("lineset.position.too.high"), eq(new Object[]{new Integer(3), new Integer(2)})})
        .will(returnValue("errorMessage"));
          
            lineSet.getLine(3);
            fail("An exception should have been thrown");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }
    }

    /**
     * Test that it is possible to set the value of a line at a given position
     * in this line.
     */
    public void testPutValueByPosition() throws Exception {
        lineSet.putValueByPosition(1, 2, 4, "abc");
        assertEquals("Labc 1", lineSet.getLine(1));
        lineSet.putValueByPosition(1, 4, 5, "zz");
        assertEquals("Labzz1", lineSet.getLine(1));
    }

    /**
     * Test that it is possible to set the value of a line at a given position,
     * knowing the start position and the length of the field value.
     */
    public void testPutValueByLength() throws Exception {
        lineSet.putValueByLength(1, 2, 3, "abc");
        assertEquals("Labc 1", lineSet.getLine(1));
    }
    
    /**
     * Test the get of a null line.
     */
    public void testGetNullLine() throws Exception {
        lineSet.addLine(null);
        assertNull(lineSet.getValueByStartAndEnd(3, 1, 2));
        assertNull(lineSet.getValueByStartAndLength(3, 1, 2));
    }
    
    /**
     * Test error cases.
     */
    public void testGetWithInvalidPositions() throws Exception {
        try {
          messageContainer.expects(once()).method("getMessage")
        .with(new Constraint[] {eq("lineset.start.position.too.low")})
        .will(returnValue("errorMessage"));
          
            lineSet.getValueByStartAndEnd(1, 0, 1);
            fail("An exception should have been thrown");
        } catch (LinesReadingException e) {
            assertTrue(true);
        }
        
        try {
          messageContainer.expects(once()).method("getMessage")
        .with(new Constraint[] {eq("lineset.end.position.too.high"), eq(new Object[]{new Integer(6), "Line 1"})})
        .will(returnValue("errorMessage"));
          
            lineSet.getValueByStartAndEnd(1, 1, 10);
            fail("An exception should have been thrown");
        } catch (LinesReadingException e) {
            assertTrue(true);
        }
        
        try {
          messageContainer.expects(once()).method("getMessage")
        .with(new Constraint[] {eq("lineset.length.too.high"), eq(new Integer(6))})
        .will(returnValue("errorMessage"));
          
            lineSet.getValueByStartAndLength(1, 1, 8);
            fail("An exception should have been thrown");
        } catch (LinesReadingException e) {
            assertTrue(true);
        }
        
        try {
          messageContainer.expects(once()).method("getMessage")
        .with(new Constraint[] {eq("lineset.end.position.too.low"), eq(new Object[]{new Integer(3), new Integer(5)})})
        .will(returnValue("errorMessage"));
          
            lineSet.getValueByStartAndEnd(1, 5, 3);
            fail("An exception should have been thrown");
        } catch (LinesReadingException e) {
            assertTrue(true);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.