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.utest.utils;
18  
19  import static org.apache.juneau.commons.utils.IoUtils.*;
20  
21  import java.io.*;
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.parser.*;
27  
28  /**
29   * Utility class for creating mocked stream parser.
30   */
31  public class FakeStreamParser extends InputStreamParser {
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 InputStreamParser.Builder {
46  		Function3<InputStreamParserSession,byte[],ClassMeta<?>,Object> function;
47  
48  		public Builder function(Function3<InputStreamParserSession,byte[],ClassMeta<?>,Object> value) {
49  			function = value;
50  			return this;
51  		}
52  
53  		@Override
54  		public Builder consumes(String value) {
55  			super.consumes(value);
56  			return this;
57  		}
58  
59  		@Override
60  		public Builder copy() {
61  			return this;
62  		}
63  	}
64  
65  	//-------------------------------------------------------------------------------------------------------------------
66  	// Instance
67  	//-------------------------------------------------------------------------------------------------------------------
68  
69  	private final Function3<InputStreamParserSession,byte[],ClassMeta<?>,Object> function;
70  
71  	public FakeStreamParser(Builder builder) {
72  		super(builder);
73  		this.function = builder.function;
74  	}
75  
76  	@Override
77  	public <T> T doParse(ParserSession session, ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException {
78  		if (function != null)
79  			return type.cast(function.apply((InputStreamParserSession)session, readBytes(pipe.getInputStream()), type));
80  		return null;
81  	}
82  }