test.it.unimi.dsi.parser.BulletParserTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsiutils Show documentation
Show all versions of dsiutils Show documentation
The DSI utilities are a mishmash of classes accumulated during the last twenty years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.
/*
* DSI utilities
*
* Copyright (C) 2010-2020 Sebastiano Vigna
*
* This library 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.
*
* This library 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 this program; if not, see .
*
*/
package it.unimi.dsi.parser;
import static org.junit.Assert.assertEquals;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import com.google.common.base.Charsets;
import it.unimi.dsi.lang.MutableString;
import it.unimi.dsi.parser.callback.Callback;
import it.unimi.dsi.parser.callback.DefaultCallback;
public class BulletParserTest {
@Test
public void testParser() throws FileNotFoundException, IOException {
final char[] text = IOUtils.toCharArray(this.getClass().getResourceAsStream("test.data"), Charsets.UTF_8);
final Callback mockCallback = (Callback)Proxy.newProxyInstance(Callback.class.getClassLoader(), new Class[] { Callback.class }, new InvocationHandler() {
int call = 0;
String[] methods = { "configure", "startDocument", "endDocument" };
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (call < methods.length)
assertEquals(method.getName(), methods[call++]);
return Boolean.TRUE;
}
});
new BulletParser().setCallback(mockCallback).parse(text, 0, text.length);
}
private final static class VisibleBulletParser extends BulletParser {
@Override
public int scanEntity(final char[] a, final int offset, final int length, final boolean loose, final MutableString entity) {
return super.scanEntity(a, offset, length, loose, entity);
}
}
@Test
public void testScanEntityAtEndOfArray() {
final VisibleBulletParser parser = new VisibleBulletParser();
char[] test = "&test".toCharArray();
assertEquals(-1, parser.scanEntity(test, 0, test.length, false, new MutableString()));
assertEquals(-1, parser.scanEntity(test, 0, test.length, true, new MutableString()));
test = "&apos".toCharArray();
assertEquals(-1, parser.scanEntity(test, 0, test.length, false, new MutableString()));
assertEquals(5, parser.scanEntity(test, 0, test.length, true, new MutableString()));
}
@Test
public void testCdata() {
final BulletParser parser = new BulletParser();
final Callback callback = new DefaultCallback() {
@Override
public boolean cdata(final Element element, final char[] text, final int offset, final int length) {
assertEquals("Test > 0", new String(text, offset, length));
return true;
}
};
parser.setCallback(callback);
parser.parseCDATA(true);
parser.parse(" 0]]> ".toCharArray());
parser.parse(" 0".toCharArray());
}
}