~elambert/gearmanij/gearman_java_library

« back to all changes in this revision

Viewing changes to test/org/gearman/common/MockConnection.java

  • Committer: Eric Lambert
  • Date: 2009-07-10 03:56:00 UTC
  • Revision ID: eric.d.lambert@gmail.com-20090710035600-fy5ghxevm51b5lvm
started refactoring unused classes and tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2009 by Eric Herman <eric@freesa.org>
3
 
 * Use and distribution licensed under the 
4
 
 * GNU Lesser General Public License (LGPL) version 2.1.
5
 
 * See the COPYING file in the parent directory for full text.
6
 
 */
7
 
package org.gearman.common;
8
 
 
9
 
import java.util.LinkedList;
10
 
import java.util.List;
11
 
import java.util.Queue;
12
 
 
13
 
import org.gearman.util.ByteUtils;
14
 
 
15
 
public class MockConnection extends ThrowingConnection {
16
 
    private boolean open;
17
 
    private boolean opened;
18
 
    private boolean closed;
19
 
    private List<String> canDo;
20
 
    private List<String> cantDo;
21
 
    private List<String> clientId;
22
 
 
23
 
    protected Queue<GearmanPacket> readQueue;
24
 
    protected List<GearmanPacket> written;
25
 
 
26
 
    public MockConnection() {
27
 
        open = false;
28
 
        opened = false;
29
 
        closed = false;
30
 
        canDo = new LinkedList<String>();
31
 
        cantDo = new LinkedList<String>();
32
 
        clientId = new LinkedList<String>();
33
 
 
34
 
        readQueue = new LinkedList<GearmanPacket>();
35
 
        written = new LinkedList<GearmanPacket>();
36
 
    }
37
 
 
38
 
    public void open() {
39
 
        open = true;
40
 
        opened = true;
41
 
    }
42
 
 
43
 
    public void close() {
44
 
        open = false;
45
 
        closed = true;
46
 
    }
47
 
 
48
 
    public GearmanPacket read() {
49
 
        if (!readQueue.isEmpty()) {
50
 
            return readQueue.poll();
51
 
        }
52
 
        throw new RuntimeException("unexpected read");
53
 
    }
54
 
 
55
 
    public void write(Packet request) {
56
 
        written.add(request);
57
 
        PacketType packetType = request.getPacketType();
58
 
        switch (packetType) {
59
 
        case SET_CLIENT_ID:
60
 
            clientId.add(ByteUtils.fromAsciiBytes(request.getData()));
61
 
            break;
62
 
        case CAN_DO:
63
 
            canDo.add(ByteUtils.fromAsciiBytes(request.getData()));
64
 
            break;
65
 
        case CANT_DO:
66
 
            cantDo.add(ByteUtils.fromAsciiBytes(request.getData()));
67
 
            break;
68
 
        default:
69
 
            break;
70
 
        }
71
 
    }
72
 
 
73
 
    public boolean isOpen() {
74
 
        return open;
75
 
    }
76
 
 
77
 
    public boolean wasOpened() {
78
 
        return opened;
79
 
    }
80
 
 
81
 
    public boolean wasClosed() {
82
 
        return closed;
83
 
    }
84
 
 
85
 
    public List<String> clientId() {
86
 
        return clientId;
87
 
    }
88
 
 
89
 
    public List<String> canDo() {
90
 
        return canDo;
91
 
    }
92
 
 
93
 
    public List<String> cantDo() {
94
 
        return cantDo;
95
 
    }
96
 
 
97
 
}