View Javadoc

    1   package org.example.customer;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertNotNull;
    6   import static org.junit.Assert.assertTrue;
    7
    8   import org.apache.logging.log4j.Level;
    9   import org.apache.logging.log4j.LogManager;
    10  import org.apache.logging.log4j.core.config.Configurator;
    11  import org.junit.Before;
    12  import org.junit.BeforeClass;
    13  import org.junit.Test;
    14
    15
    16  public class OrderItemTest
    17  {
    18      private OrderItem testOrderItem = null;
    19
    20      @BeforeClass // this runs only once before any test
    21      public static void setup()
    22      {
    23          Configurator.setLevel(LogManager.getLogger(OrderItem.class).getName(), Level.WARN);
    24      }
    25
    26      @Before
    27      public void setUp() throws Exception
    28      {
    29          testOrderItem = new OrderItem();
    30      }
    31
    32      @Test
    33      public void testOrderDefaultConstructor()
    34      {
    35          OrderItem defaultOrderItem = new OrderItem();
    36          assertNotNull(defaultOrderItem);
    37      }
    38
    39      @Test
    40      public void testDefaultOrderId()
    41      {
    42          int expected = 1;
    43          int actual = testOrderItem.getOrderId();
    44          assertEquals(expected, actual);
    45      }
    46
    47      @Test
    48      public void testValidOrderId()
    49      {
    50          int[] good = { 2, 34, 999, 22, 1, 565656 };
    51          for (int entry : good) {
    52              testOrderItem.setOrderId(entry);
    53          }
    54      }
    55
    56      @Test(expected = IllegalArgumentException.class)
    57      public void testInValidOrderId()
    58      {
    59          testOrderItem.setOrderId(0);
    60      }
    61
    62      @Test
    63      public void testDefaultProductId()
    64      {
    65          int expected = 1;
    66          int actual = testOrderItem.getProductId();
    67          assertEquals(expected, actual);
    68      }
    69
    70      @Test
    71      public void testValidProductId()
    72      {
    73          int[] good = { 2, 34, 999, 22, 1, 565656 };
    74          for (int entry : good) {
    75              testOrderItem.setProductId(entry);
    76          }
    77      }
    78
    79      @Test(expected = IllegalArgumentException.class)
    80      public void testInValidProductId()
    81      {
    82          testOrderItem.setProductId(0);
    83      }
    84
    85      @Test
    86      public void testDefaultUnitPrice()
    87      {
    88          double expected = 0.01;
    89          double actual = testOrderItem.getUnitPrice();
    90          double precision = 0.001;
    91          assertEquals(expected, actual, precision);
    92      }
    93
    94      @Test
    95      public void testValidUnitPrice()
    96      {
    97          double[] good = {2.0, 0.34, 999.50, 22.25, 1.0};
    98          for (double entry : good) {
    99              testOrderItem.setUnitPrice(entry);
    100         }
    101     }
    102
    103     @Test(expected = IllegalArgumentException.class)
    104     public void testInValidUnitPrice()
    105     {
    106         testOrderItem.setUnitPrice(0.0);
    107     }
    108
    109     @Test
    110     public void testDefaultQuantity()
    111     {
    112         int expected = 1;
    113         int actual = testOrderItem.getQuantity();
    114         assertEquals(expected, actual);
    115     }
    116
    117     @Test
    118     public void testValidQuantity()
    119     {
    120         int[] good = { 2, 34, 999, 22, 1, 565656 };
    121         for (int entry : good) {
    122             testOrderItem.setQuantity(entry);
    123         }
    124     }
    125
    126     @Test(expected = IllegalArgumentException.class)
    127     public void testInValidQuantity()
    128     {
    129         testOrderItem.setQuantity(0);
    130     }
    131
    132     @Test
    133     public void testDefaultSubTotal()
    134     {
    135         double expected = 0.01;
    136         double actual = testOrderItem.getSubTotal();
    137         double precision = 0.001;
    138         assertEquals(expected, actual, precision);
    139     }
    140
    141     @Test
    142     public void testChangedSubTotal()
    143     {
    144         testOrderItem.setQuantity(23);
    145         testOrderItem.setUnitPrice(5.55);
    146
    147         double expected = 23 * 5.55;
    148         double actual = testOrderItem.getSubTotal();
    149         double precision = 0.001;
    150         assertEquals(expected, actual, precision);
    151     }
    152
    153     @Test
    154     public void testToString()
    155     {
    156         String expected = "OrderItem [Id=1, "
    157                 + "OrderId=1, ProductId=1, "
    158                 + "UnitPrice=0.01, Quantity=1, "
    159                 + "SubTotal=0.01]";
    160         String actual = testOrderItem.toString();
    161         assertEquals(expected, actual);
    162     }
    163
    164     @Test public void testHashCode()
    165     {
    166         OrderItem sample = new OrderItem();
    167         int expected = sample.hashCode();
    168         int actual = testOrderItem.hashCode();
    169         assertEquals(expected, actual);
    170     }
    171
    172     @Test public void testEquals()
    173     {
    174         OrderItem sample = new OrderItem();
    175         assertTrue(testOrderItem.equals(sample));
    176     }
    177
    178     @Test public void testNotEquals()
    179     {
    180         OrderItem sample = new OrderItem();
    181         sample.setOrderId(2);
    182         assertFalse(testOrderItem.equals(sample));
    183
    184         Order junk = new Order();
    185         assertFalse(testOrderItem.equals(junk));
    186     }
    187
    188 }