001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.save;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileOutputStream;
026: import java.io.InputStream;
027:
028: import org.apache.jmeter.junit.JMeterTestCase;
029: import org.apache.jmeter.util.JMeterUtils;
030: import org.apache.jorphan.collections.HashTree;
031:
032: /**
033: * @author mstover
034: *
035: */
036: public class TestSaveService extends JMeterTestCase {
037: private static final String[] FILES = new String[] {
038: "AssertionTestPlan.jmx", "AuthManagerTestPlan.jmx",
039: "HeaderManagerTestPlan.jmx", "InterleaveTestPlan2.jmx",
040: "InterleaveTestPlan.jmx", "LoopTestPlan.jmx",
041: "Modification Manager.jmx", "OnceOnlyTestPlan.jmx",
042: "proxy.jmx", "ProxyServerTestPlan.jmx",
043: "SimpleTestPlan.jmx", "GuiTest.jmx", };
044:
045: private static boolean saveOut = JMeterUtils.getPropDefault(
046: "testsaveservice.saveout", false);
047:
048: public TestSaveService(String name) {
049: super (name);
050: }
051:
052: public void testPropfile() throws Exception {
053: assertTrue("Property Version mismatch", SaveService
054: .checkPropertyVersion());
055: assertTrue("Property File Version mismatch", SaveService
056: .checkFileVersion());
057: }
058:
059: public void testVersions() throws Exception {
060: assertTrue("Unexpected version found", SaveService
061: .checkVersions());
062: }
063:
064: public void testLoadAndSave() throws Exception {
065: byte[] original = new byte[1000000];
066:
067: boolean failed = false; // Did a test fail?
068:
069: for (int i = 0; i < FILES.length; i++) {
070: InputStream in = new FileInputStream(new File("testfiles/"
071: + FILES[i]));
072: int len = in.read(original);
073:
074: in.close();
075:
076: in = new ByteArrayInputStream(original, 0, len);
077: HashTree tree = SaveService.loadTree(in);
078:
079: in.close();
080:
081: ByteArrayOutputStream out = new ByteArrayOutputStream(
082: 1000000);
083:
084: SaveService.saveTree(tree, out);
085: out.close(); // Make sure all the data is flushed out
086:
087: // We only check the length of the result. Comparing the
088: // actual result (out.toByteArray==original) will usually
089: // fail, because the order of the properties within each
090: // test element may change. Comparing the lengths should be
091: // enough to detect most problem cases...
092: int outsz = out.size();
093: // Allow for input in CRLF and output in LF only
094: int lines = 0;
095: byte ba[] = out.toByteArray();
096: for (int j = 0; j < ba.length; j++) {
097: if (ba[j] == '\n') {
098: lines++;
099: }
100: }
101: if (len != outsz && len != outsz + lines) {
102: failed = true;
103: System.out.println();
104: System.out.println("Loading file testfiles/" + FILES[i]
105: + " and "
106: + "saving it back changes its size from " + len
107: + " to " + outsz + ".");
108: System.out.println("Diff " + (len - outsz) + " lines "
109: + lines);
110: if (saveOut) {
111: String outfile = "testfiles/" + FILES[i] + ".out";
112: System.out.println("Write " + outfile);
113: FileOutputStream outf = new FileOutputStream(
114: new File(outfile));
115: outf.write(out.toByteArray());
116: outf.close();
117: System.out.println("Wrote " + outfile);
118: }
119: }
120:
121: // Note this test will fail if a property is added or
122: // removed to any of the components used in the test
123: // files. The way to solve this is to appropriately change
124: // the test file.
125: }
126: if (failed) // TODO make these separate tests?
127: {
128: fail("One or more failures detected");
129: }
130: }
131: }
|