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

📄 ordertest.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.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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.Account;
import com.rdacorp.petstore.domain.Address;
import com.rdacorp.petstore.domain.CreditCardCompany;
import com.rdacorp.petstore.domain.Item;
import com.rdacorp.petstore.domain.Order;
import com.rdacorp.petstore.domain.OrderItem;
import com.rdacorp.petstore.domain.Payment;
import com.rdacorp.petstore.domain.Person;

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

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

      DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
      Date initialOrderDate = formatter.parse("12/31/2002");
      Date updatedOrderDate = formatter.parse("12/12/2003");
      BigDecimal paymentAmount = new BigDecimal(123);

      em.getTransaction().begin();
      // create a payment record
      Payment payment = new Payment(paymentAmount, new Date(), "4444111122223333", 12, 2005, em.find(
               CreditCardCompany.class, 1), em.find(Account.class, 1));
      Order order = new Order(new BigDecimal(123), initialOrderDate, em.find(Person.class, 1), em
               .find(Address.class, 1), em.find(Person.class, 2), em.find(Address.class, 2), payment);
      OrderItem orderItem = new OrderItem(123, order, em.find(Item.class, 1));
      order.addItem(orderItem);
      em.persist(payment);
      em.persist(order);
      em.getTransaction().commit();

      // verify primary key
      Integer orderId = order.getOrderId();
      assertNotNull(orderId);

      em.getTransaction().begin();
      // read the record
      order = (Order) em.find(Order.class, orderId);
      // verify order info
      assertEquals(initialOrderDate, order.getOrderTs());
      assertEquals(paymentAmount.doubleValue(), order.getPayment().getAmount().doubleValue());
      assertEquals(em.find(Person.class, 1).getLastName(), order.getBillingPerson().getLastName());
      assertEquals(em.find(Address.class, 1).getZip(), order.getBillingAddress().getZip());
      assertEquals(em.find(Person.class, 2).getLastName(), order.getShippingPerson().getLastName());
      assertEquals(em.find(Address.class, 2).getZip(), order.getShippingAddress().getZip());

      // update existing person and account records
      order.setAmount(new BigDecimal(321));
      order.setOrderTs(updatedOrderDate);
      order.setBillingPerson(em.find(Person.class, 2));
      order.setShippingPerson(em.find(Person.class, 1));
      order.setBillingAddress(em.find(Address.class, 2));
      order.setShippingAddress(em.find(Address.class, 1));
      em.getTransaction().commit();

      em.getTransaction().begin();
      // reread the record
      order = (Order) em.find(Order.class, orderId);
      // verify the updated record
      assertEquals(321.0, order.getAmount().doubleValue());
      assertEquals(updatedOrderDate, order.getOrderTs());
      assertEquals(em.find(Person.class, 2).getLastName(), order.getBillingPerson().getLastName());
      assertEquals(em.find(Address.class, 2).getZip(), order.getBillingAddress().getZip());
      assertEquals(em.find(Person.class, 1).getLastName(), order.getShippingPerson().getLastName());
      assertEquals(em.find(Address.class, 1).getZip(), order.getShippingAddress().getZip());

      // delete the record
      em.remove(order);
      em.getTransaction().commit();

      // retrive the person and verify it is null
      order = (Order) em.find(Order.class, orderId);
      assertNull(order);

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

⌨️ 快捷键说明

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