/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Xml; using Apache.NMS.Test; using Apache.NMS.Util; using NUnit.Framework; using Apache.NMS.XMS; namespace Apache.NMS.XMS.Test { /// /// Useful class for test cases support. /// public class XMSTestSupport : NMSTestSupport { /// /// Gets the environment variable name for the configuration file path. /// /// Environment variable name public override string GetConfigEnvVarName() { return "XMSTESTCONFIGPATH"; } /// /// Gets the default name for the configuration filename. /// /// Default name of the configuration filename public override string GetDefaultConfigFileName() { return "xmsprovider-test.config"; } /// /// Gets a new client id. /// /// Client id public override string GetTestClientId() { return base.GetTestClientId(); //return null; } /// /// Create a new connection to the broker. /// /// Client ID of the new connection. /// New connection public override IConnection CreateConnection(string newClientId) { IConnection newConnection; // IBM.XMS throws an exception if attempting to set the ClientId // on the Connection after it was created. // In a multithreaded environment, it would probably be best to // create as many factories as client ids, rather than change the // client id on the factory before creating a connection. Plus it // wouldn't be a good idea to protect the code section through a // semaphore, since connections creation takes a long time to be // performed. if(newClientId != null) { ((Apache.NMS.XMS.ConnectionFactory)Factory).ClientId = newClientId; } if(this.userName == null) { newConnection = Factory.CreateConnection(); } else { newConnection = Factory.CreateConnection(userName, passWord); } Assert.IsNotNull(newConnection, "Connection not created"); return newConnection; } ISession cleanupSession = null; /// /// Gets a clear destination. This will try to delete an existing /// destination and re-create it. /// /// Session /// Destination URI /// Clear destination public override IDestination GetClearDestination(ISession session, string destinationURI) { IDestination destination = SessionUtil.GetDestination(session, destinationURI); // Destination exists. Can't use the given session to clean up: // once used synchronously, IBM XMS doesn't allow the session to be // used asynchronously. Therefore, we create a specific synchronous // session to perform cleanups. if(cleanupSession == null) { IConnection cleanupConnection = CreateConnection("Cleanup"); cleanupSession = cleanupConnection.CreateSession(); } IDestination cleanupDestination = SessionUtil.GetDestination(cleanupSession, destinationURI); using(IMessageConsumer consumer = cleanupSession.CreateConsumer(destination)) { while(consumer.Receive(TimeSpan.FromMilliseconds(750)) != null) { } } return destination; } } }