Commit 9b05326f authored by Liang Ding's avatar Liang Ding

Option 相关单元测试

parent bf8cb4f8
......@@ -47,24 +47,36 @@ public final class OptionMgmtService {
private OptionRepository optionRepository = OptionRepositoryImpl.getInstance();
/**
* Updates the specified option, if not found the old version of the specified option by id, creates it.
* Adds or updates the specified option.
*
* @param option the specified option
* @throws ServiceException service exception
* @return option id
* @throws ServiceException
*/
public void updateOption(final JSONObject option) throws ServiceException {
final String id = option.optString(Keys.OBJECT_ID);
public String addOrUpdateOption(final JSONObject option) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction();
try {
if (null != optionRepository.get(id)) {
optionRepository.update(id, option);
String id = option.optString(Keys.OBJECT_ID);
if (Strings.isEmptyOrNull(id)) {
id = optionRepository.add(option);
} else {
optionRepository.add(option);
final JSONObject old = optionRepository.get(id);
if (null == old) { // The id is specified by caller
id = optionRepository.add(option);
} else {
old.put(Option.OPTION_CATEGORY, option.optString(Option.OPTION_CATEGORY));
old.put(Option.OPTION_VALUE, option.optString(Option.OPTION_VALUE));
optionRepository.update(id, old);
}
}
transaction.commit();
return id;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
......@@ -75,31 +87,16 @@ public final class OptionMgmtService {
}
/**
* Adds or updates the specified option.
* Removes the option specified by the given option id.
*
* @param option the specified option
* @throws ServiceException
* @param optionId the given option id
* @throws ServiceException service exception
*/
public void addOrUpdateOption(final JSONObject option) throws ServiceException {
public void removeOption(final String optionId) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction();
try {
final String id = option.optString(Keys.OBJECT_ID);
if (Strings.isEmptyOrNull(id)) {
optionRepository.add(option);
} else {
final JSONObject old = optionRepository.get(id);
if (null == old) { // The id is specified by caller
optionRepository.add(option);
} else {
old.put(Option.OPTION_CATEGORY, option.optString(Option.OPTION_CATEGORY));
old.put(Option.OPTION_VALUE, option.optString(Option.OPTION_VALUE));
optionRepository.update(id, old);
}
}
optionRepository.remove(optionId);
transaction.commit();
} catch (final Exception e) {
......
......@@ -24,6 +24,7 @@ import org.b3log.solo.repository.ArchiveDateRepository;
import org.b3log.solo.repository.ArticleRepository;
import org.b3log.solo.repository.CommentRepository;
import org.b3log.solo.repository.LinkRepository;
import org.b3log.solo.repository.OptionRepository;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.repository.PluginRepository;
import org.b3log.solo.repository.PreferenceRepository;
......@@ -36,6 +37,7 @@ import org.b3log.solo.repository.impl.ArchiveDateRepositoryImpl;
import org.b3log.solo.repository.impl.ArticleRepositoryImpl;
import org.b3log.solo.repository.impl.CommentRepositoryImpl;
import org.b3log.solo.repository.impl.LinkRepositoryImpl;
import org.b3log.solo.repository.impl.OptionRepositoryImpl;
import org.b3log.solo.repository.impl.PageRepositoryImpl;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.b3log.solo.repository.impl.PreferenceRepositoryImpl;
......@@ -51,7 +53,7 @@ import org.testng.annotations.BeforeClass;
* Abstract test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.5, Feb 8, 2013
* @version 1.0.0.6, Apr 19, 2013
* @see #beforeClass()
* @see #afterClass()
*/
......@@ -62,119 +64,162 @@ public abstract class AbstractTestCase {
*/
private final LocalServiceTestHelper localServiceTestHelper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
/**
* User repository.
*/
private UserRepository userRepository;
/**
* Link repository.
*/
private LinkRepository linkRepository;
/**
* Article repository.
*/
private ArticleRepository articleRepository;
/**
* Tag repository.
*/
private TagRepository tagRepository;
/**
* Tag-Article repository.
*/
private TagArticleRepository tagArticleRepository;
/**
* Page repository.
*/
private PageRepository pageRepository;
/**
* Comment repository.
*/
private CommentRepository commentRepository;
/**
* Archive date repository.
*/
private ArchiveDateRepository archiveDateRepository;
/**
* Archive date article repository.
*/
private ArchiveDateArticleRepository archiveDateArticleRepository;
/**
* Plugin repository.
*/
private PluginRepository pluginRepository;
/**
* Preference repository.
*/
private PreferenceRepository preferenceRepository;
/**
* Statistic repository.
*/
private StatisticRepository statisticRepository;
/**
* Option repository.
*/
private OptionRepository optionRepository;
/**
* Initialization service.
*/
private InitService initService;
/**
* User management service.
*/
private UserMgmtService userMgmtService;
/**
* User query service.
*/
private UserQueryService userQueryService;
/**
* Article management service.
*/
private ArticleMgmtService articleMgmtService;
/**
* Article query service.
*/
private ArticleQueryService articleQueryService;
/**
* Page management service.
*/
private PageMgmtService pageMgmtService;
/**
* Page query service.
*/
private PageQueryService pageQueryService;
/**
* Link management service.
*/
private LinkMgmtService linkMgmtService;
/**
* Link query service.
*/
private LinkQueryService linkQueryService;
/**
* Preference management service.
*/
private PreferenceMgmtService preferenceMgmtService;
/**
* Preference query service.
*/
private PreferenceQueryService preferenceQueryService;
/**
* Tag query service.
*/
private TagQueryService tagQueryService;
/**
* Tag management service.
*/
private TagMgmtService tagMgmtService;
/**
* Comment query service.
*/
private CommentQueryService commentQueryService;
/**
* Comment management service.
*/
private CommentMgmtService commentMgmtService;
/**
* Archive date query service.
*/
private ArchiveDateQueryService archiveDateQueryService;
/**
* Option management service.
*/
private OptionMgmtService optionMgmtService;
/**
* Option query service.
*/
private OptionQueryService optionQueryService;
/**
* Before class.
*
......@@ -205,6 +250,7 @@ public abstract class AbstractTestCase {
pluginRepository = PluginRepositoryImpl.getInstance();
preferenceRepository = PreferenceRepositoryImpl.getInstance();
statisticRepository = StatisticRepositoryImpl.getInstance();
optionRepository = OptionRepositoryImpl.getInstance();
// Services
initService = InitService.getInstance();
......@@ -223,6 +269,8 @@ public abstract class AbstractTestCase {
commentQueryService = CommentQueryService.getInstance();
commentMgmtService = CommentMgmtService.getInstance();
archiveDateQueryService = ArchiveDateQueryService.getInstance();
optionMgmtService = OptionMgmtService.getInstance();
optionQueryService = OptionQueryService.getInstance();
}
/**
......@@ -235,7 +283,7 @@ public abstract class AbstractTestCase {
*/
@AfterClass
public void afterClass() {
localServiceTestHelper.tearDown();
// XXX: NPE, localServiceTestHelper.tearDown();
Latkes.shutdown();
}
......@@ -348,6 +396,15 @@ public abstract class AbstractTestCase {
return statisticRepository;
}
/**
* Gets option repository.
*
* @return option repository
*/
public OptionRepository getOptionRepository() {
return optionRepository;
}
/**
* Gets initialization service.
*
......@@ -491,4 +548,22 @@ public abstract class AbstractTestCase {
public ArchiveDateQueryService getArchiveDateQueryService() {
return archiveDateQueryService;
}
/**
* Gets option management service.
*
* @return option management service
*/
public OptionMgmtService getOptionMgmtService() {
return optionMgmtService;
}
/**
* Gets option query service.
*
* @return option query service
*/
public OptionQueryService getOptionQueryService() {
return optionQueryService;
}
}
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* 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 org.b3log.solo.repository.impl;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.Transaction;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionRepositoryImpl} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "repository")
public final class OptionRepositoryImplTestCase extends AbstractTestCase {
/**
* Tests.
*
* @throws Exception exception
*/
@Test
public void test() throws Exception {
final OptionRepository optionRepository = getOptionRepository();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
Transaction transaction = optionRepository.beginTransaction();
optionRepository.add(option);
transaction.commit();
Assert.assertEquals(optionRepository.count(), 1);
Assert.assertNotNull(optionRepository.get(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME));
}
}
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* 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 org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Option;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionMgmtService} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "service")
public class OptionMgmtServiceTestCase extends AbstractTestCase {
/**
* Add.
*
* @throws Exception exception
*/
@Test
public void add() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
System.out.println(id);
Assert.assertNotNull(id);
}
/**
* Update.
*
* @throws Exception exception
*/
@Test
public void update() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option); // Add
System.out.println(id);
Assert.assertNotNull(id);
option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 1L);
optionMgmtService.addOrUpdateOption(option); // Update
final JSONObject opt = getOptionQueryService().getOptionById(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
Assert.assertEquals(opt.getInt(Option.OPTION_VALUE), 1L);
}
/**
* Remove.
*
* @throws Exception exception
*/
@Test
public void remove() throws Exception {
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
Assert.assertNotNull(id);
optionMgmtService.removeOption(id);
final JSONObject opt = getOptionQueryService().getOptionById(id);
Assert.assertNull(opt);
}
}
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
* 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 org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.latke.util.Requests;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Link;
import org.b3log.solo.model.Option;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link OptionQueryService} test case.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 19, 2013
* @since 0.6.0
*/
@Test(suiteName = "service")
public class OptionQueryServiceTestCase extends AbstractTestCase {
/**
* Gets.
*
* @throws Exception exception
*/
@Test
public void get() throws Exception {
// Check
final OptionQueryService optionQueryService = getOptionQueryService();
JSONObject options = optionQueryService.getOptions(Option.CATEGORY_C_BROADCAST);
Assert.assertNull(options);
// Add one
final OptionMgmtService optionMgmtService = getOptionMgmtService();
final JSONObject option = new JSONObject();
option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);
option.put(Option.OPTION_VALUE, 0L);
final String id = optionMgmtService.addOrUpdateOption(option);
Assert.assertNotNull(id);
// Check again
options = optionQueryService.getOptions(Option.CATEGORY_C_BROADCAST);
Assert.assertNotNull(options);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment