View Javadoc

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.tck.tests.query;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
23  
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.api.QueryResult;
27  import org.apache.chemistry.opencmis.client.api.Session;
28  import org.apache.chemistry.opencmis.commons.enums.BindingType;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
31  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
32  
33  public class InvalidQueryTest extends AbstractQueryTest {
34  
35      @Override
36      public void init(Map<String, String> parameters) {
37          super.init(parameters);
38          setName("Invalid Query Test");
39          setDescription("Tests invalid queries.");
40      }
41  
42      @Override
43      public void run(Session session) {
44          if (supportsQuery(session)) {
45              doInvalidQuery(session, "");
46              doInvalidQuery(session, "SELECT");
47              doInvalidQuery(session, "SELECT *");
48              doInvalidQuery(session, "THIS_IS_NOT_A_SELECT");
49              doInvalidQuery(session, "SELECT FROM cmis:document");
50              doInvalidQuery(session, "SELECT ,cmis:name FROM cmis:document");
51          } else {
52              try {
53                  doQuery(session, "SELECT * FROM cmis:document");
54                  addResult(createResult(WARNING, "Query capability is set to 'none' but calling query() works."));
55              } catch (CmisNotSupportedException nse) {
56                  // excepted
57              } catch (CmisObjectNotFoundException onfe) {
58                  // excepted for AtomPub
59                  if (getBinding() != BindingType.ATOMPUB) {
60                      addResult(createResult(FAILURE,
61                              "Query capability is set to 'none' but calling query() throws an exception, which is not a notSupported exception("
62                                      + onfe.toString() + ").", onfe, false));
63                  }
64              } catch (Exception ex) {
65                  addResult(createResult(FAILURE,
66                          "Query capability is set to 'none' but calling query() throws an exception, which is not a notSupported exception("
67                                  + ex.toString() + ").", ex, false));
68              }
69          }
70      }
71  
72      private void doQuery(Session session, String stmt) {
73          for (QueryResult qr : session.query(stmt, false)) {
74              qr.getPropertyByQueryName("cmis:name");
75          }
76      }
77  
78      private void doInvalidQuery(Session session, String stmt) {
79          try {
80              doQuery(session, stmt);
81              addResult(createResult(FAILURE, "This query is invalid but has been accepted: " + stmt));
82          } catch (CmisInvalidArgumentException e) {
83              // excepted
84          } catch (Exception ex) {
85              addResult(createResult(FAILURE, "This query is invalid and an unexcepted exception (" + ex.toString()
86                      + ") has been thrown: " + stmt, ex, false));
87          }
88      }
89  }