Commit 2b84f8f1 authored by Liang Ding's avatar Liang Ding

#12256 持久层单元测试

parent 6e34cc5f
......@@ -35,7 +35,7 @@ import java.util.Locale;
* Abstract test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.1.0.9, Mar 28, 2017
* @version 2.2.0.9, Apr 12, 2017
* @see #beforeClass()
*/
public abstract class AbstractTestCase {
......@@ -52,6 +52,7 @@ public abstract class AbstractTestCase {
* <li>Initializes Latke runtime</li>
* <li>Instantiates repositories</li>
* </ol>
* </p>
*
* @throws Exception exception
*/
......@@ -197,6 +198,24 @@ public abstract class AbstractTestCase {
return beanManager.getReference(OptionRepositoryImpl.class);
}
/**
* Gets category query service.
*
* @return category query service
*/
public CategoryQueryService getCategoryQueryService() {
return beanManager.getReference(CategoryQueryService.class);
}
/**
* Gets category management service.
*
* @return category management service
*/
public CategoryMgmtService getCategoryMgmtService() {
return beanManager.getReference(CategoryMgmtService.class);
}
/**
* Gets initialization service.
*
......
/*
* Copyright (c) 2010-2017, b3log.org & hacpai.com
*
* 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.repository.Transaction;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Category;
import org.b3log.solo.repository.CategoryRepository;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link CategoryRepositoryImpl} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Apr 12, 2017
* @since 2.0.0
*/
@Test(suiteName = "repository")
public final class CategoryRepositoryImplTestCase extends AbstractTestCase {
/**
* Tests.
*
* @throws Exception exception
*/
@Test
public void test() throws Exception {
final CategoryRepository categoryRepository = getCategoryRepository();
final int category1Order = 1, category2Order = 2, category3Order = 3;
JSONObject category1 = new JSONObject();
category1.put(Category.CATEGORY_TITLE, "category title");
category1.put(Category.CATEGORY_DESCRIPTION, "cateogry description");
category1.put(Category.CATEGORY_URI, "category uri");
category1.put(Category.CATEGORY_ORDER, category1Order);
category1.put(Category.CATEGORY_TAG_CNT, 0);
Transaction transaction = categoryRepository.beginTransaction();
categoryRepository.add(category1);
transaction.commit();
Assert.assertNull(categoryRepository.getByTitle("title"));
Assert.assertNotNull(categoryRepository.getByTitle("category title"));
Assert.assertNull(categoryRepository.getByOrder(0));
Assert.assertNotNull(categoryRepository.getByOrder(category1Order));
final JSONObject category2 = new JSONObject();
category2.put(Category.CATEGORY_TITLE, "category title");
category2.put(Category.CATEGORY_DESCRIPTION, "cateogry description");
category2.put(Category.CATEGORY_URI, "category uri");
category2.put(Category.CATEGORY_ORDER, category2Order);
category2.put(Category.CATEGORY_TAG_CNT, 0);
transaction = categoryRepository.beginTransaction();
final String category2Id = categoryRepository.add(category2);
transaction.commit();
Assert.assertEquals(categoryRepository.getMaxOrder(), category2Order);
JSONObject category3 = new JSONObject();
category3.put(Category.CATEGORY_TITLE, "category title");
category3.put(Category.CATEGORY_DESCRIPTION, "cateogry description");
category3.put(Category.CATEGORY_URI, "category uri");
category3.put(Category.CATEGORY_ORDER, category3Order);
category3.put(Category.CATEGORY_TAG_CNT, 0);
transaction = categoryRepository.beginTransaction();
categoryRepository.add(category3);
transaction.commit();
final int total = 3;
Assert.assertEquals(categoryRepository.count(), total);
category1 = categoryRepository.getUpper(category2Id);
Assert.assertNotNull(category1);
Assert.assertEquals(category1.getInt(Category.CATEGORY_ORDER), category1Order);
category3 = categoryRepository.getUnder(category2Id);
Assert.assertNotNull(category3);
Assert.assertEquals(category3.getInt(Category.CATEGORY_ORDER), category3Order);
}
}
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