|
| 1 | +/* |
| 2 | + * Copyright 2022, Backblaze Inc. All Rights Reserved. |
| 3 | + * License https://www.backblaze.com/using_b2_code.html |
| 4 | + */ |
| 5 | + |
| 6 | +package com.backblaze.b2.json; |
| 7 | + |
| 8 | +import com.backblaze.b2.util.B2BaseTest; |
| 9 | +import org.junit.Rule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.rules.ExpectedException; |
| 12 | + |
| 13 | +import java.util.*; |
| 14 | + |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for B2Json relying on javac's '-parameters' option instead of {@link B2Json.constructor#params}. |
| 19 | + */ |
| 20 | +@SuppressWarnings({ |
| 21 | + "unused", // A lot of the test classes have things that aren't used, but we don't care. |
| 22 | + "WeakerAccess" // A lot of the test classes could have weaker access, but we don't care. |
| 23 | +}) |
| 24 | +public class B2JsonInferredParametersTest extends B2BaseTest { |
| 25 | + |
| 26 | + @Rule |
| 27 | + public ExpectedException thrown = ExpectedException.none(); |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testConstructorWithMissingFields() throws B2JsonException { |
| 31 | + String json = "{\"a\": 41}"; |
| 32 | + thrown.expect(B2JsonException.class); |
| 33 | + thrown.expectMessage("constructor does not have the right number of parameters"); |
| 34 | + b2Json.fromJson(json, ConstructorWithMissingFields.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testConstructorWithExtraFields() throws B2JsonException { |
| 39 | + String json = "{\"a\": 41}"; |
| 40 | + thrown.expect(B2JsonException.class); |
| 41 | + thrown.expectMessage("constructor does not have the right number of parameters"); |
| 42 | + b2Json.fromJson(json, ConstructorWithExtraFields.class); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testConstructorWithVersion() throws B2JsonException { |
| 47 | + final String json = "{}"; |
| 48 | + final B2JsonOptions options = B2JsonOptions.builder().setVersion(1).build(); |
| 49 | + final VersionedContainer obj = b2Json.fromJson(json, VersionedContainer.class, options); |
| 50 | + assertEquals(0, obj.x); |
| 51 | + assertEquals(1, obj.version); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testDeserializeEmpty() throws B2JsonException { |
| 56 | + final Empty actual = B2Json.fromJsonOrThrowRuntime("{}", Empty.class); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Deserialize a class with fields declared in different order than their corresponding |
| 61 | + * parameters in the constructor. |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void testDeserializeWithMismatchingParamOrder() throws B2JsonException { |
| 65 | + final MismatchingOrderContainer actual = B2Json.fromJsonOrThrowRuntime( |
| 66 | + "{\n" + |
| 67 | + " \"a\": 41,\n" + |
| 68 | + " \"b\": \"hello\",\n" + |
| 69 | + " \"c\": 101\n" + |
| 70 | + "}", |
| 71 | + MismatchingOrderContainer.class); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private static class Empty { |
| 76 | + @B2Json.constructor Empty() {} |
| 77 | + } |
| 78 | + |
| 79 | + private static class ConstructorWithMissingFields { |
| 80 | + @B2Json.required |
| 81 | + public int a; |
| 82 | + |
| 83 | + @B2Json.constructor ConstructorWithMissingFields() {} |
| 84 | + } |
| 85 | + |
| 86 | + private static class ConstructorWithExtraFields { |
| 87 | + @B2Json.required |
| 88 | + public int a; |
| 89 | + |
| 90 | + @B2Json.constructor ConstructorWithExtraFields(int a, String extraField) {} |
| 91 | + } |
| 92 | + |
| 93 | + private static class VersionedContainer { |
| 94 | + @B2Json.versionRange(firstVersion = 4, lastVersion = 6) |
| 95 | + @B2Json.required |
| 96 | + public final int x; |
| 97 | + |
| 98 | + @B2Json.ignored |
| 99 | + public final int version; |
| 100 | + |
| 101 | + @B2Json.constructor(versionParam = "v") |
| 102 | + public VersionedContainer(int x, int v) { |
| 103 | + this.x = x; |
| 104 | + this.version = v; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static final class MismatchingOrderContainer { |
| 109 | + |
| 110 | + @B2Json.required |
| 111 | + public final int a; |
| 112 | + |
| 113 | + @B2Json.required |
| 114 | + public final String b; |
| 115 | + |
| 116 | + @B2Json.required |
| 117 | + public int c; |
| 118 | + |
| 119 | + @B2Json.optional |
| 120 | + public final Empty d; |
| 121 | + |
| 122 | + @B2Json.constructor |
| 123 | + public MismatchingOrderContainer(int c, String b, int a, Empty d) { |
| 124 | + this.c = c; |
| 125 | + this.b = b; |
| 126 | + this.a = a; |
| 127 | + this.d = d; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean equals(Object o) { |
| 132 | + if (this == o) return true; |
| 133 | + if (o == null || getClass() != o.getClass()) return false; |
| 134 | + |
| 135 | + MismatchingOrderContainer that = (MismatchingOrderContainer) o; |
| 136 | + |
| 137 | + if (a != that.a) return false; |
| 138 | + if (c != that.c) return false; |
| 139 | + if (!Objects.equals(b, that.b)) return false; |
| 140 | + return Objects.equals(d, that.d); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public int hashCode() { |
| 145 | + int result = a; |
| 146 | + result = 31 * result + (b != null ? b.hashCode() : 0); |
| 147 | + result = 31 * result + c; |
| 148 | + result = 31 * result + (d != null ? d.hashCode() : 0); |
| 149 | + return result; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static final class Container { |
| 154 | + |
| 155 | + @B2Json.required |
| 156 | + public final int a; |
| 157 | + |
| 158 | + @B2Json.optional |
| 159 | + public final String b; |
| 160 | + |
| 161 | + @B2Json.ignored |
| 162 | + public int c; |
| 163 | + |
| 164 | + @B2Json.constructor |
| 165 | + public Container(int a, String b) { |
| 166 | + this.a = a; |
| 167 | + this.b = b; |
| 168 | + this.c = 5; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean equals(Object o) { |
| 173 | + if (!(o instanceof Container)) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + Container other = (Container) o; |
| 177 | + return a == other.a && (b == null ? other.b == null : b.equals(other.b)); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static final B2Json b2Json = B2Json.get(); |
| 182 | + |
| 183 | +} |
0 commit comments