/*
* 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.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.internna.iwebmvc.core.crypto.impl.DES3CiphererDecipherer;
import org.internna.iwebmvc.core.validation.ValidationError;
import org.internna.iwebmvc.core.validation.ValidationVisitor;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.model.core.hierarchy.I18nHierarchy;
import org.internna.iwebmvc.utils.StringUtils;
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 AbstractDomainEntityTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
private UUID pk;
@BeforeClass
public static void setUpClass() throws Exception {
factory = Persistence.createEntityManagerFactory("MODEL");
entityManager = factory.createEntityManager();
}
public void setup() {
entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
}
@Before
public void init() {
setup();
Locale locale = new Locale();
locale.setDescription("English");
locale.setLocale("en");
entityManager.persist(locale);
pk = locale.getId();
tearDown();
setup();
}
@Test
public void getFields() {
assertTrue("", 2 == new Locale().getFields(new String[] {"locale", "description"}).size());
}
@Test
public void getPrimaryKey() {
assertNull("No pk", new Locale().getId());
Locale locale = entityManager.find(Locale.class, pk);
assertEquals("UUID based PK", pk.toString(), locale.getId().toString());
}
@Test
public void find_and_save() {
Locale locale = entityManager.find(Locale.class, pk);
assertNotNull("Locale was retrieved", locale);
assertNotNull("Binary UUID generator worked", locale.getId());
assertTrue("The generated uuid is correct", StringUtils.isUUID(locale.getId().toString()));
}
@Test
public void acceptClone() throws Exception {
Locale locale = entityManager.find(Locale.class, pk);
DES3CiphererDecipherer visitor = new DES3CiphererDecipherer();
visitor.init();
Locale clone = visitor.visit(locale);
assertTrue("Just the same", clone.equals(locale));
}
@Test
public void acceptField() throws Exception {
Locale locale = entityManager.find(Locale.class, pk);
ValidationVisitor visitor = new ValidationVisitor();
Collection<ValidationError> errors = visitor.visit(locale);
assertTrue("No errors for valid entities", errors.size() == 0);
errors = visitor.visit(new Locale());
assertTrue("Errors if missing properties", errors.size() == 2);
}
@Test
public void equals() {
Locale locale = entityManager.find(Locale.class, pk);
assertFalse(locale.equals(null));
Locale other = new Locale();
assertFalse(locale.equals(other));
other.setId(new UUID("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"));
assertFalse(locale.equals(other));
assertTrue(locale.equals(locale));
}
@Test
public void hascode() {
Locale locale = entityManager.find(Locale.class, pk);
Locale other = new Locale();
assertFalse(locale.hashCode() == other.hashCode());
other.setId(new UUID("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"));
assertFalse(locale.hashCode() == other.hashCode());
assertTrue(locale.hashCode() == locale.hashCode());
}
@Test
public void isI18n() throws Exception {
assertTrue("LocalizedValue has a i18n property", new LocalizedValue().isI18n());
assertTrue("LocalizedValue has a i18n property", new I18nHierarchy().isI18n());
assertFalse("Locale has not a i18n property", new Locale().isI18n());
}
@After
public void tearDown() {
entityManager.getTransaction().commit();
entityManager.close();
}
}