01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.test.stress;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.StringWriter;
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: public class TestRepository {
26:
27: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
28: .getLogger(TestRepository.class);
29:
30: private List tests = new ArrayList();
31: private List initiators = new ArrayList();
32: private List xmlBeans = new ArrayList();
33:
34: private static TestRepository INSTANCE = new TestRepository();
35:
36: private TestRepository() {
37: }
38:
39: public static TestRepository getInstance() {
40: return INSTANCE;
41: }
42:
43: public Test getRandomTest() throws Exception {
44: LOG.debug("getting random test");
45: BasicTest basicTest = (BasicTest) BasicTest.class.newInstance();
46: basicTest.setInitiator(getRandomInitiator());
47: XmlBean xmlBean = getRandomXmlBean();
48: basicTest.setApplicationContent(loadXML(xmlBean
49: .getXmlFileName()));
50: basicTest.setDocumentTypeName(xmlBean.getDocumentTypeName());
51: basicTest.setAttributeDefinitions(new ArrayList());
52: LOG.debug("returning test for document type "
53: + xmlBean.getDocumentTypeName() + " xml file of "
54: + xmlBean.getXmlFileName());
55: return basicTest;
56: // return new BasicTest("tkirkend", "StressTestDocument", "route1.xml", new ArrayList());
57: // int index = (int)Math.round(Math.random() * (tests.size() - 1));
58: // return (Test)tests.get(index);
59: }
60:
61: public void addTest(Test test) {
62: tests.add(test);
63: }
64:
65: public void addInitiators(String initiator) {
66: initiators.add(initiator);
67: }
68:
69: public void addXml(XmlBean xmlBean) {
70: xmlBeans.add(xmlBean);
71: }
72:
73: private String getRandomInitiator() {
74: return (String) StressTestUtils.getRandomListObject(initiators);
75: }
76:
77: private XmlBean getRandomXmlBean() {
78: return (XmlBean) StressTestUtils.getRandomListObject(xmlBeans);
79: }
80:
81: private String loadXML(String resourceName) {
82: try {
83: InputStream inputStream = getClass().getClassLoader()
84: .getResourceAsStream(resourceName);
85: StringWriter writer = new StringWriter();
86: int data = -1;
87: while ((data = inputStream.read()) != -1) {
88: writer.write(data);
89: }
90: return writer.toString();
91: } catch (IOException e) {
92: throw new RuntimeException(
93: "Fatal error initializing Stress Tests", e);
94: }
95: }
96:
97: }
|