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.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.common.utils.*;
26  import org.junit.jupiter.api.*;
27  
28  /**
29   * Tests the {@link MsgPackParser} class.
30   */
31  class MsgPackParser_Test extends TestBase {
32  
33  	//====================================================================================================
34  	// testStreamsAutoClose
35  	// Validates PARSER_autoCloseStreams.
36  	//====================================================================================================
37  	@Test void a01_streamsAutoClose() throws Exception {
38  		var p = MsgPackParser.DEFAULT.copy().autoCloseStreams().build();
39  		var is = is("00 01");
40  
41  		var r = p.parse(is, Object.class);
42  		assertJson("0", r);
43  		assertThrowsWithMessage(Exception.class, "Stream is closed", ()->p.parse(is, Object.class));
44  	}
45  
46  	//====================================================================================================
47  	// testMultipleObjectsInStream
48  	// Validates that input streams are not closed so that we can read streams of POJOs.
49  	//====================================================================================================
50  	@Test void a02_multipleObjectsInStream() throws Exception {
51  		var p = MsgPackParser.DEFAULT;
52  		var is = is("00 01");
53  
54  		var r = p.parse(is, Object.class);
55  		assertEquals("0", r.toString());
56  		r = p.parse(is, Object.class);
57  		assertJson("1", r);
58  
59  		is = is("D1 00 80 D1 00 81");
60  		r = p.parse(is, Object.class);
61  		assertJson("128", r);
62  		r = p.parse(is, Object.class);
63  		assertJson("129", r);
64  
65  		is = is("D2 00 00 80 00 D2 00 00 80 01");
66  		r = p.parse(is, Object.class);
67  		assertJson("32768", r);
68  		r = p.parse(is, Object.class);
69  		assertJson("32769", r);
70  
71  		is = is("CA 00 00 00 00 CA 3F 80 00 00");
72  		r = p.parse(is, Object.class);
73  		assertJson("0.0", r);
74  		r = p.parse(is, Object.class);
75  		assertJson("1.0", r);
76  
77  		is = is("CB 3F F0 00 00 00 00 00 00 CB BF F0 00 00 00 00 00 00");
78  		r = p.parse(is, Object.class);
79  		assertJson("1.0", r);
80  		r = p.parse(is, Object.class);
81  		assertJson("-1.0", r);
82  
83  		is = is("A0 A0");
84  		r = p.parse(is, Object.class);
85  		assertString("", r);
86  		r = p.parse(is, Object.class);
87  		assertString("", r);
88  		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");
89  		r = p.parse(is, Object.class);
90  		assertString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", r);
91  		r = p.parse(is, Object.class);
92  		assertString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", r);
93  		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");
94  		r = p.parse(is, Object.class);
95  		assertString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", r);		r = p.parse(is, Object.class);
96  
97  		assertString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", r);
98  		is = is("90 90");
99  		r = p.parse(is, Object.class);
100 		assertEmpty(r);
101 		r = p.parse(is, Object.class);
102 		assertJson("[]", r);
103 
104 		is = is("91 01 91 02");
105 		r = p.parse(is, Object.class);
106 		assertJson("[1]", r);
107 		r = p.parse(is, Object.class);
108 		assertJson("[2]", r);
109 
110 		is = is("80 80");
111 		r = p.parse(is, Object.class);
112 		assertJson("{}", r);
113 		r = p.parse(is, Object.class);
114 		assertJson("{}", r);
115 
116 		is = is("81 A1 31 01 81 A1 31 02");
117 		r = p.parse(is, Object.class);
118 		assertJson("{'1':1}", r);
119 		r = p.parse(is, Object.class);
120 		assertJson("{'1':2}", r);
121 	}
122 
123 	private InputStream is(String spacedHex) {
124 		return new CloseableByteArrayInputStream(StringUtils.fromSpacedHex(spacedHex));
125 	}
126 }