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