/**
* This file is part of JSurveyLib.
*
* JSurveyLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSurveyLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JSurveyLib. If not, see <http://www.gnu.org/licenses/>.
**/
package org.jsurveylib.io;
import org.jsurveylib.Survey;
import org.jsurveylib.utils.XMLUtil;
import static org.junit.Assert.*;
import org.junit.Test;
import org.xml.sax.SAXException;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.IOException;
import java.util.Map;
/**
* Copyright (c)2007, Daniel Kaplan
*
* @author Daniel Kaplan
* @since 7.10.4
*/
public class XMLResultWriterTest {
@Test
public void saveXML() throws Exception {
Survey survey = new Survey("src\\test\\org\\jsurveylib\\io\\xmlresultwriterfiles\\savexmlinput.xml");
saveXMLTest(survey);
}
@Test
public void saveXMLReset() throws Exception {
Survey survey = new Survey("src\\test\\org\\jsurveylib\\io\\xmlresultwriterfiles\\savexmlinput.xml");
saveXMLTest(survey);
survey.reset();
saveXMLTest(survey);
}
private void saveXMLTest(Survey survey) throws SAXException, IOException {
survey.setAnswer("X", "yes");
survey.setAnswer("Y", "no");
StringWriter stringWriter = new StringWriter();
XMLResultWriter writer = new XMLResultWriter(stringWriter);
writer.write(survey);
BufferedReader input = new BufferedReader(new StringReader(stringWriter.toString()));
Map<String, String> answerMap = XMLUtil.getAnswerMapFromElement(XMLUtil.getXMLRoot(input));
assertEquals("yes", answerMap.get("X"));
assertEquals("no", answerMap.get("Y"));
assertEquals(2, answerMap.size());
input = new BufferedReader(new StringReader(stringWriter.toString()));
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", input.readLine()); //check for header
String secondLine = input.readLine();
assertTrue(secondLine.contains(XMLResultWriter.XSD_LOCATION));
assertTrue(secondLine.contains("<answers"));
assertTrue(input.readLine().contains("<qa id=\""));
assertTrue(input.readLine().contains("<question>"));
}
@Test
public void finalState() throws Exception {
Survey survey = new Survey("src\\test\\org\\jsurveylib\\io\\xmlresultwriterfiles\\finalstate.xml");
StringWriter stringWriter = new StringWriter();
XMLResultWriter writer = new XMLResultWriter(stringWriter);
writer.write(survey);
String s = stringWriter.toString();
BufferedReader input = new BufferedReader(new StringReader(stringWriter.toString()));
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", input.readLine()); //check for header
String secondLine = input.readLine();
assertTrue(secondLine.contains(XMLResultWriter.XSD_LOCATION));
String line;
int qas = 0;
while ((line = input.readLine()) != null) {
if (line.contains("<qa")) {
qas++;
if (qas == 1)
assertTrue(checkFlags(line, false, false, false));
if (qas == 2)
assertTrue(checkFlags(line, true, false, false));
if (qas == 3)
assertTrue(checkFlags(line, false, true, false));
if (qas == 4)
assertTrue(checkFlags(line, false, false, true));
}
}
assertEquals(4, qas);
}
private boolean checkFlags(String line, boolean invisible, boolean disabled, boolean invalid) {
boolean vis = line.contains("visible=\"false\"");
boolean en = line.contains("enabled=\"false\"");
boolean val = line.contains("valid=\"false\"");
return vis == invisible && en == disabled && val == invalid;
}
}