大規模なテストファイル作成ツール

7160 ワード


package com.hp.testdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

public class GenerateTestFile {
	//${number} ${ip} ${mac}
	static String fileContent = "testFiles/generate/senario1_source.xml";
	static String fileHeader = "testFiles/generate/senario1_header.txt";
	static String fileFoot = "testFiles/generate/senario1_foot.txt";
	static String fileOutput = "testFiles/generate/senario1_result.xml";
	static int loopNum = 3000;
	static int p1 =1;
	static int p2 =1;
	static int p3 =1;
	static int p4 =1;
	static Random r = new Random();
	
	public static void main(String[] args) throws Exception {
		gen();
	}

	public static String genMacAddress() {
		Random random = new Random();
		String s = "";
		s = Integer.toHexString(random.nextInt());
		return "TEST"+StringUtils.upperCase(s);
	}

	
	public static String genIP() {
		p1 = r.nextInt(255);
		p2 = r.nextInt(255);
		p3 = r.nextInt(255);
		p4 = r.nextInt(255);
		
		return p1+"."+p2+"."+p3+"."+p4;
	}

	public static void gen() throws Exception {
		File fs1 = new File(fileContent);
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				fs1.getAbsoluteFile()), "UTF-8");
		BufferedReader br = new BufferedReader(isr);

		String temp = "";
		List<String> list = new ArrayList<String>();
		while ((temp = br.readLine()) != null) {
			list.add(temp);
		}

		File fs2 = new File(fileHeader);
		InputStreamReader isr2 = new InputStreamReader(new FileInputStream(
				fs2.getAbsoluteFile()), "UTF-8");
		BufferedReader br2 = new BufferedReader(isr2);
		String temp2 = "";
		List<String> list2 = new ArrayList<String>();
		while ((temp2 = br2.readLine()) != null) {
			list2.add(temp2);
		}

		File fs3 = new File(fileFoot);
		InputStreamReader isr3 = new InputStreamReader(new FileInputStream(
				fs3.getAbsoluteFile()), "UTF-8");
		BufferedReader br3 = new BufferedReader(isr3);
		String temp3 = "";
		List<String> list3 = new ArrayList<String>();
		while ((temp3 = br3.readLine()) != null) {
			list3.add(temp3);
		}

		File fs4 = new File(fileOutput);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(fs4), "UTF-8"));

		for (String s : list2) {
			//System.out.println(s);
			bw.write(s);
			bw.write("\r
"); } for (int i = 1; i <= loopNum; i++) { String ip =""; for (String s : list) { String s2 = s.replace("${number}", String.valueOf(i)); if(s2.contains("${ip}")){ if("".equals(ip)){ ip=genIP(); } s2 = s2.replace("${ip}", ip); } s2 = s2.replace("${mac}", genMacAddress()); //System.out.println(s2); bw.write(s2); bw.write("\r
"); } } for (String s : list3) { //System.out.println(s); bw.write(s); bw.write("\r
"); } bw.flush(); bw.close(); } }

		<ns0:Device>
			<ns4:Printer xmlns:ns4="http://HP.CIS.Device">
				<ns2:ExternalId xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}</ns2:ExternalId>  
				<ns2:Description xmlns:ns2="http://HP.CIS.Common">GBR/Leicester DC-0 F</ns2:Description>
				<ns2:Name xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}</ns2:Name> 
				<ns2:TenantOwner xmlns:ns2="http://HP.CIS.Common">ACT</ns2:TenantOwner>
				<ns2:CIEnvironment xmlns:ns2="http://HP.CIS.Common">Production</ns2:CIEnvironment>
				<ns2:SacmLifecycleStatus xmlns:ns2="http://HP.CIS.Common">Installed</ns2:SacmLifecycleStatus>
				<ns4:SerialNo xmlns:ns2="http://HP.CIS.Device">1085EB957_${number}</ns4:SerialNo>  
				<ns4:OriginalBrand xmlns:ns2="http://HP.CIS.Device">Brother</ns4:OriginalBrand>
				<ns4:OriginalModel xmlns:ns2="http://HP.CIS.Device">DCP-6690cw</ns4:OriginalModel>
			</ns4:Printer>
			<ns4:Interface xmlns:ns4="http://HP.CIS.Device">
				<ns2:ExternalId xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}::00A013000CC7</ns2:ExternalId> 
				<ns2:Name xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}::00A013000CC7</ns2:Name> 
				<ns2:TenantOwner xmlns:ns2="http://HP.CIS.Common">ACT</ns2:TenantOwner>
				<ns2:RelatedCIExternalId xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}</ns2:RelatedCIExternalId> 
				<ns2:RelatedCit xmlns:ns2="http://HP.CIS.Common">Printer</ns2:RelatedCit>
				<ns2:SacmLifecycleStatus xmlns:ns2="http://HP.CIS.Common">Installed</ns2:SacmLifecycleStatus>
				<ns4:MacAddress xmlns:ns4="http://HP.CIS.Device">${mac}</ns4:MacAddress> 
			</ns4:Interface>
			<ns4:IPAddress xmlns:ns4="http://HP.CIS.Device">
				<ns2:ExternalId xmlns:ns2="http://HP.CIS.Common">1085EB957::${ip}</ns2:ExternalId> 
				<ns2:Name xmlns:ns2="http://HP.CIS.Common">${ip}</ns2:Name> 
				<ns2:TenantOwner xmlns:ns2="http://HP.CIS.Common">ACT</ns2:TenantOwner>
				<ns2:RelatedCIExternalId xmlns:ns2="http://HP.CIS.Common">1085EB957_${number}</ns2:RelatedCIExternalId> 
				<ns2:RelatedCit xmlns:ns2="http://HP.CIS.Common">Printer</ns2:RelatedCit>
				<ns2:SacmLifecycleStatus xmlns:ns2="http://HP.CIS.Common">Installed</ns2:SacmLifecycleStatus>
				<ns4:IPAddress xmlns:ns4="http://HP.CIS.Device">${ip}</ns4:IPAddress> 
				<ns4:IPAddressType xmlns:ns4="http://HP.CIS.Device">IPv4</ns4:IPAddressType>
				<ns4:RoutingDomain xmlns:ns4="http://HP.CIS.Device">DefaultDomain</ns4:RoutingDomain>
			</ns4:IPAddress> 
		</ns0:Device>