/*
* 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.model.core;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.internna.iwebmvc.model.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Jose Noheda
* @since 1.0
*/
public class I18nTextTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
private static I18nText hi;
@BeforeClass
public static void setUpClass() throws Exception {
factory = Persistence.createEntityManagerFactory("MODEL");
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
Locale locale_en = new Locale("en", "English");
entityManager.persist(locale_en);
Locale locale_es = new Locale("es", "Español");
entityManager.persist(locale_es);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
LocalizedValue value_en = new LocalizedValue();
value_en.setLocale(locale_en);
value_en.setLocalizedValue("hello");
LocalizedValue value_es = new LocalizedValue();
value_es.setLocale(locale_es);
value_es.setLocalizedValue("hola");
hi = new I18nText();
value_en.setI18nEntry(hi);
value_es.setI18nEntry(hi);
hi.setValues(new ArrayList<LocalizedValue>(2));
hi.getValues().add(value_en);
hi.getValues().add(value_es);
hi.setKey("key.hi");
}
@Before
public void init() {
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
}
@Test
public void find_and_save() {
entityManager.persist(hi);
entityManager.getTransaction().commit();
UUID pk = hi.getId();
entityManager.getTransaction().begin();
hi = entityManager.find(I18nText.class, pk);
assertNotNull("Record was saved", hi.getId());
assertTrue("Collection was saved", hi.getValues().size() == 2);
assertNotNull("Sub records were saved", hi.getValues().iterator().next().getId());
Query q = entityManager.createNamedQuery("Locale.count");
Long c = (Long) q.getSingleResult();
assertTrue("Two locales", c == 2);
entityManager.getTransaction().rollback();
}
@After
public void tearDown() {
entityManager.close();
}
}