⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saveservice.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		// Get the OutputWriter to use
		OutputStreamWriter outputStreamWriter = getOutputStreamWriter(out);
		writeXmlHeader(outputStreamWriter);
		// Use deprecated method, to avoid duplicating code
		saver.toXML(el, outputStreamWriter);
		outputStreamWriter.close();
	}

	// Used by Test code
	public static Object loadElement(InputStream in) throws IOException {
		// Get the InputReader to use
		InputStreamReader inputStreamReader = getInputStreamReader(in);
		// Use deprecated method, to avoid duplicating code
		Object element = saver.fromXML(inputStreamReader);
		inputStreamReader.close();
		return element;
	}

    /**
     * Save a sampleResult to an XML output file using XStream.
     * 
     * @param evt sampleResult wrapped in a sampleEvent
     * @param writer output stream which must be created using {@link #getFileEncoding(String)}
     */
	// Used by ResultCollector#recordResult()
	public synchronized static void saveSampleResult(SampleEvent evt, Writer writer) throws IOException {
		DataHolder dh = saver.newDataHolder();
		dh.put(SAMPLE_EVENT_OBJECT, evt);
		// This is effectively the same as saver.toXML(Object, Writer) except we get to provide the DataHolder
		// Don't know why there is no method for this in the XStream class
		saver.marshal(evt.getResult(), new XppDriver().createWriter(writer), dh);
		writer.write('\n');
	}

    /**
     * @param elem test element
     * @param writer output stream which must be created using {@link #getFileEncoding(String)}
     */
	// Used by ResultCollector#recordStats()
	public synchronized static void saveTestElement(TestElement elem, Writer writer) throws IOException {
		saver.toXML(elem, writer);
		writer.write('\n');
	}

	private static boolean versionsOK = true;

	// Extract version digits from String of the form #Revision: n.mm #
	// (where # is actually $ above)
	private static final String REVPFX = "$Revision: ";
	private static final String REVSFX = " $"; // $NON-NLS-1$

	private static String extractVersion(String rev) {
		if (rev.length() > REVPFX.length() + REVSFX.length()) {
			return rev.substring(REVPFX.length(), rev.length() - REVSFX.length());
		}
		return rev;
	}

//	private static void checkVersion(Class clazz, String expected) {
//
//		String actual = "*NONE*"; // $NON-NLS-1$
//		try {
//			actual = (String) clazz.getMethod("getVersion", null).invoke(null, null);
//			actual = extractVersion(actual);
//		} catch (Exception ignored) {
//			// Not needed
//		}
//		if (0 != actual.compareTo(expected)) {
//			versionsOK = false;
//			log.warn("Version mismatch: expected '" + expected + "' found '" + actual + "' in " + clazz.getName());
//		}
//	}

    // Routines for TestSaveService
    static boolean checkPropertyVersion(){
        return SaveService.PROPVERSION.equals(SaveService.propertiesVersion);
    }
    
    static boolean checkFileVersion(){
        return SaveService.FILEVERSION.equals(SaveService.fileVersion);
    }

    static boolean checkVersions() {
		versionsOK = true;
		// Disable converter version checks as they are more of a nuisance than helpful
//		checkVersion(BooleanPropertyConverter.class, "493779"); // $NON-NLS-1$
//		checkVersion(HashTreeConverter.class, "514283"); // $NON-NLS-1$
//		checkVersion(IntegerPropertyConverter.class, "493779"); // $NON-NLS-1$
//		checkVersion(LongPropertyConverter.class, "493779"); // $NON-NLS-1$
//		checkVersion(MultiPropertyConverter.class, "514283"); // $NON-NLS-1$
//		checkVersion(SampleResultConverter.class, "571992"); // $NON-NLS-1$
//
//        // Not built until later, so need to use this method:
//        try {
//            checkVersion(
//                    Class.forName("org.apache.jmeter.protocol.http.util.HTTPResultConverter"), // $NON-NLS-1$
//                    "514283"); // $NON-NLS-1$
//        } catch (ClassNotFoundException e) {
//            versionsOK = false;
//            log.warn(e.getLocalizedMessage());
//        }
//		checkVersion(StringPropertyConverter.class, "493779"); // $NON-NLS-1$
//		checkVersion(TestElementConverter.class, "549987"); // $NON-NLS-1$
//		checkVersion(TestElementPropertyConverter.class, "549987"); // $NON-NLS-1$
//		checkVersion(ScriptWrapperConverter.class, "514283"); // $NON-NLS-1$
//		checkVersion(TestResultWrapperConverter.class, "514283"); // $NON-NLS-1$
//        checkVersion(SampleSaveConfigurationConverter.class,"549936"); // $NON-NLS-1$

        if (!PROPVERSION.equalsIgnoreCase(propertiesVersion)) {
			log.warn("Bad _version - expected " + PROPVERSION + ", found " + propertiesVersion + ".");
		}
        if (!FILEVERSION.equalsIgnoreCase(fileVersion)) {
            log.warn("Bad _file_version - expected " + FILEVERSION + ", found " + fileVersion +".");
        }
		if (versionsOK) {
			log.info("All converter versions present and correct");
		}
        return versionsOK;
	}

	public static TestResultWrapper loadTestResults(InputStream reader) throws Exception {
		// Get the InputReader to use
		InputStreamReader inputStreamReader = getInputStreamReader(reader);
		TestResultWrapper wrapper = (TestResultWrapper) saver.fromXML(inputStreamReader);
		inputStreamReader.close();
		return wrapper;
	}

	public static HashTree loadTree(InputStream reader) throws Exception {
		if (!reader.markSupported()) {
			reader = new BufferedInputStream(reader);
		}
		reader.mark(Integer.MAX_VALUE);
		ScriptWrapper wrapper = null;
		try {
			// Get the InputReader to use
			InputStreamReader inputStreamReader = getInputStreamReader(reader);
			wrapper = (ScriptWrapper) saver.fromXML(inputStreamReader);
			inputStreamReader.close();
			return wrapper.testPlan;
		} catch (CannotResolveClassException e) {
			log.warn("Problem loading new style: " + e.getLocalizedMessage());
			reader.reset();
			return OldSaveService.loadSubTree(reader);
		} catch (NoClassDefFoundError e) {
			log.warn("Missing class ", e);
			return null;
		}
	}
	
	private static InputStreamReader getInputStreamReader(InputStream inStream) {
		// Check if we have a encoding to use from properties
		Charset charset = getFileEncodingCharset();
		if(charset != null) {
			return new InputStreamReader(inStream, charset);
		}
		else {
			// We use the default character set encoding of the JRE
			return new InputStreamReader(inStream);
		}
	}

	private static OutputStreamWriter getOutputStreamWriter(OutputStream outStream) {
		// Check if we have a encoding to use from properties
		Charset charset = getFileEncodingCharset();
		if(charset != null) {
			return new OutputStreamWriter(outStream, charset);
		}
		else {
			// We use the default character set encoding of the JRE
			return new OutputStreamWriter(outStream);
		}
	}
	
	/**
	 * Returns the file Encoding specified in saveservice.properties or the default
	 * @param dflt value to return if file encoding was not provided
	 * 
	 * @return file encoding or default
	 */
	// Used by ResultCollector when creating output files
	public static String getFileEncoding(String dflt){
		if(fileEncoding != null && fileEncoding.length() > 0) {
			return fileEncoding;
		}
		else {
			return dflt;
		}		
	}
	
	private static Charset getFileEncodingCharset() {
		// Check if we have a encoding to use from properties
		if(fileEncoding != null && fileEncoding.length() > 0) {
			return Charset.forName(fileEncoding);
		}
		else {
			// We use the default character set encoding of the JRE
			return null;
		}
	}
	
	private static void writeXmlHeader(OutputStreamWriter writer) throws IOException {
		// Write XML header if we have the charset to use for encoding
		Charset charset = getFileEncodingCharset();
		if(charset != null) {
			// We do not use getEncoding method of Writer, since that returns
			// the historical name
			String header = XML_HEADER.replaceAll("<ph>", charset.name());
			writer.write(header);
			writer.write('\n');
		}
	}

	public static boolean isSaveTestPlanFormat20() {
		return IS_TESTPLAN_FORMAT_20;
	}

	public static boolean isSaveTestLogFormat20() {
		return IS_TESTLOG_FORMAT_20;
	}

    // New test format - more compressed class names
    public static boolean isSaveTestPlanFormat22() {
        return IS_TESTPLAN_FORMAT_22;
    }

    
//  Normal output
//  ---- Debugging information ----
//  required-type       : org.apache.jorphan.collections.ListedHashTree 
//  cause-message       : WebServiceSampler : WebServiceSampler 
//  class               : org.apache.jmeter.save.ScriptWrapper 
//  message             : WebServiceSampler : WebServiceSampler 
//  line number         : 929 
//  path                : /jmeterTestPlan/hashTree/hashTree/hashTree[4]/hashTree[5]/WebServiceSampler 
//  cause-exception     : com.thoughtworks.xstream.alias.CannotResolveClassException 
//  -------------------------------

    /**
     * Simplify getMessage() output from XStream ConversionException
     * @param ce - ConversionException to analyse
     * @return string with details of error
     */
    public static String CEtoString(ConversionException ce){
        String msg = 
            "XStream ConversionException at line: " + ce.get("line number")
            + "\n" + ce.get("message")
            + "\nPerhaps a missing jar? See log file.";
        return msg;
    }

    public static String getPropertiesVersion() {
        return propertiesVersion;
    }

    public static String getVERSION() {
        return VERSION;
    }
}

⌨️ 快捷键说明

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