rmonico/Neutrino — Issue #4: Problema com parsing de código
Author: guerraem
Created: 2011-12-09 11:10:00+00:00
State: open

Esse erro veio de um dos alunos que tentou usar o Neutrino no laboratóro:

java.lang.RuntimeException: Parsing failed
at org.ita.neutrino.testsmells.core.EclipseTestSmellDetector.getParsedCode(EclipseTestSmellDetector.java:91)
at org.ita.neutrino.testsmells.core.EclipseTestSmellDetector.run(EclipseTestSmellDetector.java:61)
at org.ita.neutrino.testsmells.core.EclipseTestSmellDetector.run(EclipseTestSmellDetector.java:51)
at org.ita.neutrino.testsmells.ActionController.runDetection(ActionController.java:51)
at org.ita.neutrino.testsmells.ActionController.run(ActionController.java:42)
at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)
at org.eclipse.ui.internal.WWinPluginAction.runWithEvent(WWinPluginAction.java:229)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
at org.eclipse.equinox.launcher.Main.main(Main.java:1384)
Caused by: org.ita.neutrino.astparser.BlockParser$UnsupportedSintaxException: Sintax não suportada.
at org.ita.neutrino.astparser.BlockParser.parseVariableDeclaration(BlockParser.java:123)
at org.ita.neutrino.astparser.BlockParser.parseStatement(BlockParser.java:61)
at org.ita.neutrino.astparser.BlockParser.parse(BlockParser.java:48)
at org.ita.neutrino.astparser.ASTParser.parseAllCodeBlocksInWorkspace(ASTParser.java:198)
at org.ita.neutrino.astparser.ASTParser.parse(ASTParser.java:76)
at org.ita.neutrino.testsmells.core.EclipseTestSmellDetector.getParsedCode(EclipseTestSmellDetector.java:89)
... 34 more

O código que ele usou é o seguinte:

@RunWith(JMock.class)
public class TestDAO {
Mockery ctx = new JUnit4Mockery();
@Test
public void deveAdicionarObjetoAoCache(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Identifiable obj = ctx.mock(Identifiable.class);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj);
allowing(obj).getID(); will(returnValue(0));
}});
proxy.insert(obj);
assertTrue(proxy.isCached(obj));
}

@Test
public void deveAdicionarAoCacheERecuperarObjeto(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Identifiable obj = ctx.mock(Identifiable.class);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj);
allowing(obj).getID(); will(returnValue(0));
}});
proxy.insert(obj);
assertTrue(proxy.isCached(obj));
assertEquals(obj,proxy.getByID(obj.getID()));
}

@Test
public void deveRecuperarObjetoPorIdEAdicionarAoCache(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Identifiable obj = ctx.mock(Identifiable.class);
final Object id = new Integer(1);
ctx.checking(new Expectations(){{
allowing(obj).getID(); will(returnValue(id));
oneOf(dao).getByID(id); will(returnValue(obj));
}});
assertFalse(proxy.isCached(obj));
Identifiable found = proxy.getByID(id);
assertTrue(proxy.isCached(obj));
assertEquals(obj, found);
}

@Test
public void deveAtualizarObjetoNoCache(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Object id = new Integer(1);
final Identifiable obj1 = new IdentifiableMock(id);
final Identifiable obj2 = new IdentifiableMock(id);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj1);
oneOf(dao).update(obj2);
}});
assertFalse(obj1.equals(obj2));
proxy.insert(obj1);
assertFalse(proxy.isCached(obj2));
proxy.update(obj2);
assertTrue(proxy.isCached(obj2));
}

@Test
public void deveExcluirObjetoDoCache(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Object id = new Integer(1);
final Identifiable obj = new IdentifiableMock(id);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj);
oneOf(dao).delete(id);
}});
proxy.insert(obj);
proxy.delete(id);
assertFalse(proxy.isCached(obj));
}
@Test
public void deveAdicionarListaAoCache(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final List objetos = new ArrayList();
for(int i = 1; i<=5; i++){
objetos.add(new IdentifiableMock(i));
}
ctx.checking(new Expectations(){{
exactly(5).of(dao).insert(with(any(IdentifiableMock.class)));
oneOf(dao).list(); will(returnValue(objetos));
}});
for(Identifiable obj : objetos){
proxy.insert(obj);
}
List lista = proxy.list();
assertEquals(objetos, lista);
}

@Test
public void deveAdicionarRecarregarListaQuandoDeletaObjeto(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final List objetos = new ArrayList();
for(int i = 0; i<5; i++){
objetos.add(new IdentifiableMock(i));
}
ctx.checking(new Expectations(){{
exactly(5).of(dao).insert(with(any(IdentifiableMock.class)));
exactly(2).of(dao).list(); will(returnValue(objetos));
oneOf(dao).delete(4);
}});
for(Identifiable obj : objetos){
proxy.insert(obj);
}
proxy.list();
proxy.delete(4);
objetos.remove(4);
proxy.list();
List lista = proxy.list();
assertEquals(objetos, lista);
}

@Test
public void deveAdicionarRecarregarListaQuandoInsereObjeto(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final List objetos = new ArrayList();
final Identifiable obj = new IdentifiableMock(5);
for(int i = 0; i<5; i++){
objetos.add(new IdentifiableMock(i));
}
ctx.checking(new Expectations(){{
exactly(5).of(dao).insert(with(any(IdentifiableMock.class)));
exactly(2).of(dao).list(); will(returnValue(objetos));
oneOf(dao).insert(obj);
}});
for(Identifiable ident : objetos){
proxy.insert(ident);
}
proxy.list();
proxy.insert(obj);
objetos.add(obj);
proxy.list();
List lista = proxy.list();
assertEquals(objetos, lista);
}
@Test
public void deveRemoverCacheComExcecaoNoInsert(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Object id = new Integer(1);
final Identifiable obj = new IdentifiableMock(id);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj); will(throwException(new RuntimeException()));
}});
try{
proxy.insert(obj);
fail();
}
catch(RuntimeException e){
assertFalse(proxy.isCached(obj));
}
}
@Test
public void deveRemoverCacheComExcecaoNoUpdate(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Object id = new Integer(1);
final Identifiable obj = new IdentifiableMock(id);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj);
oneOf(dao).update(obj); will(throwException(new RuntimeException()));
}});
try{
proxy.insert(obj);
proxy.update(obj);
fail();
}
catch(RuntimeException e){
assertFalse(proxy.isCached(obj));
}
}
@Test
public void deveRemoverCacheComExcecaoNoDelete(){
final BasicDao dao = ctx.mock(BasicDao.class);
ProxyDao proxy = new ProxyDao(dao);
final Object id = new Integer(1);
final Identifiable obj = new IdentifiableMock(id);
ctx.checking(new Expectations(){{
oneOf(dao).insert(obj);
oneOf(dao).delete(id); will(throwException(new RuntimeException()));
}});
try{
proxy.insert(obj);
proxy.delete(id);
fail();
}
catch(RuntimeException e){
assertFalse(proxy.isCached(obj));
}
}

private class IdentifiableMock implements Identifiable{
public Object ID;
public IdentifiableMock(Object id) {
this.ID = id;
}
public IdentifiableMock(int id){
this.ID = new Integer(id);
}
@Override
public Object getID() {
return ID;
}
}

}