FileUtils.java 6.24 KB
Newer Older
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtils {

	public static void visitAllFiles(File f, ArrayList<File> files) {
		if (f.isDirectory()) {
			String[] children = f.list();
			for (int i = 0; i < children.length; i++) {
				visitAllFiles(new File(f, children[i]), files);
			}
		} else {
			if (f.toString().endsWith(".ttcn")) {
				files.add(f);
			}
		}
	}

	// -----------------------------------------------------------------------------------------------

	public static void loadConfig(Properties config) throws IOException {
		FileInputStream fis = new FileInputStream(
				"resources/TestCastTester.properties");
		config.load(fis);
		fis.close();
		if ((config.get("TestCastCommandLineTool") == null)
				|| (config.get("LoopbackAdapterPath") == null)
				|| (config.get("TempPath") == null)
				|| (config.get("LogPath") == null)
				|| (config.get("ATSPath") == null)) {
			throw new IOException();
		}

	}

	// -----------------------------------------------------------------------------------------------

	public static void copyFile(File in, File out) throws IOException {
		FileChannel inChannel = new FileInputStream(in).getChannel();
		FileChannel outChannel = new FileOutputStream(out).getChannel();
		try {
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} catch (IOException e) {
			throw e;
		} finally {
			if (inChannel != null)
				inChannel.close();
			if (outChannel != null)
				outChannel.close();
		}
	}

	// -----------------------------------------------------------------------------------------------

	public static int countT3Modules(File f) {
		int moduleCount = 0;
		try {
			FileInputStream fs = new FileInputStream(f.getAbsolutePath());
			DataInputStream in = new DataInputStream(fs);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine = "";
			while ((strLine = br.readLine()) != null) {
				if (strLine.matches("^\\s*module\\s+\\w+\\s*.*")) {
					moduleCount++;
				}
			}
			in.close();
			fs.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return moduleCount;
	}

	// -----------------------------------------------------------------------------------------------

	private static Pattern s_verdictPassMatch = Pattern.compile("^^\\s*\\*\\*\\s*@verdict\\s*pass\\s*(.*)");
	private static Pattern s_verdictMatch = Pattern.compile("^\\s*\\*\\*\\s*@verdict\\s*.*");
	private static Pattern s_configMatch = Pattern.compile("^\\s*\\*\\*\\s*@configuration\\s*(.*)");
	public static T3ExpectedOutput extractExpectedOutput(File f) {
		T3ExpectedOutput t3ExpectedOutput = new T3ExpectedOutput();
		
		try {
			FileInputStream fs = new FileInputStream(f.getAbsolutePath());
			DataInputStream in = new DataInputStream(fs);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine = "";
			Matcher configMatch; 
			while ((strLine = br.readLine()) != null) {
				if (s_verdictMatch.matcher(strLine).matches()) {
					Matcher matcher = s_verdictPassMatch.matcher(strLine);
					if (matcher.find()) {
						String result = matcher.group(1);
						String[] split = result.split(",");
						t3ExpectedOutput.setExpectedOutput(split[0].trim());
						if (split.length > 1)
							t3ExpectedOutput.setExecutionResult(split[1].trim());
					}
					
//					System.out.println(strLine);
				} else if ((configMatch = s_configMatch.matcher(strLine)).matches()) {
					t3ExpectedOutput.setConfiguration(configMatch.group(1));
				}
			}
			in.close();
			fs.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return t3ExpectedOutput;
	}
	
	// -----------------------------------------------------------------------------------------------

	public static ArrayList<File> splitT3ModuleFiles(File f) {
		ArrayList<File> files = new ArrayList<File>();
		StringBuffer current = new StringBuffer();
		File newFile = new File(f.getParent() + "//temp.ttcn");
		if (f.exists()) {
			f.renameTo(newFile);
		}
				
		try {
			FileInputStream fs = new FileInputStream(newFile.getAbsolutePath());
			DataInputStream in = new DataInputStream(fs);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine = "";
			int moduleCounter = 0;
			String currentModuleName = "";
						
			while ((strLine = br.readLine()) != null) {
				if (strLine.matches("^\\s*module\\s+\\w+\\s*.*")) {
					moduleCounter++;
					if (moduleCounter > 1) { // write file
						String targetFilename = f.getParent() + "//" + currentModuleName + ".ttcn";
						FileWriter fw = new FileWriter(targetFilename);
						fw.write(current.toString());
						fw.close();
						current = new StringBuffer();
						files.add(new File(targetFilename));
					}
					Matcher matcher = Pattern.compile("^\\s*module\\s+(\\w+)\\s*.*").matcher(strLine);
					if (matcher.find()) {
						currentModuleName = matcher.group(1);
					}
				}
				current.append(strLine + "\n");
			}
			// write file
			String targetFilename = f.getParent() + "//" + currentModuleName + ".ttcn";
			FileWriter fw = new FileWriter(targetFilename);
			fw.write(current.toString());
			fw.close();
			in.close();
			fs.close();
			files.add(new File(targetFilename));
			newFile.delete();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return files;
	}

	public static ArrayList<String> readExclusionList(File f) {
		ArrayList<String> files = new ArrayList<String>();
				
		try {
			FileInputStream fs = new FileInputStream(f.getAbsolutePath());
			DataInputStream in = new DataInputStream(fs);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine = "";
			while ((strLine = br.readLine()) != null) {
				if (strLine.trim().length() > 0)
					files.add(strLine);
			}
			return files;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}