diff --git a/impl/core/src/test/java/pagination/DummyEntity.java b/impl/core/src/test/java/pagination/DummyEntity.java new file mode 100644 index 0000000..c691f3c --- /dev/null +++ b/impl/core/src/test/java/pagination/DummyEntity.java @@ -0,0 +1,40 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program 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 Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package pagination; + +public class DummyEntity { +} diff --git a/impl/core/src/test/java/pagination/PaginationBasicTest.java b/impl/core/src/test/java/pagination/PaginationBasicTest.java new file mode 100644 index 0000000..91d4f9a --- /dev/null +++ b/impl/core/src/test/java/pagination/PaginationBasicTest.java @@ -0,0 +1,74 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program 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 Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package pagination; + +import static junit.framework.Assert.assertEquals; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; +import transaction.defaultstrategy.TransactionDefaultTest; +import br.gov.frameworkdemoiselle.internal.implementation.PaginationImpl; +import br.gov.frameworkdemoiselle.pagination.Pagination; + +@RunWith(Arquillian.class) +public class PaginationBasicTest { + + private Pagination pagination; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(TransactionDefaultTest.class); + return deployment; + } + + @Test + public void createDefaultPagination(){ + pagination = new PaginationImpl(); + + assertEquals(pagination.getPageSize(), 0); + assertEquals(pagination.getCurrentPage(), 0); + assertEquals(pagination.getTotalResults(), 0); + assertEquals(pagination.getTotalPages(), 0); + assertEquals(pagination.getFirstResult(), 0); + } + +} diff --git a/impl/core/src/test/java/pagination/PaginationContextBasicTest.java b/impl/core/src/test/java/pagination/PaginationContextBasicTest.java new file mode 100644 index 0000000..e128920 --- /dev/null +++ b/impl/core/src/test/java/pagination/PaginationContextBasicTest.java @@ -0,0 +1,224 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program 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 Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package pagination; + +import static junit.framework.Assert.assertEquals; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; +import transaction.defaultstrategy.TransactionDefaultTest; +import br.gov.frameworkdemoiselle.internal.configuration.PaginationConfig; +import br.gov.frameworkdemoiselle.internal.context.ContextManager; +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; +import br.gov.frameworkdemoiselle.pagination.Pagination; +import br.gov.frameworkdemoiselle.pagination.PaginationContext; + +@RunWith(Arquillian.class) +public class PaginationContextBasicTest { + + private static final int VALID_PAGE_SIZE = 5; + + private static final int VALID_CURRENT_PAGE = 2; + + private static final int VALID_TOTAL_RESULTS = 30; + + private static final int VALID_FIRST_RESULT = 5; + + private static final int INVALID_PAGE_SIZE = -5; + + private static final int INVALID_NEGATIVE_CURRENT_PAGE = -2; + + private static final int INVALID_CURRENT_PAGE = 21; + + private static final int INVALID_TOTAL_RESULTS = -1; + + private static final int INVALID_FIRST_RESULT = VALID_TOTAL_RESULTS + 1; + + private static final int INVALID_NEGATIVE_FIRST_RESULT = -19; + + @Inject + private PaginationContext paginationContext; + + @Inject + private PaginationConfig paginationConfig; + + private Pagination pagination; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(TransactionDefaultTest.class); + return deployment; + } + + @Before + public void activeContext() { + ContextManager.activate(ThreadLocalContext.class, SessionScoped.class); + pagination = paginationContext.getPagination(DummyEntity.class, true); + } + + @After + public void deactiveContext() { + ContextManager.deactivate(ThreadLocalContext.class, SessionScoped.class); + } + + @Test + public void defaultPaginationContext() { + assertEquals(pagination.getPageSize(), paginationConfig.getPageSize()); + assertEquals(pagination.getCurrentPage(), 0); + assertEquals(pagination.getTotalResults(), 0); + assertEquals(pagination.getTotalPages(), + (int) Math.ceil(pagination.getTotalResults() * 1d / pagination.getPageSize())); + assertEquals(pagination.getFirstResult(), pagination.getCurrentPage() * pagination.getPageSize()); + } + + @Test + public void changeToValidValuesAttributes() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setCurrentPage(VALID_CURRENT_PAGE); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + assertEquals(pagination.getPageSize(), VALID_PAGE_SIZE); + assertEquals(pagination.getCurrentPage(), VALID_CURRENT_PAGE); + assertEquals(pagination.getTotalResults(), VALID_TOTAL_RESULTS); + assertEquals(pagination.getTotalPages(), (int) Math.ceil(VALID_TOTAL_RESULTS * 1d / VALID_PAGE_SIZE)); + assertEquals(pagination.getFirstResult(), VALID_CURRENT_PAGE * VALID_PAGE_SIZE); + + pagination.setFirstResult(VALID_FIRST_RESULT); + assertEquals(pagination.getFirstResult(), VALID_FIRST_RESULT); + } + + @Test + public void currentPageGreaterThanTotalPages() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setCurrentPage((int) Math.ceil(VALID_TOTAL_RESULTS * 1d / VALID_PAGE_SIZE) + 1); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + + assertEquals(pagination.getCurrentPage(), (int) Math.ceil(VALID_TOTAL_RESULTS * 1d / VALID_PAGE_SIZE) - 1); + } + + @Test + public void resetCurrentAndTotalPagesWithTotalResults() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setCurrentPage(VALID_CURRENT_PAGE); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + + assertEquals(pagination.getCurrentPage(), VALID_CURRENT_PAGE); + assertEquals(pagination.getTotalPages(), + (int) Math.ceil(pagination.getTotalResults() * 1d / pagination.getPageSize())); + + pagination.setTotalResults(0); + + assertEquals(pagination.getCurrentPage(), 0); + assertEquals(pagination.getTotalPages(), 0); + } + + @Test + public void resetCurrentAndTotalPagesWithPageSize() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setCurrentPage(VALID_CURRENT_PAGE); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + + assertEquals(pagination.getCurrentPage(), VALID_CURRENT_PAGE); + assertEquals(pagination.getTotalPages(), + (int) Math.ceil(pagination.getTotalResults() * 1d / pagination.getPageSize())); + + pagination.setPageSize(0); + + assertEquals(pagination.getCurrentPage(), 0); + assertEquals(pagination.getTotalPages(), 0); + } + + @Test + public void resetCurrentPageWithFirstResult() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setCurrentPage(VALID_CURRENT_PAGE); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + + pagination.setFirstResult(0); + + assertEquals(pagination.getCurrentPage(), 0); + } + + @Test + public void paginationToString() { + assertEquals("PaginationImpl [currentPage=0, pageSize=10, totalResults=0, totalPages=0]", pagination.toString()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changeCurrentPageToInvalidNegativeValuesAttributes() { + pagination.setCurrentPage(INVALID_NEGATIVE_CURRENT_PAGE); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changeCurrentPageToInvalidValuesAttributes() { + pagination.setPageSize(VALID_PAGE_SIZE); + pagination.setTotalResults(VALID_TOTAL_RESULTS); + + pagination.setCurrentPage(INVALID_CURRENT_PAGE); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changePageSizeToInvalidValuesAttributes() { + pagination.setPageSize(INVALID_PAGE_SIZE); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changeTotalResultsToInvalidValuesAttributes() { + pagination.setTotalResults(INVALID_TOTAL_RESULTS); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changeFirstResultToInvalidNegativeValuesAttributes() { + pagination.setFirstResult(INVALID_NEGATIVE_FIRST_RESULT); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void changeFirstResultToInvalidValuesAttributes() { + pagination.setTotalResults(VALID_TOTAL_RESULTS); + pagination.setFirstResult(INVALID_FIRST_RESULT); + } + +} diff --git a/impl/core/src/test/java/pagination/PaginationContextCache.java b/impl/core/src/test/java/pagination/PaginationContextCache.java new file mode 100644 index 0000000..6a88e72 --- /dev/null +++ b/impl/core/src/test/java/pagination/PaginationContextCache.java @@ -0,0 +1,106 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program 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 Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package pagination; + +import static org.junit.Assert.assertEquals; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; +import transaction.defaultstrategy.TransactionDefaultTest; +import br.gov.frameworkdemoiselle.internal.context.ContextManager; +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; +import br.gov.frameworkdemoiselle.pagination.Pagination; +import br.gov.frameworkdemoiselle.pagination.PaginationContext; + +@RunWith(Arquillian.class) +public class PaginationContextCache { + + @Inject + private PaginationContext paginationContext; + + private Pagination paginationOne; + + private Pagination paginationTwo; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(TransactionDefaultTest.class); + return deployment; + } + + @Before + public void activeContext() { + ContextManager.activate(ThreadLocalContext.class, SessionScoped.class); + } + + @After + public void deactiveContext() { + ContextManager.deactivate(ThreadLocalContext.class, SessionScoped.class); + } + + @Test + public void createTwoCachedPagination(){ + paginationOne = paginationContext.getPagination(DummyEntity.class, true); + paginationTwo = paginationContext.getPagination(DummyEntity.class, true); + assertEquals(paginationOne, paginationTwo); + } + + @Test + public void createOneCachedPaginationMethodOne(){ + paginationOne = paginationContext.getPagination(DummyEntity.class, true); + paginationTwo = paginationContext.getPagination(DummyEntity.class); + assertEquals(paginationOne, paginationTwo); + } + + @Test + public void createOneCachedPaginationMethodTwo(){ + paginationOne = paginationContext.getPagination(DummyEntity.class, true); + paginationTwo = paginationContext.getPagination(DummyEntity.class, false); + assertEquals(paginationOne, paginationTwo); + } +} + diff --git a/impl/core/src/test/java/pagination/PaginationContextNullTest.java b/impl/core/src/test/java/pagination/PaginationContextNullTest.java new file mode 100644 index 0000000..b438cd9 --- /dev/null +++ b/impl/core/src/test/java/pagination/PaginationContextNullTest.java @@ -0,0 +1,94 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program 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 Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package pagination; + +import static org.junit.Assert.assertNull; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Inject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import test.Tests; +import transaction.defaultstrategy.TransactionDefaultTest; +import br.gov.frameworkdemoiselle.internal.context.ContextManager; +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; +import br.gov.frameworkdemoiselle.pagination.Pagination; +import br.gov.frameworkdemoiselle.pagination.PaginationContext; + +@RunWith(Arquillian.class) +public class PaginationContextNullTest { + + @Inject + private PaginationContext paginationContext; + + private Pagination pagination; + + @Deployment + public static JavaArchive createDeployment() { + JavaArchive deployment = Tests.createDeployment(TransactionDefaultTest.class); + return deployment; + } + + @Before + public void activeContext() { + ContextManager.activate(ThreadLocalContext.class, SessionScoped.class); + } + + @After + public void deactiveContext() { + ContextManager.deactivate(ThreadLocalContext.class, SessionScoped.class); + } + + @Test + public void createNullPaginationMethodOne(){ + pagination = paginationContext.getPagination(DummyEntity.class); + assertNull(pagination); + } + + @Test + public void createNullPaginationMethodTwo(){ + pagination = paginationContext.getPagination(DummyEntity.class, false); + assertNull(pagination); + } +} -- libgit2 0.21.2