test.it.unimi.dsi.lang.ObjectParserTest 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.
package it.unimi.dsi.lang;
/*
* DSI utilities
*
* Copyright (C) 2010-2018 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 .
*
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Set;
import org.junit.Test;
public class ObjectParserTest {
@Test
public void testObject() throws Exception {
assertEquals(Object.class, ObjectParser.fromSpec("java.lang.Object").getClass());
assertEquals(Object.class, ObjectParser.fromSpec("java.lang.Object()").getClass());
assertEquals(Object.class, ObjectParser.fromSpec("Object", Object.class, new String[] { "java.lang" }).getClass());
assertEquals(Object.class, ObjectParser.fromSpec("Object", Object.class, new String[] { "foo", "java.lang" }).getClass());
}
@Test
public void testString() throws Exception {
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(foo)"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(\"foo\")"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(foo)"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(\"foo\")"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(foo)"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(\"f\\oo\")"));
assertEquals("f\\oo", ObjectParser.fromSpec("java.lang.String(f\\oo)"));
assertEquals("foo", ObjectParser.fromSpec("java.lang.String(\"foo\")"));
assertEquals("fo\"o", ObjectParser.fromSpec("java.lang.String(\"fo\\\"o\")"));
boolean error = false;
try {
ObjectParser.fromSpec("java.lang.String(\"fo\"o\")");
}
catch(final IllegalArgumentException thisIsWhatWeWant) {
error = true;
}
assertTrue(error);
error = false;
try {
ObjectParser.fromSpec("java.lang.String(fo");
}
catch(final IllegalArgumentException thisIsWhatWeWant) {
error = true;
}
assertTrue(error);
error = false;
try {
ObjectParser.fromSpec("java.lang.String()", Set.class);
}
catch(final ClassCastException thisIsWhatWeWant) {
error = true;
}
assertTrue(error);
assertEquals("", ObjectParser.fromSpec("java.lang.String()"));
assertEquals("", ObjectParser.fromSpec("java.lang.String"));
assertEquals(")foo", ObjectParser.fromSpec("java.lang.String()foo)"));
}
public final static class FactoryMethodAfter {
public static FactoryMethodAfter getInstance(@SuppressWarnings("unused") final String a) {
return new FactoryMethodAfter();
}
@Override
public boolean equals(final Object o) {
return true;
}
@Override
public int hashCode() {
return 0;
}
}
@Test
public void testTwoStrings() throws Exception {
final Object context = new Object();
assertEquals(new TwoStrings("foo", "bar"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(\"foo\", \"bar\")"));
assertEquals(new TwoStrings("2", "2"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(\"foo\", \"bar\")", Object.class, null, new String[] { "getInstance" }));
assertEquals(new FactoryMethodAfter(), ObjectParser.fromSpec("it.unimi.dsi.lang.ObjectParserTest$FactoryMethodAfter(unused)", Object.class, null, ObjectParser.DEFAULT_FACTORY_METHODS));
assertEquals(new TwoStrings("foo", "bar"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(\"foo\", \"bar\")"));
assertEquals(new TwoStrings("foo", "bar"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(foo, bar)"));
assertEquals(new TwoStrings("foo", "bar"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(foo , bar)"));
assertEquals(new TwoStrings("", ""), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(,)"));
assertEquals(new TwoStrings("", ""), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(,)"));
assertEquals(new TwoStrings("", ""), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(,\"\")"));
assertEquals(new TwoStrings("foo", "foo"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(foo)", Object.class, null, new String[] { "getInstance" }));
assertEquals(new TwoStrings("3", "3"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(foo,bar,boo)", Object.class, null, new String[] { "getInstance" }));
assertEquals(new TwoStrings("foo", "3"), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(foo,bar,boo)", Object.class));
assertEquals(new TwoStrings(context, "foo", "bar"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(\"foo\", \"bar\")"));
assertEquals(new TwoStrings(context, "foo", "bar"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(foo, bar)"));
assertEquals(new TwoStrings(context, "foo", "bar"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(foo , bar)"));
assertEquals(new TwoStrings(context, "", ""), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(,)"));
assertEquals(new TwoStrings(context, "", ""), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(,)"));
assertEquals(new TwoStrings(context, "", ""), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(,\"\")"));
assertEquals(new TwoStrings(context, "foo", "foo"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(foo)", Object.class, null, new String[] { "getInstance" }));
assertEquals(new TwoStrings(context, "3", "3"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(foo,bar,boo)", Object.class, null, new String[] { "getInstance" }));
assertEquals(new TwoStrings(context, "foo", "3"), ObjectParser.fromSpec(context, "it.unimi.dsi.lang.TwoStrings(foo,bar,boo)", Object.class));
}
@Test
public void testTwoStringsNull() throws Exception {
assertEquals(new TwoStrings("foo", (String)null), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(\"foo\",null)"));
assertEquals(new TwoStrings((String)null, (String)null), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(null,null)"));
assertEquals(new TwoStrings("null", (String)null), ObjectParser.fromSpec("it.unimi.dsi.lang.TwoStrings(\"null\",null)"));
}
}