KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > bpe > BPEComponentTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.servicemix.bpe;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.net.HttpURLConnection JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import javax.jbi.messaging.ExchangeStatus;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.xml.namespace.QName JavaDoc;
30
31 import junit.framework.TestCase;
32 import loanbroker.Bank;
33 import loanbroker.CreditAgency;
34
35 import org.apache.servicemix.client.DefaultServiceMixClient;
36 import org.apache.servicemix.client.ServiceMixClient;
37 import org.apache.servicemix.http.HttpEndpoint;
38 import org.apache.servicemix.http.HttpSpringComponent;
39 import org.apache.servicemix.jbi.container.ActivationSpec;
40 import org.apache.servicemix.jbi.container.JBIContainer;
41 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
42 import org.apache.servicemix.jbi.jaxp.StringSource;
43 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
44
45 public class BPEComponentTest extends TestCase {
46
47     private JBIContainer jbi;
48     private BPEComponent bpe;
49     private ServiceMixClient client;
50     
51     protected void setUp() throws Exception JavaDoc {
52         jbi = new JBIContainer();
53         jbi.setEmbedded(true);
54         jbi.init();
55         client = new DefaultServiceMixClient(jbi);
56         bpe = new BPEComponent();
57         jbi.activateComponent(bpe, "bpe");
58     }
59     
60     protected void tearDown() throws Exception JavaDoc {
61         if (jbi != null) {
62             jbi.shutDown();
63         }
64     }
65     
66     protected void registerCreditAgency() throws Exception JavaDoc {
67         ActivationSpec creditAgency = new ActivationSpec();
68         creditAgency.setInterfaceName(new QName JavaDoc("urn:logicblaze:soa:creditagency", "CreditAgency"));
69         creditAgency.setComponent(new CreditAgency());
70         jbi.activateComponent(creditAgency);
71     }
72     
73     protected void registerBanks() throws Exception JavaDoc {
74         for (int i = 1; i <= 5; i++) {
75             ActivationSpec bank = new ActivationSpec();
76             bank.setInterfaceName(new QName JavaDoc("urn:logicblaze:soa:bank", "Bank"));
77             bank.setComponent(new Bank(i));
78             jbi.activateComponent(bank);
79         }
80     }
81     
82     
83     protected void registerHttp() throws Exception JavaDoc {
84         HttpSpringComponent http = new HttpSpringComponent();
85         HttpEndpoint ep = new HttpEndpoint();
86         ep.setSoap(true);
87         ep.setDefaultMep(MessageExchangeSupport.IN_OUT);
88         ep.setRoleAsString("consumer");
89         ep.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
90         ep.setEndpoint("loanbroker");
91         ep.setLocationURI("http://localhost:8192");
92         http.setEndpoints(new HttpEndpoint[] { ep });
93         jbi.activateComponent(http, "http");
94     }
95     
96     public static void copyInputStream(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
97         byte[] buffer = new byte[1024];
98         int len;
99         while ((len = in.read(buffer)) >= 0) {
100             out.write(buffer, 0, len);
101         }
102     }
103     
104     public void testWithHttp() throws Exception JavaDoc {
105         registerCreditAgency();
106         registerBanks();
107         registerHttp();
108         jbi.start();
109         
110         URL JavaDoc url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel");
111         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
112         path = path.getParentFile();
113         bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath());
114         bpe.getServiceUnitManager().start("loanbroker");
115         
116         HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc) new URL JavaDoc("http://localhost:8192").openConnection();
117         con.setDoOutput(true);
118         con.setDoInput(true);
119         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream("request.xml");
120         OutputStream JavaDoc os = con.getOutputStream();
121         copyInputStream(is, os);
122         copyInputStream(con.getInputStream(), System.out);
123     }
124     
125     public void testBPEOk() throws Exception JavaDoc {
126         registerCreditAgency();
127         registerBanks();
128         jbi.start();
129         
130         URL JavaDoc url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel");
131         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
132         path = path.getParentFile();
133         bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath());
134         bpe.getServiceUnitManager().start("loanbroker");
135         
136         //
137
// Message for bank1 and bank2
138
//
139
MessageExchange me = client.createInOutExchange();
140         me.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
141         me.setOperation(new QName JavaDoc("getLoanQuote"));
142         me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>100000.0</amount><duration>12</duration></getLoanQuoteRequest>"));
143         long t0 = System.currentTimeMillis();
144         client.sendSync(me);
145         long t1 = System.currentTimeMillis();
146         if (me.getError() != null) {
147             throw me.getError();
148         }
149         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
150         String JavaDoc out = new SourceTransformer().contentToString(me.getMessage("out"));
151         System.err.println(out);
152         System.err.println("Time: " + (t1 - t0));
153         client.done(me);
154         
155         //
156
// Message for bank3 and bank4
157
//
158
me = client.createInOutExchange();
159         me.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
160         me.setOperation(new QName JavaDoc("getLoanQuote"));
161         me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>50000.0</amount><duration>12</duration></getLoanQuoteRequest>"));
162         t0 = System.currentTimeMillis();
163         client.sendSync(me);
164         t1 = System.currentTimeMillis();
165         if (me.getError() != null) {
166             throw me.getError();
167         }
168         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
169         out = new SourceTransformer().contentToString(me.getMessage("out"));
170         System.err.println(out);
171         System.err.println("Time: " + (t1 - t0));
172         client.done(me);
173         
174         //
175
// Message for bank5
176
//
177
me = client.createInOutExchange();
178         me.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
179         me.setOperation(new QName JavaDoc("getLoanQuote"));
180         me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>1200.0</amount><duration>12</duration></getLoanQuoteRequest>"));
181         t0 = System.currentTimeMillis();
182         client.sendSync(me);
183         t1 = System.currentTimeMillis();
184         if (me.getError() != null) {
185             throw me.getError();
186         }
187         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
188         out = new SourceTransformer().contentToString(me.getMessage("out"));
189         System.err.println(out);
190         System.err.println("Time: " + (t1 - t0));
191         client.done(me);
192     }
193     
194     public void testBPEWithFault() throws Exception JavaDoc {
195         registerCreditAgency();
196         jbi.start();
197         
198         URL JavaDoc url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel");
199         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
200         path = path.getParentFile();
201         bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath());
202         bpe.getServiceUnitManager().start("loanbroker");
203         
204         MessageExchange me = client.createInOutExchange();
205         me.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
206         me.setOperation(new QName JavaDoc("getLoanQuote"));
207         me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>234341</ssn></getLoanQuoteRequest>"));
208         client.sendSync(me);
209         if (me.getError() != null) {
210             throw me.getError();
211         }
212         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
213         assertNotNull(me.getFault());
214         client.done(me);
215     }
216     
217     public void testBPEWithException() throws Exception JavaDoc {
218         registerCreditAgency();
219         jbi.start();
220         
221         URL JavaDoc url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel");
222         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
223         path = path.getParentFile();
224         bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath());
225         bpe.getServiceUnitManager().start("loanbroker");
226         
227         MessageExchange me = client.createInOutExchange();
228         me.setService(new QName JavaDoc("urn:logicblaze:soa:loanbroker", "LoanBrokerService"));
229         me.setOperation(new QName JavaDoc("getLoanQuote"));
230         me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn></ssn></getLoanQuoteRequest>"));
231         client.sendSync(me);
232         assertEquals(ExchangeStatus.ERROR, me.getStatus());
233         assertNotNull(me.getError());
234     }
235     
236 }
237
Popular Tags