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.utils;
18  
19  import static org.apache.juneau.reflect.Mutaters.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  class MutatersTest extends TestBase {
26  
27  	//-----------------------------------------------------------------------------------------------------------------
28  	// Constructors.
29  	//-----------------------------------------------------------------------------------------------------------------
30  
31  	public static class A {
32  		private String f;
33  		public A(String f) {
34  			this.f = f;
35  		}
36  		public A(int f) {
37  			this.f = String.valueOf(f);
38  		}
39  		public A(Integer f) {
40  			this.f = String.valueOf(f);
41  		}
42  	}
43  	@Test void stringConstructor() {
44  		assertEquals("foo", get(String.class, A.class).mutate("foo").f);
45  	}
46  	@Test void intConstructor() {
47  		assertEquals("1", get(int.class, A.class).mutate(1).f);
48  	}
49  	@Test void integerConstructor() {
50  		assertEquals("2", get(Integer.class, A.class).mutate(2).f);
51  	}
52  
53  	//-----------------------------------------------------------------------------------------------------------------
54  	// fromString methods.
55  	//-----------------------------------------------------------------------------------------------------------------
56  
57  	public static class D1 {
58  		private String f;
59  		public static D1 create(String f) {
60  			D1 d = new D1(); d.f = f; return d;
61  		}
62  	}
63  	@Test void fromString_create() {
64  		assertEquals("foo", get(String.class, D1.class).mutate("foo").f);
65  	}
66  
67  	public static class D2 {
68  		private String f;
69  		public static D2 fromString(String f) {
70  			D2 d = new D2(); d.f = f; return d;
71  		}
72  	}
73  	@Test void fromString_fromString() {
74  		assertEquals("foo", get(String.class, D2.class).mutate("foo").f);
75  	}
76  
77  	public static class D3 {
78  		private String f;
79  		public static D3 fromValue(String f) {
80  			D3 d = new D3(); d.f = f; return d;
81  		}
82  	}
83  	@Test void fromString_fromValue() {
84  		assertEquals("foo", get(String.class, D3.class).mutate("foo").f);
85  	}
86  
87  	public static class D4 {
88  		private String f;
89  		public static D4 valueOf(String f) {
90  			D4 d = new D4(); d.f = f; return d;
91  		}
92  	}
93  	@Test void fromString_valueOf() {
94  		assertEquals("foo", get(String.class, D4.class).mutate("foo").f);
95  	}
96  
97  	public static class D5 {
98  		private String f;
99  		public static D5 parse(String f) {
100 			D5 d = new D5(); d.f = f; return d;
101 		}
102 	}
103 	@Test void fromString_parse() {
104 		assertEquals("foo", get(String.class, D5.class).mutate("foo").f);
105 	}
106 
107 	public static class D6 {
108 		private String f;
109 		public static D6 parseString(String f) {
110 			D6 d = new D6(); d.f = f; return d;
111 		}
112 	}
113 	@Test void fromString_parseString() {
114 		assertEquals("foo", get(String.class, D6.class).mutate("foo").f);
115 	}
116 
117 	public static class D7 {
118 		private String f;
119 		public static D7 forName(String f) {
120 			D7 d = new D7(); d.f = f; return d;
121 		}
122 	}
123 	@Test void fromString_forName() {
124 		assertEquals("foo", get(String.class, D7.class).mutate("foo").f);
125 	}
126 
127 	public static class D8 {
128 		private String f;
129 		public static D8 forString(String f) {
130 			D8 d = new D8(); d.f = f; return d;
131 		}
132 	}
133 	@Test void fromString_forString() {
134 		assertEquals("foo", get(String.class, D8.class).mutate("foo").f);
135 	}
136 
137 	//-----------------------------------------------------------------------------------------------------------------
138 	// fromX methods.
139 	//-----------------------------------------------------------------------------------------------------------------
140 
141 	public static class X {}
142 
143 	public static class E1 {
144 		private String f;
145 		public static E1 create(X x) {
146 			E1 e = new E1(); e.f = "ok"; return e;
147 		}
148 	}
149 	@Test void fromX_create() {
150 		assertEquals("ok", get(X.class, E1.class).mutate(new X()).f);
151 	}
152 
153 	public static class E2 {
154 		private String f;
155 		public static E2 fromX(X x) {
156 			E2 e = new E2(); e.f = "ok"; return e;
157 		}
158 	}
159 	@Test void fromX_fromX() {
160 		assertEquals("ok", get(X.class, E2.class).mutate(new X()).f);
161 	}
162 }