package test.com.cjpowered.learn.inventory;

import static org.junit.Assert.*;

import org.junit.Test;

import com.cjpowered.learn.inventory.MarketingSpec;
import com.cjpowered.learn.marketing.Season;

public class MarketingSpecTest {

    final MarketingSpec control = new MarketingSpec(Season.Fall, true);
    
    @Test public void reflexiveReferenceEquality() {
        
        assertTrue(control.equals(control));
    }
    
    @Test public void valueEquality() {
        // given
        final MarketingSpec copy = new MarketingSpec(control.season, control.onSale);
        
        // then
        assertTrue(control.equals(copy));
    }
    
    @Test public void notEqualArbitrary() {
        
        assertFalse(control.equals("not a spec"));
    }
    
    @Test public void notEqualNull() {
        
        assertFalse(control.equals(null));
        
    }
    
    @Test public void symmetricEquality() {

        // given
        final MarketingSpec copy = new MarketingSpec(control.season, control.onSale);
        
        // then
        assertTrue(copy.equals(control));

        
    }
    
    @Test public void compatibleHash() {
        
        // given
        final MarketingSpec copy = new MarketingSpec(control.season, control.onSale);
        
        // then
        assertEquals(control.hashCode(), copy.hashCode());
        
    }
    
    @Test public void repeatableHash() {
        
        assertEquals(control.hashCode(), control.hashCode());
    }
    
    private void require(boolean constraint) {
        if (!constraint)
            throw new RuntimeException("test is not valid");
    }
    
    @Test public void equalComparesSeason() {
        
        // given
        final Season differentSeason = Season.Winter;
        require(!differentSeason.equals(control.season));
        final MarketingSpec partialCopy = new MarketingSpec(differentSeason, control.onSale);
        
        // then
        assertFalse(control.equals(partialCopy));
        assertFalse(partialCopy.equals(control));
        
    }
    
    @Test public void equalComparesOnSale() {
        
        // given
        final boolean differentOnSale = false;
        require(differentOnSale != control.onSale);
        final MarketingSpec partialCopy = new MarketingSpec(control.season, differentOnSale);
        
        // then
        assertFalse(control.equals(partialCopy));
        assertFalse(partialCopy.equals(control));
        
    }
    
}

@@ -520,56 +520,25 @@
 y = 
-new MarketingSpec(control.season, control.onSale
+duplicate(control
 );%0A 
@@ -941,56 +941,25 @@
 y = 
-new MarketingSpec(control.season, control.onSale
+duplicate(control
 );%0A 
@@ -1151,56 +1151,25 @@
 y = 
-new MarketingSpec(control.season, control.onSale
+duplicate(control
 );%0A 
@@ -1398,152 +1398,8 @@
    %0A
-    private void require(boolean constraint) %7B%0A        if (!constraint)%0A            throw new RuntimeException(%22test is not valid%22);%0A    %7D%0A    %0A
     
@@ -2208,29 +2208,29 @@
 rol));%0A        %0A
-
     %7D%0A    %0A%7D%0A
@@ -2227,10 +2227,288 @@
  %7D%0A    %0A
+    private void require(boolean constraint) %7B%0A        if (!constraint)%0A            throw new RuntimeException(%22test is not valid%22);%0A    %7D%0A    %0A    private MarketingSpec duplicate(MarketingSpec from) %7B%0A        return new MarketingSpec(from.season, from.onSale);%0A    %7D%0A    %0A    %0A
 %7D%0A

factor out obj duplication