Package

Source Code of ItemTest

import com.directededge.Database;
import com.directededge.Database.ResourceException;
import com.directededge.Item;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ItemTest
{
    private Database database;

    public ItemTest()
    {

    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {

    }

    @AfterClass
    public static void tearDownClass() throws Exception
    {

    }

    @Before
    public void setUp() throws ResourceException
    {
        database = new Database("testdb", "test");
        database.importFromFile("../testdb.xml");
    }

    @After
    public void tearDown()
    {

    }

    @Test
    public void create()
    {
        Item item = new Item(database, "test");
        item.linkTo("customer0");
        item.linkTo("customer1");
        item.save();

        item = new Item(database, "test");
        assertTrue(item.getLinks("").containsKey("customer0"));
        assertTrue(item.getLinks("").containsKey("customer1"));
    }

    @Test
    public void delete() throws ResourceException
    {
        Item item = new Item(database, "customer1");
        assertEquals(item.getLinks().size(), 1);
        item.destroy();
        item = new Item(database, "customer1");
        assertEquals(item.getLinks().size(), 0);

        boolean thrown = false;

        try
        {
            item.destroy();
        }
        catch (ResourceException ex)
        {
            thrown = true;
        }

        assertTrue(thrown);
    }

    @Test
    public void unweightedLinks()
    {
        Item customer0 = new Item(database, "customer0");
        customer0.linkTo("customer1");
        customer0.save();

        customer0 = new Item(database, "customer0");
        assertTrue(customer0.getLinks("").containsKey("customer1"));

        customer0.unlinkFrom("customer1");
        customer0.save();
        customer0 = new Item(database, "customer0");
        assertFalse(customer0.getLinks("").containsKey("customer1"));
    }

    @Test
    public void weightedLinks()
    {
        Item customer0 = new Item(database, "customer0");
        customer0.linkTo("product7", 5);
        customer0.save();

        customer0 = new Item(database, "customer0");
        assertTrue(customer0.getLinks("").containsKey("product7"));
        assertEquals((int) customer0.getLinks("").get("product7"), 5);
        assertEquals(customer0.weightFor("product7"), 5);

        customer0.unlinkFrom("product7");
        customer0.save();
        customer0 = new Item(database, "customer0");
        assertFalse(customer0.getLinks("").containsKey("product7"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void weightedLinksUpperLimit()
    {
        Item customer = new Item(database, "customer0");
        Item product = new Item(database, "product0");
        customer.linkTo(product, 11);
    }

    @Test(expected = IllegalArgumentException.class)
    public void weightedLinksLowerLimit()
    {
        Item customer = new Item(database, "customer0");
        Item product = new Item(database, "product0");
        customer.linkTo(product, -1);
    }

    @Test
    public void typedLinks()
    {
        Item customer = new Item(database, "customer0");
        Item product = new Item(database, "product0");
        customer.linkTo(product, "purchase");
        customer.save();

        customer = new Item(database, "customer0");
        assertTrue(customer.getLinks("purchase").containsKey("product0"));

        customer.linkTo(product, 5, "wishlist");
        customer.save();

        customer = new Item(database, "customer0");
        customer.getLinks();
        assertEquals(5, customer.getLinks("wishlist").get("product0").intValue());
    }

    @Test
    public void tags()
    {
        Item customer = new Item(database, "customer0");
        assertTrue(customer.getTags().contains("customer"));
        customer.addTag("test");
        assertTrue(customer.getTags().contains("test"));
        customer.save();
        customer = new Item(database, "customer0");
        assertTrue(customer.getTags().contains("test"));
        customer.removeTag("test");
        assertFalse(customer.getTags().contains("test"));
        customer.save();
        customer = new Item(database, "customer0");
        assertFalse(customer.getTags().contains("test"));
    }

    @Test
    public void properties()
    {
        Item customer = new Item(database, "customer0");
        customer.setProperty("test", "first");
        assertEquals("first", customer.getProperty("test"));
        customer.setProperty("test", "second");
        assertEquals("second", customer.getProperty("test"));
        customer.save();
        customer = new Item(database, "customer0");
        assertEquals(1, customer.getProperties().size());
        assertEquals("second", customer.getProperty("test"));
        customer.setProperty("test", "third");
        customer.save();
        assertEquals("third", customer.getProperty("test"));
        customer = new Item(database, "customer0");
        assertEquals("third", customer.getProperty("test"));
        customer.clearProperty("test");
        assertEquals(0, customer.getProperties().size());
        customer.save();
        customer = new Item(database, "customer0");
        assertEquals(0, customer.getProperties().size());
    }

    @Test
    public void recommended()
    {
        HashSet<String> tags = new HashSet<String>();
        tags.add("product");

        Item customer = new Item(database, "customer0");
        assertEquals(20, customer.getRecommended().size());
        assertEquals(20, customer.getRecommended(tags).size());
        assertEquals(5, customer.getRecommended(tags, 5).size());
    }

    @Test
    public void related()
    {
        HashSet<String> tags = new HashSet<String>();
        tags.add("product");

        Item product = new Item(database, "product0");
        assertEquals(20, product.getRelated().size());
        assertEquals(20, product.getRelated(tags).size());
        assertEquals(5, product.getRelated(tags, 5).size());

        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("popularity", 5);

        List<String> popular = product.getRelated(tags, options);

        options.put("popularity", 1);

        List<String> unpopular = product.getRelated(tags, options);

        assertFalse(popular.equals(unpopular));
    }

    @Test
    public void tagOperations()
    {
        HashSet<String> tags = new HashSet<String>();
        tags.add("product");

        Item product = new Item(database, "product0");
        assertTrue(product.getRelated(tags).size() > 0);

        tags.add("customer");
        assertTrue(product.getRelated(tags).size() > 0);

        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("tagOperation", "OR");
        assertTrue(product.getRelated(tags, options).size() > 0);

        options.put("tagOperation", "AND");
        assertFalse(product.getRelated(tags, options).size() > 0);

        Item taggedAsBoth = new Item(database, "product35");
        taggedAsBoth.addTag("customer");
        taggedAsBoth.save();

        assertTrue(product.getRelated(tags, options).size() > 0);
    }

    @Test
    public void characters()
    {
        Item item = new Item(database, ";@%&!");
        item.setProperty("foo", "bar");
        item.save();

        item = new Item(database, ";@%&!");
        assertEquals("bar", item.getProperty("foo"));

        item = new Item(database, "foo/bar");
        item.setProperty("foo", "bar");
        item.save();

        item = new Item(database, "foo/bar");
        assertEquals("bar", item.getProperty("foo"));
    }

    @Test
    public void missingItem()
    {
        Item item = new Item(database, "missing");
        item.getRelated();
    }
}
TOP

Related Classes of ItemTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.