|
| 1 | +package tools.jackson.databind.testutil; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.util.Arrays; |
| 9 | + |
| 10 | +import tools.jackson.core.ErrorReportConfiguration; |
| 11 | +import tools.jackson.core.JsonEncoding; |
| 12 | +import tools.jackson.core.JsonParser; |
| 13 | +import tools.jackson.core.JsonToken; |
| 14 | +import tools.jackson.core.ObjectReadContext; |
| 15 | +import tools.jackson.core.StreamReadConstraints; |
| 16 | +import tools.jackson.core.StreamWriteConstraints; |
| 17 | +import tools.jackson.core.io.ContentReference; |
| 18 | +import tools.jackson.core.io.IOContext; |
| 19 | +import tools.jackson.core.util.BufferRecycler; |
| 20 | + |
| 21 | +/** |
| 22 | + * Container for various factories needed by (unit) tests. |
| 23 | + */ |
| 24 | +public class JacksonTestUtilBase |
| 25 | +{ |
| 26 | + /* |
| 27 | + /********************************************************************** |
| 28 | + /* Factory methods for test contexts |
| 29 | + /********************************************************************** |
| 30 | + */ |
| 31 | + |
| 32 | + /** |
| 33 | + * Factory method for creating {@link IOContext}s for tests |
| 34 | + */ |
| 35 | + public static IOContext testIOContext() { |
| 36 | + return testIOContext(StreamReadConstraints.defaults(), |
| 37 | + StreamWriteConstraints.defaults(), |
| 38 | + ErrorReportConfiguration.defaults()); |
| 39 | + } |
| 40 | + |
| 41 | + protected static IOContext testIOContext(StreamReadConstraints src, |
| 42 | + StreamWriteConstraints swc, |
| 43 | + ErrorReportConfiguration erc) { |
| 44 | + return new IOContext(src, swc, erc, |
| 45 | + new BufferRecycler(), ContentReference.unknown(), false, |
| 46 | + JsonEncoding.UTF8); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public static ObjectReadContext testObjectReadContext() { |
| 51 | + return ObjectReadContext.empty(); |
| 52 | + } |
| 53 | + |
| 54 | + /* |
| 55 | + /********************************************************************** |
| 56 | + /* Escaping/quoting |
| 57 | + /********************************************************************** |
| 58 | + */ |
| 59 | + |
| 60 | + public static String q(String str) { |
| 61 | + return '"'+str+'"'; |
| 62 | + } |
| 63 | + |
| 64 | + public static String a2q(String json) { |
| 65 | + return json.replace('\'', '"'); |
| 66 | + } |
| 67 | + |
| 68 | + /* |
| 69 | + /********************************************************************** |
| 70 | + /* Assertions |
| 71 | + /********************************************************************** |
| 72 | + */ |
| 73 | + |
| 74 | + public static void assertToken(JsonToken expToken, JsonToken actToken) |
| 75 | + { |
| 76 | + if (actToken != expToken) { |
| 77 | + fail("Expected token "+expToken+", current token "+actToken); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void assertToken(JsonToken expToken, JsonParser p) |
| 82 | + { |
| 83 | + assertToken(expToken, p.currentToken()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param e Exception to check |
| 88 | + * @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included |
| 89 | + * in {@code e.getMessage()} -- using case-INSENSITIVE comparison |
| 90 | + */ |
| 91 | + public static void verifyException(Throwable e, String... anyMatches) |
| 92 | + { |
| 93 | + String msg = e.getMessage(); |
| 94 | + String lmsg = (msg == null) ? "" : msg.toLowerCase(); |
| 95 | + for (String match : anyMatches) { |
| 96 | + String lmatch = match.toLowerCase(); |
| 97 | + if (lmsg.indexOf(lmatch) >= 0) { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\""); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Method that gets textual contents of the current token using |
| 106 | + * available methods, and ensures results are consistent, before |
| 107 | + * returning them |
| 108 | + */ |
| 109 | + public static String getAndVerifyText(JsonParser p) |
| 110 | + { |
| 111 | + // Ok, let's verify other accessors |
| 112 | + int actLen = p.getStringLength(); |
| 113 | + char[] ch = p.getStringCharacters(); |
| 114 | + String str2 = new String(ch, p.getStringOffset(), actLen); |
| 115 | + String str = p.getString(); |
| 116 | + |
| 117 | + if (str.length() != actLen) { |
| 118 | + fail("Internal problem (p.token == "+p.currentToken()+"): p.getText().length() ['"+str+"'] == "+str.length()+"; p.getTextLength() == "+actLen); |
| 119 | + } |
| 120 | + assertEquals(str, str2, "String access via getText(), getTextXxx() must be the same"); |
| 121 | + |
| 122 | + return str; |
| 123 | + } |
| 124 | + |
| 125 | + /* |
| 126 | + /********************************************************************** |
| 127 | + /* Character encoding support |
| 128 | + /********************************************************************** |
| 129 | + */ |
| 130 | + |
| 131 | + public static byte[] encodeInUTF32BE(String input) |
| 132 | + { |
| 133 | + int len = input.length(); |
| 134 | + byte[] result = new byte[len * 4]; |
| 135 | + int ptr = 0; |
| 136 | + for (int i = 0; i < len; ++i, ptr += 4) { |
| 137 | + char c = input.charAt(i); |
| 138 | + result[ptr] = result[ptr+1] = (byte) 0; |
| 139 | + result[ptr+2] = (byte) (c >> 8); |
| 140 | + result[ptr+3] = (byte) c; |
| 141 | + } |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + public static byte[] utf8Bytes(String str) { |
| 146 | + return str.getBytes(StandardCharsets.UTF_8); |
| 147 | + } |
| 148 | + |
| 149 | + public String utf8String(ByteArrayOutputStream bytes) { |
| 150 | + return new String(bytes.toByteArray(), StandardCharsets.UTF_8); |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + /********************************************************************** |
| 155 | + /* Resource reading helpers |
| 156 | + /********************************************************************** |
| 157 | + */ |
| 158 | + |
| 159 | + public static byte[] readResource(String ref) |
| 160 | + { |
| 161 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 162 | + final byte[] buf = new byte[4000]; |
| 163 | + |
| 164 | + InputStream in = JacksonTestUtilBase.class.getResourceAsStream(ref); |
| 165 | + if (in != null) { |
| 166 | + try { |
| 167 | + int len; |
| 168 | + while ((len = in.read(buf)) > 0) { |
| 169 | + bytes.write(buf, 0, len); |
| 170 | + } |
| 171 | + in.close(); |
| 172 | + } catch (IOException e) { |
| 173 | + throw new RuntimeException("Failed to read resource '"+ref+"': "+e); |
| 174 | + } |
| 175 | + } |
| 176 | + if (bytes.size() == 0) { |
| 177 | + throw new IllegalArgumentException("Failed to read resource '"+ref+"': empty resource?"); |
| 178 | + } |
| 179 | + return bytes.toByteArray(); |
| 180 | + } |
| 181 | + |
| 182 | + /* |
| 183 | + /********************************************************************** |
| 184 | + /* JDK serialization helpers |
| 185 | + /********************************************************************** |
| 186 | + */ |
| 187 | + |
| 188 | + public static byte[] jdkSerialize(Object o) throws IOException |
| 189 | + { |
| 190 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); |
| 191 | + ObjectOutputStream obOut = new ObjectOutputStream(bytes); |
| 192 | + obOut.writeObject(o); |
| 193 | + obOut.close(); |
| 194 | + return bytes.toByteArray(); |
| 195 | + } |
| 196 | + |
| 197 | + @SuppressWarnings("unchecked") |
| 198 | + public static <T> T jdkDeserialize(byte[] raw) throws IOException |
| 199 | + { |
| 200 | + ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw)); |
| 201 | + try { |
| 202 | + return (T) objIn.readObject(); |
| 203 | + } catch (ClassNotFoundException e) { |
| 204 | + fail("Missing class: "+e.getMessage()); |
| 205 | + return null; |
| 206 | + } finally { |
| 207 | + objIn.close(); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments