PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/solr/solrj/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java

http://github.com/apache/lucene-solr
Java | 122 lines | 76 code | 18 blank | 28 comment | 0 complexity | 1b1b55b1d4369b5beb8eecf2c5a189f4 MD5 | raw file
Possible License(s): LGPL-2.1, CPL-1.0, MPL-2.0-no-copyleft-exception, JSON, Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, MIT, BSD-3-Clause
  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.solr.client.solrj.response;
  18. import org.apache.solr.SolrTestCase;
  19. import org.apache.solr.common.util.NamedList;
  20. import org.junit.Test;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. /**
  25. * A test case for the {@link FieldAnalysisResponse} class.
  26. *
  27. *
  28. * @since solr 1.4
  29. */
  30. @SuppressWarnings("unchecked")
  31. public class FieldAnalysisResponseTest extends SolrTestCase {
  32. /**
  33. * Tests the {@link FieldAnalysisResponse#setResponse(org.apache.solr.common.util.NamedList)} method.
  34. */
  35. @Test
  36. public void testSetResponse() throws Exception {
  37. // the parsing of the analysis phases is already tested in the AnalysisResponseBaseTest. So we can just fake
  38. // the phases list here and use it.
  39. final List<AnalysisResponseBase.AnalysisPhase> phases = new ArrayList<>(1);
  40. AnalysisResponseBase.AnalysisPhase expectedPhase = new AnalysisResponseBase.AnalysisPhase("Tokenizer");
  41. phases.add(expectedPhase);
  42. NamedList responseNL = buildResponse();
  43. FieldAnalysisResponse response = new FieldAnalysisResponse() {
  44. @Override
  45. protected List<AnalysisPhase> buildPhases(NamedList<Object> phaseNL) {
  46. return phases;
  47. }
  48. };
  49. response.setResponse(responseNL);
  50. assertEquals(1, response.getFieldNameAnalysisCount());
  51. FieldAnalysisResponse.Analysis analysis = response.getFieldNameAnalysis("name");
  52. Iterator<AnalysisResponseBase.AnalysisPhase> iter = analysis.getIndexPhases().iterator();
  53. assertTrue(iter.hasNext());
  54. assertSame(expectedPhase, iter.next());
  55. assertFalse(iter.hasNext());
  56. iter = analysis.getQueryPhases().iterator();
  57. assertTrue(iter.hasNext());
  58. assertSame(expectedPhase, iter.next());
  59. assertFalse(iter.hasNext());
  60. analysis = response.getFieldTypeAnalysis("text");
  61. iter = analysis.getIndexPhases().iterator();
  62. assertTrue(iter.hasNext());
  63. assertSame(expectedPhase, iter.next());
  64. assertFalse(iter.hasNext());
  65. iter = analysis.getQueryPhases().iterator();
  66. assertTrue(iter.hasNext());
  67. assertSame(expectedPhase, iter.next());
  68. assertFalse(iter.hasNext());
  69. }
  70. //================================================ Helper Methods ==================================================
  71. private NamedList buildResponse() {
  72. NamedList response = new NamedList();
  73. NamedList responseHeader = new NamedList();
  74. response.add("responseHeader", responseHeader);
  75. NamedList params = new NamedList();
  76. responseHeader.add("params", params);
  77. params.add("analysis.showmatch", "true");
  78. params.add("analysis.query", "the query");
  79. params.add("analysis.fieldname", "name");
  80. params.add("analysis.fieldvalue", "The field value");
  81. params.add("analysis.fieldtype", "text");
  82. responseHeader.add("status", 0);
  83. responseHeader.add("QTime", 66);
  84. NamedList analysis = new NamedList();
  85. response.add("analysis", analysis);
  86. NamedList fieldTypes = new NamedList();
  87. analysis.add("field_types", fieldTypes);
  88. NamedList text = new NamedList();
  89. fieldTypes.add("text", text);
  90. NamedList index = new NamedList();
  91. text.add("index", index);
  92. NamedList query = new NamedList();
  93. text.add("query", query);
  94. NamedList fieldNames = new NamedList();
  95. analysis.add("field_names", fieldNames);
  96. NamedList name = new NamedList();
  97. fieldNames.add("name", name);
  98. index = new NamedList();
  99. name.add("index", index);
  100. query = new NamedList();
  101. name.add("query", query);
  102. return response;
  103. }
  104. }