/*---------------------------------------------------------------------------
* Speculoos, LDAP to objet mapping.
* Copyright (C) 2006 Norsys and oQube
*
* 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 2.1 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* --------------------------------------------------------------------------*/
package speculoos;
import java.io.IOException;
import java.io.StringWriter;
import junit.framework.TestCase;
import speculoos.beans.BeanMap;
import speculoos.beans.MapEntry;
import speculoos.beans.MapsGenerator;
import speculoos.beans.SyntheticMap;
public class MapsGeneratorTest extends TestCase {
private StringWriter iface;
private StringWriter impl;
private MapsGenerator gen;
protected void setUp() throws Exception {
super.setUp();
this.iface = new StringWriter();
this.impl = new StringWriter();
this.gen = new MapsGenerator();
this.gen.setOutputInterface(iface);
this.gen.setOutputImpl(impl);
this.gen.setPack("speculoos");
}
public void test01SimpleFlatMap() throws IOException {
this.gen.setName(getName());
SyntheticMap map = new SyntheticMap();
MapEntry me = new MapEntry("toto");
map.put("toto", me);
me = new MapEntry("tutuTiti");
me.setType("int");
me.setValue("0");
map.put("tutuTiti", me);
map.setName("GenTest");
/* run */
gen.generate(map);
String ifas = iface.getBuffer().toString();
System.err.print(ifas);
String im = impl.getBuffer().toString();
System.err.print(im);
assertTrue(ifas.indexOf("interface GenTestIface") != -1);
assertTrue(ifas.indexOf("int getTutuTiti ()") != -1);
assertTrue(ifas.indexOf("void setToto (String toto)") != -1);
assertTrue(im.indexOf("private String toto") != -1);
assertTrue(im.indexOf("private int tutuTiti = 0") != -1);
}
/*
public void test02MapWithMapParents() throws IOException {
SyntheticMap map = new SyntheticMap();
MapEntry me = new MapEntry("toto");
map.put("toto", me);
me = new MapEntry("tutuTiti");
me.setType("int");
me.setValue("0");
map.put("tutuTiti", me);
map.setName("GenTest");
SyntheticMap par = new SyntheticMap();
me = new MapEntry("foo");
me.setType("java.util.List");
par.put("foo", me);
me = new MapEntry("bar");
me.setType("float");
me.setValue("0.0f");
par.put("bar", me);
par.setName("ParentMap");
map.addInherits(par);
gen.generate(map);
String ifas = iface.getBuffer().toString();
System.err.print(ifas);
String im = impl.getBuffer().toString();
System.err.print(im);
assertTrue(ifas.contains("interface GenTestIface extends ParentMapIface"));
assertTrue(ifas.contains("int getTutuTiti ()"));
assertTrue(ifas.contains("void setToto (String toto)"));
assertTrue(im.contains("private java.util.List foo = null"));
assertTrue(im.contains("float getBar ()"));
}
*/
public void test03MapWithIfaceBeanAndMapParents() throws IOException {
SyntheticMap map = new SyntheticMap();
MapEntry me = new MapEntry("toto");
map.put("toto", me);
me = new MapEntry("tutuTiti");
me.setType("int");
me.setValue("0");
map.put("tutuTiti", me);
map.setName("GenTest");
SyntheticMap par = new SyntheticMap();
me = new MapEntry("foo");
me.setType("java.util.List");
par.put("foo", me);
me = new MapEntry("bar");
me.setType("float");
me.setValue("0.0f");
par.put("bar", me);
par.setName("ParentMap");
map.addInherits(par);
BeanMap bean = new BeanMap(IPersonne.class);
map.addInherits(bean);
/* run */
gen.generate(map);
String ifas = iface.getBuffer().toString();
System.err.print(ifas);
String im = impl.getBuffer().toString();
System.err.print(im);
assertTrue(ifas.indexOf("IPersonne") != -1);
assertTrue(ifas.indexOf("int getTutuTiti ()") != -1);
assertTrue(ifas.indexOf("void setToto (String toto)") != -1);
assertTrue(im.indexOf("public int getChildNumber ()") != -1);
}
public void test04MapWithConcreteBeanAndMapParents() throws IOException {
SyntheticMap map = new SyntheticMap();
MapEntry me = new MapEntry("toto");
map.put("toto", me);
me = new MapEntry("tutuTiti");
me.setType("int");
me.setValue("0");
map.put("tutuTiti", me);
map.setName("GenTest");
SyntheticMap par = new SyntheticMap();
me = new MapEntry("foo");
me.setType("java.util.List");
par.put("foo", me);
me = new MapEntry("bar");
me.setType("float");
me.setValue("0.0f");
par.put("bar", me);
par.setName("ParentMap");
map.addInherits(par);
BeanMap bean = new BeanMap(Personne.class);
map.addInherits(bean);
/* run */
gen.generate(map);
String ifas = iface.getBuffer().toString();
System.err.print(ifas);
String im = impl.getBuffer().toString();
System.err.print(im);
assertTrue(im.indexOf("class GenTestImpl extends speculoos.Personne") != -1);
// don't reimplement superclass fields
assertTrue(im.indexOf("public int getChildNumber ()") == -1);
}
public void test05MapWithIfaceBeanHierarchy() throws IOException {
SyntheticMap map = new SyntheticMap();
MapEntry me = new MapEntry("toto");
map.put("toto", me);
me = new MapEntry("tutuTiti");
me.setType("int");
me.setValue("0");
map.put("tutuTiti", me);
map.setName("GenTest");
BeanMap bean = new BeanMap(IPersonne.class);
map.addInherits(bean);
/* run */
gen.generate(map);
String ifas = iface.getBuffer().toString();
System.err.print(ifas);
String im = impl.getBuffer().toString();
System.err.print(im);
assertTrue(ifas.indexOf("IPersonne") != -1);
assertTrue(ifas.indexOf("IHuman") == -1);
assertTrue(im.indexOf("public java.lang.String getGender ()") != -1);
}
}