diff options
Diffstat (limited to 'csharp/test/TestCommon/TestApp.cs')
-rw-r--r-- | csharp/test/TestCommon/TestApp.cs | 295 |
1 files changed, 184 insertions, 111 deletions
diff --git a/csharp/test/TestCommon/TestApp.cs b/csharp/test/TestCommon/TestApp.cs index 7acc40248a8..e27bb618e8b 100644 --- a/csharp/test/TestCommon/TestApp.cs +++ b/csharp/test/TestCommon/TestApp.cs @@ -8,145 +8,218 @@ // ********************************************************************** using System; - -#if SILVERLIGHT -using System.Collections.Generic; -using System.Net; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Documents; -using System.Windows.Ink; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Animation; -using System.Windows.Shapes; -#endif +using System.Diagnostics; namespace TestCommon { - public abstract class TestApp + public abstract class Application { - protected static void test(bool b) + public + Application() { - if(!b) + } + + // + // This runmain() must be called by the global main(). runmain() + // initializes the Communicator, calls run(), and destroys + // the Communicator upon return from run(). It thereby handles + // all exceptions properly, i.e., error messages are printed + // if exceptions propagate to main(), and the Communicator is + // always destroyed, regardless of exceptions. + // + public int runmain(string[] args) + { + Ice.InitializationData initData = getInitData(ref args); + return runmain(args, initData); + } + + public int runmain(string[] args, Ice.InitializationData initializationData) + { + _testName = AppDomain.CurrentDomain.FriendlyName; + + if(_communicator != null) { - System.Diagnostics.Debug.Assert(false); - throw new System.Exception(); + Console.Out.WriteLine(_testName + ": only one instance of the Application class can be used"); + return 1; } + + // + // We parse the properties here to extract Ice.ProgramName. + // + if(initializationData == null) + { + initializationData = getInitData(ref args); + } + + Ice.InitializationData initData; + if(initializationData != null) + { + initData = (Ice.InitializationData)initializationData.Clone(); + } + else + { + initData = new Ice.InitializationData(); + } + initData.properties = Ice.Util.createProperties(ref args, initData.properties); + + // + // If the process logger is the default logger, we replace it with a + // a logger that uses the program name as the prefix. + // + if(Ice.Util.getProcessLogger() is Ice.LoggerI) + { + Ice.Util.setProcessLogger(new Ice.ConsoleLoggerI(initData.properties.getProperty("Ice.ProgramName"))); + } + + int status = 0; + try + { + _communicator = Ice.Util.initialize(ref args, initData); + status = run(args); + } + catch(Exception ex) + { + Console.Out.WriteLine(_testName + ": " + ex); + status = 1; + } + + if(_communicator != null) + { + _communicator.destroy(); + _communicator = null; + } + return status; } -#if !SILVERLIGHT - static -#endif - public void Write(string msg) + public void stop() { -#if SILVERLIGHT - Console.Out.Write(msg); -#else - Console.Out.Write(msg); -#endif + if(_communicator != null) + { + _communicator.shutdown(); + } + } + + public abstract int run(string[] args); + + // + // Hook to override the initialization data. This hook is + // necessary because some properties must be set prior to + // communicator initialization. + // + protected virtual Ice.InitializationData getInitData(ref string[] args) + { + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(ref args); + args = initData.properties.parseCommandLineOptions("Test", args); + return initData; } -#if !SILVERLIGHT - static -#endif - public void WriteLine(string msg) + // + // Return the application name, i.e., argv[0]. + // + public string appName() { -#if SILVERLIGHT - Console.Out.WriteLine(msg); -#else - Console.Out.WriteLine(msg); -#endif + return _testName; } -#if !SILVERLIGHT - static -#endif - public void Flush() - { - Console.Out.Flush(); + public Ice.Communicator communicator() + { + return _communicator; } -#if SILVERLIGHT - - public abstract void run(Ice.Communicator communicator); + public string getTestEndpoint(int num) + { + return getTestEndpoint(num, ""); + } - public virtual Ice.InitializationData - initData() + public string getTestEndpoint(int num, string prot) { - return new Ice.InitializationData(); + return getTestEndpoint(_communicator.getProperties(), num, prot); } - public void main() + static public string getTestEndpoint(Ice.Properties properties, int num) { - int args = Application.Current.Host.Source.OriginalString.IndexOf("?"); - Dictionary<string, string> properties = new Dictionary<string, string>(); - if(args > 0 && args + 1 < Application.Current.Host.Source.OriginalString.Length) + return getTestEndpoint(properties, num, ""); + } + + static public string getTestEndpoint(Ice.Properties properties, int num, string prot) + { + string protocol = prot; + if(protocol == "") + { + protocol = properties.getPropertyWithDefault("Ice.Default.Protocol", "default"); + } + int basePort = properties.getPropertyAsIntWithDefault("Test.BasePort", 12010); + return protocol + " -p " + (basePort + num); + } + + public string getTestHost() + { + return getTestHost(_communicator.getProperties()); + } + + static public string getTestHost(Ice.Properties properties) + { + return properties.getPropertyWithDefault("Ice.Default.Host", "127.0.0.1"); + } + + public string getTestProtocol() + { + return getTestProtocol(_communicator.getProperties()); + } + + static public string getTestProtocol(Ice.Properties properties) + { + return properties.getPropertyWithDefault("Ice.Default.Protocol", "tcp"); + } + + public int getTestPort(int num) + { + return getTestPort(_communicator.getProperties(), num); + } + + static public int getTestPort(Ice.Properties properties, int num) + { + return properties.getPropertyAsIntWithDefault("Test.BasePort", 12010) + num; + } + + protected static void test(bool b) + { + if(!b) { - string[] props = Application.Current.Host.Source.OriginalString.Substring(args + 1).Split(';'); - foreach (string prop in props) - { - int pos = prop.IndexOf('='); - if(pos > 0) - { - properties[prop.Substring(0, pos)] = prop.Substring(pos + 1); - } - } + Debug.Assert(false); + throw new Exception(); } - - System.Threading.Thread t = new System.Threading.Thread(() => - { - Ice.Communicator communicator = null; - try - { - Ice.InitializationData initializationData = initData(); - if(initializationData.properties == null) - { - initializationData.properties = Ice.Util.createProperties(); - } - - foreach(KeyValuePair<String,String> entry in properties) - { - if(initializationData.properties.getProperty(entry.Key).Equals("")) - { - initializationData.properties.setProperty(entry.Key, entry.Value); - } - } - - communicator = Ice.Util.initialize(initializationData); - run(communicator); - completed(); - } - catch(System.Exception ex) - { - failed(ex); - } - finally - { - if(communicator != null) - { - communicator.destroy(); - } - } - }); - t.Start(); - } - - public void completed() - { - Deployment.Current.Dispatcher.BeginInvoke(delegate() + } + + private string _testName; + private Ice.Communicator _communicator; + } + + public abstract class AllTests + { + protected static void test(bool b) + { + if(!b) { - Application.Current.MainWindow.Close(); - }); + Debug.Assert(false); + throw new Exception(); + } + } + + public static void Write(string msg) + { + Console.Out.Write(msg); } - public void failed(System.Exception ex) + public static void WriteLine(string msg) { - System.Environment.ExitCode = 1; - WriteLine(Environment.NewLine + "Test Failed:"); - WriteLine("Exception: " + ex.ToString()); - completed(); + Console.Out.WriteLine(msg); + } + + public static void Flush() + { + Console.Out.Flush(); } -#endif } } |