为测试数据创建大型XML文件(快速)

时间:2015-09-24 08:50:21

标签: xml testing

我一直在研究使用大型Xml文件测试我的程序,以了解它如何使用大型数据集执行,但是我不想生成包含~1000个条目的文件。我整个星期都会来这里!

有没有办法快速完成某个结构? 我的程序在这个布局中读取Xml文档:

           @"<root> 
                <Person>
                    <Name>Joe Doe</Name>
                    <StartDate>2007-01-01</StartDate>
                    <EndDate>2009-01-01</EndDate>
                    <Location>London</Location>
                </Person>                    
                <Person>
                    <Name>John Smith</Name>
                    <StartDate>2012-06-15</StartDate>
                    <EndDate>2014-12-31</EndDate>
                    <Location>Cardiff</Location>
                </Person>
                ...

等。 我发现了一些像Mockaroo这样的在线生成器,但它们在生成的结构中很僵硬。我也查看了诸如autoFixture之类的库,但我不觉得这就是我在这种情况下所寻求的。

如果有人能就如何实现这一点向我提供一些建议,我将非常感谢你的帮助!谢谢!

编辑:解决方案

        class Program
        {
        static void Main(string[] args)
        {

        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

        XmlNode rootNode = xmlDoc.CreateElement("Root");
        xmlDoc.AppendChild(rootNode);

        for (int i = 0; i <= 1000; i++)
        {

            XmlNode PersonNode = xmlDoc.CreateElement("Person");
            rootNode.AppendChild(PersonNode);


            XmlNode NameNode = xmlDoc.CreateElement("Name");
            NameNode.InnerText = RandomString();
            PersonNode.AppendChild(NameNode);

            XmlNode StartNode = xmlDoc.CreateElement("StartDate");
            StartNode.InnerText = RandomStartTime();
            PersonNode.AppendChild(StartNode);

            XmlNode EndNode = xmlDoc.CreateElement("EndDate");
            EndNode.InnerText = RandomEndTime();
            PersonNode.AppendChild(EndNode);

            XmlNode LocationNode = xmlDoc.CreateElement("Location");
            LocationNode.InnerText = "None";
            PersonNode.AppendChild(LocationNode);

            System.Threading.Thread.Sleep(20);
        }
        xmlDoc.Save("XmlWhitelist_Test.xml");
    }

    public static string RandomString()
    {
        int length = 6;
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        return new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray());
    }

    public static string RandomEndTime()
    {
        Random rnd = new Random();

        int year = rnd.Next(2005, 2020);
        int month = rnd.Next(1, 12);
        int day = DateTime.DaysInMonth(year, month);
        int Day = rnd.Next(1, day);

        DateTime dt = new DateTime(year, month, Day);
        string date = dt.ToString("yyyy-MM-dd");
        return date;
    }

    public static string RandomStartTime()
    {
        Random rnd = new Random();

        int year = rnd.Next(1970, 2004);
        int month = rnd.Next(1, 12);
        int day = DateTime.DaysInMonth(year, month);
        int Day = rnd.Next(1, day);

        DateTime dt = new DateTime(year, month, Day);
        string date = dt.ToString("yyyy-MM-dd");
        return date;
    }
}

1 个答案:

答案 0 :(得分:0)

可以使用Jaxb在java中快速生成大型xml文件。这是代码的例子:

Person.java

package stackoverflow;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.GregorianCalendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person", propOrder = { "name", "startDate", "endDate",
        "location" })
public class Person {

    @XmlElement(name = "Name")
    private String name;

    @XmlElement(name = "StartDate")
    private String startDate;

    @XmlElement(name = "EndDate")
    private String endDate;

    @XmlElement(name = "Location")
    private String location;

    public Person() {
        super();
        SecureRandom random = new SecureRandom();
        name = new BigInteger(20, random).toString(32)+" "+new BigInteger(20, random).toString(32);
        location = new BigInteger(50, random).toString(32);
        startDate = randomDate();
        endDate = randomDate();
    }

    private String randomDate() {
        GregorianCalendar gc = new GregorianCalendar();
        int year = randBetween(1900, 2010);
        gc.set(gc.YEAR, year);
        int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR));
        gc.set(gc.DAY_OF_YEAR, dayOfYear);
        return gc.get(gc.YEAR) + "-" + gc.get(gc.MONTH) + "-" + gc.get(gc.DAY_OF_MONTH);
    }

    public static int randBetween(int start, int end) {
        return start + (int)Math.round(Math.random() * (end - start));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

Root.java

package stackoverflow;

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

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root", propOrder = { "person"})
public class Root {

    @XmlElement(name="Person")
    private List<Person> person;

    public Root() {
        super();
        this.person = new ArrayList<Person>();
        for(int i = 0; i<1000; i++ ){
            person.add(new Person());
        }

    }

    public List<Person> getPerson() {
        return person;
    }

    public void setPerson(List<Person> person) {
        this.person = person;
    }

}

生成文件的主程序:

package stackoverflow;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {

    public static void main(String[] args) {
        try {
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(Root.class);
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING,"UTF-8");
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
            OutputStream os = new FileOutputStream("./test.xml");
            marshaller.marshal(new Root(), os);

        } catch (javax.xml.bind.JAXBException | FileNotFoundException ex) {
            ex.printStackTrace();
        }

    }
}

outputFile的例子:

...

</Person>
<Person>
    <Name>fqe8 fbct</Name>
    <StartDate>1996-7-1</StartDate>
    <EndDate>1918-11-29</EndDate>
    <Location>9aaqibb54s</Location>
</Person>
<Person>

...