This project has retired. For details please refer to its Attic page.
AbstractQueryTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory.query;
20  
21  import java.io.IOException;
22  import java.io.UnsupportedEncodingException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.antlr.runtime.RecognitionException;
29  import org.antlr.runtime.tree.Tree;
30  import org.apache.chemistry.opencmis.commons.definitions.PropertyBooleanDefinition;
31  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
32  import org.apache.chemistry.opencmis.commons.definitions.PropertyStringDefinition;
33  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
34  import org.apache.chemistry.opencmis.commons.enums.Updatability;
35  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyBooleanDefinitionImpl;
36  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDateTimeDefinitionImpl;
37  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerDefinitionImpl;
38  import org.apache.chemistry.opencmis.inmemory.types.InMemoryDocumentTypeDefinition;
39  import org.apache.chemistry.opencmis.inmemory.types.PropertyCreationHelper;
40  import org.apache.chemistry.opencmis.server.support.query.CmisQlStrictLexer;
41  import org.apache.chemistry.opencmis.server.support.query.CmisQueryWalker;
42  import org.apache.chemistry.opencmis.server.support.query.PredicateWalkerBase;
43  import org.apache.chemistry.opencmis.server.support.query.QueryObject;
44  import org.apache.chemistry.opencmis.server.support.query.QueryUtil;
45  
46  public abstract class AbstractQueryTest {
47  
48      protected CmisQueryWalker walker; // the walker object
49      protected QueryObject queryObj;
50      PredicateWalkerBase predicateWalker;
51      protected TypeDefinition myType, myTypeCopy, bookType;
52      protected QueryUtil queryUtil;
53  
54      protected static final String MY_DOC_TYPE = "MyDocType";
55      protected static final String MY_DOC_TYPE_COPY = "MyDocTypeCopy";
56      protected static final String BOOL_PROP = "MyBooleanProp";
57      protected static final String STRING_PROP = "MyStringProp";
58      protected static final String INT_PROP = "MyIntegerProp";
59  
60      protected static final String BOOK_TYPE = "BookType";
61      protected static final String TITLE_PROP = "Title";
62      protected static final String AUTHOR_PROP = "Author";
63      protected static final String ISBN_PROP = "ISBN";
64      protected static final String PUB_DATE_PROP = "PublishingDate";
65  
66      protected void setUp(QueryObject qo, PredicateWalkerBase pw) {
67          queryObj = qo;
68          predicateWalker = pw;
69          queryUtil = new QueryUtil();
70      }
71  
72      protected CmisQueryWalker traverseStatement(String statement) throws UnsupportedEncodingException, IOException, RecognitionException {
73          walker =  queryUtil.traverseStatement(statement, queryObj, predicateWalker);
74          return walker;
75      }
76  
77      protected CmisQueryWalker traverseStatementAndCatchExc(String statement) {
78          walker = queryUtil.traverseStatementAndCatchExc(statement, queryObj, predicateWalker);
79          return walker;
80      }
81  
82      protected CmisQueryWalker getWalker(String statement) throws RecognitionException {
83          walker = QueryUtil.getWalker(statement);
84          return walker;
85      }
86  
87  //    protected Tree getWhereTree(Tree root) {
88  //        int count = root.getChildCount();
89  //        for (int i=0; i<count; i++) {
90  //            Tree child = root.getChild(i);
91  //            if (child.getType() == CmisQlStrictLexer.WHERE) {
92  //                return child;
93  //            }
94  //        }
95  //        return null;
96  //    }
97  
98      // Helper to create some types for testing
99  
100     protected  List<TypeDefinition> createTypes() {
101 
102         List<TypeDefinition> typeDefs = new ArrayList<TypeDefinition>();
103 
104         // First test type
105         InMemoryDocumentTypeDefinition cmisType = new InMemoryDocumentTypeDefinition(MY_DOC_TYPE,
106                 "Document Type for Validation", InMemoryDocumentTypeDefinition.getRootDocumentType());
107 
108         Map<String, PropertyDefinition<?>> propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
109 
110         PropertyBooleanDefinition prop1 = PropertyCreationHelper.createBooleanDefinition(BOOL_PROP,
111                 "Sample Boolean Property", Updatability.READWRITE);
112         ((PropertyBooleanDefinitionImpl) prop1).setIsRequired(true);
113         propertyDefinitions.put(prop1.getId(), prop1);
114 
115         PropertyStringDefinition prop2 = PropertyCreationHelper.createStringDefinition(STRING_PROP,
116                 "Sample String Property", Updatability.READWRITE);
117         propertyDefinitions.put(prop2.getId(), prop2);
118 
119         PropertyIntegerDefinitionImpl prop3 = PropertyCreationHelper.createIntegerDefinition(INT_PROP,
120                 "Sample Integer Property", Updatability.READWRITE);
121         propertyDefinitions.put(prop2.getId(), prop2);
122 
123         cmisType.setPropertyDefinitions(propertyDefinitions);
124 
125         typeDefs.add(cmisType);
126         myType = cmisType;
127 
128         // add another type definition with exactly the same properties
129         cmisType = new InMemoryDocumentTypeDefinition(MY_DOC_TYPE_COPY,
130                 "Document Type Duplicated", InMemoryDocumentTypeDefinition.getRootDocumentType());
131         cmisType.setPropertyDefinitions(propertyDefinitions); // add same properties
132         typeDefs.add(cmisType);
133         myTypeCopy = cmisType;
134 
135 
136         // Second test type
137 
138         cmisType = new InMemoryDocumentTypeDefinition(BOOK_TYPE,
139                 "Book Document Type", InMemoryDocumentTypeDefinition.getRootDocumentType());
140 
141         propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
142 
143         prop2 = PropertyCreationHelper.createStringDefinition(TITLE_PROP, "Title of Book", Updatability.READWRITE);
144         propertyDefinitions.put(prop2.getId(), prop2);
145 
146         prop2 = PropertyCreationHelper.createStringDefinition(AUTHOR_PROP, "Author of Book", Updatability.READWRITE);
147         propertyDefinitions.put(prop2.getId(), prop2);
148 
149         prop3 = PropertyCreationHelper.createIntegerDefinition(ISBN_PROP,
150                 "ISBN of Book", Updatability.READWRITE);
151         propertyDefinitions.put(prop3.getId(), prop3);
152 
153         PropertyDateTimeDefinitionImpl prop4 = PropertyCreationHelper.createDateTimeDefinition(PUB_DATE_PROP,
154                 "Publishing Date of Book", Updatability.READWRITE);
155         propertyDefinitions.put(prop4.getId(), prop4);
156 
157         cmisType.setPropertyDefinitions(propertyDefinitions);
158 
159         typeDefs.add(cmisType);
160         bookType = cmisType;
161 
162         return typeDefs;
163     }
164 
165 }