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  
18  package org.apache.juneau.utest.utils;
19  
20  import java.io.*;
21  import java.lang.reflect.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.commons.function.*;
25  import org.apache.juneau.commons.reflect.*;
26  import org.apache.juneau.httppart.*;
27  import org.apache.juneau.parser.*;
28  
29  /**
30   * Utility class for creating mocked reader parsers.
31   */
32  public class FakeReaderParser extends ReaderParser implements HttpPartParser {
33  
34  	//-------------------------------------------------------------------------------------------------------------------
35  	// Static
36  	//-------------------------------------------------------------------------------------------------------------------
37  
38  	public static Builder create() {
39  		return new Builder();
40  	}
41  
42  	//-------------------------------------------------------------------------------------------------------------------
43  	// Builder
44  	//-------------------------------------------------------------------------------------------------------------------
45  
46  	public static class Builder extends ReaderParser.Builder {
47  		Function3<ReaderParserSession,String,ClassMeta<?>,Object> function;
48  		Function4<HttpPartType,HttpPartSchema,String,ClassMeta<?>,Object> partFunction;
49  
50  		public Builder function(Function3<ReaderParserSession,String,ClassMeta<?>,Object> value) {
51  			function = value;
52  			return this;
53  		}
54  
55  		public Builder partFunction(Function4<HttpPartType,HttpPartSchema,String,ClassMeta<?>,Object> value) {
56  			partFunction = value;
57  			return this;
58  		}
59  
60  		@Override
61  		public Builder consumes(String value) {
62  			super.consumes(value);
63  			return this;
64  		}
65  
66  		@Override
67  		public Builder copy() {
68  			return this;
69  		}
70  	}
71  
72  	//-------------------------------------------------------------------------------------------------------------------
73  	// Instance
74  	//-------------------------------------------------------------------------------------------------------------------
75  
76  	private final Function3<ReaderParserSession,String,ClassMeta<?>,Object> function;
77  	private final Function4<HttpPartType,HttpPartSchema,String,ClassMeta<?>,Object> partFunction;
78  
79  	public FakeReaderParser(Builder builder) {
80  		super(builder);
81  		this.function = builder.function;
82  		this.partFunction = builder.partFunction;
83  	}
84  
85  	@Override
86  	public <T> T doParse(ParserSession session, ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException {
87  		if (function != null)
88  			return type.cast(function.apply((ReaderParserSession)session, pipe.asString(), type));
89  		return null;
90  	}
91  
92  	@Override
93  	public HttpPartParserSession getPartSession() {
94  		return new HttpPartParserSession() {
95  			@Override
96  			public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException {
97  				return toType.cast(partFunction.apply(partType, schema, in, toType));
98  			}
99  		};
100 	}
101 
102 	@Override
103 	public <T> ClassMeta<T> getClassMeta(Class<T> c) {
104 		return this.getBeanContext().getClassMeta(c);
105 	}
106 
107 	@Override
108 	public <T> ClassMeta<T> getClassMeta(Type t, Type... args) {
109 		return this.getBeanContext().getClassMeta(t, args);
110 	}
111 }