summaryrefslogtreecommitdiffstats
blob: 25fb817a0696ec5c64e38c4c40f4a835a4a4ea3a (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
/*
 * Copyright (c) 2012, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010 Remko Tronçon
 * All rights reserved.
 */
package com.isode.stroke.client;

import java.util.Vector;

import com.isode.stroke.elements.IQ;
import com.isode.stroke.elements.Message;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.elements.Presence;
import com.isode.stroke.elements.Stanza;
import com.isode.stroke.jid.JID;

/**
 * Dummy Stanza Channel for Unit Testing
 *
 */
public class DummyStanzaChannel extends StanzaChannel {

    public Vector<Stanza> sentStanzas = new Vector<Stanza>();
    public boolean available_;

    public DummyStanzaChannel()  {
        available_ = true;
    }

    public void sendStanza(Stanza stanza) {
        sentStanzas.add(stanza);
    }

    public void setAvailable(boolean available) {
        available_ = available;
        onAvailableChanged.emit(available);
    }

    public void sendIQ(IQ iq) {
        sentStanzas.add(iq);
    }

    public void sendMessage(Message message) {
        sentStanzas.add(message);
    }

    public void sendPresence(Presence presence) {
        sentStanzas.add(presence);
    }

    public String getNewIQID() {
        return "test-id";
    }

    public boolean isAvailable() {
        return available_;
    }

    public boolean getStreamManagementEnabled() {
        return false;
    }

    public <T extends Payload> boolean isRequestAtIndex(int index, JID jid, IQ.Type type, T plType) {
        if (index >= sentStanzas.size()) {
            return false;
        }
        Stanza stanza = (sentStanzas.get(index));
        IQ iqStanza = null;
        if(stanza instanceof IQ) {
            iqStanza = (IQ)(sentStanzas.get(index));
        }
        return iqStanza != null && iqStanza.getType() == type && iqStanza.getTo().equals(jid) 
        && iqStanza.getPayload(plType) != null;
    }
    
    public boolean isResultAtIndex(int index, String id) {
        if (index >= sentStanzas.size()) {
                return false;
        }
        Stanza stanza = (sentStanzas.get(index));
        IQ iqStanza = null;
        if(stanza instanceof IQ) {
            iqStanza = (IQ)(sentStanzas.get(index));
        }
        return iqStanza != null && iqStanza.getType() == IQ.Type.Result && iqStanza.getID().equals(id) ;
}


    public boolean isErrorAtIndex(int index, String id) {
        if (index >= sentStanzas.size()) {
            return false;
        }
        Stanza stanza = (sentStanzas.get(index));
        IQ iqStanza = null;
        if(stanza instanceof IQ) {
            iqStanza = (IQ)(sentStanzas.get(index));
        }
        return iqStanza != null && iqStanza.getType() == IQ.Type.Error && iqStanza.getID().equals(id);
    }

    public <T> T getStanzaAtIndex(T object,int index) {
        if (sentStanzas.size() <= index) {
            return null;
        }
        Stanza stanza = sentStanzas.get(index);
        T obj = null;
        if(object.getClass().isAssignableFrom(stanza.getClass())) {
            return (T)(sentStanzas.get(index));
        }
        return null;
    }   
}