Package org.jsurveylib

Source Code of org.jsurveylib.SurveyTest

/**
* 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;

import org.jsurveylib.io.XMLResultWriter;
import org.jsurveylib.io.XMLSurveyReader;
import org.jsurveylib.model.Page;
import org.jsurveylib.model.listeners.PageListener;
import org.jsurveylib.model.question.CheckboxQuestion;
import org.jsurveylib.model.question.Choice;
import org.jsurveylib.model.question.InsertQuestionListener;
import org.jsurveylib.model.question.Question;
import org.jsurveylib.model.question.RadioButtonsQuestion;
import org.jsurveylib.utils.XMLUtil;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

/**
* Copyright (c)2007, Daniel Kaplan
*
* @author Daniel Kaplan
* @since 7.10.4
*/
public class SurveyTest implements SurveyListener, PageListener, InsertQuestionListener {

    private boolean finished = false;
    private int forwardChanges = 0;
    private int backwardChanges = 0;
    private int lastPageNumber = 0;
    private static final String OUTPUT_FILEPATH = "src\\test\\org\\jsurveylib\\surveyfiles\\outputsaveresultfile.xml";

    private Question lastQuestionInserted;
    private int lastPageInserted;
    private int lastRowInserted;
    private Survey survey;

    @Before
    public void setUp() {
        cleanUp();
    }

    @After
    public void tearDown() {
        cleanUp();
    }

    private void cleanUp() {
        File file = new File(OUTPUT_FILEPATH);
        if (file.exists()) {
            file.delete();
        }
    }



    @Test
    public void listener() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\listener.xml");
        survey.addSurveyListener(this);

        assertFalse(finished);
        assertFalse(survey.isFinished());
        survey.finish();
        assertTrue(finished);
        assertTrue(survey.isFinished());
    }

    @Test
    public void loadAnswers() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersconfig.xml");
        loadAnswersTest(true, true);
    }

    @Test
    public void loadAnswersString() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersconfig.xml");
        loadAnswersTest(false, true);
    }

    @Test
    public void loadAnswersReset() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersconfig.xml");
        loadAnswersTest(true, true);
        survey.reset();
        loadAnswersTest(true, true);
    }

    @Test
    public void loadAnswersStringReset() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersconfig.xml");
        loadAnswersTest(false, true);
        survey.reset();
        loadAnswersTest(false, false);
    }

    private void loadAnswersTest(boolean useReader, boolean expectWorkingPathToBeNullAtStart) throws IOException, SAXException {
        assertFalse(survey.getQuestionByID("task_comprehension").isAnswered());
        assertFalse(survey.getQuestionByID("gender").isAnswered());
        assertFalse(survey.getQuestionByID("age_range").isAnswered());
        assertFalse(survey.getQuestionByID("studies").isAnswered());
        assertFalse(survey.getQuestionByID("occupation").isAnswered());
        assertFalse(survey.getQuestionByID("native_english").isAnswered());
        assertFalse(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertFalse(survey.getQuestionByID("gender2").isAnswered());
        assertTrue(survey.getQuestionByID("age_range2").isAnswered());
        assertEquals("two", survey.getQuestionByID("age_range2").getAnswer());
        assertFalse(survey.getQuestionByID("studies2").isAnswered());
        assertFalse(survey.getQuestionByID("occupation2").isAnswered());
        assertFalse(survey.getQuestionByID("native_english2").isAnswered());
        assertFalse(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        if (expectWorkingPathToBeNullAtStart)
            assertNull(survey.getWorkingFilePath());
        else
            assertNotNull(survey.getWorkingFilePath());
        if (useReader) {
            survey.loadXMLAnswers(new FileReader("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersdata.xml"));
            assertNull(survey.getWorkingFilePath());
        } else {
            survey.loadXMLAnswers("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersdata.xml");
            assertEquals(new File("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswersdata.xml").getAbsolutePath(), survey.getWorkingFilePath());
        }

        assertTrue(survey.getQuestionByID("task_comprehension").isAnswered());
        assertTrue(survey.getQuestionByID("gender").isAnswered());
        assertTrue(survey.getQuestionByID("age_range").isAnswered());
        assertTrue(survey.getQuestionByID("studies").isAnswered());
        assertTrue(survey.getQuestionByID("occupation").isAnswered());
        assertTrue(survey.getQuestionByID("native_english").isAnswered());
        assertTrue(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertTrue(survey.getQuestionByID("gender2").isAnswered());
        assertTrue(survey.getQuestionByID("age_range2").isAnswered());
        assertTrue(survey.getQuestionByID("studies2").isAnswered());
        assertTrue(survey.getQuestionByID("occupation2").isAnswered());
        assertTrue(survey.getQuestionByID("native_english2").isAnswered());
        assertTrue(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        assertEquals("1", survey.getQuestionByID("task_comprehension").getAnswer());
        assertEquals("male", survey.getQuestionByID("gender").getAnswer());
        assertEquals("<18", survey.getQuestionByID("age_range").getAnswer());
        assertEquals("zzz", survey.getQuestionByID("studies").getAnswer());
        assertEquals("zzzz", survey.getQuestionByID("occupation").getAnswer());
        assertEquals("yes", survey.getQuestionByID("native_english2").getAnswer());
        assertEquals("i r changed", survey.getQuestionByID("myInvisibleQuestion").getAnswer());
        assertEquals("Testbench.java", survey.getQuestionByID("pickFile").getAnswer());
        assertEquals("one", survey.getQuestionByID("age_range2").getAnswer());
    }

    @Test
    public void loadanswerswithjustanswers() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswerswithjustanswersconfig.xml");
        assertFalse(survey.getQuestionByID("task_comprehension").isAnswered());
        assertFalse(survey.getQuestionByID("gender").isAnswered());
        assertFalse(survey.getQuestionByID("age_range").isAnswered());
        assertFalse(survey.getQuestionByID("studies").isAnswered());
        assertFalse(survey.getQuestionByID("occupation").isAnswered());
        assertFalse(survey.getQuestionByID("native_english").isAnswered());
        assertFalse(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertFalse(survey.getQuestionByID("gender2").isAnswered());
        assertFalse(survey.getQuestionByID("age_range2").isAnswered());
        assertFalse(survey.getQuestionByID("studies2").isAnswered());
        assertFalse(survey.getQuestionByID("occupation2").isAnswered());
        assertFalse(survey.getQuestionByID("native_english2").isAnswered());
        assertFalse(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        survey.loadXMLAnswers(new FileReader("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswerswithjustanswersdata.xml"));

        assertTrue(survey.getQuestionByID("task_comprehension").isAnswered());
        assertTrue(survey.getQuestionByID("gender").isAnswered());
        assertTrue(survey.getQuestionByID("age_range").isAnswered());
        assertTrue(survey.getQuestionByID("studies").isAnswered());
        assertTrue(survey.getQuestionByID("occupation").isAnswered());
        assertTrue(survey.getQuestionByID("native_english").isAnswered());
        assertTrue(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertTrue(survey.getQuestionByID("gender2").isAnswered());
        assertTrue(survey.getQuestionByID("age_range2").isAnswered());
        assertTrue(survey.getQuestionByID("studies2").isAnswered());
        assertTrue(survey.getQuestionByID("occupation2").isAnswered());
        assertTrue(survey.getQuestionByID("native_english2").isAnswered());
        assertTrue(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        assertEquals("1", survey.getQuestionByID("task_comprehension").getAnswer());
        assertEquals("male", survey.getQuestionByID("gender").getAnswer());
        assertEquals("<18", survey.getQuestionByID("age_range").getAnswer());
        assertEquals("zzz", survey.getQuestionByID("studies").getAnswer());
        assertEquals("zzzz", survey.getQuestionByID("occupation").getAnswer());
        assertEquals("yes", survey.getQuestionByID("native_english2").getAnswer());
        assertEquals("i r changed", survey.getQuestionByID("myInvisibleQuestion").getAnswer());
        assertEquals("Testbench.java", survey.getQuestionByID("pickFile").getAnswer());
    }

    @Test
    public void loadanswerswithnoanswers() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswerswithnoanswersconfig.xml");
        assertFalse(survey.getQuestionByID("task_comprehension").isAnswered());
        assertFalse(survey.getQuestionByID("gender").isAnswered());
        assertFalse(survey.getQuestionByID("age_range").isAnswered());
        assertFalse(survey.getQuestionByID("studies").isAnswered());
        assertFalse(survey.getQuestionByID("occupation").isAnswered());
        assertFalse(survey.getQuestionByID("native_english").isAnswered());
        assertFalse(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertFalse(survey.getQuestionByID("gender2").isAnswered());
        assertFalse(survey.getQuestionByID("age_range2").isAnswered());
        assertFalse(survey.getQuestionByID("studies2").isAnswered());
        assertFalse(survey.getQuestionByID("occupation2").isAnswered());
        assertFalse(survey.getQuestionByID("native_english2").isAnswered());
        assertFalse(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        survey.loadXMLAnswers(new FileReader("src\\test\\org\\jsurveylib\\surveyfiles\\loadanswerswithnoanswersdata.xml"));

        assertFalse(survey.getQuestionByID("task_comprehension").isAnswered());
        assertFalse(survey.getQuestionByID("gender").isAnswered());
        assertFalse(survey.getQuestionByID("age_range").isAnswered());
        assertFalse(survey.getQuestionByID("studies").isAnswered());
        assertFalse(survey.getQuestionByID("startunanswered").isAnswered());
        assertTrue(survey.getQuestionByID("startanswered").isAnswered());
        assertFalse(survey.getQuestionByID("occupation").isAnswered());
        assertFalse(survey.getQuestionByID("native_english").isAnswered());
        assertFalse(survey.getQuestionByID("task_comprehension2").isAnswered());
        assertFalse(survey.getQuestionByID("gender2").isAnswered());
        assertFalse(survey.getQuestionByID("age_range2").isAnswered());
        assertFalse(survey.getQuestionByID("studies2").isAnswered());
        assertFalse(survey.getQuestionByID("occupation2").isAnswered());
        assertFalse(survey.getQuestionByID("native_english2").isAnswered());
        assertFalse(survey.getQuestionByID("pickFile").isAnswered());
        assertTrue(survey.getQuestionByID("myInvisibleQuestion").isAnswered());

        assertEquals("", survey.getQuestionByID("task_comprehension").getAnswer());
        assertEquals("", survey.getQuestionByID("gender").getAnswer());
        assertEquals("", survey.getQuestionByID("age_range").getAnswer());
        assertEquals("", survey.getQuestionByID("studies").getAnswer());
        assertEquals("", survey.getQuestionByID("occupation").getAnswer());
        assertEquals("", survey.getQuestionByID("native_english2").getAnswer());
        assertEquals("i r invisible", survey.getQuestionByID("myInvisibleQuestion").getAnswer());
        assertEquals("", survey.getQuestionByID("pickFile").getAnswer());
        assertEquals("", survey.getQuestionByID("startunanswered").getAnswer());
        assertEquals("a", survey.getQuestionByID("startanswered").getAnswer());
    }

    @Test
    public void answer() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\answer.xml");
        assertFalse(survey.getQuestionByID("age_range2").isAnswered());
        survey.setAnswer("age_range2", "two");
        assertTrue(survey.getQuestionByID("age_range2").isAnswered());
        assertEquals("two", survey.getQuestionByID("age_range2").getAnswer());
        assertEquals("two", survey.getAnswer("age_range2"));
    }

    @Test
    public void currentPage() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\currentpage.xml");
        assertEquals(0, survey.getCurrentPageNumber());
        survey.goToNextPage();
        assertEquals(1, survey.getCurrentPageNumber());
    }

    @Test
    public void saveResultFile() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\saveresultfile.xml");
        saveResultFileTest(true);
    }

    @Test
    public void saveResultFileString() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\saveresultfile.xml");
        saveResultFileTest(false);
    }

    private void saveResultFileTest(boolean useReader) throws SAXException, IOException {
        BufferedReader input1 = null;
        BufferedReader input2 = null;
        FileWriter writer = null;
        try {
            assertTrue(survey.isDirty());
            survey.setAnswer("Y", "yes");
            assertTrue(survey.isDirty());
            survey.setAnswer("X", "no");
            File output = new File(OUTPUT_FILEPATH);
            assertFalse(output.exists());
            assertNull(survey.getWorkingFilePath());
            if (useReader) {
                writer = new FileWriter(OUTPUT_FILEPATH);
                survey.saveXMLAnswers(writer);
                assertTrue(survey.isDirty());   //this may change in the future
                assertNull(survey.getWorkingFilePath());
                writer.close();
            } else {
                survey.saveXMLAnswers(OUTPUT_FILEPATH);
                assertFalse(survey.isDirty());
                assertEquals(new File(OUTPUT_FILEPATH).getAbsolutePath(), survey.getWorkingFilePath());
            }
            assertTrue(output.exists());
            input1 = new BufferedReader(new FileReader(OUTPUT_FILEPATH));

            Map<String, String> answerMap = XMLUtil.getAnswerMapFromElement(XMLUtil.getXMLRoot(input1));
            assertEquals("no", answerMap.get("X"));
            assertEquals("yes", answerMap.get("Y"));
            assertEquals(2, answerMap.size());

            answerMap = survey.getAnswerMap();
            assertEquals("no", answerMap.get("X"));
            assertEquals("yes", answerMap.get("Y"));
            assertEquals(2, answerMap.size());

            input1.close();

            input2 = new BufferedReader(new FileReader(OUTPUT_FILEPATH));
            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", input2.readLine()); //check for header
            String secondLine = input2.readLine();
            assertTrue(secondLine.contains(XMLResultWriter.XSD_LOCATION));
            assertTrue(secondLine.contains("<answers"));

            assertNextLineContains(input2, "<qa id=\"Y");
            assertNextLineContains(input2, "<question><![CDATA[Y:]]></question>");
            assertNextLineContains(input2, "<answer><![CDATA[yes]]></answer>");
            assertNextLineContains(input2, "</qa>");

            assertNextLineContains(input2, "<qa id=\"X");
            assertNextLineContains(input2, "<question><![CDATA[X:]]></question>");
            assertNextLineContains(input2, "<answer><![CDATA[no]]></answer>");
            assertNextLineContains(input2, "</qa>");

            assertNextLineContains(input2, "</answers>");
            input2.close();
        } finally {
            if (input1 != null) {
                input1.close();
            }
            if (input2 != null) {
                input2.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }

    private void assertNextLineContains(BufferedReader input, String contains) throws IOException {
        String line = input.readLine();
        assertTrue("\nWAS     : '" + line + "'\n" +
                "EXPECTED: '" + contains + "'", line.contains(contains));
    }

    @Test
    public void saveToFileOnFinish() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\savetofileonfinish.xml");
        assertTrue(survey.saveToFileOnFinish());
    }

    @Test
    public void saveResultWriter() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\saveresultwriter.xml");
        assertFalse(survey.saveToFileOnFinish());
        survey.setAnswer("X", "yes");
        survey.setAnswer("Y", "no");

        StringWriter stringWriter = new StringWriter();
        survey.saveXMLAnswers(stringWriter);

        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 saveUnsetMandatory() throws Exception {
        survey = new Survey("src\\test\\org\\jsurveylib\\surveyfiles\\saveunsetmandatory.xml");
        File output = new File(OUTPUT_FILEPATH);
        assertFalse(output.exists());
        survey.saveXMLAnswers(OUTPUT_FILEPATH);
        assertTrue(true);       //apparently this is ok behavior.  Is that what we want?
    }

    @Test(expected = SAXException.class)
    public void invalidUsingXSD() throws Exception {
        new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\invalidUsingXSD.xml")));
    }

    @Test
    public void invalidNoXSD() throws Exception {
        new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\invalidNoXSD.xml")));
        assertTrue(true);
    }

    @Test
    public void validWithXSD() throws Exception {
        new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\valid.xml")));
        assertTrue(true);
    }

    @Test
    public void page() throws Exception {
        Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\page.xml")));
        int pageCount = 0;
        for (Page page : survey.getPages()) {

            assertEquals(page, survey.getPages().get(pageCount));
            pageCount++;
        }
        assertEquals(3, pageCount);
        assertEquals(3, survey.getPages().size());
        assertEquals("DESCRIPTION", survey.getPages().get(0).getLabel());
        assertEquals("TASK", survey.getPages().get(1).getLabel());
        assertEquals("EXTRA", survey.getPages().get(2).getLabel());
    }

    @Test
    public void title() throws Exception {
        Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\title.xml")));
        assertEquals("My Title", survey.getTitle());
        survey.setTitle("My New Title");
        assertEquals("My New Title", survey.getTitle());
    }

    @Test
    public void questionById() throws Exception {
        Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\questionbyid.xml")));
        Question q = survey.getQuestionByID("X");
        assertEquals("X", q.getId());
        assertEquals("X:", q.getLabel().getText());

        q = survey.getQuestionByID("Y");
        assertEquals("Y", q.getId());
        assertEquals("Y:", q.getLabel().getText());

        assertNull(survey.getQuestionByID("Z"));
    }

    @Test
    public void onePageMovement() throws Exception {
        Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\onepagemovement.xml")));
        survey.addPageListener(this);
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("A").setAnswer("no"); //all mandatory & visible answered
        assertFalse(survey.isNextPageAvailable());   //only one page
        assertFalse(survey.isPreviousPageAvailable());//only one page
        assertTrue(survey.isLastPageAndComplete());

        survey.getQuestionByID("A").setAnswer("yes"); //an invisible, mandatory just showed up
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("B").setAnswer("yes");
        assertFalse(survey.isNextPageAvailable());   //only one page
        assertFalse(survey.isPreviousPageAvailable());//only one page
        assertTrue(survey.isLastPageAndComplete());

        survey.getQuestionByID("C").setAnswer("no"); //this made an invisible show up in an invalid state
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("C").setAnswer("yes"); //this made the previous question valid
        assertFalse(survey.isNextPageAvailable());   //only one page
        assertFalse(survey.isPreviousPageAvailable());//only one page
        assertTrue(survey.isLastPageAndComplete());

        survey.getQuestionByID("E").setAnswer("yes");    //this made a mandatory question enabled that was disabled
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("F").setAnswer("yes");    //this answered the mandatory enabled question
        assertFalse(survey.isNextPageAvailable());   //only one page
        assertFalse(survey.isPreviousPageAvailable());//only one page
        assertTrue(survey.isLastPageAndComplete());
    }

    @Test
    public void multiPageMovement() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\multipagemovement.xml")));
        survey.addPageListener(this);
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("A").setAnswer("no"); //all mandatory & visible answered
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete()); //not last page

        survey.getQuestionByID("A").setAnswer("yes"); //an invisible, mandatory just showed up
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("B").setAnswer("yes");
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete()); //not last page

        survey.getQuestionByID("E").setAnswer("yes");    //this made a mandatory question enabled that was disabled
        assertFalse(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("F").setAnswer("yes");    //this answered the mandatory enabled question
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.goToNextPage();
        assertFalse(survey.isNextPageAvailable()); //last page
        assertTrue(survey.isPreviousPageAvailable());
        assertTrue(survey.isLastPageAndComplete());
        assertEquals(0, backwardChanges);
        assertEquals(1, forwardChanges);


        survey.getQuestionByID("C").setAnswer("no"); //this made an invisible show up in an invalid state
        assertFalse(survey.isNextPageAvailable());
        assertTrue(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());

        survey.getQuestionByID("C").setAnswer("yes"); //this made the previous question valid
        assertFalse(survey.isNextPageAvailable());
        assertTrue(survey.isPreviousPageAvailable());
        assertTrue(survey.isLastPageAndComplete());
    }

    @Test
    public void goToFirstPage() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\gotofirstpage.xml")));
        assertEquals("1", survey.getCurrentPage().getLabel());
        survey.goToFirstPage();
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertEquals("1", survey.getCurrentPage().getLabel());
        survey.goToFirstPage();
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertEquals("1", survey.getCurrentPage().getLabel());

        survey.goToLastPage()//LAST page
        survey.setAnswer("Y", "yes");
        assertTrue(survey.isNextPageAvailable());
        survey.goToFirstPage();
        assertTrue(survey.isNextPageAvailable());
        assertFalse(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());
        assertEquals("1", survey.getCurrentPage().getLabel());
    }

    @Test
    public void goToLastPage() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\gotolastpage.xml")));
        survey.goToLastPage();
        assertFalse(survey.isNextPageAvailable());
        assertTrue(survey.isPreviousPageAvailable());
        assertEquals("3", survey.getCurrentPage().getLabel());
        survey.goToLastPage();
        assertFalse(survey.isNextPageAvailable());
        assertTrue(survey.isPreviousPageAvailable());
        assertFalse(survey.isLastPageAndComplete());
        assertEquals("3", survey.getCurrentPage().getLabel());

        survey.setAnswer("Y", "yes");
        assertTrue(survey.isNextPageAvailable());
        survey.goToLastPage();
        assertFalse(survey.isNextPageAvailable());
        assertTrue(survey.isPreviousPageAvailable());
        assertTrue(survey.isLastPageAndComplete());
        assertEquals("4", survey.getCurrentPage().getLabel());
    }

    @Test
    public void insertQuestionCorrectlyPageNum() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestioncorrectly.xml")));
        survey.addInsertQuestionListener(this);
        Question z = survey.getQuestionByID("Y").populateTemplate("Z", null, false, "");
        survey.insertQuestion(z, 0, 2);
        checkQuestionInserted(z, 0, 2);
        assertEquals(z, survey.getPages().get(0).getQuestions().get(2));
        assertEquals(survey.getQuestionByID("X"), survey.getPages().get(0).getQuestions().get(1));

        Question a = z.populateTemplate("A", null, false, "");
        survey.insertQuestion(a, 0, 0);
        checkQuestionInserted(a, 0, 0);
        assertEquals(a, survey.getPages().get(0).getQuestions().get(0));
        assertEquals(survey.getQuestionByID("Y"), survey.getPages().get(0).getQuestions().get(1));

        Question m = z.populateTemplate("M", null, false, "");
        survey.insertQuestion(m, 0, 1);
        checkQuestionInserted(m, 0, 1);
        assertEquals(m, survey.getPages().get(0).getQuestions().get(1));
        assertEquals(a, survey.getPages().get(0).getQuestions().get(0));
        assertEquals(survey.getQuestionByID("Y"), survey.getPages().get(0).getQuestions().get(2));

        //here we insert on a different page
        Question c = z.populateTemplate("C", null, false, "");
        survey.insertQuestion(c, 1, 1);
        checkQuestionInserted(c, 1, 1);
        assertEquals(survey.getQuestionByID("J"), survey.getPages().get(1).getQuestions().get(0));
        assertEquals(c, survey.getPages().get(1).getQuestions().get(1));
        assertEquals(survey.getQuestionByID("L"), survey.getPages().get(1).getQuestions().get(2));
    }

    @Test
    public void insertQuestionCorrectlyPageObject() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestioncorrectly.xml")));
        survey.addInsertQuestionListener(this);
        Question y = survey.getQuestionByID("Y");
        Question z = y.populateTemplate("Z", null, false, "");
        survey.insertQuestion(z, survey.pageOf(y.getId()), 2);
        checkQuestionInserted(z, 0, 2);
        assertEquals(z, survey.getPages().get(0).getQuestions().get(2));
        assertEquals(survey.getQuestionByID("X"), survey.getPages().get(0).getQuestions().get(1));

        Question a = z.populateTemplate("A", null, false, "");
        survey.insertQuestion(a, survey.pageOf(z.getId()), 0);
        checkQuestionInserted(a, 0, 0);
        assertEquals(a, survey.getPages().get(0).getQuestions().get(0));
        assertEquals(y, survey.getPages().get(0).getQuestions().get(1));

        Question m = z.populateTemplate("M", null, false, "");
        survey.insertQuestion(m, survey.pageOf(z.getId()), 1);
        checkQuestionInserted(m, 0, 1);
        assertEquals(m, survey.getPages().get(0).getQuestions().get(1));
        assertEquals(a, survey.getPages().get(0).getQuestions().get(0));
        assertEquals(survey.getQuestionByID("Y"), survey.getPages().get(0).getQuestions().get(2));

        //here we insert on a different page
        Question c = z.populateTemplate("C", null, false, "");
        survey.insertQuestion(c, survey.pageOf(survey.getQuestionByID("J").getId()), 1);
        checkQuestionInserted(c, 1, 1);
        assertEquals(survey.getQuestionByID("J"), survey.getPages().get(1).getQuestions().get(0));
        assertEquals(c, survey.getPages().get(1).getQuestions().get(1));
        assertEquals(survey.getQuestionByID("L"), survey.getPages().get(1).getQuestions().get(2));
    }

    private void checkQuestionInserted(Question z, int page, int row) {
        assertEquals(z, lastQuestionInserted);
        assertEquals(page, lastPageInserted);
        assertEquals(row, lastRowInserted);
    }

    @Test(expected = IllegalArgumentException.class)
    public void insertQuestionTooHigh() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestiontoohigh.xml")));
        survey.insertQuestion(survey.getQuestionByID("Y").populateTemplate("Z", null, false, ""), 2, 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void insertQuestionTooLow() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestiontoolow.xml")));
        survey.insertQuestion(survey.getQuestionByID("Y").populateTemplate("Z", null, false, ""), -1, 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void insertQuestionImitate() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestionduplicate.xml")));
        survey.insertQuestion(survey.getQuestionByID("Y").populateTemplate("Y", null, false, ""), 1, 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void insertQuestionImitateImitate() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\insertquestionduplicate.xml")));
        survey.insertQuestion(survey.getQuestionByID("Y").populateTemplate("W", null, false, ""), 1, 0);
        survey.insertQuestion(survey.getQuestionByID("Y").populateTemplate("W", null, false, ""), 1, 0);
    }

    @Test
    public void template() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\template.xml")));
        for (int i = 0; i < 5; ++i) {
            checkAgreementTemplate(survey, "a" + i);
        }
        for (int i = 0; i < 3; ++i) {
            checkCheckTemplate(survey, "c" + i);
        }

        Question e = survey.getQuestionByID("everything");
        assertEquals("everything", e.getLabel().getText());
        assertTrue(e.isMandatory());
        assertEquals("setVisible(\"everything\", false);", e.getOnAnswerChanged());
        assertEquals("checked", e.getAnswer());

        assertNull(survey.getQuestionByID("TEMPLATE")); //this is what the surveyreader uses for the name of template questions
        assertEquals(3, survey.getTemplateMap().keySet().size());
        assertNotNull(survey.getTemplateMap().get("Agreement"));
        assertNotNull(survey.getTemplateMap().get("CheckMe"));
        assertNotNull(survey.getTemplateMap().get("e"));
    }

    @Test
    public void pageOf() throws Exception {
        survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\surveyfiles\\pageof.xml")));
        Page p2 = survey.pageOf("task_comprehension1");
        checkPageNum(2, p2, survey);
        p2 = survey.pageOf("task_easyness3");
        checkPageNum(2, p2, survey);

        Page p3 = survey.pageOf("Y");
        checkPageNum(3, p3, survey);

        Page none = survey.pageOf("WWW");
        assertNull(none);
    }

    private void checkPageNum(int expectedPageNum, Page page, Survey survey) {
        assertTrue(page == survey.getPages().get(expectedPageNum - 1));
    }

    private void checkCheckTemplate(Survey survey, String id) {
        CheckboxQuestion check = (CheckboxQuestion) survey.getQuestionByID(id);
        assertFalse(check.isCheckboxOnRight());
        assertFalse(check.getLabel().getText().contains("TEMPLATE"));
        assertFalse(check.getId().contains("TEMPLATE"));

    }

    private void checkAgreementTemplate(Survey survey, String id) {
        RadioButtonsQuestion radio = (RadioButtonsQuestion) survey.getQuestionByID(id);
        assertFalse(radio.getLabel().getText().contains("TEMPLATE"));
        assertFalse(radio.getId().contains("TEMPLATE"));
        for (int i = 1; i <= 7; i++) {
            Choice c = radio.getChoices().get(i - 1);
            assertEquals(String.valueOf(i), c.getId());
            assertEquals(String.valueOf(i), c.getLabel());
        }

    }

    public void surveyFinished(Survey survey) {
        finished = true;
    }

    public void currentPageChanged() {
        boolean forwardChange = lastPageNumber < survey.getCurrentPageNumber();
        lastPageNumber = survey.getCurrentPageNumber();
        if (forwardChange) {
            forwardChanges++;
        } else {
            backwardChanges++;
        }
    }

    public void questionInserted(Question question, int page, int row) {
        lastQuestionInserted = question;
        lastPageInserted = page;
        lastRowInserted = row;
    }
}
TOP

Related Classes of org.jsurveylib.SurveyTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.