testconfiguration.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 275 行

JAVA
275
字号
/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 *
 */


package com.sun.testme.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;

public class TestConfiguration {
    
    /** Creates a new instance of TestConfiguration */
    public TestConfiguration(String folderName, Vector platformCp, Vector appCp) {
        this.folderName = folderName;
        this.platformCp = platformCp;
        this.appCp = appCp;
    }
    
    private String getStringFromVector(Vector v) {
        String res = "";
        for (int i = 0; i < v.size(); i++) {
            String cur = (String)v.get(i);
            res += cur + ";";
        }
        return res;
    }
    
    private static Vector getVectorFromString(String str) {
        Vector res = new Vector();
        StringTokenizer token = new StringTokenizer(str, ";");
        while (token.hasMoreElements()) {
            res.add(token.nextToken());
        }
        return res;
    }
    
    public void store() {
        try {
            Properties prop = new Properties();
            prop.put("platformCp", getStringFromVector(platformCp));
            prop.put("appCp", getStringFromVector(appCp));
            if (-1 == folderName.indexOf("test.config")) {
                folderName = folderName + "/test.config";
            }
            prop.store(new FileOutputStream(folderName),
                    "");
            storeInBuildConfiguration();
            storeInTestSuiteConfiguration();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void storeInBuildConfiguration() {
        OpenProjects openProjects = OpenProjects.getDefault();
        Project project = openProjects.getMainProject();
        FileObject fob= project.getProjectDirectory().getFileObject("build/plugin.properties");
        if (fob == null) {
            return;
        }
        Properties prop = new Properties();
        try {
            InputStream in = fob.getInputStream();
            prop.load(in);
            in.close();
            String value = prop.getProperty("testedClasspath");
            if (!value.equals("")) {
                value += ";";
            }
            String newValue = getStringFromVector(appCp);
            if (newValue.equals("")) {
                return;
            }
            value += newValue;
            prop.put("testedClasspath", value);
            value = prop.getProperty("testedPackagePrefix");
            String testedPackagePrefix = value + " ";
            for (int i2 = 0; i2 < appCp.size(); i2++) {
                File file = new File((String)appCp.elementAt(i2));
                if (file.isFile()) { //jar file
                    JarFile jarFile = new JarFile(file);
                    Enumeration entries = jarFile.entries();
                    while(entries.hasMoreElements()) {
                        JarEntry entry = (JarEntry)entries.nextElement();
                        if (entry.isDirectory()) {
                            String name = entry.getName();
                            name = name.substring(0, name.length() - 1);
                            if (name.contains("/")) {
                                continue;
                            }
                            //System.out.println(name);
                            if (name.contains("META-INF")) {
                                continue;
                            }
                            testedPackagePrefix += " -in " + name;
                        }

                    }
                } else {
                    //directory
                    String[] subDirs = file.list();
                    for (int i = 0; i < subDirs.length; i++) {
                        if (subDirs[i].contains("META-INF")) {
                            continue;
                        }
                        testedPackagePrefix += " -in " + subDirs[i];
                    }
                }
            }
            prop.put("testedPackagePrefix", testedPackagePrefix);
            FileLock lock = fob.lock();
            prop.store(fob.getOutputStream(lock), "");
            lock.releaseLock();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void storeInTestSuiteConfiguration() {
        OpenProjects openProjects = OpenProjects.getDefault();
        Project project = openProjects.getMainProject();
        FileObject fob= project.getProjectDirectory().getFileObject("testsuite.jtt");
        if (fob == null) {
            return;
        }
        Properties prop = new Properties();
        try {
            InputStream in = fob.getInputStream();
            prop.load(in);
            in.close();
            String value = prop.getProperty("testclasspath");
            if (!value.equals("")) {
                value += ";";
            }
            String newValue = getStringFromVector(appCp);
            if (newValue.equals("")) {
                return;
            }
            value += newValue;
            prop.put("testclasspath", value);
            FileLock lock = fob.lock();
            prop.store(fob.getOutputStream(lock), "");
            lock.releaseLock();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static TestConfiguration tryToLoad(FileObject fob) {
        FileObject confFob = fob.getFileObject("test.config");
        if (confFob == null) {
            confFob = fob.getParent().getFileObject("test.config");
            if (confFob == null) {
                return null;
            }
        }
        
        File file = FileUtil.toFile(confFob);
        try {
            Properties prop = new Properties();
            InputStream in = new FileInputStream(file);
            prop.load(in);
            in.close();
            TestConfiguration conf = new TestConfiguration(file.getAbsolutePath(),
                    getVectorFromString((String)prop.get("platformCp")),
                    getVectorFromString((String)prop.get("appCp")));
            return conf;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public void update() {
        try {
            Properties prop = new Properties();
            InputStream in = new FileInputStream(getConfFileName());
            prop.load(in);
            in.close();
            platformCp = getVectorFromString((String)prop.get("platformCp"));
            appCp = getVectorFromString((String)prop.get("appCp"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Vector getPlatformCp() {
        return platformCp;
    }
    
    public Vector getAppCp() {
        return appCp;
    }
    
    public String getFolderName() {
        return folderName;
    }
    
    public String getConfFileName() {
        return folderName;// + "/test.config";
    }
    
    public boolean equals(Object obj) {
        TestConfiguration conf = (TestConfiguration)obj;
        String s1 = folderName.replace('\\', '/');
        String s2 = conf.getFolderName().replace('\\', '/');
        if (!s1.equals(s2)) {
            return false;
        }
        Vector platformCp = conf.getPlatformCp();
        Vector appCp = conf.getAppCp();
        if (!equalsVectors(this.platformCp, platformCp)) {
            return false;
        }
        if (!equalsVectors(this.appCp, appCp)) {
            return false;
        }
        return true;
    }
    
    private boolean equalsVectors(Vector v1, Vector v2) {
        if (v1.size() != v2.size()) {
            return false;
        }
        
        for (int i = 0; i < v1.size(); i++) {
            if (!v1.get(i).equals(v2.get(i))) {
                return false;
            }
        }
        return true;
    }
    
    private String folderName;
    private Vector platformCp;
    private Vector appCp;
}
    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?