Question

So I'm attempting to run various tests to check that my XML parsing is correct. Unfortunately none of them pass which I assume is due to the fact my XML file isn't being stored correctly to the ArrayList. I'd appreciate any help!

VideoFile.java:

package server;

public class VideoFile {
    private int id;
    private String title;
    private String filename;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }  
}

XMLReader.java:

package server;


import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class XMLReader extends DefaultHandler {

    //List to hold VideoFiles object
    private List<VideoFile> videoList = null;
    private VideoFile emp = null;


    //getter method for employee list
    public List<VideoFile> getList() {
        return videoList;
    }


    boolean bTitle = false;
    boolean bFilename = false;


    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {

        if (qName.equalsIgnoreCase("video")) {
            //create a new VideoFile and put it in Map
            String id = attributes.getValue("id");
            //initialize VideoFile object and set id attribute
            emp = new VideoFile();
            emp.setId(Integer.parseInt(id));
            //initialize list
            if (videoList == null)
                videoList = new ArrayList<>();
        } else if (qName.equalsIgnoreCase("title")) {
            //set boolean values for fields, will be used in setting VideoFile variables
            bTitle = true;
        } else if (qName.equalsIgnoreCase("filename")) {
            bFilename = true;
        }    
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("video")) {
            //add VideoFile object to list
            videoList.add(emp);
        }
    }


    @Override
    public void characters(char ch[], int start, int length) throws SAXException {

        if (bTitle) {
            emp.setTitle(new String(ch, start, length));
            bTitle = false;
        } else if (bFilename) {
            emp.setFilename(new String(ch, start, length));
            bFilename = false;
        }
    }

    public static void main(String[] args) {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        SAXParser saxParser = saxParserFactory.newSAXParser();
        XMLReader handler = new XMLReader();
        saxParser.parse(new File("videoList.xml"), handler);
        //Get VideoFiles list
        List<VideoFile> videoList = handler.getList();
        //print employee information
        for(VideoFile emp : videoList)
            System.out.println(emp);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
    }
}

XMLReaderTest.java:

package server;

import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;

public class XMLReaderTest {
    private XMLReader reader;
    private List<VideoFile> videoList;

    @Before
    public void setUp() throws Exception {
        reader = new XMLReader();
        videoList = reader.getList();
    }

    @Test
    public void createListOfVideos() {
        assertTrue(videoList instanceof List);
    }

    @Test
    public void listContainsVideoFiles() {
        assertTrue(videoList.get(0) instanceof VideoFile);
    }

    @Test
    public void videoFileReturnsCorrectFields() {
        VideoFile videoFile = videoList.get(0);
        assertNotNull(videoFile.getId());
        assertNotNull(videoFile.getTitle());
        assertNotNull(videoFile.getFilename());
    }

    @Test
    public void videoFileReturnsCorrectData() {
        VideoFile videoFile = videoList.get(0);
        assertEquals("201202132", videoFile.getId());
        assertEquals("Monsters Inc.", videoFile.getTitle());
        assertEquals("monstersinc_high.mpg", videoFile.getFilename());
    }
}

videoList.xml:

<?xml version="1.0"?>
<videoList version="sample">
    <video id="4352524242">
        <title>Video 1</title>
        <filename>vid1_high.mpg</filename>
    </video>
    <video id="20120102b7">
        <title>Video 2</title>
        <filename>vid2-featurehp.mp4</filename>
    </video>
    <video id="1242102b7">
        <title>Vid3</title>
        <filename>vid3-featureukFhp.mp4</filename>
    </video>
</videoList>
Was it helpful?

Solution

Your tests are failing because your code does not parse the XML and your list of videos is left at null. Your XML document is valid, but the code that parses it isn't being run.

The only code that parses the XML document is in the main method of XMLReader. Because XMLReader isn't the main class (the main class will be a JUnit test runner class), it's main method is ignored.

Move the XML handling code out of the main method and ensure that your test calls it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top