Skip to content

Commit

Permalink
fix for jenkins-bug #JENKINS-17165
Browse files Browse the repository at this point in the history
overriding method readResolve() from base-class BPHostConfiguration for
resoving Reflection-problems with XStream;
removing own SecretHelper (replacement/hiding of the API-class) and
creating new class MySecretHelper;
resolves some problems with unit-tests under MS-Windows
  • Loading branch information
afischer211 committed Apr 15, 2013
1 parent 33356f2 commit 304aaab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
Expand Up @@ -24,72 +24,85 @@

package jenkins.plugins.publish_over_ftp;

import static org.easymock.EasyMock.expect;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.util.SecretHelper;

import java.io.File;
import java.io.IOException;

import jenkins.plugins.publish_over.BPBuildInfo;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.easymock.classextension.EasyMock;
import org.easymock.classextension.IMocksControl;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static org.easymock.EasyMock.expect;
import org.jvnet.hudson.test.HudsonTestCase;

@SuppressWarnings({ "PMD.SignatureDeclareThrowsException", "PMD.TooManyMethods" })
public class BapHostConfigurationTest {
public class BapHostConfigurationTest extends HudsonTestCase {

@BeforeClass
public static void before() {
SecretHelper.setSecretKey();
MySecretHelper.setSecretKey();
}

@AfterClass
public static void after() {
SecretHelper.clearSecretKey();
MySecretHelper.clearSecretKey();
}

@Before
public void initializeHostConfig() {
this.bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient);
}

private final transient BPBuildInfo buildInfo = new BPBuildInfo(TaskListener.NULL, "", new FilePath(new File("")), null, null);
private final transient IMocksControl mockControl = EasyMock.createStrictControl();
private final transient FTPClient mockFTPClient = mockControl.createMock(FTPClient.class);
private transient BapFtpHostConfiguration bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient);
private transient BapFtpHostConfiguration bapFtpHostConfiguration;

@Test public void testChangeToRootDir() throws Exception {
@Test
public void testChangeToRootDir() throws Exception {
assertChangeToInitialDirectory("/");
}

@Test public void testChangeToRootDirWin() throws Exception {
@Test
public void testChangeToRootDirWin() throws Exception {
assertChangeToInitialDirectory("\\");
}

@Test public void testChangeToRootDirLongerPath() throws Exception {
@Test
public void testChangeToRootDirLongerPath() throws Exception {
assertChangeToInitialDirectory("/this/is/my/root");
}

@Test public void testChangeToRootDirRelativePath() throws Exception {
@Test
public void testChangeToRootDirRelativePath() throws Exception {
assertChangeToInitialDirectory("this/is/my/rel/root", true);
}

@Test public void testNoChangeDirectoryRemoteDirNull() throws Exception {
@Test
public void testNoChangeDirectoryRemoteDirNull() throws Exception {
assertNoChangeToInitialDirectory(null);
}

@Test public void testNoChangeDirectoryRemoteDirEmptyString() throws Exception {
@Test
public void testNoChangeDirectoryRemoteDirEmptyString() throws Exception {
assertNoChangeToInitialDirectory("");
}

@Test public void testNoChangeDirectoryRemoteDirOnlySpaceInString() throws Exception {
@Test
public void testNoChangeDirectoryRemoteDirOnlySpaceInString() throws Exception {
assertNoChangeToInitialDirectory(" ");
}

private void assertNoChangeToInitialDirectory(final String remoteRoot) throws Exception {
this.bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient);
bapFtpHostConfiguration.setRemoteRootDir(remoteRoot);
expectConnectAndLogin();
expect(mockFTPClient.printWorkingDirectory()).andReturn("/pub");
Expand All @@ -103,6 +116,7 @@ private void assertChangeToInitialDirectory(final String remoteRoot) throws Exce
}

private void assertChangeToInitialDirectory(final String remoteRoot, final boolean expectPwd) throws Exception {
this.bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient);
bapFtpHostConfiguration.setRemoteRootDir(remoteRoot);
expectConnectAndLogin();
expect(mockFTPClient.changeWorkingDirectory(remoteRoot)).andReturn(true);
Expand All @@ -113,14 +127,17 @@ private void assertChangeToInitialDirectory(final String remoteRoot, final boole
assertEquals(remoteRoot, client.getAbsoluteRemoteRoot());
}

@Test public void testSetActive() throws Exception {
@Test
public void testSetActive() throws Exception {
this.bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient);
bapFtpHostConfiguration.setUseActiveData(true);
expectConnectAndLogin();
expect(mockFTPClient.printWorkingDirectory()).andReturn("/");
assertCreateSession();
}

@Test public void testDisableMakeNestedDirs() throws Exception {
@Test
public void testDisableMakeNestedDirs() throws Exception {
bapFtpHostConfiguration = new BapFtpHostConfigurationWithMockFTPClient(mockFTPClient, true);
expectConnectAndLogin();
expect(mockFTPClient.printWorkingDirectory()).andReturn("/");
Expand Down Expand Up @@ -158,17 +175,25 @@ private static class BapFtpHostConfigurationWithMockFTPClient extends BapFtpHost
private static final String TEST_USERNAME = "myTestUsername";
private static final String TEST_PASSWORD = "myTestPassword";
private final transient FTPClient ftpClient;

BapFtpHostConfigurationWithMockFTPClient(final FTPClient ftpClient, final boolean disableMakeNestedDirs) {
super(TEST_CFG_NAME, TEST_HOSTNAME, TEST_USERNAME, TEST_PASSWORD, "", DEFAULT_PORT, DEFAULT_TIMEOUT, false, null, disableMakeNestedDirs);
this.ftpClient = ftpClient;
}

BapFtpHostConfigurationWithMockFTPClient(final FTPClient ftpClient) {
this(ftpClient, false);
}

@Override
public FTPClient createFTPClient() {
return ftpClient;
}

@Override
public Object readResolve() {
return super.readResolve();
}
}

}
15 changes: 15 additions & 0 deletions src/test/java/jenkins/plugins/publish_over_ftp/MySecretHelper.java
@@ -0,0 +1,15 @@
package jenkins.plugins.publish_over_ftp;

import hudson.util.SecretHelper;

public class MySecretHelper extends SecretHelper {
private static final String KEY = "7b41d2675f759b479460a41a9758bd0f6049a23ac685dc739bf7bb2d2319472d";

public static void setSecretKey() {
set(KEY);
}

public static void clearSecretKey() {
set(null);
}
}

0 comments on commit 304aaab

Please sign in to comment.