devinsy-xml/test/fr/devinsy/util/StringListTest.java

402 lines
7.3 KiB
Java

/**
* Copyright (C) 2013,2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils 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.
*
* Devinsy-utils 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 Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.Iterator;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.strings.StringListCharIterator;
import fr.devinsy.util.strings.StringListCharPosition;
/**
*
* @author Christian P. Momon
*/
public class StringListTest
{
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringListTest.class);
/**
*
*/
@Before
public void before()
{
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
*
*/
@Test
public void testCharAt01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(0);
Assert.assertEquals(target, 'a');
//
target = source.charAt(4);
Assert.assertEquals(target, 'e');
//
target = source.charAt(11);
Assert.assertEquals(target, 'l');
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testCharAt02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abc");
source.append("def");
source.append("ghi");
source.append("jkl");
source.append("mno");
//
char target = source.charAt(0);
Assert.assertEquals('a', target);
//
target = source.charAt(4);
Assert.assertEquals('e', target);
//
target = source.charAt(11);
Assert.assertEquals('l', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(-2);
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(100);
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testCharAtException03()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abcdefghijklm");
//
char target = source.charAt(source.length());
Assert.assertEquals('a', target);
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testConstructor01()
{
//
logger.debug("===== test starting...");
String[] source = { "a", "b", "c" };
//
StringList target = new StringList(source);
Assert.assertEquals(3, target.size());
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testConstructor02()
{
//
logger.debug("===== test starting...");
//
StringList target = new StringList("a", "b", "c");
Assert.assertEquals(3, target.size());
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testIteratorOfChar01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("abc");
source.append("def");
source.append("ghi");
source.append("jkl");
source.append("mno");
//
Iterator<Character> iterator = source.iteratorOfChar();
for (int index = 0; index < source.length(); index++)
{
StringListCharPosition position = ((StringListCharIterator) iterator).nextPosition();
System.out.println(index + ":" + source.charAt(index) + " " + position.getCharIndex() + " " + position.getStringIndex() + " " + position.getLocalCharIndex());
Assert.assertTrue(iterator.hasNext());
Character character = ((StringListCharIterator) iterator).next();
Assert.assertEquals(character, new Character(source.charAt(index)));
}
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testSubstring01()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("hamburger");
//
StringList target = source.substring(4, 8);
Assert.assertEquals("urge", target.toString());
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testSubstring02()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("ham").append("bur").append("ger");
//
StringList target = source.substring(4, 8);
Assert.assertEquals("urge", target.toString());
//
logger.debug("===== test done.");
}
/**
*/
@Test
public void testSubstring03()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("smiles");
//
StringList target = source.substring(1, 5);
Assert.assertEquals("mile", target.toString());
//
logger.debug("===== test done.");
}
/**
*/
@Test
public void testSubstring04()
{
//
logger.debug("===== test starting...");
//
StringList source = new StringList();
source.append("sm").append("il").append("es");
//
StringList target = source.substring(1, 5);
Assert.assertEquals("mile", target.toString());
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testToString01()
{
//
logger.debug("===== test starting...");
//
StringList buffer = new StringList();
String target = buffer.toString();
Assert.assertTrue(target.equals(""));
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testToString02()
{
//
logger.debug("===== test starting...");
//
String source = "abcdefghijklm";
StringList buffer = new StringList();
buffer.append(source);
String target = buffer.toString();
Assert.assertEquals(source, target);
Assert.assertTrue(target.equals(source));
//
logger.debug("===== test done.");
}
/**
*
*/
@Test
public void testToString03()
{
//
logger.debug("===== test starting...");
//
String source1 = "abcdefghijklm";
String source2 = "other stuff";
StringList buffer = new StringList();
buffer.append(source1);
buffer.append(source2);
String target = buffer.toString();
Assert.assertTrue(target.equals(source1 + source2));
//
logger.debug("===== test done.");
}
}