View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.msgpack;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.commons.utils.StringUtils.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.io.*;
25  
26  import org.apache.juneau.*;
27  import org.junit.jupiter.api.*;
28  
29  /**
30   * Tests the {@link MsgPackParser} class.
31   */
32  class MsgPackParser_Test extends TestBase {
33  
34  	//====================================================================================================
35  	// testStreamsAutoClose
36  	// Validates PARSER_autoCloseStreams.
37  	//====================================================================================================
38  	@Test void a01_streamsAutoClose() throws Exception {
39  		var p = MsgPackParser.DEFAULT.copy().autoCloseStreams().build();
40  		var is = is("00 01");
41  
42  		var r = p.parse(is, Object.class);
43  		assertJson("0", r);
44  		assertThrowsWithMessage(Exception.class, "Stream is closed", ()->p.parse(is, Object.class));
45  	}
46  
47  	//====================================================================================================
48  	// testMultipleObjectsInStream
49  	// Validates that input streams are not closed so that we can read streams of POJOs.
50  	//====================================================================================================
51  	@Test void a02_multipleObjectsInStream() throws Exception {
52  		var p = MsgPackParser.DEFAULT;
53  		var is = is("00 01");
54  
55  		var r = p.parse(is, Object.class);
56  		assertEquals("0", r.toString());
57  		r = p.parse(is, Object.class);
58  		assertJson("1", r);
59  
60  		is = is("D1 00 80 D1 00 81");
61  		r = p.parse(is, Object.class);
62  		assertJson("128", r);
63  		r = p.parse(is, Object.class);
64  		assertJson("129", r);
65  
66  		is = is("D2 00 00 80 00 D2 00 00 80 01");
67  		r = p.parse(is, Object.class);
68  		assertJson("32768", r);
69  		r = p.parse(is, Object.class);
70  		assertJson("32769", r);
71  
72  		is = is("CA 00 00 00 00 CA 3F 80 00 00");
73  		r = p.parse(is, Object.class);
74  		assertJson("0.0", r);
75  		r = p.parse(is, Object.class);
76  		assertJson("1.0", r);
77  
78  		is = is("CB 3F F0 00 00 00 00 00 00 CB BF F0 00 00 00 00 00 00");
79  		r = p.parse(is, Object.class);
80  		assertJson("1.0", r);
81  		r = p.parse(is, Object.class);
82  		assertJson("-1.0", r);
83  
84  		is = is("A0 A0");
85  		r = p.parse(is, Object.class);
86  		assertString("", r);
87  		r = p.parse(is, Object.class);
88  		assertString("", r);
89  		is = is("BF 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 BF 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62");
90  		r = p.parse(is, Object.class);
91  		assertString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", r);
92  		r = p.parse(is, Object.class);
93  		assertString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", r);
94  		is = is("D9 20 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 D9 20 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62");
95  		r = p.parse(is, Object.class);
96  		assertString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", r);		r = p.parse(is, Object.class);
97  
98  		assertString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", r);
99  		is = is("90 90");
100 		r = p.parse(is, Object.class);
101 		assertEmpty(r);
102 		r = p.parse(is, Object.class);
103 		assertJson("[]", r);
104 
105 		is = is("91 01 91 02");
106 		r = p.parse(is, Object.class);
107 		assertJson("[1]", r);
108 		r = p.parse(is, Object.class);
109 		assertJson("[2]", r);
110 
111 		is = is("80 80");
112 		r = p.parse(is, Object.class);
113 		assertJson("{}", r);
114 		r = p.parse(is, Object.class);
115 		assertJson("{}", r);
116 
117 		is = is("81 A1 31 01 81 A1 31 02");
118 		r = p.parse(is, Object.class);
119 		assertJson("{'1':1}", r);
120 		r = p.parse(is, Object.class);
121 		assertJson("{'1':2}", r);
122 	}
123 
124 	private static InputStream is(String spacedHex) {
125 		return new CloseableByteArrayInputStream(fromSpacedHex(spacedHex));
126 	}
127 }