001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.testutil.common;
019:
020: import java.net.HttpURLConnection;
021: import java.net.URL;
022: import java.net.URLConnection;
023: import java.util.concurrent.FutureTask;
024: import java.util.concurrent.LinkedBlockingQueue;
025: import java.util.concurrent.ThreadPoolExecutor;
026: import java.util.concurrent.TimeUnit;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.BusFactory;
030: import org.junit.After;
031: import org.junit.AfterClass;
032:
033: public abstract class AbstractBusClientServerTestBase extends
034: AbstractClientServerTestBase {
035:
036: static {
037: System.setProperty("javax.xml.ws.spi.Provider",
038: "org.apache.cxf.jaxws.spi.ProviderImpl");
039: }
040:
041: protected static String defaultConfigFileName;
042: protected static Bus staticBus;
043:
044: protected String configFileName = defaultConfigFileName;
045: protected Bus bus;
046:
047: public void createBus(String config) throws Exception {
048: configFileName = config;
049: createBus();
050: }
051:
052: public void createBus() throws Exception {
053: if (configFileName != null) {
054: System.setProperty("cxf.config.file", configFileName);
055: }
056: BusFactory bf = BusFactory.newInstance();
057: bus = bf.createBus();
058: BusFactory.setDefaultBus(bus);
059: }
060:
061: public static void createStaticBus(String config) throws Exception {
062: defaultConfigFileName = config;
063: createStaticBus();
064: }
065:
066: public static void createStaticBus() throws Exception {
067: if (defaultConfigFileName != null) {
068: System
069: .setProperty("cxf.config.file",
070: defaultConfigFileName);
071: }
072: BusFactory bf = BusFactory.newInstance();
073: staticBus = bf.createBus();
074: BusFactory.setDefaultBus(staticBus);
075: }
076:
077: @After
078: public void deleteBus() throws Exception {
079: if (null != bus) {
080: bus.shutdown(true);
081: bus = null;
082: }
083: if (configFileName != null) {
084: System.clearProperty("cxf.config.file");
085: }
086: }
087:
088: @AfterClass
089: public static void deleteStaticBus() throws Exception {
090: if (null != staticBus) {
091: staticBus.shutdown(true);
092: staticBus = null;
093: }
094: if (defaultConfigFileName != null) {
095: System.clearProperty("cxf.config.file");
096: }
097: }
098:
099: protected Bus getBus() {
100: if (bus == null) {
101: return staticBus;
102: }
103: return bus;
104: }
105:
106: protected void setBus(Bus b) {
107: bus = b;
108: }
109:
110: protected HttpURLConnection getHttpConnection(String target)
111: throws Exception {
112: URL url = new URL(target);
113:
114: URLConnection connection = url.openConnection();
115:
116: assertTrue(connection instanceof HttpURLConnection);
117: return (HttpURLConnection) connection;
118: }
119:
120: protected boolean runClient(Runnable clientImpl, long timeOut,
121: TimeUnit timeUnit) throws InterruptedException {
122: FutureTask<?> client = new FutureTask<Object>(clientImpl, null);
123: ThreadPoolExecutor tpe = new ThreadPoolExecutor(1, 1, 10000L,
124: TimeUnit.MILLISECONDS,
125: new LinkedBlockingQueue<Runnable>());
126: tpe.execute(client);
127: tpe.shutdown();
128: tpe.awaitTermination(timeOut, timeUnit);
129: if (!client.isDone()) {
130: return false;
131: }
132: return true;
133: }
134: }
|