1 /*
2 * Copyright (c) 2004, RV Test Team
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of the "RV Test Team" nor the names of its contributors may
16 * be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32 package com.reuters.msgtest.fixture;
33
34 import com.reuters.msgtest.RvTestException;
35 import com.reuters.msgtest.config.RvTestConfiguration;
36 import com.tibco.tibrv.TibrvMsg;
37 import java.util.Collection;
38 import java.util.Iterator;
39 import java.util.LinkedList;
40 import java.util.List;
41
42
43 /***
44 * @author Michael Ward
45 */
46 public class Responses {
47 private List responses;
48 private RvTestConfiguration rvTestConfiguration;
49
50 public Responses(RvTestConfiguration rvTestConfiguration) {
51 this.rvTestConfiguration = rvTestConfiguration;
52 responses = new LinkedList();
53 }
54
55 /***
56 * Add a <code>Respnse</code> to the collection. Will create from a URI if
57 * applicable
58 *
59 * @param response
60 * a <code>Response</code> value
61 */
62 public void addResponse(Response response) {
63 // if the response has a uri build the msg from it... this may be able
64 // to be done cleaner (aspect?)
65 if (response.getUri() != null) {
66 try {
67 List list = (List) rvTestConfiguration.create(response.getUri());
68
69 response.setTibrvMsg((TibrvMsg) list.get(0));
70 response.getTibrvMsg().setSendSubject(response.getSubject());
71
72 //TODO look into setting the subject for the message
73 } catch (Exception e) {
74 throw new RuntimeException("Unable to create Messages", e);
75 }
76 }
77
78 responses.add(response);
79 }
80
81 /***
82 * Return a <code>Response</code> from the collection.
83 *
84 * @param i
85 * an <code>int</code> value
86 * @return a <code>Response</code> value
87 */
88 public Response getResponse(int i) {
89 return (Response) responses.get(i);
90 }
91
92 /***
93 * Return a <code>Response</code> from the collection with a given name.
94 * It return the first match if successful and null if no match was found.
95 *
96 * @param name
97 * The name of a Response to retrieve.
98 * @throws RvTestException
99 * If the response is not found
100 */
101 public Response getResponse(String name) throws RvTestException {
102 Iterator iterator = responses.iterator();
103
104 while (iterator.hasNext()) {
105 Response response = (Response) iterator.next();
106
107 if (name.equalsIgnoreCase(response.getName())) {
108 return response;
109 }
110 }
111
112 throw new RvTestException("Unknown response: " + name);
113 }
114
115 public Iterator iterator() {
116 return responses.iterator();
117 }
118
119 public int size() {
120 return responses.size();
121 }
122
123 public void addAll(Collection c) {
124 responses.addAll(c);
125 }
126 }