File: TestHelper.cs

package info (click to toggle)
zeroc-ice 3.7.10-3.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 75,696 kB
  • sloc: cpp: 356,894; java: 226,081; cs: 98,312; javascript: 35,027; python: 28,716; objc: 27,050; php: 7,526; ruby: 7,190; yacc: 2,949; ansic: 2,469; xml: 1,589; lex: 1,241; makefile: 472; sh: 52
file content (237 lines) | stat: -rw-r--r-- 6,792 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

using System;
using System.Diagnostics;
using System.Text;
using System.IO;

namespace Test
{
    public interface ControllerHelper
    {
        void serverReady();
        void communicatorInitialized(Ice.Communicator communicator);
    }

    public interface PlatformAdapter
    {
        bool isEmulator();

        string processControllerRegistryHost();

        string processControllerIdentity();
    }

    public abstract class TestHelper
    {
        // A custom trace listener that always aborts the application upon failure.
        internal class TestTraceListener : DefaultTraceListener
        {
            public override void Fail(string message)
            {
                TestHelper.fail(message, null);
            }

            public override void Fail(string message, string detailMessage)
            {
                TestHelper.fail(message, detailMessage);
            }
        }

        static TestHelper()
        {
            // Replace the default trace listener that is responsible of displaying the retry/abort dialog
            // with our custom trace listener that always aborts upon failure.
            // see: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.defaulttracelistener?view=net-5.0#remarks
            Trace.Listeners.Clear();
            Trace.Listeners.Add(new TestTraceListener());
        }

        public abstract void run(string[] args);

        public string getTestEndpoint(int num = 0, string protocol = "")
        {
            return getTestEndpoint(_communicator.getProperties(), num, protocol);
        }

        static public string getTestEndpoint(Ice.Properties properties, int num = 0, string protocol = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(protocol == "" ? properties.getPropertyWithDefault("Ice.Default.Protocol", "default") :
                                       protocol);
            sb.Append(" -p ");
            sb.Append(properties.getPropertyAsIntWithDefault("Test.BasePort", 12010) + num);
            return sb.ToString();
        }

        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;
        }

        public TextWriter getWriter()
        {
            if(_writer == null)
            {
                return Console.Out;
            }
            else
            {
                return _writer;
            }
        }

        public void setWriter(TextWriter writer)
        {
            _writer = writer;
        }

        public Ice.Properties createTestProperties(ref string[] args)
        {
            Ice.Properties properties = Ice.Util.createProperties(ref args);
            args = properties.parseCommandLineOptions("Test", args);
            return properties;
        }

        public Ice.Communicator initialize(ref string[] args)
        {
            Ice.InitializationData initData = new Ice.InitializationData();
            initData.properties = createTestProperties(ref args);
            return initialize(initData);
        }

        public Ice.Communicator initialize(Ice.Properties properties)
        {
            Ice.InitializationData initData = new Ice.InitializationData();
            initData.properties = properties;
            return initialize(initData);
        }

        public Ice.Communicator initialize(Ice.InitializationData initData)
        {
            Ice.Communicator communicator = Ice.Util.initialize(initData);
            if(_communicator == null)
            {
                _communicator = communicator;
            }
            if(_controllerHelper != null)
            {
                _controllerHelper.communicatorInitialized(communicator);
            }
            return  communicator;
        }

        public Ice.Communicator communicator()
        {
            return _communicator;
        }

        public static void test(bool b)
        {
            if (!b)
            {
                fail(null, null);
            }
        }

        internal static void fail(string message, string detailMessage)
        {
            var sb = new StringBuilder();
            sb.Append("failed:\n");
            if (message != null && message.Length > 0)
            {
                sb.Append("message: ").Append(message).Append('\n');
            }
            if (detailMessage != null && detailMessage.Length > 0)
            {
                sb.Append("details: ").Append(detailMessage).Append('\n');
            }
            try
            {
                sb.Append(new StackTrace(fNeedFileInfo: true).ToString()).Append('\n');
            }
            catch
            {
            }

            Console.WriteLine(sb.ToString());
            Environment.Exit(1);
        }

        public void setControllerHelper(ControllerHelper controllerHelper)
        {
            _controllerHelper = controllerHelper;
        }

        public void serverReady()
        {
            if (_controllerHelper != null)
            {
                _controllerHelper.serverReady();
            }
        }

        private Ice.Communicator _communicator;
        private ControllerHelper _controllerHelper;
        private TextWriter _writer;
    }

    public abstract class AllTests
    {
        protected static void test(bool b)
        {
            if (!b)
            {
                Debug.Assert(false);
                throw new Exception();
            }
        }
    }

    public static class TestDriver
    {
        public static int runTest<T>(string[] args)
            where T : TestHelper, new()
        {
            int status = 0;
            try
            {
                T h = new T();
                h.run(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                status = 1;
            }
            return status;
        }
    }
}