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.rest.annotation;
18  
19  import org.apache.juneau.*;
20  import org.apache.juneau.collections.*;
21  import org.apache.juneau.rest.*;
22  import org.apache.juneau.rest.mock.*;
23  import org.junit.jupiter.api.*;
24  
25  import jakarta.servlet.*;
26  
27  class RestInit_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// @RestInit
31  	//------------------------------------------------------------------------------------------------------------------
32  
33  	@Rest(children={A_Super.class,A_Sub.class})
34  	public static class A {}
35  
36  	@Rest(path="/super")
37  	public static class A_Super {
38  		protected JsonList events = new JsonList();
39  		@RestInit
40  		public void init1c(RestContext.Builder builder) {
41  			events.add("super-1c");
42  		}
43  		@RestInit
44  		public void init1a(ServletConfig config) {
45  			events.add("super-1a");
46  		}
47  		@RestInit
48  		public void init1b() {
49  			events.add("super-1b");
50  		}
51  		@RestInit
52  		public void init2a() {
53  			events.add("super-2a");
54  		}
55  		@RestGet
56  		public JsonList getEvents() {
57  			return events;
58  		}
59  	}
60  
61  	@Rest(path="/sub", children={A_Child.class})
62  	public static class A_Sub extends A_Super {
63  		@Override
64  		@RestInit
65  		public void init1c(RestContext.Builder builder) {
66  			events.add("sub-1c");
67  		}
68  		@Override
69  		@RestInit
70  		public void init1a(ServletConfig config) {
71  			events.add("sub-1a");
72  		}
73  		@Override
74  		@RestInit
75  		public void init1b() {
76  			events.add("sub-1b");
77  		}
78  		@RestInit
79  		public void init2b() {
80  			events.add("sub-2b");
81  		}
82  	}
83  
84  	@Rest(path="/child")
85  	public static class A_Child extends A_Super {
86  		@Override
87  		@RestInit
88  		public void init1c(RestContext.Builder builder) {
89  			events.add("child-1c");
90  		}
91  		@RestInit
92  		public void init2b() {
93  			events.add("child-2b");
94  		}
95  	}
96  
97  	@Test void a01_init() throws Exception {
98  		var a = MockRestClient.build(A.class);
99  		a.get("/super/events").run().assertContent("['super-1a','super-1b','super-1c','super-2a']");
100 		a.get("/sub/events").run().assertContent("['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']");
101 		a.get("/sub/child/events").run().assertContent("['super-1a','super-1b','child-1c','super-2a','child-2b']");
102 	}
103 }