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