⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userprofiletest.java

📁 Java EE 5 许多新功能都包含经过修补的 EJB 架构
💻 JAVA
字号:
/*
 * Copyright 2006 Borys Burnayev
 * 
 * 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 test.persistence;

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 junit.framework.TestCase;

import com.rdacorp.petstore.domain.Category;
import com.rdacorp.petstore.domain.Language;
import com.rdacorp.petstore.domain.Person;
import com.rdacorp.petstore.domain.UserProfile;

/**
 * @author Borys Burnayev
 */
public class UserProfileTest extends TestCase {

   public void testCRUD() throws Exception {
      Map config = new HashMap();
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1", config);
      EntityManager em = emf.createEntityManager();

      em.getTransaction().begin();
      // create a person record
      List categories = em.createQuery("from Category").getResultList();
      Category initialCategory = (Category) categories.get(0);
      Category updatedCategory = (Category) categories.get(1);
      String initialCategoryCode = initialCategory.getCode();
      String updatedCategoryCode = updatedCategory.getCode();
      List languages = em.createQuery("from Language").getResultList();
      Language initialLanguage = (Language) languages.get(0);
      Person person = new Person("Pablo", "LastName", "email@email.com", "1112223333", new Integer(1), new Integer(1),
               initialCategory, initialLanguage);
      // create an user profile record
      UserProfile profile = new UserProfile("login", "password", person);
      profile.setPerson(person);
      em.persist(person);
      em.persist(profile);
      em.getTransaction().commit();

      // verify primary key
      Integer userProfileId = profile.getUserProfileId();
      assertNotNull(userProfileId);

      em.getTransaction().begin();
      // read the profile
      profile = (UserProfile) em.find(UserProfile.class, userProfileId);
      // verify user profile info
      assertEquals("login", profile.getLogin());
      assertEquals("password", profile.getPassword());
      // verify some info related to person
      person = profile.getPerson();
      assertEquals(initialCategoryCode, person.getFavoriteCategory().getCode());
      assertEquals("Pablo", person.getFirstName());

      // update profile
      profile.setLogin("new_login");
      profile.setPassword("new_password");
      // update existing person and account records
      profile.getPerson().setFirstName("Josh");
      profile.getPerson().setFavoriteCategory(em.merge(updatedCategory));
      em.getTransaction().commit();

      em.getTransaction().begin();
      // reread the account
      profile = (UserProfile) em.find(UserProfile.class, userProfileId);
      // verify profile info
      assertEquals("new_login", profile.getLogin());
      assertEquals("new_password", profile.getPassword());
      // verify some info related to address and person
      person = profile.getPerson();
      assertEquals(updatedCategoryCode, person.getFavoriteCategory().getCode());
      assertEquals("Josh", person.getFirstName());

      // delete the profile and the person
      em.remove(profile);
      em.remove(person);
      em.getTransaction().commit();

      // retrive the user profile and verify it is null
      profile = (UserProfile) em.find(UserProfile.class, userProfileId);
      assertNull(profile);

      em.close();
      emf.close();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -