|
| 1 | +package com.github.ppodgorsek.juncacher.model.impl; |
| 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.junit.Test; |
| 9 | + |
| 10 | +import com.github.ppodgorsek.juncacher.model.InvalidationEntryType; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for the {@link TypedInvalidationEntry} class. |
| 14 | + * |
| 15 | + * @author Paul Podgorsek |
| 16 | + */ |
| 17 | +public class TypedInvalidationEntryTest { |
| 18 | + |
| 19 | + private final TypedInvalidationEntry typedInvalidationEntry = new TypedInvalidationEntry( |
| 20 | + DefaultInvalidationEntryType.GLOBAL); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void constructWithCorrectType() { |
| 24 | + new TypedInvalidationEntry(DefaultInvalidationEntryType.GLOBAL); |
| 25 | + } |
| 26 | + |
| 27 | + @Test(expected = IllegalArgumentException.class) |
| 28 | + public void constructWithNullType() { |
| 29 | + new TypedInvalidationEntry(null); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void equalsWithSameInstance() { |
| 34 | + assertTrue("The equals() method should return true", |
| 35 | + typedInvalidationEntry.equals(typedInvalidationEntry)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void equalsWithDifferentClass() { |
| 40 | + assertFalse("The equals() method should return false", |
| 41 | + typedInvalidationEntry.equals(new Object())); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void equalsWithDifferentInstanceHavingSameType() { |
| 46 | + |
| 47 | + final TypedInvalidationEntry otherEntry = new TypedInvalidationEntry( |
| 48 | + DefaultInvalidationEntryType.GLOBAL); |
| 49 | + |
| 50 | + assertTrue("The equals() method should return true", |
| 51 | + typedInvalidationEntry.equals(otherEntry)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void equalsWithDifferentInstanceHavingDifferentType() { |
| 56 | + |
| 57 | + final TypedInvalidationEntry otherEntry = new TypedInvalidationEntry( |
| 58 | + new ClassInvalidationEntryType(Object.class)); |
| 59 | + |
| 60 | + assertFalse("The equals() method should return false", |
| 61 | + typedInvalidationEntry.equals(otherEntry)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void hashCodeWithCorrectValue() { |
| 66 | + |
| 67 | + final int hashCode = typedInvalidationEntry.hashCode(); |
| 68 | + |
| 69 | + assertTrue("Wrong hashCode", hashCode != 0); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void toStringWithCorrectValues() { |
| 74 | + |
| 75 | + final StringBuilder sbld = new StringBuilder(); |
| 76 | + sbld.append("TypedInvalidationEntry[type="); |
| 77 | + sbld.append(DefaultInvalidationEntryType.GLOBAL); |
| 78 | + sbld.append("]"); |
| 79 | + |
| 80 | + final String toStringValue = typedInvalidationEntry.toString(); |
| 81 | + |
| 82 | + assertNotNull("The toString() value shouldn't be null", toStringValue); |
| 83 | + assertEquals("Wrong toString() value", sbld.toString(), toStringValue); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void typeGetterSetterWithCorrectValue() { |
| 88 | + |
| 89 | + final InvalidationEntryType newType = new ClassInvalidationEntryType(getClass()); |
| 90 | + |
| 91 | + typedInvalidationEntry.setType(newType); |
| 92 | + |
| 93 | + final InvalidationEntryType type = typedInvalidationEntry.getType(); |
| 94 | + |
| 95 | + assertNotNull("The type shouldn't be null", type); |
| 96 | + assertEquals("Wrong type", newType, type); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected = IllegalArgumentException.class) |
| 100 | + public void typeGetterSetterWithNullValue() { |
| 101 | + typedInvalidationEntry.setType(null); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments