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.svl.vars;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.io.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.svl.*;
25  import org.junit.jupiter.api.*;
26  
27  class RestrictedVarsTest extends TestBase {
28  
29  	@Test void a01_noNest() throws Exception {
30  		var vr = VarResolver.create().vars(NoNestVar.class).build();
31  
32  		test(vr, "$NoNest{foo}", "foo");
33  		test(vr, "$NoNest{$NoNest{foo}}", "$NoNest{foo}");
34  		test(vr, "$NoNest{foo $NoNest{foo} bar}", "foo $NoNest{foo} bar");
35  	}
36  
37  	public static class XVar extends SimpleVar {
38  
39  		public XVar() {
40  			super("X");
41  		}
42  
43  		@Override
44  		public String resolve(VarResolverSession session, String arg) throws Exception {
45  			return 'x' + arg + 'x';
46  		}
47  	}
48  
49  	public static class NoNestVar extends SimpleVar {
50  
51  		public NoNestVar() {
52  			super("NoNest");
53  		}
54  
55  		@Override
56  		public String resolve(VarResolverSession session, String arg) throws Exception {
57  			return arg.replaceAll("\\$", "\\\\\\$");
58  		}
59  
60  		@Override
61  		protected boolean allowNested() {
62  			return false;
63  		}
64  	}
65  
66  	@Test void a02_noRecurse() throws Exception {
67  		var vr = VarResolver.create().vars(XVar.class, NoRecurseVar.class).build();
68  
69  		test(vr, "$NoRecurse{foo}", "$X{foo}");
70  		test(vr, "$NoRecurse{$NoRecurse{foo}}", "$X{$X{foo}}");
71  		test(vr, "$NoRecurse{foo $NoRecurse{foo} bar}", "$X{foo $X{foo} bar}");
72  	}
73  
74  	public static class NoRecurseVar extends SimpleVar {
75  
76  		public NoRecurseVar() {
77  			super("NoRecurse");
78  		}
79  
80  		@Override
81  		public String resolve(VarResolverSession session, String arg) throws Exception {
82  			return "$X{"+arg+"}";
83  		}
84  
85  		@Override
86  		protected boolean allowRecurse() {
87  			return false;
88  		}
89  	}
90  
91  	@Test void a03_noNestOrRecurse() throws Exception {
92  		var vr = VarResolver.create().vars(XVar.class, NoEitherVar.class).build();
93  
94  		test(vr, "$NoEither{foo}", "$X{foo}");
95  		test(vr, "$NoEither{$NoEither{foo}}", "$X{$NoEither{foo}}");
96  		test(vr, "$NoEither{foo $NoEither{foo} bar}", "$X{foo $NoEither{foo} bar}");
97  	}
98  
99  	public static class NoEitherVar extends SimpleVar {
100 
101 		public NoEitherVar() {
102 			super("NoEither");
103 		}
104 
105 		@Override
106 		public String resolve(VarResolverSession session, String arg) throws Exception {
107 			return "$X{" + arg + "}";
108 		}
109 
110 		@Override
111 		protected boolean allowNested() {
112 			return false;
113 		}
114 
115 		@Override
116 		protected boolean allowRecurse() {
117 			return false;
118 		}
119 	}
120 
121 	private void test(VarResolver vr, String s, String expected) throws IOException {
122 		var sw = new StringWriter();
123 		vr.resolveTo(s, sw);
124 		assertEquals(expected, sw.toString());
125 		assertEquals(expected, vr.resolve(s));
126 	}
127 }