1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vera Y. Petrashkova
20*/
21
22package org.apache.harmony.auth.tests.javax.security.sasl.serialization;
23
24import java.io.Serializable;
25
26import javax.security.sasl.RealmChoiceCallback;
27
28import org.apache.harmony.testframework.serialization.SerializationTest;
29
30/**
31 * Test for RealmChoiceCallback serialization
32 *
33 */
34
35public class RealmChoiceCallbackTest extends SerializationTest implements
36        SerializationTest.SerializableAssert {
37
38    public static String[] msgs = {
39            "New String",
40            "Another string",
41            "Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string. Long string.",
42            "t"};
43
44    public static final int [] idx = {2, 3};
45
46    @Override
47    protected Object[] getData() {
48        Object [] oo = {
49                new RealmChoiceCallback(msgs[0], msgs, 0, true),
50                new RealmChoiceCallback(msgs[1], msgs, 1, true),
51                new RealmChoiceCallback(msgs[1], msgs, 0, false),
52                new RealmChoiceCallback(msgs[2], msgs, 0, false)
53
54        };
55        for (Object element : oo) {
56            RealmChoiceCallback rc = (RealmChoiceCallback)element;
57            if (rc.allowMultipleSelections()) {
58                rc.setSelectedIndexes(idx);
59            } else {
60                rc.setSelectedIndex(msgs.length - 1);
61            }
62        }
63        return oo;
64    }
65
66    public void assertDeserialized(Serializable oref, Serializable otest) {
67        RealmChoiceCallback ref = (RealmChoiceCallback) oref;
68        RealmChoiceCallback test = (RealmChoiceCallback) otest;
69
70        boolean all = ref.allowMultipleSelections();
71        assertEquals(all, test.allowMultipleSelections());
72        String prompt = ref.getPrompt();
73        assertEquals(prompt, test.getPrompt());
74
75        String [] ch = ref.getChoices();
76        String [] tCh = test.getChoices();
77        assertEquals(ch.length, tCh.length);
78        for (int i = 0; i < ch.length; i++) {
79            assertEquals(ch[i], tCh[i]);
80        }
81        assertEquals(ref.getDefaultChoice(), test.getDefaultChoice());
82        int [] in = ref.getSelectedIndexes();
83        int [] tIn = test.getSelectedIndexes();
84//        assertNull("in is not null", in);
85//        assertNull("tIn is not null", tIn);
86
87        if (!all) {
88            assertEquals("Incorrect length in ", in.length, 1);
89            assertEquals("Incorrect length tIn ", tIn.length, 1);
90            assertEquals("Incorrect index", in[0], tIn[0]);
91        } else {
92            assertEquals("Incorrect length", in.length, tIn.length);
93            for (int i = 0; i < in.length; i++) {
94                assertEquals(in[i], tIn[i]);
95            }
96        }
97    }
98}