UrlDataConverterTest.java :  » Database-ORM » csvbeans » org » csvbeans » converters » Java Open Source

Java Open Source » Database ORM » csvbeans 
csvbeans » org » csvbeans » converters » UrlDataConverterTest.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.converters;

import junit.framework.TestCase;

import org.csvbeans.converters.UrlDataConverter;

/**
 * Tests for the UrlDataConverter class.
 *
 * @author Fouad Hamdi
 * @since 0.7
 */
public class UrlDataConverterTest extends TestCase {
    private UrlDataConverter converter;

    protected void setUp() throws Exception {
        converter = new UrlDataConverter();
        converter.init(null);
    }

    /**
     * Test the encoding/decoding functions.
     */
    public void testEncodingDecoding() throws Exception {
        String decodedValue = "This#is^a&String with reserved @/characters";
        String encodedValue = "This%23is%5Ea%26String+with+reserved+%40%2Fcharacters";

        assertEquals(decodedValue, converter.decode(encodedValue));
        assertEquals(encodedValue, converter.encode(decodedValue));
        assertNull(converter.encode(null));
    }

    /**
     * Test that an exception is thrown when trying to encode invalid data.
     */
    public void testEncodingException() throws Exception {
        try {
            converter.addProperty("charset", "Inexisting");
            converter.init(null);
            converter.encode("Hello");
            fail("An exception should have been thrown");
        } catch (ConverterException e) {
            assertTrue(true);
        }
    }

    /**
     * Test that an exception is thrown when trying to decode invalid data.
     */
    public void testDecodingException() throws Exception {
        try {
            converter.decode("%");
            fail("An exception should have been thrown");
        } catch (ConverterException 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.