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.parser;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.json.*;
24  import org.junit.jupiter.api.*;
25  
26  class ParserSet_Test extends TestBase {
27  
28  	//====================================================================================================
29  	// Test parser group matching
30  	//====================================================================================================
31  	@Test void a01_parserGroupMatching() {
32  
33  		var s = ParserSet.create().add(Parser1.class, Parser2.class, Parser3.class).build();
34  		assertInstanceOf(Parser1.class, s.getParser("text/foo"));
35  		assertInstanceOf(Parser1.class, s.getParser("text/foo_a"));
36  		assertInstanceOf(Parser1.class, s.getParser("text/foo_a+xxx"));
37  		assertInstanceOf(Parser1.class, s.getParser("text/xxx+foo_a"));
38  		assertInstanceOf(Parser2.class, s.getParser("text/foo+bar"));
39  		assertInstanceOf(Parser2.class, s.getParser("text/foo+bar_a"));
40  		assertInstanceOf(Parser2.class, s.getParser("text/bar+foo"));
41  		assertInstanceOf(Parser2.class, s.getParser("text/bar+foo+xxx"));
42  		assertInstanceOf(Parser3.class, s.getParser("text/baz"));
43  		assertInstanceOf(Parser3.class, s.getParser("text/baz_a"));
44  		assertInstanceOf(Parser3.class, s.getParser("text/baz+yyy"));
45  		assertInstanceOf(Parser3.class, s.getParser("text/baz_a+yyy"));
46  		assertInstanceOf(Parser3.class, s.getParser("text/yyy+baz"));
47  		assertInstanceOf(Parser3.class, s.getParser("text/yyy+baz_a"));
48  	}
49  
50  
51  	public static class Parser1 extends JsonParser { public Parser1(JsonParser.Builder b) { super(b.consumes("text/foo,text/foo_a")); }}
52  	public static class Parser2 extends JsonParser { public Parser2(JsonParser.Builder b) { super(b.consumes("text/foo+bar,text/foo+bar_a")); }}
53  	public static class Parser3 extends JsonParser { public Parser3(JsonParser.Builder b) { super(b.consumes("text/baz,text/baz_a")); }}
54  
55  	//====================================================================================================
56  	// Test inheritence
57  	//====================================================================================================
58  	@Test void a02_inheritence() {
59  		var sb = ParserSet.create().add(P1.class, P2.class);
60  		var s = sb.build();
61  		assertList(s.getSupportedMediaTypes(), "text/1", "text/2", "text/2a");
62  
63  		sb = ParserSet.create().add(P1.class, P2.class).add(P3.class, P4.class);
64  		s = sb.build();
65  		assertList(s.getSupportedMediaTypes(), "text/3", "text/4", "text/4a", "text/1", "text/2", "text/2a");
66  
67  		sb = ParserSet.create().add(P1.class, P2.class).add(P3.class, P4.class).add(P5.class);
68  		s = sb.build();
69  		assertList(s.getSupportedMediaTypes(), "text/5", "text/3", "text/4", "text/4a", "text/1", "text/2", "text/2a");
70  	}
71  
72  	public static class P1 extends JsonParser { public P1(JsonParser.Builder b) { super(b.consumes("text/1")); }}
73  	public static class P2 extends JsonParser { public P2(JsonParser.Builder b) { super(b.consumes("text/2,text/2a")); }}
74  	public static class P3 extends JsonParser { public P3(JsonParser.Builder b) { super(b.consumes("text/3")); }}
75  	public static class P4 extends JsonParser { public P4(JsonParser.Builder b) { super(b.consumes("text/4,text/4a"));} }
76  	public static class P5 extends JsonParser { public P5(JsonParser.Builder b) { super(b.consumes("text/5"));}}
77  }