|
16 | 16 |
|
17 | 17 | package com.google.gson;
|
18 | 18 |
|
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
19 | 22 | import com.google.gson.annotations.Since;
|
| 23 | +import com.google.gson.annotations.Until; |
20 | 24 | import com.google.gson.internal.Excluder;
|
21 |
| -import junit.framework.TestCase; |
| 25 | +import org.junit.Test; |
22 | 26 |
|
23 | 27 | /**
|
24 | 28 | * Unit tests for the {@link Excluder} class.
|
25 | 29 | *
|
26 | 30 | * @author Joel Leitch
|
27 | 31 | */
|
28 |
| -public class VersionExclusionStrategyTest extends TestCase { |
| 32 | +public class VersionExclusionStrategyTest { |
29 | 33 | private static final double VERSION = 5.0D;
|
30 | 34 |
|
31 |
| - public void testClassAndFieldAreAtSameVersion() throws Exception { |
| 35 | + @Test |
| 36 | + public void testSameVersion() throws Exception { |
32 | 37 | Excluder excluder = Excluder.DEFAULT.withVersion(VERSION);
|
33 |
| - assertFalse(excluder.excludeClass(MockObject.class, true)); |
34 |
| - assertFalse(excluder.excludeField(MockObject.class.getField("someField"), true)); |
| 38 | + assertFalse(excluder.excludeClass(MockClassSince.class, true)); |
| 39 | + assertFalse(excluder.excludeField(MockClassSince.class.getField("someField"), true)); |
| 40 | + |
| 41 | + // Until version is exclusive |
| 42 | + assertTrue(excluder.excludeClass(MockClassUntil.class, true)); |
| 43 | + assertTrue(excluder.excludeField(MockClassUntil.class.getField("someField"), true)); |
| 44 | + |
| 45 | + assertFalse(excluder.excludeClass(MockClassBoth.class, true)); |
| 46 | + assertFalse(excluder.excludeField(MockClassBoth.class.getField("someField"), true)); |
35 | 47 | }
|
36 | 48 |
|
37 |
| - public void testClassAndFieldAreBehindInVersion() throws Exception { |
38 |
| - Excluder excluder = Excluder.DEFAULT.withVersion(VERSION + 1); |
39 |
| - assertFalse(excluder.excludeClass(MockObject.class, true)); |
40 |
| - assertFalse(excluder.excludeField(MockObject.class.getField("someField"), true)); |
| 49 | + @Test |
| 50 | + public void testNewerVersion() throws Exception { |
| 51 | + Excluder excluder = Excluder.DEFAULT.withVersion(VERSION + 5); |
| 52 | + assertFalse(excluder.excludeClass(MockClassSince.class, true)); |
| 53 | + assertFalse(excluder.excludeField(MockClassSince.class.getField("someField"), true)); |
| 54 | + |
| 55 | + assertTrue(excluder.excludeClass(MockClassUntil.class, true)); |
| 56 | + assertTrue(excluder.excludeField(MockClassUntil.class.getField("someField"), true)); |
| 57 | + |
| 58 | + assertTrue(excluder.excludeClass(MockClassBoth.class, true)); |
| 59 | + assertTrue(excluder.excludeField(MockClassBoth.class.getField("someField"), true)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testOlderVersion() throws Exception { |
| 64 | + Excluder excluder = Excluder.DEFAULT.withVersion(VERSION - 5); |
| 65 | + assertTrue(excluder.excludeClass(MockClassSince.class, true)); |
| 66 | + assertTrue(excluder.excludeField(MockClassSince.class.getField("someField"), true)); |
| 67 | + |
| 68 | + assertFalse(excluder.excludeClass(MockClassUntil.class, true)); |
| 69 | + assertFalse(excluder.excludeField(MockClassUntil.class.getField("someField"), true)); |
| 70 | + |
| 71 | + assertTrue(excluder.excludeClass(MockClassBoth.class, true)); |
| 72 | + assertTrue(excluder.excludeField(MockClassBoth.class.getField("someField"), true)); |
41 | 73 | }
|
42 | 74 |
|
43 |
| - public void testClassAndFieldAreAheadInVersion() throws Exception { |
44 |
| - Excluder excluder = Excluder.DEFAULT.withVersion(VERSION - 1); |
45 |
| - assertTrue(excluder.excludeClass(MockObject.class, true)); |
46 |
| - assertTrue(excluder.excludeField(MockObject.class.getField("someField"), true)); |
| 75 | + @Since(VERSION) |
| 76 | + private static class MockClassSince { |
| 77 | + |
| 78 | + @Since(VERSION) |
| 79 | + public final int someField = 0; |
| 80 | + } |
| 81 | + |
| 82 | + @Until(VERSION) |
| 83 | + private static class MockClassUntil { |
| 84 | + |
| 85 | + @Until(VERSION) |
| 86 | + public final int someField = 0; |
47 | 87 | }
|
48 | 88 |
|
49 | 89 | @Since(VERSION)
|
50 |
| - private static class MockObject { |
| 90 | + @Until(VERSION + 2) |
| 91 | + private static class MockClassBoth { |
51 | 92 |
|
52 | 93 | @Since(VERSION)
|
| 94 | + @Until(VERSION + 2) |
53 | 95 | public final int someField = 0;
|
54 | 96 | }
|
55 | 97 | }
|
0 commit comments