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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.response;
20
import java.io.IOException;
21
import java.io.StringWriter;
22
import java.util.Arrays;
23
import java.util.LinkedHashMap;
25
import org.apache.solr.SolrTestCaseJ4;
26
import org.apache.solr.common.util.NamedList;
27
import org.apache.solr.response.PHPSerializedResponseWriter;
28
import org.apache.solr.request.SolrQueryRequest;
29
import org.apache.solr.response.QueryResponseWriter;
30
import org.apache.solr.response.SolrQueryResponse;
31
import org.apache.solr.common.SolrDocument;
32
import org.apache.solr.common.SolrDocumentList;
33
import org.junit.BeforeClass;
34
import org.junit.Test;
37
* Basic PHPS tests based on JSONWriterTest
40
public class TestPHPSerializedResponseWriter extends SolrTestCaseJ4 {
42
public static void beforeClass() throws Exception {
43
initCore("solrconfig.xml","schema.xml");
47
public void testSimple() throws IOException {
48
SolrQueryRequest req = req("dummy");
49
SolrQueryResponse rsp = new SolrQueryResponse();
50
QueryResponseWriter w = new PHPSerializedResponseWriter();
52
StringWriter buf = new StringWriter();
53
rsp.add("data1", "hello");
55
rsp.add("data3", true);
56
w.write(buf, req, rsp);
57
assertEquals("a:3:{s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;}",
64
public void testSolrDocuments() throws IOException {
65
SolrQueryRequest req = req("q","*:*");
66
SolrQueryResponse rsp = new SolrQueryResponse();
67
QueryResponseWriter w = new PHPSerializedResponseWriter();
68
StringWriter buf = new StringWriter();
70
SolrDocument d = new SolrDocument();
74
d.addField("data1","hello");
75
d.addField("data2",42);
76
d.addField("data3",true);
78
// multivalued fields:
80
// extremely odd edge case: value is a map
82
// we use LinkedHashMap because we are doing a string comparison
83
// later and we need predictible ordering
84
LinkedHashMap<String,String> nl = new LinkedHashMap<String,String>();
85
nl.put("data4.1", "hashmap");
86
nl.put("data4.2", "hello");
87
d.addField("data4",nl);
89
d.addField("data5",Arrays.asList("data5.1", "data5.2", "data5.3"));
91
// adding one more document to test array indexes
92
d = new SolrDocument();
96
SolrDocumentList sdl = new SolrDocumentList();
99
rsp.add("response", sdl);
101
w.write(buf, req, rsp);
102
assertEquals("a:1:{s:8:\"response\";a:3:{s:8:\"numFound\";i:0;s:5:\"start\";i:0;s:4:\"docs\";a:2:{i:0;a:6:{s:2:\"id\";s:1:\"1\";s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;s:5:\"data4\";a:2:{s:7:\"data4.1\";s:7:\"hashmap\";s:7:\"data4.2\";s:5:\"hello\";}s:5:\"data5\";a:3:{i:0;s:7:\"data5.1\";i:1;s:7:\"data5.2\";i:2;s:7:\"data5.3\";}}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}",