1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.specification;
17  
18  
19  import java.util.Date;
20  
21  import org.junit.Test;
22  
23  import net.sourceforge.domian.test.domain.Customer;
24  import net.sourceforge.domian.test.domain.Order;
25  import net.sourceforge.domian.test.domain.VipCustomer;
26  
27  import static net.sourceforge.domian.specification.ParameterizedSpecification.createParameterizedSpecification;
28  import static net.sourceforge.domian.specification.SpecificationFactory.before;
29  import static net.sourceforge.domian.specification.SpecificationFactory.is;
30  import static net.sourceforge.domian.specification.SpecificationFactory.isAfter;
31  import static net.sourceforge.domian.specification.SpecificationFactory.isBefore;
32  import static net.sourceforge.domian.specification.SpecificationFactory.isEqualTo;
33  import static net.sourceforge.domian.specification.SpecificationFactory.isLessThan;
34  import static net.sourceforge.domian.specification.SpecificationFactory.isLessThanOrEqualTo;
35  import static net.sourceforge.domian.test.domain.Testdata.thirtyYearsAgo;
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertTrue;
38  
39  
40  public class IsDisjointWith_ParameterizedSpecifications_Test {
41  
42      ////////////////////////////////////////
43      // Not disjoint
44      ////////////////////////////////////////
45  
46      @Test
47      public void identicalParameterizedSpecificationsIsNotDisjoint() {
48          ParameterizedSpecification<Customer, Long> customerIdSpec1 = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
49          ParameterizedSpecification<Customer, Long> customerIdSpec2 = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
50          ParameterizedSpecification<VipCustomer, Long> customerIdSpec3 = createParameterizedSpecification(VipCustomer.class, "customerId", isEqualTo(42L));
51          assertFalse(customerIdSpec1.isDisjointWith(customerIdSpec1));
52          assertFalse(customerIdSpec1.isDisjointWith(customerIdSpec2));
53          assertFalse(customerIdSpec1.isDisjointWith(customerIdSpec3));
54          assertFalse(customerIdSpec2.isDisjointWith(customerIdSpec1));
55          assertFalse(customerIdSpec3.isDisjointWith(customerIdSpec1));
56      }
57  
58  
59      @Test
60      public void nonDisjointAccessibleObjectSpecificationMeansNotDisjointParameterizedSpecification() {
61          ParameterizedSpecification<Customer, Long> customerIdSpec1 = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
62          ParameterizedSpecification<Customer, Long> customerIdSpec2 = createParameterizedSpecification(Customer.class, "customerId", isLessThanOrEqualTo(42L));
63          ParameterizedSpecification<VipCustomer, Long> customerIdSpec3 = createParameterizedSpecification(VipCustomer.class, "customerId", isLessThanOrEqualTo(42L));
64          assertFalse(customerIdSpec1.isDisjointWith(customerIdSpec2));
65          assertFalse(customerIdSpec1.isDisjointWith(customerIdSpec3));
66          assertFalse(customerIdSpec2.isDisjointWith(customerIdSpec1));
67          assertFalse(customerIdSpec3.isDisjointWith(customerIdSpec1));
68      }
69  
70  
71      ////////////////////////////////////////
72      // Disjoint
73      ////////////////////////////////////////
74  
75      @Test
76      public void differentDeclaringTypeMeansDisjointParameterizedSpecification() {
77          ParameterizedSpecification<Customer, Long> customerIdSpec = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
78          ParameterizedSpecification<Order, Long> orderIdSpec = createParameterizedSpecification(Order.class, "orderId", isEqualTo(42L));
79          assertTrue(customerIdSpec.isDisjointWith(orderIdSpec));
80          assertTrue(orderIdSpec.isDisjointWith(customerIdSpec));
81      }
82  
83      @Test
84      public void differentAccessibleObjectsMeansDisjointParameterizedSpecification() {
85          ParameterizedSpecification<Customer, Long> customerIdSpec = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
86          ParameterizedSpecification<Customer, String> customerNameSpec = createParameterizedSpecification(Customer.class, "name", is("Johnny"));
87          assertTrue(customerIdSpec.isDisjointWith(customerNameSpec));
88          assertTrue(customerNameSpec.isDisjointWith(customerIdSpec));
89      }
90  
91      @Test
92      public void disjointAccessibleObjectSpecificationMeansDisjointParameterizedSpecification() {
93          ParameterizedSpecification<Customer, Long> customerIdSpec1 = createParameterizedSpecification(Customer.class, "customerId", isEqualTo(42L));
94          ParameterizedSpecification<Customer, Long> customerIdSpec2 = createParameterizedSpecification(Customer.class, "customerId", isLessThan(42L));
95          assertTrue(customerIdSpec1.isDisjointWith(customerIdSpec2));
96          assertTrue(customerIdSpec2.isDisjointWith(customerIdSpec1));
97  
98          ParameterizedSpecification<Customer, Date> customerIdSpec3 = createParameterizedSpecification(Customer.class, "birthDate", isAfter(thirtyYearsAgo));
99          ParameterizedSpecification<Customer, Date> customerIdSpec4 = createParameterizedSpecification(Customer.class, "birthDate", isBefore(thirtyYearsAgo));
100         assertTrue(customerIdSpec3.isDisjointWith(customerIdSpec4));
101         assertTrue(customerIdSpec4.isDisjointWith(customerIdSpec3));
102 
103         ParameterizedSpecification<Customer, Date> customerIdSpec5 = createParameterizedSpecification(Customer.class, "membershipDate", isAfter("1990-10-10").and(before("1991-10-10")));
104         ParameterizedSpecification<Customer, Date> customerIdSpec6 = createParameterizedSpecification(Customer.class, "membershipDate", isAfter("1992-10-10").and(before("1994-10-10")));
105         assertTrue(customerIdSpec5.isDisjointWith(customerIdSpec6));
106         assertTrue(customerIdSpec6.isDisjointWith(customerIdSpec5));
107     }
108 }