Package org.internna.iwebmvc.spring.services.dwr.impl

Source Code of org.internna.iwebmvc.spring.services.dwr.impl.RemoteEntityManagerImplTest

/*
* Copyright 2002-2007 the original author or authors.
*
* 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.internna.iwebmvc.spring.services.dwr.impl;

import demoapp.domain.Classification;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.internna.iwebmvc.core.context.ContextHolder;
import org.internna.iwebmvc.core.crypto.impl.DES3CiphererDecipherer;
import org.internna.iwebmvc.core.dao.AbstractDAO;
import org.internna.iwebmvc.model.DomainEntity;
import org.internna.iwebmvc.model.Filter;
import org.internna.iwebmvc.model.core.Color;
import org.internna.iwebmvc.model.core.I18nText;
import org.internna.iwebmvc.model.core.Locale;
import org.internna.iwebmvc.model.core.LocalizedValue;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Jose Noheda
* @since 1.0
*/
public class RemoteEntityManagerImplTest {

    private static EntityManagerFactory factory;
    protected static EntityManager entityManager;
    private static RemoteEntityManagerImpl managerImpl;
    private static DES3CiphererDecipherer crypto;
    private static List<Filter> filters;

    @BeforeClass
    public static void setUpPersistence() throws Exception {
        managerImpl = new RemoteEntityManagerImpl();
        managerImpl.context = new ContextHolder() {
            @Override
            public List<Locale> getAvailableLocales() {
                Locale l = new Locale();
                l.setDescription("English");
                l.setLocale("en");
                Locale le = new Locale();
                le.setDescription("Spanish");
                le.setLocale("es");
                List<Locale> list = new ArrayList<Locale>(1);
                list.add(l);
                list.add(le);
                return list;
            }
        };
        crypto = new DES3CiphererDecipherer();
        crypto.init();
        managerImpl.decipherer = crypto;
        managerImpl.dao = new AbstractDAO() {
            @Override
            protected EntityManager getEntityManager() {
                return RemoteEntityManagerImplTest.entityManager;
            }
        };
        factory = Persistence.createEntityManagerFactory("MODEL");
        entityManager = factory.createEntityManager();
        entityManager.getTransaction().begin();
        Locale en = new Locale("en", "English");
        managerImpl.dao.create(en);
        Locale es = new Locale("es", "Spanish");
        managerImpl.dao.create(es);
        Classification c = new Classification();
        c.setColor(new Color());
        c.getColor().setRed(100);c.getColor().setGreen(100);c.getColor().setBlue(20);
        c.setLetter("A");
        c.setNumber(1);
        c.setName(new I18nText());
        c.getName().setKey("key.1");
        c.getName().setValues(new ArrayList<LocalizedValue>(2));
        c.getName().getValues().add(new LocalizedValue());
        c.getName().getValues().get(0).setLocale(en);
        c.getName().getValues().get(0).setLocalizedValue("one");
        c.getName().getValues().add(new LocalizedValue());
        c.getName().getValues().get(1).setLocale(es);
        c.getName().getValues().get(1).setLocalizedValue("uno");
        managerImpl.dao.create(c);
        Classification c2 = new Classification();
        c2.setColor(new Color());
        c2.getColor().setRed(20);c2.getColor().setGreen(100);c2.getColor().setBlue(100);
        c2.setLetter("B");
        c2.setNumber(2);
        c2.setName(new I18nText());
        c2.getName().setKey("key.2");
        c2.getName().setValues(new ArrayList<LocalizedValue>(2));
        c2.getName().getValues().add(new LocalizedValue());
        c2.getName().getValues().get(0).setLocale(en);
        c2.getName().getValues().get(0).setLocalizedValue("two");
        c2.getName().getValues().add(new LocalizedValue());
        c2.getName().getValues().get(1).setLocale(es);
        c2.getName().getValues().get(1).setLocalizedValue("dos");
        managerImpl.dao.create(c2);
        entityManager.getTransaction().commit();
        entityManager.close();
    }

    @Before
    public void setup() throws Exception {
        entityManager = factory.createEntityManager();
        entityManager.getTransaction().begin();
        filters = new ArrayList<Filter>(3);
        filters.add(new Filter());
        filters.get(0).setType("i18ntext");
        filters.get(0).setI18n(true);
        filters.get(0).setPath("name");
        filters.get(0).setPathValue("o");
    }

    @Test
    @Ignore
    public void mapFilters() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        assertTrue("One filter", managerImpl.mapFilters(Locale.class, filters).size() == 1);
        filters.get(0).setMin("_min_locale");
        filters.get(0).setMinValue("e");
        assertTrue("One filter", managerImpl.mapFilters(Locale.class, filters).size() == 2);
    }

    @Test
    public void getQuery() throws Exception {
        assertTrue("I18n only filter", managerImpl.getQuery(Classification.class, filters).length() == 248);
        filters.get(0).setMin("_min_name");
        filters.get(0).setMinValue("d");
        assertTrue("I18n only filter", managerImpl.getQuery(Classification.class, filters).length() == 466);
        filters.get(0).setI18n(false);
        filters.get(0).setType("text");
        assertEquals("Text ranged query", "SELECT e FROM Classification e WHERE LOWER(name) >= :_min_name AND LOWER(name) <= :name", managerImpl.getQuery(Classification.class, filters));
        filters.get(0).setType("number");
        assertEquals("No text ranged query", "SELECT e FROM Classification e WHERE name >= :_min_name AND name <= :name", managerImpl.getQuery(Classification.class, filters));
        filters.get(0).setMin(null);
        filters.get(0).setMinValue(null);
        assertEquals("No text query", "SELECT e FROM Classification e WHERE name = :name", managerImpl.getQuery(Classification.class, filters));
        filters.add(new Filter());
        filters.get(1).setPath("number");
        filters.get(1).setPathValue("sample");
        filters.get(1).setType("text");
        assertEquals("Two filter query", "SELECT e FROM Classification e WHERE name = :name AND LOWER(number) LIKE :number", managerImpl.getQuery(Classification.class, filters));
        filters.get(1).setMin("_min_number");
        filters.get(1).setMinValue("min sample");
        assertEquals("Two filter query with range", "SELECT e FROM Classification e WHERE name = :name AND LOWER(number) >= :_min_number AND LOWER(number) <= :number", managerImpl.getQuery(Classification.class, filters));
    }

    @Test
    public void fetch() throws Exception {
        List<? extends DomainEntity> l = managerImpl.fetch(Locale.class, 0, 5, new Filter[0]);
        assertTrue("Retrieved 2 rows", 2 == l.size());
        l = managerImpl.fetch(Classification.class, 0, 5, filters.toArray(new Filter[0]));
        assertTrue("Retrieved 1 row for i18n", 1 == l.size());
        filters.get(0).setMin("_min_name");
        filters.get(0).setMinValue("b");
        filters.get(0).setPathValue("z");
        l = managerImpl.fetch(Classification.class, 0, 5, filters.toArray(new Filter[0]));
        assertTrue("Retrieved 2 rows for ranged i18n", 2 == l.size());
        filters.get(0).setPathValue("e");
        l = managerImpl.fetch(Classification.class, 0, 5, filters.toArray(new Filter[0]));
        assertTrue("Retrieved 1 rows for ranged i18n", 1 == l.size());
    }

    @After
    public void tearDown() {
        entityManager.getTransaction().commit();
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.services.dwr.impl.RemoteEntityManagerImplTest

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.