/**
* 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.model;
import org.jsurveylib.Survey;
import org.jsurveylib.io.XMLSurveyReader;
import org.jsurveylib.model.question.Question;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
/**
* Copyright (c)2007, Daniel Kaplan
*
* @author Daniel Kaplan
* @since 7.10.4
*/
public class PageTest {
@Test
public void label() throws Exception {
Survey m = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\label.xml")));
Page page = m.getCurrentPage();
assertEquals("My Page", page.getLabel());
}
@Test
public void elementsAndQuestions() throws Exception {
Survey m = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\elementsandquestions.xml")));
Page page = m.getCurrentPage();
assertEquals(3, page.countElements());
Question q1 = (Question) page.getSurveyElements().get(0);
assertEquals("Y", q1.getId());
Label label = (Label) page.getSurveyElements().get(1);
assertEquals("I Am a question too!!!", label.getText());
Question q2 = (Question) page.getSurveyElements().get(2);
assertEquals("X", q2.getId());
assertEquals(3, page.getSurveyElements().size());
}
@Test
public void isRequiredQuestionsAnswered() throws Exception {
Survey m = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\allmandatoryanswered.xml")));
Page firstPage = m.getCurrentPage();
assertFalse(firstPage.areRequirementsMet());
firstPage.getQuestions().get(0).setAnswer("yes");
assertFalse(firstPage.areRequirementsMet());
m.getQuestionByID("Z").setAnswer("yes"); //this will make a mandatory question on page 0 become visible
assertTrue(m.getQuestionByID("X").isVisible());
assertFalse(firstPage.areRequirementsMet());
m.getQuestionByID("X").setAnswer("yes");
assertTrue(firstPage.areRequirementsMet());
m.goToNextPage();
Page secondPage = m.getCurrentPage();
assertFalse(secondPage.areRequirementsMet()); //page 0's "Y" must be set to "no" for question "B" to be valid
assertFalse(m.getQuestionByID("B").isValid());
m.getQuestionByID("Y").setAnswer("no");
assertTrue(secondPage.areRequirementsMet()); //page 0's "Y" must be set to "no" for question "B" to be valid
assertTrue(m.getQuestionByID("B").isValid());
}
@Test
public void insertQuestionCorrectly() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\insertquestioncorrectly.xml")));
Page p = survey.getPages().get(0);
Question z = survey.getQuestionByID("Y").populateTemplate("Z", null, false, "");
p.insertQuestion(z, 2);
assertEquals(z, p.getQuestions().get(2));
assertEquals(survey.getQuestionByID("X"), p.getQuestions().get(1));
Question a = z.populateTemplate("A", null, false, "");
p.insertQuestion(a, 0);
assertEquals(a, p.getQuestions().get(0));
assertEquals(survey.getQuestionByID("Y"), p.getQuestions().get(1));
Question m = z.populateTemplate("A", null, false, "");
p.insertQuestion(m, 1);
assertEquals(m, p.getQuestions().get(1));
assertEquals(a, p.getQuestions().get(0));
assertEquals(survey.getQuestionByID("Y"), p.getQuestions().get(2));
}
@Test(expected = IllegalArgumentException.class)
public void insertQuestionTooHigh() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\insertquestiontoohigh.xml")));
survey.getPages().get(0).insertQuestion(survey.getQuestionByID("Y").populateTemplate("Z", null, false, ""), 3);
}
@Test(expected = IllegalArgumentException.class)
public void insertQuestionTooLow() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\insertquestiontoolow.xml")));
survey.getPages().get(0).insertQuestion(survey.getQuestionByID("Y").populateTemplate("Z", null, false, ""), -1);
}
@Test
public void rowOf() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\rowof.xml")));
Page page = survey.getPages().get(0);
assertEquals(0, page.rowOf("X"));
assertEquals(1, page.rowOf("Y"));
assertEquals(2, page.rowOf("Z"));
assertTrue(page.rowOf("NONE") < 0);
}
@Test
public void skipped() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\skipped.xml")));
assertFalse(survey.getPages().get(0).isSkipped());
assertTrue(survey.getPages().get(1).isSkipped());
assertEquals(3, survey.getPages().size());
assertEquals(2, survey.getTotalPagesExcludingSkipped());
assertEquals(0, survey.getCurrentPageNumber());
assertEquals(0, survey.getCurrentPageNumberExcludingSkipped());
survey.goToNextPage();
assertEquals(2, survey.getCurrentPageNumber()); //we skipped a page
assertEquals(1, survey.getCurrentPageNumberExcludingSkipped());
assertTrue(survey.isLastPageAndComplete());
survey.goToPreviousPage();
assertEquals(0, survey.getCurrentPageNumber());
assertEquals(0, survey.getCurrentPageNumberExcludingSkipped());
assertFalse(survey.isLastPageAndComplete());
//we have mandatory / invalid questions on a skipped page but we should still be allowed to finish
Question mandatory = survey.getQuestionByID("mandatory");
assertTrue(mandatory.isMandatory() && !mandatory.isAnswered());
Question invalid = survey.getQuestionByID("invalid");
assertFalse(invalid.isValid());
assertTrue(survey.getPages().get(1).areRequirementsMet());
survey.goToNextPage(); //last page
assertTrue(survey.isLastPageAndComplete());
}
@Test(expected = IllegalArgumentException.class)
public void visitSkippedPage() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\visitskippedpage.xml")));
survey.goToPage(1); //goto a skipped page
}
@Test
public void firstPagesSkipped() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\firstpageskipped.xml")));
assertEquals(2, survey.getCurrentPageNumber());
assertEquals(0, survey.getCurrentPageNumberExcludingSkipped());
assertEquals(1, survey.getTotalPagesExcludingSkipped());
assertEquals(3, survey.getPages().size());
assertFalse(survey.isPreviousPageAvailable());
assertTrue(survey.isLastPageAndComplete());
}
@Test
public void lastPagesSkipped() throws Exception {
Survey survey = new Survey(new XMLSurveyReader(new File("src\\test\\org\\jsurveylib\\model\\pagefiles\\lastpageskipped.xml")));
assertEquals(0, survey.getCurrentPageNumber());
assertEquals(0, survey.getCurrentPageNumberExcludingSkipped());
assertEquals(2, survey.getTotalPagesExcludingSkipped());
assertEquals(4, survey.getPages().size());
survey.goToNextPage();
assertEquals(1, survey.getCurrentPageNumber());
assertEquals(1, survey.getCurrentPageNumberExcludingSkipped());
assertEquals(2, survey.getTotalPagesExcludingSkipped());
assertEquals(4, survey.getPages().size());
assertFalse(survey.isNextPageAvailable());
assertTrue(survey.isLastPageAndComplete());
}
}