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