summaryrefslogtreecommitdiff
path: root/csharp/test/TestCommon/TestHelper.cs
blob: d96d786bcccc30b4550783cfc7170c100537d39c (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

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

namespace Test
{
    public abstract class TestHelper
    {
        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 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, bool defaultCommunicator = true)
        {
            Ice.InitializationData initData = new Ice.InitializationData();
            initData.properties = createTestProperties(ref args);
            return initialize(initData, defaultCommunicator);
        }

        public Ice.Communicator initialize(Ice.Properties properties, bool defaultCommunicator = true)
        {
            Ice.InitializationData initData = new Ice.InitializationData();
            initData.properties = properties;
            return initialize(initData, defaultCommunicator);
        }
        public Ice.Communicator initialize(Ice.InitializationData initData, bool defaultCommunicator = true)
        {
            Ice.Communicator communicator = Ice.Util.initialize(initData);
            if(defaultCommunicator)
            {
                _communicator = communicator;
            }
            return  communicator;
        }
        public Ice.Communicator communicator()
        {
            return _communicator;
        }

        public void shutdown()
        {
            if(_communicator != null)
            {
                _communicator.shutdown();
            }
        }

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

        private Ice.Communicator _communicator;
    }

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

        public static void Write(string msg)
        {
            Console.Out.Write(msg);
        }

        public static void WriteLine(string msg)
        {
            Console.Out.WriteLine(msg);
        }

        public static void Flush()
        {
            Console.Out.Flush();
        }
    }

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