gxu.software_engineering.shen10.market.service.ItemServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for gxu.software_engineering.shen10.market.service.ItemServiceTest.java

Source

/*
 * Copyright 2013 Department of Computer Science and Technology, Guangxi University
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package gxu.software_engineering.shen10.market.service;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import java.util.Calendar;
import java.util.List;

import gxu.software_engineering.shen10.market.entity.Category;
import gxu.software_engineering.shen10.market.entity.Item;
import gxu.software_engineering.shen10.market.entity.User;

import javax.inject.Inject;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author longkai()
 * @email  im.longkai@gmail.com
 * @since  2013-6-19
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/spring.xml")
@Transactional
public class ItemServiceTest {

    @Inject
    private ItemService itemService;

    @Inject
    private UserService userService;

    @Inject
    private CategoryService categoryService;

    private Item item;
    private User seller;
    private Category category;

    @Before
    public void setUp() throws Exception {
        item = new Item();
        seller = userService.profile(1L);
        category = categoryService.view(1L);

        item.setName("");
        item.setDescription("??????????????????????");
        item.setPrice(99.9F);
    }

    //   @Test(expected = RuntimeException.class)
    //   public void testAddWithLimit() {
    ////      ???so
    //      itemService.create(item, category.getId(), seller.getId());
    //   }

    @Test
    public void testModify() {
        itemService.create(item, category.getId(), seller.getId());
        assertThat(item.getId(), notNullValue());

        String newName = "newName";
        item.setName(newName);
        assertThat(itemService.modify(item, category.getId(), seller.getId()).getName(), is(newName));
    }

    @Test
    public void testClose() {
        itemService.create(item, category.getId(), seller.getId());
        assertThat(item.getId(), notNullValue());

        item = itemService.close(true, seller.getId(), item.getId());
        assertThat(item.isClosed(), is(true));
    }

    @Test
    public void testOpen() {
        itemService.create(item, category.getId(), seller.getId());
        assertThat(item.getId(), notNullValue());

        item = itemService.close(false, seller.getId(), item.getId());
        assertThat(item.isClosed(), is(false));
    }

    @Test
    public void testDetail() {
        Item i = itemService.detail(1L);
        assertThat(i, notNullValue());
    }

    @Test
    public void testLatest() {
        List<Item> items = itemService.latest(49);
        assertThat(items.size() >= 2, is(true));
        assertThat(items.get(0).getId() > items.get(1).getId(), is(true));
    }

    @Test
    public void testListLongInt() {
        long target = 4L;
        List<Item> items = itemService.list(target, 50);
        assertThat(items.get(0).getId() < target, is(true));
    }

    @Test
    public void testSizeBoolean() {
        long size = itemService.size(true);
        System.err.println(size);
        assertThat(size, not(0L));
        size = itemService.size(false);
        System.err.println(size);
        //      ??????
        //      assertThat(itemService.size(true), is(0L));
    }

    @Test
    public void testSizeLongBoolean() {
        //      ??so
        //      long size = itemService.size(2, true);
        //      assertThat(size, is(2L));
        //      size = itemService.size(2, false);
        //      assertThat(size, is(0L));
    }

    @Test
    public void testListLongIntBooleanLong() {
        long size = itemService.size(category.getId());
        //      ???
        //      assertThat(size, is(1L));
    }

    @Test
    public void testListWithCategory() {
        List<Item> list = itemService.list(category.getId(), 5, 0);
        int size = list.size();
        System.err.println(size);
        assertThat(size >= 1, is(true));
    }

    @Test
    public void testListLongIntLong() {
        //      ??????
        //      List<Item> list = itemService.list(1, 5, true, 0);
        //      assertThat(list.size(), is(2));
        //      
        //      list = itemService.list(1, 5, true, 2);
        //      assertThat(list.size(), is(1));
    }

    @Test
    public void testSizeLong() {

    }

    @Test
    public void testHot() {
        //      ??-.-
        //      List<Item> items = itemService.hot(50);
        //      assertThat(items.get(0).getId(), is(2L));
    }

    @Test
    public void testSync() {
        Calendar c = Calendar.getInstance();
        c.set(2013, Calendar.JUNE, 24);
        List<Item> items = itemService.sync(c.getTimeInMillis(), 50);
        System.err.println(items.size());
        assertThat(items.size() > 1, is(true));
    }

}