简体   繁体   English

尝试使用SAX解析XML,但是我的JUnit测试都无法正常工作吗?

[英]Attempting to parse XML using SAX, but none of my JUnit tests work?

So I'm attempting to run various tests to check that my XML parsing is correct. 因此,我试图运行各种测试来检查我的XML解析是否正确。 Unfortunately none of them pass which I assume is due to the fact my XML file isn't being stored correctly to the ArrayList. 不幸的是,我认为它们都没有通过,原因是我的XML文件没有正确存储到ArrayList中。 I'd appreciate any help! 我将不胜感激!

VideoFile.java: 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: 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: 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: 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>

Your tests are failing because your code does not parse the XML and your list of videos is left at null. 测试失败,因为您的代码无法解析XML,并且您的视频列表为空。 Your XML document is valid, but the code that parses it isn't being run. 您的XML文档有效,但是解析该文档的代码未在运行。

The only code that parses the XML document is in the main method of XMLReader . 解析XML文档的唯一代码是XMLReadermain方法。 Because XMLReader isn't the main class (the main class will be a JUnit test runner class), it's main method is ignored. 因为XMLReader不是主类(主类将是JUnit测试运行器类),所以将忽略它的main方法。

Move the XML handling code out of the main method and ensure that your test calls it. 将XML处理代码移出main方法,并确保您的测试调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM