com.sfs.dao.GadgetPreferencesDAOImplTest.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.dao.GadgetPreferencesDAOImplTest.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.dao;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;

import javax.annotation.Resource;

import com.sfs.beans.GadgetPreferencesBean;
import com.sfs.beans.GadgetsPreferencesBean;
import com.sfs.beans.UserBean;
import com.mockobjects.servlet.MockHttpServletRequest;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * The Class GadgetPreferencesDAOImplTest.
 *
 * @author David Harrison
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring-context.xml" })
public class GadgetPreferencesDAOImplTest {

    /** The url. */
    private final String url = "http://www.trademegadgets.com/my_auctions/gadget/my_auctions.xml";

    /** The title. */
    private final String title = "TradeMe Gadget Test";

    /** The width. */
    private final int width = 200;

    /** The height. */
    private final int height = 250;

    /** The user pref keys. */
    private final String[] userPrefKeys = { "nickname", "showBids", "width" };

    /** The user pref values. */
    private final String[] userPrefValues = { "nickname", "true", "standard" };

    /** The position. */
    private final int position = 2;

    /** The gadget preferences dao. */
    @Resource
    private GadgetPreferencesDAOImpl gadgetPreferencesDAO;

    /** The test user. */
    @Resource
    private UserBean testUser;

    /**
     * Test of load method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testLoadString() throws Exception {
        System.out.println("load");

        final int expResult = 3;

        GadgetsPreferencesBean result = this.gadgetPreferencesDAO.load(testUser.getDN());

        assertEquals(expResult, result.getGadgetPreferences().size());
    }

    /**
     * Test of loadMap method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testLoadMap() throws Exception {
        System.out.println("loadMap");

        final int expResult = 3;

        HashMap<Integer, GadgetPreferencesBean> result = this.gadgetPreferencesDAO.loadMap(testUser.getDN());

        assertEquals(expResult, result.size());
    }

    /**
     * Test of load method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testLoadInt() throws Exception {
        System.out.println("load");

        final String expResult = testUser.getDN();

        int gadgetPreferenceId = 1;

        GadgetPreferencesBean result = this.gadgetPreferencesDAO.load(gadgetPreferenceId);

        assertEquals(expResult, result.getUserDn());
    }

    /**
     * Test of load method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testLoadHttpServletRequest() throws Exception {
        System.out.println("load");

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setupAddParameter("URL", url);
        request.setupAddParameter("Title", title);
        request.setupAddParameter("Width", String.valueOf(width));
        request.setupAddParameter("Height", String.valueOf(height));
        request.setupAddParameter("UP_" + userPrefKeys[0], userPrefValues[0]);
        request.setupAddParameter("UP_" + userPrefKeys[1], userPrefValues[1]);
        request.setupAddParameter("UP_" + userPrefKeys[2], userPrefValues[2]);

        GadgetPreferencesBean result = this.gadgetPreferencesDAO.load(request);

        assertEquals(title, result.getTitle());
    }

    /**
     * Test of setGadgetPreference method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testSetAndRemoveGadgetPreference() throws Exception {
        System.out.println("setGadgetPreference");

        // Load the existing GadgetsPreferencesBean for the default user
        GadgetsPreferencesBean gadgetsPreferences = this.gadgetPreferencesDAO.load(testUser.getDN());

        final int expResult = gadgetsPreferences.getGadgetPreferences().size();

        // Setup a new GadgetPreferencesBean to add to this preference set
        GadgetPreferencesBean newGadgetPreference = newGadgetPreferencesBean();

        gadgetsPreferences = this.gadgetPreferencesDAO.setGadgetPreference(gadgetsPreferences, newGadgetPreference);

        final int setResult = gadgetsPreferences.getGadgetPreferences().size();

        // The expected result should be one more than the original result
        assertEquals(expResult + 1, setResult);

        // Now remove this gadget that was just added
        System.out.println("removeGadgetPreference");

        gadgetsPreferences = this.gadgetPreferencesDAO.removeGadgetPreference(gadgetsPreferences,
                newGadgetPreference);

        final int removeResult = gadgetsPreferences.getGadgetPreferences().size();

        // The expected result should be one more than the original result
        assertEquals(expResult, removeResult);
    }

    /**
     * Test of reorderGadgets method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testReorderGadgets() throws Exception {
        System.out.println("reorderGadgets");

        GadgetsPreferencesBean gadgetsPreferences = this.gadgetPreferencesDAO.load(testUser.getDN());

        List<Integer> gadgetIdList = (List<Integer>) gadgetsPreferences.getOrderedGadgetIds();

        // Reverse the order of the Gadget IDs
        Collections.reverse(gadgetIdList);

        // Build the orderString
        String expResult = "";
        for (int gadgetId : gadgetIdList) {
            if (expResult.compareTo("") != 0) {
                expResult += ",";
            }
            expResult += String.valueOf(gadgetId);
        }

        GadgetsPreferencesBean reorderedGadgetsPreferences = this.gadgetPreferencesDAO
                .reorderGadgets(gadgetsPreferences, expResult);

        String result = "";

        Collection<Integer> reorderedGadgets = reorderedGadgetsPreferences.getOrderedGadgetIds();
        for (int gadgetId : reorderedGadgets) {
            if (result.compareTo("") != 0) {
                result += ",";
            }
            result += String.valueOf(gadgetId);
        }

        assertEquals(expResult, result);
    }

    /**
     * Test of create method, of class GadgetPreferencesDAOImpl.
     *
     * @throws Exception the exception
     */
    @Test
    public final void testCreateUpdateDelete() throws Exception {
        System.out.println("create");

        final boolean expResult = true;

        GadgetPreferencesBean newGadgetPreference = newGadgetPreferencesBean();

        // Create the gadget preference
        int gadgetPreferenceId = this.gadgetPreferencesDAO.create(newGadgetPreference);

        boolean create = false;
        if (gadgetPreferenceId > 0) {
            create = true;
        }
        assertEquals(expResult, create);

        // Now load this new preference and update it
        System.out.println("update");

        GadgetPreferencesBean gadgetPreferences = this.gadgetPreferencesDAO.load(gadgetPreferenceId);

        // Modify a couple of values
        gadgetPreferences.setTitle(gadgetPreferences.getTitle() + " - Modified");
        final int addHeight = 100;
        gadgetPreferences.setHeight(gadgetPreferences.getHeight() + addHeight);

        boolean update = this.gadgetPreferencesDAO.update(gadgetPreferences);

        assertEquals(expResult, update);

        // Now delete this gadget preference
        System.out.println("delete");
        boolean delete = this.gadgetPreferencesDAO.delete(gadgetPreferences);

        assertEquals(expResult, delete);
    }

    /**
     * A function for preparing the default GadgetPreferencesBean.
     *
     * @return the gadget preferences bean
     */
    private GadgetPreferencesBean newGadgetPreferencesBean() {
        GadgetPreferencesBean newGadgetPreference = new GadgetPreferencesBean();
        // Set default values
        newGadgetPreference.setUrl(url);
        newGadgetPreference.setTitle(title);
        newGadgetPreference.setHeight(height);
        newGadgetPreference.setWidth(width);
        newGadgetPreference.setPosition(position);
        newGadgetPreference.setUserPref(userPrefKeys[0], userPrefValues[0]);
        newGadgetPreference.setUserPref(userPrefKeys[1], userPrefValues[1]);
        newGadgetPreference.setUserPref(userPrefKeys[2], userPrefValues[2]);
        newGadgetPreference.setUserDn(testUser.getDN());

        return newGadgetPreference;
    }

}