1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <glib.h>
21 #include <string.h>
22
23 #include <thrift/c_glib/thrift_application_exception.h>
24
25 static void
test_create_and_destroy(void)26 test_create_and_destroy (void)
27 {
28 GObject *object = NULL;
29
30 /* A ThriftApplicationException can be created... */
31 object = g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);
32
33 g_assert (object != NULL);
34 g_assert (THRIFT_IS_APPLICATION_EXCEPTION (object));
35
36 /* ...and destroyed */
37 g_object_unref (object);
38 }
39
40 static void
test_initialize(void)41 test_initialize (void)
42 {
43 ThriftApplicationException *xception = NULL;
44 gint32 type = THRIFT_APPLICATION_EXCEPTION_ERROR_INTERNAL_ERROR;
45 gchar *message = "Exception message";
46 gint32 retrieved_type = 0;
47 gchar *retrieved_message = NULL;
48
49 /* A ThriftApplicationException has "type" and "message" properties that can
50 be initialized at object creation */
51 xception =
52 g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION,
53 "type", type,
54 "message", message,
55 NULL);
56
57 g_assert (xception != NULL);
58
59 /* A ThriftApplicationException's properties can be retrieved */
60 g_object_get (xception,
61 "type", &retrieved_type,
62 "message", &retrieved_message,
63 NULL);
64
65 g_assert (retrieved_type == type);
66 g_assert (retrieved_message != NULL);
67 g_assert_cmpstr (retrieved_message, ==, message);
68
69 g_free (retrieved_message);
70 g_object_unref (xception);
71 }
72
73 static void
test_properties_test(void)74 test_properties_test (void)
75 {
76 ThriftApplicationException *xception = NULL;
77 gint32 retrieved_type;
78
79 xception = g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);
80
81 #define TEST_TYPE_VALUE(_type) \
82 retrieved_type = -1; \
83 g_object_set (xception, "type", _type, NULL); \
84 g_object_get (xception, "type", &retrieved_type, NULL); \
85 g_assert_cmpint (retrieved_type, ==, _type);
86
87 /* The "type" property can be set to any valid Thrift exception type */
88 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN);
89 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN_METHOD);
90 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_MESSAGE_TYPE);
91 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_WRONG_METHOD_NAME);
92 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_BAD_SEQUENCE_ID);
93 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_MISSING_RESULT);
94 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_INTERNAL_ERROR);
95 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_PROTOCOL_ERROR);
96 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_TRANSFORM);
97 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_PROTOCOL);
98 TEST_TYPE_VALUE (THRIFT_APPLICATION_EXCEPTION_ERROR_UNSUPPORTED_CLIENT_TYPE);
99
100 /* "g_test_expect_message" is required for the property range tests below but is
101 not present in GLib before version 2.34 */
102 #if (GLIB_CHECK_VERSION (2, 34, 0))
103 g_object_set (xception,
104 "type", THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN,
105 NULL);
106
107 /* The "type" property cannot be set to a value too low (less than zero) */
108 g_test_expect_message ("GLib-GObject",
109 G_LOG_LEVEL_WARNING,
110 "value*out of range*type*");
111 g_object_set (xception, "type", -1, NULL);
112 g_test_assert_expected_messages ();
113
114 g_object_get (xception, "type", &retrieved_type, NULL);
115 g_assert_cmpint (retrieved_type, !=, -1);
116 g_assert_cmpint (retrieved_type,
117 ==,
118 THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN);
119
120 /* The "type" property cannot be set to a value too high (greater than the
121 highest defined exception-type value) */
122 g_test_expect_message ("GLib-GObject",
123 G_LOG_LEVEL_WARNING,
124 "value*out of range*type*");
125 g_object_set (xception, "type", THRIFT_APPLICATION_EXCEPTION_ERROR_N, NULL);
126 g_test_assert_expected_messages ();
127
128 g_object_get (xception, "type", &retrieved_type, NULL);
129 g_assert_cmpint (retrieved_type, !=, THRIFT_APPLICATION_EXCEPTION_ERROR_N);
130 g_assert_cmpint (retrieved_type,
131 ==,
132 THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN);
133 #endif
134
135 g_object_unref (xception);
136 }
137
138 static void
test_properties_message(void)139 test_properties_message (void)
140 {
141 ThriftApplicationException *xception = NULL;
142 gchar *message = "Exception message";
143 gchar *retrieved_message;
144
145 xception = g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);
146
147 /* The "message" property can be set to NULL */
148 g_object_set (xception, "message", NULL, NULL);
149 g_object_get (xception, "message", &retrieved_message, NULL);
150 g_assert (retrieved_message == NULL);
151
152 /* The "message" property can be set to a valid string */
153 g_object_set (xception, "message", message, NULL);
154 g_object_get (xception, "message", &retrieved_message, NULL);
155 g_assert_cmpint (strcmp (retrieved_message, message), ==, 0);
156
157 g_free (retrieved_message);
158 g_object_unref (xception);
159 }
160
161 int
main(int argc,char ** argv)162 main (int argc, char **argv)
163 {
164 #if (!GLIB_CHECK_VERSION (2, 36, 0))
165 g_type_init ();
166 #endif
167
168 g_test_init (&argc, &argv, NULL);
169
170 g_test_add_func ("/testapplicationexception/CreateAndDestroy",
171 test_create_and_destroy);
172 g_test_add_func ("/testapplicationexception/Initialize",
173 test_initialize);
174 g_test_add_func ("/testapplicationexception/Properties/test",
175 test_properties_test);
176 g_test_add_func ("/testapplicationexception/Properties/message",
177 test_properties_message);
178
179 return g_test_run ();
180 }
181