All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.interpreter.collection.CollectionInterpreter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2006 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
 * or see the FSF site: http://www.fsf.org.
 */

package com.greenpepper.interpreter.collection;

import com.greenpepper.*;
import com.greenpepper.annotation.Annotations;
import com.greenpepper.call.Annotate;
import com.greenpepper.call.Compile;
import com.greenpepper.interpreter.AbstractTableInterpreter;
import com.greenpepper.interpreter.CollectionHeaderForm;
import com.greenpepper.interpreter.column.Column;
import com.greenpepper.interpreter.column.ExpectedColumn;
import com.greenpepper.interpreter.column.NullColumn;
import com.greenpepper.reflect.CollectionProvider;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.reflect.NoSuchMessageException;
import com.greenpepper.util.StringUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.greenpepper.GreenPepper.canContinue;
import static com.greenpepper.GreenPepper.shouldStop;
import static com.greenpepper.annotation.Annotations.exception;
import static com.greenpepper.annotation.Annotations.ignored;
import static com.greenpepper.util.ExampleUtil.contentOf;

// TODO: STATS compile stats here and in derived classes ( no test for that yet )
/**
 * 

Abstract CollectionInterpreter class.

* * @author oaouattara * @version $Id: $Id */ public abstract class CollectionInterpreter extends AbstractTableInterpreter { /** *

statistics.

* * @return a {@link com.greenpepper.Statistics} object. */ public Statistics statistics() { return this.stats; } /** *

Constructor for CollectionInterpreter.

* * @param fixture a {@link com.greenpepper.reflect.Fixture} object. */ protected CollectionInterpreter( Fixture fixture ) { super(fixture); } protected ExpectedColumn parseColumn(Example header ) { try { return CollectionHeaderForm.parse(header.getContent()).selectColumn(); } catch (Exception e) { header.annotate( exception( e ) ); stats.exception(); return new NullColumn(); } } /** *

toList.

* * @param results a {@link java.lang.Object} object. * @return a {@link java.util.List} object. */ private List toList(Object results) { if (results instanceof Object[]) { return Arrays.asList( (Object[]) results ); } if (results instanceof Collection) { return new ArrayList( (Collection) results ); } return null; } /** *

executeRow.

* * @param valuesRow a {@link com.greenpepper.Example} object. * @param headers a {@link com.greenpepper.Example} object. * @param rowFixtureAdapter a {@link com.greenpepper.reflect.Fixture} object. */ private void executeRow(Example valuesRow, Example headers, Fixture rowFixtureAdapter) { valuesRow.annotate( Annotations.right() ); Statistics rowStats = new Statistics(); for (int i = 0; i != valuesRow.remainings(); ++i) { Example cell = valuesRow.at( i ); if (i < headers.remainings()) { // We can do the cast because #parseColumn returns an ExpectedColumn ExpectedColumn column = (ExpectedColumn) columns[i]; try { chekOneCell(rowFixtureAdapter, rowStats, cell, column); } catch (Exception e) { cell.annotate( exception( e ) ); stats.exception(); } } else { cell.annotate( ignored( cell.getContent() ) ); } } applyRowStatistic(rowStats); } /** *

addSurplusRow.

* * @param example a {@link com.greenpepper.Example} object. * @param headers a {@link com.greenpepper.Example} object. * @param rowFixtureAdapter a {@link com.greenpepper.reflect.Fixture} object. */ protected void addSurplusRow( Example example, Example headers, Fixture rowFixtureAdapter) { Example row = example.addSibling(); for (int i = 0; i < headers.remainings(); i++) { ExpectedColumn column = (ExpectedColumn) columns[i]; Example cell = row.addChild(); try { Call call = new Call( rowFixtureAdapter.check( column.header() ) ); Object actual = call.execute(); cell.setContent( TypeConversion.toString(actual)); cell.annotate( Annotations.surplus() ); if (i == 0) // Notify test listener on first cell only { stats.wrong(); } } catch (Exception e) { // TODO: STATS count stats? cell.annotate( ignored( e ) ); } } } /** *

missingRow.

* * @param row a {@link com.greenpepper.Example} object. */ protected void missingRow( Example row ) { Example firstChild = row.firstChild(); firstChild.annotate( Annotations.missing() ); stats.wrong(); if (firstChild.hasSibling()) { for (Example cell : firstChild.nextSibling()) { cell.annotate( Annotations.missing() ); } } } private List getCollectionProvider() { Object target = fixture.getTarget(); for (Method method : target.getClass().getMethods()) { if (method.isAnnotationPresent( CollectionProvider.class )) { return toList(invoke( target, method )); } } return null; } private Object invoke( Object target, Method method ) { try { return method.invoke( target ); } catch (Exception e) { return null; } } /** *

getFixtureList.

* * @return a {@link java.util.List} object. * @throws java.lang.Exception if any. */ public List getFixtureList() throws Exception { List results = getCollectionProvider(); if (results == null) { results = toList( fixture.getTarget() ); } if (results == null) { try { Call query = new Call( fixture.check( "query" ) ); results = toList( query.execute() ); } catch (NoSuchMessageException ignored) {} } if (results == null) throw new IllegalArgumentException( "results parameter is neither an Object[] nor a Collection" ); List fixtures = new ArrayList(); for (Object object : results) { fixtures.add( fixture.fixtureFor( object ) ); } return fixtures; } /** *

mustProcessMissing.

* * @return a boolean. */ protected boolean mustProcessMissing() { return false; } /** *

mustProcessSurplus.

* * @return a boolean. */ protected boolean mustProcessSurplus() { return false; } /** {@inheritDoc} */ public void interpret( Specification specification ) { stats = new Statistics(); Example table = specification.nextExample(); columns = parseColumns(table); execute( table.at( 0, 1 ) ); specification.exampleDone( stats ); } /** *

execute.

* * @param example a {@link com.greenpepper.Example} object. */ public void execute( Example example ) { try { List fixtures = getFixtureList(); Example headers = example.at( 0, 0 ); if (columns == null) { columns = getHeaderColumns(headers); } if (example.hasSibling()) { RowFixtureSplitter splitter = new RowFixtureSplitter(); splitter.split(example.at(1), fixtures, columns); for (RowFixture rowFixture : splitter.getMatch()) { Example row = rowFixture.getRow(); executeRow(row.firstChild(), headers, rowFixture.getAdapter()); if (shouldStop(stats)) { row.addChild().annotate(Annotations.stopped()); break; } } if (mustProcessMissing() && canContinue(stats)) { for (Example row : splitter.getMissing()) { missingRow(row); if (shouldStop(stats)) { row.addChild().annotate(Annotations.stopped()); break; } } } if (mustProcessSurplus() && canContinue(stats)) { for (Fixture adapter : splitter.getSurplus()) { addSurplusRow(example, headers, adapter); if (shouldStop(stats)) { example.lastSibling().addChild().annotate(Annotations.stopped()); break; } } } } } catch (Exception e) { stats.exception(); example.firstChild().annotate( exception( e ) ); if (shouldStop( stats )) { example.addChild().annotate(Annotations.stopped()); } } } private Column[] getHeaderColumns(Example headersRow) throws Exception { ArrayList columns = new ArrayList(); for (Example header: headersRow) { columns.add(CollectionHeaderForm.parse(header.getContent()).selectColumn()); } return columns.toArray(new Column[columns.size()]); } /** *

applyRowStatistic.

* * @param rowStats a {@link com.greenpepper.Statistics} object. */ protected void applyRowStatistic(Statistics rowStats) { if (rowStats.exceptionCount() > 0) { stats.exception(); } else if (rowStats.wrongCount() > 0) { stats.wrong(); } else if (rowStats.rightCount() > 0) { stats.right(); } else { stats.ignored(); } } protected void chekOneCell(Fixture rowFixtureAdapter, Statistics rowStats, Example cell, ExpectedColumn column) throws Exception { if (StringUtil.isBlank(contentOf(cell))) { column.setCheck(rowFixtureAdapter.check(column.header())); column.doCell(cell, rowStats); } else { Call call = new Call( rowFixtureAdapter.check( column.header() ) ); call.expect( cell.getContent() ); call.will( Annotate.withDetails( cell ) ); call.will( Compile.statistics( rowStats ) ); call.execute(); } } }