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 package org.eclipse.jgit.transport;
45
46 import java.net.URISyntaxException;
47 import java.text.MessageFormat;
48 import java.util.Collections;
49 import java.util.EnumSet;
50 import java.util.HashMap;
51 import java.util.Set;
52
53 import org.eclipse.jgit.errors.NotSupportedException;
54 import org.eclipse.jgit.errors.TransportException;
55 import org.eclipse.jgit.internal.JGitText;
56 import org.eclipse.jgit.lib.Repository;
57 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
58 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class TestProtocol<C> extends TransportProtocol {
78 private static final String SCHEME = "test";
79
80 private class Handle {
81 final C req;
82 final Repository remote;
83
84 Handle(C req, Repository remote) {
85 this.req = req;
86 this.remote = remote;
87 }
88 }
89
90 final UploadPackFactory<C> uploadPackFactory;
91 final ReceivePackFactory<C> receivePackFactory;
92 private final HashMap<URIish, Handle> handles;
93
94
95
96
97
98
99
100
101
102 public TestProtocol(UploadPackFactory<C> uploadPackFactory,
103 ReceivePackFactory<C> receivePackFactory) {
104 this.uploadPackFactory = uploadPackFactory;
105 this.receivePackFactory = receivePackFactory;
106 this.handles = new HashMap<>();
107 }
108
109 @Override
110 public String getName() {
111 return JGitText.get().transportProtoTest;
112 }
113
114 @Override
115 public Set<String> getSchemes() {
116 return Collections.singleton(SCHEME);
117 }
118
119 @Override
120 public Transport open(URIish uri, Repository local, String remoteName)
121 throws NotSupportedException, TransportException {
122 Handle h = handles.get(uri);
123 if (h == null) {
124 throw new NotSupportedException(MessageFormat.format(
125 JGitText.get().URINotSupported, uri));
126 }
127 return new TransportInternal(local, uri, h);
128 }
129
130 @Override
131 public Set<URIishField> getRequiredFields() {
132 return EnumSet.of(URIishField.HOST, URIishField.PATH);
133 }
134
135 @Override
136 public Set<URIishField> getOptionalFields() {
137 return Collections.emptySet();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public synchronized URIish register(C req, Repository remote) {
154 URIish uri;
155 try {
156 int n = handles.size();
157 uri = new URIish(SCHEME + "://test/conn" + n); //$NON-NLS-1$
158 } catch (URISyntaxException e) {
159 throw new IllegalStateException();
160 }
161 handles.put(uri, new Handle(req, remote));
162 return uri;
163 }
164
165 private class TransportInternal extends Transport implements PackTransport {
166 private final Handle handle;
167
168 TransportInternal(Repository local, URIish uri, Handle handle) {
169 super(local, uri);
170 this.handle = handle;
171 }
172
173 @Override
174 public FetchConnection openFetch() throws NotSupportedException,
175 TransportException {
176 handle.remote.incrementOpen();
177 return new InternalFetchConnection<>(
178 this, uploadPackFactory, handle.req, handle.remote);
179 }
180
181 @Override
182 public PushConnection openPush() throws NotSupportedException,
183 TransportException {
184 handle.remote.incrementOpen();
185 return new InternalPushConnection<>(
186 this, receivePackFactory, handle.req, handle.remote);
187 }
188
189 @Override
190 public void close() {
191
192 }
193 }
194 }