net.maurerit.dao.DeckDaoTest.java Source code

Java tutorial

Introduction

Here is the source code for net.maurerit.dao.DeckDaoTest.java

Source

/**
 *  Copyright 2008 - 2012
 *            Matthew L. Maurer maurer.it@gmail.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 net.maurerit.dao;

import static org.junit.Assert.assertEquals;

import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import net.maurerit.test.dao.CardDao;
import net.maurerit.test.dao.DeckDao;

import org.junit.Test;
import org.mage.shared.xmldb.Card;
import org.mage.shared.xmldb.CardPK;
import org.mage.shared.xmldb.Deck;
import org.mage.shared.xmldb.Rarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

/**
 * TODO: Javadoc me
 *
 * @author Matthew L. Maurer maurer.it@gmail.com
 */
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class DeckDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
    @Autowired
    private DeckDao deckDao;
    @Autowired
    private CardDao cardDao;

    @PersistenceContext
    private EntityManager em;

    @Test
    public void saveAndRetrieveADeck() {
        Deck deck = new Deck();
        UUID id = UUID.randomUUID();
        deck.setId(id.toString());
        deck.setName("Name");
        deck.setAuthor("Author");
        deck.setFormat("Format");
        deck.setLocation("Location");

        Card card = cardDao.read(new CardPK("Card", "M12"));

        if (card == null) {
            card = new Card();
            card.setCardNumber(1);
            card.setExpansionSetCode("M12");
            card.setFaceDown(false);
            card.setName("Card");
            card.setNightCard(false);
            card.setPower(1);
            card.setRarity(Rarity.COMMON);
            card.setToughness(1);

            cardDao.create(card);
        }

        deck.getMainBoardCards().add(card);

        deckDao.create(deck);

        em.flush();

        String query = "select count(*) from Deck where id = '" + id.toString() + "'";
        int count = simpleJdbcTemplate.queryForInt(query);
        assertEquals("Deck with id=" + id.toString() + " should exist.", 1, count);

        String cardQuery = "select count(*) from Card where name='Card' and expansionSetCode='M12'";
        count = simpleJdbcTemplate.queryForInt(cardQuery);
        assertEquals("Card with name='Card' and expansionSetCode='M12' should exist.", 1, count);

        Deck retrievedDeck = deckDao.read(id.toString());
        assertEquals("Deck mainboard should have one card.", 1, retrievedDeck.getMainBoardCards().size());
    }
}