/*
* Copyright 2011 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.comuns.relational;
import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import java.util.Iterator;
import java.util.List;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import br.com.objectos.comuns.relational.EntityIteratorGen;
import br.com.objectos.comuns.relational.EntityIteratorGenGuice;
import br.com.objectos.comuns.relational.QueryExec;
import br.com.objectos.comuns.relational.search.Page;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
@Test
public class TesteDeIteradorDeEntidade {
private EntityIteratorGen gen;
private Asserts asserts;
private List<String> itens;
@BeforeClass
public void setUp() {
QueryExec exec = new FakeQueryExec();
gen = new EntityIteratorGenGuice(exec);
}
@BeforeMethod
public void reset() {
asserts = new Asserts();
itens = newArrayList();
}
public void genShouldDelegateToExecWithCorrectParameters() {
Class<String> entityClass = String.class;
Iterator<String> it = gen.iteratorFor(entityClass);
assertThat(it, is(notNullValue()));
assertThat(it.hasNext(), is(false));
assertThat(asserts.entityClass, equalTo(entityClass));
assertThat(asserts.page.getFirstIndex(), equalTo(0));
assertThat(asserts.page.getLength(), equalTo(50));
}
private class Asserts {
Class<String> entityClass;
Page page;
}
private class FakeQueryExec extends EmptyQueryExec {
@SuppressWarnings("unchecked")
@Override
public <E> List<E> listPage(Class<?> entityClass, Page page) {
asserts.entityClass = (Class<String>) entityClass;
asserts.page = page;
return (List<E>) itens;
}
}
}