r3146 - in branches/pollen-1.2.6-struts2/pollen-persistence/src: main/java/org/chorem/pollen main/java/org/chorem/pollen/business/persistence test/java/org/chorem/pollen test/java/org/chorem/pollen/business test/java/org/chorem/pollen/business/persistence test/resources
Author: tchemit Date: 2012-02-26 18:19:03 +0100 (Sun, 26 Feb 2012) New Revision: 3146 Url: http://chorem.org/repositories/revision/pollen/3146 Log: add poll account dao method + tests for dao Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PolleIOUtil.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/ branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/ branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/AbstractDAOTest.java branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollAccountDAOTest.java branches/pollen-1.2.6-struts2/pollen-persistence/src/test/resources/pollen-fake.properties Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenConfiguration.java Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PolleIOUtil.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PolleIOUtil.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PolleIOUtil.java 2012-02-26 17:19:03 UTC (rev 3146) @@ -0,0 +1,66 @@ +/* + * #%L + * Pollen :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; + +/** + * Some utils IO methods. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PolleIOUtil { + + /** Logger. */ + private static final Log log = LogFactory.getLog(PolleIOUtil.class); + + protected PolleIOUtil() { + } + + public static File getTestSpecificDirectory(Class<?> klass, String methodName, long timestamp) { + // Trying to look for the temporary folder to store data for the test + String tempDirPath = System.getProperty("java.io.tmpdir"); + if (tempDirPath == null) { + // can this really occur ? + tempDirPath = ""; + if (log.isWarnEnabled()) { + log.warn("'\"java.io.tmpdir\" not defined"); + } + } + File tempDirFile = new File(tempDirPath); + + // create the directory to store database data + String dataBasePath = klass + + File.separator // a directory with the test class name + + methodName // a sub-directory with the method name + + '_' + + timestamp; // and a timestamp + File databaseFile = new File(tempDirFile, dataBasePath); + return databaseFile; + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PolleIOUtil.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenConfiguration.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenConfiguration.java 2012-02-25 14:59:05 UTC (rev 3145) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenConfiguration.java 2012-02-26 17:19:03 UTC (rev 3146) @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; +import java.util.Map; import java.util.Properties; /** @@ -55,10 +56,10 @@ protected ApplicationConfig applicationConfig; public PollenConfiguration() { - this("pollen.properties"); + this("pollen.properties", null); } - public PollenConfiguration(String file) { + public PollenConfiguration(String file, Properties defaultOptions) { applicationConfig = new ApplicationConfig(); applicationConfig.setConfigFileName(file); @@ -66,6 +67,15 @@ if (log.isInfoEnabled()) { log.info(this + " is initializing..."); } + if (defaultOptions != null) { + for (Map.Entry<Object, Object> entry : defaultOptions.entrySet()) { + + applicationConfig.setDefaultOption( + String.valueOf(entry.getKey()), + String.valueOf(entry.getValue()) + ); + } + } try { applicationConfig.loadDefaultOptions(PollenConfigurationOption.class); applicationConfig.parse(); Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java 2012-02-26 17:19:03 UTC (rev 3146) @@ -0,0 +1,48 @@ +/* + * #%L + * Pollen :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.business.persistence; + +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; + +public class PollAccountDAOImpl<E extends PollAccount> extends PollAccountDAOAbstract<E> { + + public PollAccount getRestrictedPollAccount(String pollId, + String accountId) throws TopiaException { + +// List<PersonToList> tmp = transaction.find( +// "FROM " + PersonToList.class.getName() + +// " WHERE pollAccount.accountId = :accountUId" + +// " AND votingList.poll.pollId = :pollUId", +// "accountUId", accountId, +// "pollUId", pollId); + + TopiaQuery query = new TopiaQuery(PersonToList.class,"p"). + setSelect("p."+PersonToList.PROPERTY_POLL_ACCOUNT). + addWhere("p."+PersonToList.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_ACCOUNT_ID, TopiaQuery.Op.EQ, accountId). + addWhere("p."+PersonToList.PROPERTY_VOTING_LIST + "." + VotingList.PROPERTY_POLL + "." + Poll.PROPERTY_POLL_ID, TopiaQuery.Op.EQ, pollId); + PollAccount result = findByQuery(query); + return result; + } +} //PollAccountDAOImpl<E extends PollAccount> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/AbstractDAOTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/AbstractDAOTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/AbstractDAOTest.java 2012-02-26 17:19:03 UTC (rev 3146) @@ -0,0 +1,145 @@ +/* + * #%L + * Pollen :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.business.persistence; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PolleIOUtil; +import org.chorem.pollen.PollenConfiguration; +import org.chorem.pollen.PollenTopiaRootContextFactory; +import org.junit.Rule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; + +import java.io.File; +import java.util.Properties; + +/** + * Base test for daos. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class AbstractDAOTest { + + /** Logger. */ + private static final Log log = LogFactory.getLog(AbstractDAOTest.class); + + public static final String CONFIGURATION_PATH = "pollen-fake.properties"; + + public static final long TIMESTAMP = System.nanoTime(); + + @Rule + public T3Database db = new T3Database(); + + public T3Database getDb() { + return db; + } + + public TopiaContext beginTransaction() throws TopiaException { + return db.beginTransaction(); + } + + public PollenConfiguration getConfiguration() { + return getDb().getConfiguration(); + } + + /** + * A new database created for each test. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0 + */ + public static class T3Database extends TestWatcher { + + private File testBasedir; + + private TopiaContext rootCtxt; + + private PollenConfiguration configuration; + + public PollenConfiguration getConfiguration() { + return configuration; + } + + @Override + protected void starting(Description description) { + + testBasedir = PolleIOUtil.getTestSpecificDirectory( + description.getTestClass(), + description.getMethodName(), + TIMESTAMP); + + // create the database + rootCtxt = new PollenTopiaRootContextFactory().newEmbeddedDatabase( + testBasedir, + "/" + CONFIGURATION_PATH + ); + + Properties props = new Properties(); + props.put("testDirectory", testBasedir); + configuration = new PollenConfiguration(CONFIGURATION_PATH, props); + } + + @Override + public void finished(Description description) { + + TopiaContextImplementor rootContext = + ((TopiaContextImplementor) rootCtxt).getRootContext(); + + if (log.isInfoEnabled()) { + log.info("release database " + + rootContext.getConfig().get(TopiaContextFactory.CONFIG_URL)); + } + try { + if (rootContext != null && !rootContext.isClosed()) { + rootContext.closeContext(); + } + } catch (TopiaException e) { + // we don't want this to throw an exception, wants to close + // all possible opened contexts + if (log.isErrorEnabled()) { + log.error("Could not close context " + rootContext, e); + } + } + } + + public File getTestBasedir() { + return testBasedir; + } + + public TopiaContext getRootCtxt() { + return rootCtxt; + } + + public TopiaContext beginTransaction() throws TopiaException { + return rootCtxt.beginTransaction(); + } + } + +} Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/AbstractDAOTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollAccountDAOTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollAccountDAOTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollAccountDAOTest.java 2012-02-26 17:19:03 UTC (rev 3146) @@ -0,0 +1,90 @@ +/* + * #%L + * Pollen :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.business.persistence; + +import org.chorem.pollen.entities.PollenDAOHelper; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; + +/** + * Test the {@link PollAccountDAO}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollAccountDAOTest extends AbstractDAOTest { + + @Test + public void getRestrictedPollAccount() throws Exception { + TopiaContext tx = beginTransaction(); + + PollDAO pollDAO = PollenDAOHelper.getPollDAO(tx); + + String pollId = "pollId"; + String accountId = "accountId"; + + PollAccountDAO pollAccountDAO = PollenDAOHelper.getPollAccountDAO(tx); + PollAccount pollAccount = pollAccountDAO.newInstance(); + pollAccount.setAccountId(accountId); + pollAccount = pollAccountDAO.create(pollAccount); + + Poll poll = pollDAO.newInstance(); + poll.setPollId(pollId); + poll = pollDAO.create(poll); + + + VotingListDAO votingListDAO = PollenDAOHelper.getVotingListDAO(tx); + + VotingList votingList = votingListDAO.newInstance(); + + votingList.setPoll(poll); + votingList = votingListDAO.create(votingList); + + + PersonToListDAO personToListDAO = PollenDAOHelper.getPersonToListDAO(tx); + + PersonToList personToList = personToListDAO.newInstance(); + personToList.setPollAccount(pollAccount); + personToList.setVotingList(votingList); + personToList.setPollAccount(pollAccount); + + personToList = personToListDAO.create(personToList); + + poll.addVotingList(votingList); + pollAccount.addVotingListPersonToList(personToList); + + pollDAO.update(poll); + pollAccountDAO.update(pollAccount); + + + PollAccount restrictedPollAccount = + pollAccountDAO.getRestrictedPollAccount(pollId, accountId); + + Assert.assertNotNull(restrictedPollAccount); + Assert.assertEquals(accountId, restrictedPollAccount.getAccountId()); + Assert.assertNotNull(restrictedPollAccount.getVotingListPersonToList()); + Assert.assertTrue(restrictedPollAccount.getVotingListPersonToList().contains(personToList)); + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollAccountDAOTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/resources/pollen-fake.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/test/resources/pollen-fake.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/test/resources/pollen-fake.properties 2012-02-26 17:19:03 UTC (rev 3146) @@ -0,0 +1,38 @@ +### +# #%L +# Pollen :: Persistence +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2009 - 2012 CodeLutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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 Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# #L% +### +hibernate.show_sql=false +hibernate.hbm2ddl.auto=update +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.connection.username=sa +hibernate.connection.password=sa +hibernate.connection.driver_class=org.h2.Driver + +# Migration configuration +topia.service.migration=org.nuiton.topia.migration.TopiaMigrationEngine +topia.service.migration.callback=org.chorem.pollen.entities.migration.PollenMigrationCallback +topia.service.migration.showSql=true + +upImgDir=${testDirectory}/.pollen/uploadedImages +feedDir=${testDirectory}/.pollen/feeds +pollen.emails.directory=${testDirectory}/.pollen/emails Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/test/resources/pollen-fake.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.chorem.org