summaryrefslogtreecommitdiff
path: root/java/demo/Ice/optional/Client.java
blob: e7d585f911d3ace8f263114aa007f45d632a9515 (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
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
238
239
240
241
242
243
244
245
// **********************************************************************
//
// Copyright (c) 2003-2013 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.
//
// **********************************************************************

import Demo.*;
import Ice.Optional;

public class Client extends Ice.Application
{
    class ShutdownHook extends Thread
    {
        public void
        run()
        {
            try
            {
                communicator().destroy();
            }
            catch(Ice.LocalException ex)
            {
                ex.printStackTrace();
            }
        }
    }

    public int
    run(String[] args)
    {
        if(args.length > 0)
        {
            System.err.println(appName() + ": too many arguments");
            return 1;
        }

        ContactDBPrx contactdb = ContactDBPrxHelper.checkedCast(communicator().propertyToProxy("ContactDB.Proxy"));
        if(contactdb == null)
        {
            System.err.println(appName() + ": invalid proxy");
            return 1;
        }

        //
        // Add a contact for "john". In this case since all parameters are provided it isn't
	// necessary to construct an Optional.
        //
        String johnNumber = "123-456-7890";
        contactdb.addContact("john", NumberType.HOME, johnNumber, 0);

        System.out.print("Checking john... ");

        //
        // Find the phone number for "john". Note the use of the Ice.Optional
        // for non-generic types.
        //
        Ice.Optional<String> number = contactdb.queryNumber("john");

        //
        // isSet() tests if an optional value is set.
        //
        if(!number.isSet())
        {
            System.out.print("number is incorrect ");
        }

        //
        // Call get() to retrieve the optional value.
        //
        if(!number.get().equals(johnNumber))
        {
            System.out.print("number is incorrect ");
        }

        // Optional can also be used in an out parameter. Note that
        // primitive types don't use Ice.Optional.
        Ice.IntOptional dialgroup = new Ice.IntOptional();
        contactdb.queryDialgroup("john", dialgroup);
        if(!dialgroup.isSet() || dialgroup.get() != 0)
        {
            System.out.print("dialgroup is incorrect ");
        }

        Contact info = contactdb.query("john");

        //
        // All of the info parameters should be set. On a class call
        // has<name> to find out whether the value is set.
        //
        if(!info.hasType() || !info.hasNumber() || !info.hasDialGroup())
        {
            System.out.print("info is incorrect ");
        }
        // Call get<name> to retrieve the value.
        if(info.getType() != NumberType.HOME || !info.getNumber().equals(johnNumber) || info.getDialGroup() != 0)
        {
            System.out.print("info is incorrect ");
        }
        System.out.println("ok");

        //
        // Add a contact for "steve". Here we have to construct Optional parameters
	// since we are not passing the NumberType parameter.
	//
	// The behavior of the server is to default construct the
	// Contact, and then assign  all set parameters.  Since the
	// default value of NumberType in the slice definition is
	// NumberType.HOME and in this case the NumberType is unset
	// it will take the default value.
        //
        // The java mapping permits null to be passed to unset optional values.
        //
        String steveNumber = "234-567-8901";
        contactdb.addContact("steve", null, Optional.O(steveNumber), Optional.O(1));

        System.out.print("Checking steve... ");
        number = contactdb.queryNumber("steve");
        if(!number.get().equals(steveNumber))
        {
            System.out.print("number is incorrect ");
        }

        info = contactdb.query("steve");
        //
        // Check the value for the NumberType.
        //
        if(!info.hasType() || info.getType() != NumberType.HOME)
        {
            System.out.print("info is incorrect ");
        }

        if(!info.getNumber().equals(steveNumber) || info.getDialGroup() != 1)
        {
            System.out.print("info is incorrect ");
        }

        contactdb.queryDialgroup("steve", dialgroup);
        if(!dialgroup.isSet() || dialgroup.get() != 1)
        {
            System.out.print("dialgroup is incorrect ");
        }

        System.out.println("ok");

        //
        // Add a contact from "frank". Here the dialGroup field isn't set.
        //
        String frankNumber = "345-678-9012";
        contactdb.addContact("frank", Optional.O(NumberType.CELL), Optional.O(frankNumber), null);

        System.out.print("Checking frank... ");

        number = contactdb.queryNumber("frank");
        if(!number.get().equals(frankNumber))
        {
            System.out.print("number is incorrect ");
        }

        info = contactdb.query("frank");
        //
        // The dial group field should be unset.
        //
        if(info.hasDialGroup())
        {
            System.out.print("info is incorrect ");
        }
        if(info.getType() != NumberType.CELL || !info.getNumber().equals(frankNumber))
        {
            System.out.print("info is incorrect ");
        }

        contactdb.queryDialgroup("frank", dialgroup);
        if(dialgroup.isSet())
        {
            System.out.print("dialgroup is incorrect ");
        }
        System.out.println("ok");

        //
        // Add a contact from "anne". The number field isn't set.
        //
        contactdb.addContact("anne", Optional.O(NumberType.OFFICE), null, Optional.O(2));

        System.out.print("Checking anne... ");
        number = contactdb.queryNumber("anne");
        if(number.isSet())
        {
            System.out.print("number is incorrect ");
        }

        info = contactdb.query("anne");
        //
        // The number field should be unset.
        //
        if(info.hasNumber())
        {
            System.out.print("info is incorrect ");
        }
        if(info.getType() != NumberType.OFFICE || info.getDialGroup() != 2)
        {
            System.out.print("info is incorrect ");
        }

        contactdb.queryDialgroup("anne", dialgroup);
        if(!dialgroup.isSet() || dialgroup.get() != 2)
        {
            System.out.print("dialgroup is incorrect ");
        }

        //
        // The optional fields can be used to determine what fields to
        // update on the contact.  Here we update only the number for anne,
        // the remainder of the fields are unchanged.
        //
        String anneNumber = "456-789-0123";
        contactdb.updateContact("anne", null, Optional.O(anneNumber), null);
        number = contactdb.queryNumber("anne");
        if(!number.get().equals(anneNumber))
        {
            System.out.print("number is incorrect ");
        }
        info = contactdb.query("anne");
        if(!info.getNumber().equals(anneNumber) || info.getType() != NumberType.OFFICE || info.getDialGroup() != 2)
        {
            System.out.print("info is incorrect ");
        }
        System.out.println("ok");

        contactdb.shutdown();

        return 0;
    }

    public static void
    main(String[] args)
    {
        Client app = new Client();
        int status = app.main("Client", args, "config.client");
        System.exit(status);
    }
}