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.encoders;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class EncoderSet_Test extends TestBase {
27  
28  	//====================================================================================================
29  	// Test matching
30  	//====================================================================================================
31  	@Test void a01_encoderGroupMatching() {
32  
33  		var s = EncoderSet.create().add(Encoder1.class, Encoder2.class, Encoder3.class).build();
34  		assertInstanceOf(Encoder1.class, s.getEncoder("gzip1"));
35  		assertInstanceOf(Encoder2.class, s.getEncoder("gzip2"));
36  		assertInstanceOf(Encoder2.class, s.getEncoder("gzip2a"));
37  		assertInstanceOf(Encoder3.class, s.getEncoder("gzip3"));
38  		assertInstanceOf(Encoder3.class, s.getEncoder("gzip3a"));
39  		assertInstanceOf(Encoder3.class, s.getEncoder("gzip3,gzip2,gzip1"));
40  		assertInstanceOf(Encoder1.class, s.getEncoder("gzip3;q=0.9,gzip2;q=0.1,gzip1"));
41  		assertInstanceOf(Encoder3.class, s.getEncoder("gzip2;q=0.9,gzip1;q=0.1,gzip3"));
42  		assertInstanceOf(Encoder2.class, s.getEncoder("gzip1;q=0.9,gzip3;q=0.1,gzip2"));
43  	}
44  
45  	public static class Encoder1 extends GzipEncoder {
46  		@Override /* Encoder */
47  		public String[] getCodings() {
48  			return a("gzip1");
49  		}
50  	}
51  
52  	public static class Encoder2 extends GzipEncoder {
53  		@Override /* Encoder */
54  		public String[] getCodings() {
55  			return a("gzip2","gzip2a");
56  		}
57  	}
58  
59  	public static class Encoder3 extends GzipEncoder {
60  		@Override /* Encoder */
61  		public String[] getCodings() {
62  			return a("gzip3","gzip3a");
63  		}
64  	}
65  
66  	//====================================================================================================
67  	// Test inheritence
68  	//====================================================================================================
69  	@Test void a02_inheritence() {
70  		var sb = EncoderSet.create().add(E1.class, E2.class);
71  		var s = sb.build();
72  		assertList(s.getSupportedEncodings(), "E1", "E2", "E2a");
73  
74  		sb.add(E3.class, E4.class);
75  		s = sb.build();
76  		assertList(s.getSupportedEncodings(), "E3", "E4", "E4a", "E1", "E2", "E2a");
77  
78  		sb.add(E5.class);
79  		s = sb.build();
80  		assertList(s.getSupportedEncodings(), "E5", "E3", "E4", "E4a", "E1", "E2", "E2a");
81  	}
82  
83  	public static class E1 extends GzipEncoder {
84  		@Override /* Encoder */
85  		public String[] getCodings() {
86  			return a("E1");
87  		}
88  	}
89  
90  	public static class E2 extends GzipEncoder {
91  		@Override /* Encoder */
92  		public String[] getCodings() {
93  			return a("E2","E2a");
94  		}
95  	}
96  
97  	public static class E3 extends GzipEncoder {
98  		@Override /* Encoder */
99  		public String[] getCodings() {
100 			return a("E3");
101 		}
102 	}
103 
104 	public static class E4 extends GzipEncoder {
105 		@Override /* Encoder */
106 		public String[] getCodings() {
107 			return a("E4","E4a");
108 		}
109 	}
110 
111 	public static class E5 extends GzipEncoder {
112 		@Override /* Encoder */
113 		public String[] getCodings() {
114 			return a("E5");
115 		}
116 	}
117 }