001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr;
017:
018: import java.io.IOException;
019: import java.io.Writer;
020: import org.apache.solr.request.QueryResponseWriter;
021: import org.apache.solr.request.SolrQueryRequest;
022: import org.apache.solr.request.SolrQueryResponse;
023: import org.apache.solr.util.AbstractSolrTestCase;
024: import org.apache.solr.util.NamedList;
025: import org.apache.solr.util.TestHarness;
026:
027: /** Tests the ability to configure multiple query output writers, and select those
028: * at query time.
029: *
030: * @author <a href='mailto:mbaranczak@epublishing.com'> Mike Baranczak </a>
031: * @author the Solr project
032: */
033: public class OutputWriterTest extends AbstractSolrTestCase {
034:
035: /** The XML string that's output for testing purposes. */
036: public static final String USELESS_OUTPUT = "useless output";
037:
038: public String getSchemaFile() {
039: return "solr/crazy-path-to-schema.xml";
040: }
041:
042: public String getSolrConfigFile() {
043: return "solr/crazy-path-to-config.xml";
044: }
045:
046: /** responseHeader has changed in SOLR-59, check old and new variants */
047: public void testSOLR59responseHeaderVersions() {
048: // default version is 2.2, with "new" responseHeader
049: lrf.args.remove("version");
050: lrf.args.put("wt", "standard");
051: assertQ(req("foo"),
052: "/response/lst[@name='responseHeader']/int[@name='status'][.='0']");
053: lrf.args.remove("wt");
054: assertQ(req("foo"),
055: "/response/lst[@name='responseHeader']/int[@name='QTime']");
056:
057: // version=2.1 reverts to old responseHeader
058: lrf.args.put("version", "2.1");
059: lrf.args.put("wt", "standard");
060: assertQ(req("foo"), "/response/responseHeader/status[.='0']");
061: lrf.args.remove("wt");
062: assertQ(req("foo"), "/response/responseHeader/QTime");
063:
064: // and explicit 2.2 works as default
065: lrf.args.put("version", "2.2");
066: lrf.args.put("wt", "standard");
067: assertQ(req("foo"),
068: "/response/lst[@name='responseHeader']/int[@name='status'][.='0']");
069: lrf.args.remove("wt");
070: assertQ(req("foo"),
071: "/response/lst[@name='responseHeader']/int[@name='QTime']");
072: }
073:
074: public void testUselessWriter() throws Exception {
075: lrf.args.put("wt", "useless");
076: String out = h.query(req("foo"));
077: assertEquals(USELESS_OUTPUT, out);
078: }
079:
080: public void testTrivialXsltWriter() throws Exception {
081: lrf.args.put("wt", "xslt");
082: lrf.args.put("tr", "dummy.xsl");
083: String out = h.query(req("foo"));
084: System.out.println(out);
085: assertTrue(out.contains("DUMMY"));
086: }
087:
088: ////////////////////////////////////////////////////////////////////////////
089: /** An output writer that doesn't do anything useful. */
090:
091: public static class UselessOutputWriter implements
092: QueryResponseWriter {
093:
094: public UselessOutputWriter() {
095: }
096:
097: public void init(NamedList n) {
098: }
099:
100: public void write(Writer writer, SolrQueryRequest request,
101: SolrQueryResponse response) throws IOException {
102: writer.write(USELESS_OUTPUT);
103: }
104:
105: public String getContentType(SolrQueryRequest request,
106: SolrQueryResponse response) {
107: return CONTENT_TYPE_TEXT_UTF8;
108: }
109:
110: }
111:
112: }
|