📄 addresstest.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.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import com.rdacorp.petstore.domain.Address;
import com.rdacorp.petstore.domain.State;
/**
* @author Borys Burnayev
*/
public class AddressTest extends TestCase {
private static final String UPDATED_STATE = "MD";
private static final String UPDATED_ZIP = "54321";
private static final String UPDATED_CITY = "Updated City";
private static final String UPDATED_ADDRESS_LINE_2 = "Updated Line 2";
private static final String UPDATED_ADDRESS_LINE_1 = "Updated Line 1";
private static final String INITIAL_STATE = "VA";
private static final String INITIAL_ZIP = "12345";
private static final String INITIAL_CITY = "Somewhere";
private static final String INITIAL_ADDRESS_LINE_2 = "Ste 101";
private static final String INITIAL_ADDRESS_LINE_1 = "1234 Main St";
public void testCRUD() throws Exception {
Map config = new HashMap();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1", config);
EntityManager em = emf.createEntityManager();
// create address record
em.getTransaction().begin();
State state = (State) em.createQuery("from State where code = ?1").setParameter(1, INITIAL_STATE)
.getSingleResult();
Address address = new Address(INITIAL_ADDRESS_LINE_1, INITIAL_ADDRESS_LINE_2, INITIAL_CITY, INITIAL_ZIP, state);
em.persist(address);
em.getTransaction().commit();
// addressSysid should be populated at this point
Integer addressId = address.getAddressId();
assertNotNull(addressId);
em.getTransaction().begin();
// verify the record
address = (Address) em.find(Address.class, addressId);
assertEquals(address.getAddressLine1(), INITIAL_ADDRESS_LINE_1);
assertEquals(address.getAddressLine2(), INITIAL_ADDRESS_LINE_2);
assertEquals(address.getCity(), INITIAL_CITY);
assertEquals(address.getState().getCode(), INITIAL_STATE);
assertEquals(address.getZip(), INITIAL_ZIP);
assertEquals(address.getAddressLine1(), INITIAL_ADDRESS_LINE_1);
// update the record
address.setAddressLine1(UPDATED_ADDRESS_LINE_1);
address.setAddressLine2(UPDATED_ADDRESS_LINE_2);
address.setCity(UPDATED_CITY);
address.setZip(UPDATED_ZIP);
state = (State) em.createQuery("from State where code = ?1").setParameter(1, UPDATED_STATE).getSingleResult();
address.setState(state);
em.getTransaction().commit();
em.getTransaction().begin();
// verify the record
address = (Address) em.find(Address.class, addressId);
assertEquals(address.getAddressLine1(), UPDATED_ADDRESS_LINE_1);
assertEquals(address.getAddressLine2(), UPDATED_ADDRESS_LINE_2);
assertEquals(address.getCity(), UPDATED_CITY);
assertEquals(address.getState().getCode(), UPDATED_STATE);
assertEquals(address.getZip(), UPDATED_ZIP);
// delete the record
em.remove(address);
em.getTransaction().commit();
// retrive the addres and verify it is null
address = (Address) em.find(Address.class, addressId);
assertNull(address);
em.close();
emf.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -