|
| 1 | +package com.fasterxml.jackson.core; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.DataInput; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.StringReader; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.core.testsupport.MockDataInput; |
| 13 | +import com.fasterxml.jackson.core.testsupport.ThrottledInputStream; |
| 14 | +import com.fasterxml.jackson.core.testsupport.ThrottledReader; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.fail; |
| 17 | + |
| 18 | +/** |
| 19 | + * Intended replacement for {@link BaseTest} |
| 20 | + */ |
| 21 | +public class JUnit5TestBase |
| 22 | +{ |
| 23 | + protected final static int MODE_INPUT_STREAM = 0; |
| 24 | + protected final static int MODE_INPUT_STREAM_THROTTLED = 1; |
| 25 | + protected final static int MODE_READER = 2; |
| 26 | + protected final static int MODE_READER_THROTTLED = 3; |
| 27 | + protected final static int MODE_DATA_INPUT = 4; |
| 28 | + |
| 29 | + protected final static int[] ALL_MODES = new int[] { |
| 30 | + MODE_INPUT_STREAM, |
| 31 | + MODE_INPUT_STREAM_THROTTLED, |
| 32 | + MODE_READER, |
| 33 | + MODE_READER_THROTTLED, |
| 34 | + MODE_DATA_INPUT |
| 35 | + }; |
| 36 | + |
| 37 | + protected final static int[] ALL_BINARY_MODES = new int[] { |
| 38 | + MODE_INPUT_STREAM, |
| 39 | + MODE_INPUT_STREAM_THROTTLED, |
| 40 | + MODE_DATA_INPUT |
| 41 | + }; |
| 42 | + |
| 43 | + protected final static int[] ALL_TEXT_MODES = new int[] { |
| 44 | + MODE_READER, |
| 45 | + MODE_READER_THROTTLED |
| 46 | + }; |
| 47 | + |
| 48 | + // DataInput not streaming |
| 49 | + protected final static int[] ALL_STREAMING_MODES = new int[] { |
| 50 | + MODE_INPUT_STREAM, |
| 51 | + MODE_INPUT_STREAM_THROTTLED, |
| 52 | + MODE_READER, |
| 53 | + MODE_READER_THROTTLED |
| 54 | + }; |
| 55 | + |
| 56 | + /* |
| 57 | + /********************************************************************** |
| 58 | + /* Factory methods |
| 59 | + /********************************************************************** |
| 60 | + */ |
| 61 | + |
| 62 | + protected JsonFactory newStreamFactory() { |
| 63 | + return new JsonFactory(); |
| 64 | + } |
| 65 | + |
| 66 | + protected JsonFactoryBuilder streamFactoryBuilder() { |
| 67 | + return (JsonFactoryBuilder) JsonFactory.builder(); |
| 68 | + } |
| 69 | + |
| 70 | + protected JsonParser createParser(TokenStreamFactory f, int mode, String doc) throws IOException |
| 71 | + { |
| 72 | + switch (mode) { |
| 73 | + case MODE_INPUT_STREAM: |
| 74 | + return createParserUsingStream(f, doc, "UTF-8"); |
| 75 | + case MODE_INPUT_STREAM_THROTTLED: |
| 76 | + return f.createParser(new ThrottledInputStream(utf8Bytes(doc), 1)); |
| 77 | + case MODE_READER: |
| 78 | + return createParserUsingReader(f, doc); |
| 79 | + case MODE_READER_THROTTLED: |
| 80 | + return f.createParser(new ThrottledReader(doc, 1)); |
| 81 | + case MODE_DATA_INPUT: |
| 82 | + return createParserForDataInput(f, new MockDataInput(doc)); |
| 83 | + default: |
| 84 | + } |
| 85 | + throw new RuntimeException("internal error"); |
| 86 | + } |
| 87 | + |
| 88 | + protected JsonParser createParser(TokenStreamFactory f, int mode, byte[] doc) throws IOException |
| 89 | + { |
| 90 | + switch (mode) { |
| 91 | + case MODE_INPUT_STREAM: |
| 92 | + return f.createParser(new ByteArrayInputStream(doc)); |
| 93 | + case MODE_INPUT_STREAM_THROTTLED: |
| 94 | + return f.createParser(new ThrottledInputStream(doc, 1)); |
| 95 | + case MODE_READER: |
| 96 | + return f.createParser(new StringReader(new String(doc, "UTF-8"))); |
| 97 | + case MODE_READER_THROTTLED: |
| 98 | + return f.createParser(new ThrottledReader(new String(doc, "UTF-8"), 1)); |
| 99 | + case MODE_DATA_INPUT: |
| 100 | + return createParserForDataInput(f, new MockDataInput(doc)); |
| 101 | + default: |
| 102 | + } |
| 103 | + throw new RuntimeException("internal error"); |
| 104 | + } |
| 105 | + |
| 106 | + protected JsonParser createParserUsingReader(TokenStreamFactory f, String input) |
| 107 | + throws IOException |
| 108 | + { |
| 109 | + return f.createParser(new StringReader(input)); |
| 110 | + } |
| 111 | + |
| 112 | + protected JsonParser createParserUsingStream(TokenStreamFactory f, |
| 113 | + String input, String encoding) |
| 114 | + throws IOException |
| 115 | + { |
| 116 | + |
| 117 | + /* 23-Apr-2008, tatus: UTF-32 is not supported by JDK, have to |
| 118 | + * use our own codec too (which is not optimal since there's |
| 119 | + * a chance both encoder and decoder might have bugs, but ones |
| 120 | + * that cancel each other out or such) |
| 121 | + */ |
| 122 | + byte[] data; |
| 123 | + if (encoding.equalsIgnoreCase("UTF-32")) { |
| 124 | + data = encodeInUTF32BE(input); |
| 125 | + } else { |
| 126 | + data = input.getBytes(encoding); |
| 127 | + } |
| 128 | + InputStream is = new ByteArrayInputStream(data); |
| 129 | + return f.createParser(is); |
| 130 | + } |
| 131 | + |
| 132 | + protected JsonParser createParserForDataInput(TokenStreamFactory f, |
| 133 | + DataInput input) |
| 134 | + throws IOException |
| 135 | + { |
| 136 | + return f.createParser(input); |
| 137 | + } |
| 138 | + |
| 139 | + /* |
| 140 | + /********************************************************************** |
| 141 | + /* Assertions |
| 142 | + /********************************************************************** |
| 143 | + */ |
| 144 | + |
| 145 | + protected void assertToken(JsonToken expToken, JsonToken actToken) |
| 146 | + { |
| 147 | + if (actToken != expToken) { |
| 148 | + fail("Expected token "+expToken+", current token "+actToken); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + protected void assertToken(JsonToken expToken, JsonParser p) |
| 153 | + { |
| 154 | + assertToken(expToken, p.currentToken()); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param e Exception to check |
| 159 | + * @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included |
| 160 | + * in {@code e.getMessage()} -- using case-INSENSITIVE comparison |
| 161 | + */ |
| 162 | + public static void verifyException(Throwable e, String... anyMatches) |
| 163 | + { |
| 164 | + String msg = e.getMessage(); |
| 165 | + String lmsg = (msg == null) ? "" : msg.toLowerCase(); |
| 166 | + for (String match : anyMatches) { |
| 167 | + String lmatch = match.toLowerCase(); |
| 168 | + if (lmsg.indexOf(lmatch) >= 0) { |
| 169 | + return; |
| 170 | + } |
| 171 | + } |
| 172 | + fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\""); |
| 173 | + } |
| 174 | + |
| 175 | + /* |
| 176 | + /********************************************************************** |
| 177 | + /* Escaping/quoting |
| 178 | + /********************************************************************** |
| 179 | + */ |
| 180 | + |
| 181 | + protected String q(String str) { |
| 182 | + return '"'+str+'"'; |
| 183 | + } |
| 184 | + |
| 185 | + protected String a2q(String json) { |
| 186 | + return json.replace("'", "\""); |
| 187 | + } |
| 188 | + |
| 189 | + protected byte[] utf8Bytes(String str) { |
| 190 | + return str.getBytes(StandardCharsets.UTF_8); |
| 191 | + } |
| 192 | + |
| 193 | + protected String utf8String(ByteArrayOutputStream bytes) { |
| 194 | + return new String(bytes.toByteArray(), StandardCharsets.UTF_8); |
| 195 | + } |
| 196 | + |
| 197 | + public static byte[] encodeInUTF32BE(String input) |
| 198 | + { |
| 199 | + int len = input.length(); |
| 200 | + byte[] result = new byte[len * 4]; |
| 201 | + int ptr = 0; |
| 202 | + for (int i = 0; i < len; ++i, ptr += 4) { |
| 203 | + char c = input.charAt(i); |
| 204 | + result[ptr] = result[ptr+1] = (byte) 0; |
| 205 | + result[ptr+2] = (byte) (c >> 8); |
| 206 | + result[ptr+3] = (byte) c; |
| 207 | + } |
| 208 | + return result; |
| 209 | + } |
| 210 | +} |
0 commit comments