List of usage examples for org.eclipse.jgit.transport Transport Transport
protected Transport(Repository local, URIish uri)
From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.GitVcsSupportTest.java
License:Apache License
@Test @TestFor(issues = "TW-24084") public void should_retry_getCurrentState_if_it_fails() throws Exception { VcsRootImpl root = vcsRoot().withFetchUrl("ssh://some.org/repo.git") .withAuthMethod(AuthenticationMethod.PRIVATE_KEY_DEFAULT).build(); final FetchConnection connection = myContext.mock(FetchConnection.class); final Ref masterRef = myContext.mock(Ref.class, "master"); final Ref topicRef = myContext.mock(Ref.class, "topic"); myContext.checking(new Expectations() { {/*w w w . ja v a 2 s . co m*/ allowing(masterRef).getName(); will(returnValue("refs/heads/master")); allowing(masterRef).getObjectId(); will(returnValue(ObjectId.fromString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); allowing(topicRef).getName(); will(returnValue("refs/heads/topic")); allowing(topicRef).getObjectId(); will(returnValue(ObjectId.fromString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))); allowing(connection).getRefsMap(); will(returnValue(map("refs/heads/master", masterRef, "refs/heads/topic", topicRef))); allowing(connection).close(); } }); //setup TransportFactory so that it fails to get connection several times with well known exceptions //and then successfully gets it on the last call final AtomicInteger failCount = new AtomicInteger(0); final List<TransportException> recoverableErrors = Arrays.asList( new TransportException("Session.connect: java.net.SocketException: Connection reset", new JSchException("test")), new TransportException( "Session.connect: java.net.SocketException: Software caused connection abort", new JSchException("test")), new TransportException("com.jcraft.jsch.JSchException: connection is closed by foreign host", new JSchException("test")), new TransportException("java.net.UnknownHostException: some.org", new JSchException("test")), new TransportException("com.jcraft.jsch.JSchException: verify: false", new JSchException("test"))); ServerPluginConfig config = myConfigBuilder.withGetConnectionRetryAttempts(recoverableErrors.size() + 1) .withConnectionRetryIntervalMillis(0).build(); VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager(); TransportFactory transportFactory = new TransportFactoryImpl(config, manager) { @Override public Transport createTransport(@NotNull Repository r, @NotNull URIish url, @NotNull AuthSettings authSettings, int timeoutSeconds) throws NotSupportedException, VcsException { return new Transport(r, url) { @Override public FetchConnection openFetch() throws NotSupportedException, TransportException { if (failCount.get() < recoverableErrors.size()) { TransportException error = recoverableErrors.get(failCount.get()); failCount.incrementAndGet(); throw error; } else { return connection; } } @Override public PushConnection openPush() throws NotSupportedException, TransportException { return null; } @Override public void close() { } }; } }; FetchCommand fetchCommand = new FetchCommandImpl(config, transportFactory, new FetcherProperties(config), manager); FetchCommandCountDecorator fetchCounter = new FetchCommandCountDecorator(fetchCommand); GitVcsSupport git = gitSupport().withPluginConfig(config).withTransportFactory(transportFactory) .withFetchCommand(fetchCounter).build(); RepositoryStateData state = git.getCurrentState(root); assertEquals(state.getBranchRevisions(), map("refs/heads/master", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/topic", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")); }