View Javadoc

1   package com.github.smokestack.jpa;
2   
3   import static org.hamcrest.MatcherAssert.assertThat;
4   
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   import javax.persistence.Query;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.apache.commons.lang.text.StrTokenizer;
13  import org.hamcrest.core.Is;
14  import org.hamcrest.core.IsNull;
15  
16  public class MockQLQuery extends MockBaseQuery {
17  
18  	protected String qlString;
19  	
20  	protected Map<String, Object> parameters=new HashMap<String, Object>();
21  
22  	/**
23  	 * Constructor
24  	 * @param qlString JQL query
25  	 */
26  	public MockQLQuery(String qlString) {
27  		assertThat(qlString, IsNull.notNullValue());
28  		this.qlString=qlString;
29  		parseParameters();
30  	}
31  
32  	/**
33  	 * Parse named parameters
34  	 */
35  	protected void parseParameters() {
36  		StrTokenizer t=new StrTokenizer(qlString);
37  		for (String s: (List<String>)t.getTokenList()){
38  			if (StringUtils.startsWith(s, ":")){
39  				parameters.put(StringUtils.substring(s, 1), null);
40  			}
41  		}
42  	}
43  
44  	/* (non-Javadoc)
45  	 * @see com.github.smokestack.jpa.MockBaseQuery#setParameter(java.lang.String, java.lang.Object)
46  	 */
47  	@Override
48  	public Query setParameter(String name, Object value) {
49  		assertThat(parameters.containsKey(name), Is.is(true));
50  		_setParameter(name, value);
51  		parameters.put(name, value);
52  		return this;
53  	}
54  
55  	public Query _setParameter(String name, Object value) {
56  		return null;
57  	}
58  
59  	public Map<String, Object> getMockParameters(){
60  		return parameters;
61  	}
62  }