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