📄 accounttest.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.Account;
import com.rdacorp.petstore.domain.Address;
import com.rdacorp.petstore.domain.Category;
import com.rdacorp.petstore.domain.Language;
import com.rdacorp.petstore.domain.Person;
import com.rdacorp.petstore.domain.State;
/**
* @author Borys Burnayev
*/
public class AccountTest 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 address record
List states = em.createQuery("from State").getResultList();
State initialState = (State) states.get(0);
State updatedState = (State) states.get(1);
String initialStateCode = initialState.getCode();
String updatedStateCode = updatedState.getCode();
Address address = new Address("1234 Main St", "apt 101", "Boston", "13543", initialState);
Account account = new Account(person, address);
em.persist(person);
em.persist(address);
em.persist(account);
em.getTransaction().commit();
// verify primary key
Integer accountId = account.getAccountId();
assertNotNull(accountId);
em.getTransaction().begin();
// read the account
account = (Account) em.find(Account.class, accountId);
// verify some info related to address and person
person = account.getPerson();
address = account.getAddress();
assertEquals("1234 Main St", address.getAddressLine1());
assertEquals(initialStateCode, address.getState().getCode());
assertEquals(initialCategoryCode, person.getFavoriteCategory().getCode());
assertEquals("Pablo", person.getFirstName());
// update existing person and account records
person.setFirstName("Josh");
person.setFavoriteCategory(em.merge(updatedCategory));
address.setAddressLine1("4321 Back St");
address.setState(em.merge(updatedState));
em.getTransaction().commit();
em.getTransaction().begin();
// reread the account
account = (Account) em.find(Account.class, accountId);
// verify some info related to address and person
person = account.getPerson();
address = account.getAddress();
assertEquals("4321 Back St", address.getAddressLine1());
assertEquals(updatedStateCode, address.getState().getCode());
assertEquals(updatedCategoryCode, person.getFavoriteCategory().getCode());
assertEquals("Josh", person.getFirstName());
// delete the account, person, and address
em.remove(account);
em.remove(person);
em.remove(address);
em.getTransaction().commit();
// retrive the account and verify it is null
account = (Account) em.find(Account.class, accountId);
assertNull(account);
em.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -