add new unit tests for plugin

This commit adds a few unit tests for the gearman-plugin.
A few things were refactored from creating the tests.
1. move methods from GearmanPluginUtil to GearmanPluginConfig
2. removed a few unused methods.

Change-Id: I8ccd0000ff528867c66958294192818f35d83383
This commit is contained in:
zaro0508
2013-03-15 15:31:26 -07:00
parent 3c3851665a
commit 2b175701d2
9 changed files with 364 additions and 107 deletions

View File

@@ -0,0 +1,77 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.worker.GearmanWorker;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
/**
* Test for the {@link AbstractWorkerThread} class.
*
* @author Khai Do
*/
@PrepareForTest(GearmanWorker.class)
public class AbstractWorkerThreadTest {
/**
*/
@Before
public void setUp() {
GearmanWorker gearmanWorker = mock(GearmanWorker.class);
GearmanNIOJobServerConnection conn = new GearmanNIOJobServerConnection("localhost", 4730);
doNothing().when(gearmanWorker).work();
when(gearmanWorker.addServer(conn)).thenReturn(true);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testAnonymousThread() {
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730);
assertEquals("anonymous", fakeWorker.getName());
}
@Test
public void testNamedThread() {
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730, "faker");
assertEquals("faker", fakeWorker.getName());
}
@Test
public void testStartStopThread() {
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730);
fakeWorker.start();
assertTrue(fakeWorker.isAlive());
fakeWorker.stop();
assertFalse(fakeWorker.isAlive());
}
}

View File

@@ -0,0 +1,61 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This a fake worker which is only used for testing
*
* @author Khai Do
*
*/
public class FakeWorkerThread extends AbstractWorkerThread{
private static final Logger logger = LoggerFactory
.getLogger(Constants.PLUGIN_LOGGER_NAME);
// constructor
public FakeWorkerThread(String host, int port) {
super(host, port);
}
public FakeWorkerThread(String host, int port, String name) {
super(host, port, name);
}
/**
* Fake registerJobs
*/
@Override
public void registerJobs() {
}
/**
* Fake run
*/
@Override
public void run() {
}
}

View File

@@ -0,0 +1,77 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import jenkins.model.Jenkins;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* Test for the {@link GearmanPluginConfig} class.
*
* @author Khai Do
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(Jenkins.class)
public class GearmanPluginConfigTest {
private GearmanPluginConfig gpc;
/**
*/
@Before
public void setUp() {
TemporaryFolder folder = new TemporaryFolder();
Jenkins jenkins = mock(Jenkins.class);
PowerMockito.mockStatic(Jenkins.class);
when(Jenkins.getInstance()).thenReturn(jenkins);
gpc = new GearmanPluginConfig();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testDefaultGearmanHost() {
assertEquals(Constants.GEARMAN_DEFAULT_TCP_HOST, gpc.getHost());
}
@Test
public void testDefaultGearmanPort() {
assertEquals(Constants.GEARMAN_DEFAULT_TCP_PORT, gpc.getPort());
}
@Test
public void testDefaultLaunchWorker() {
assertEquals(Constants.GEARMAN_DEFAULT_LAUNCH_WORKER,
gpc.launchWorker());
}
}

View File

@@ -0,0 +1,64 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Set;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.worker.GearmanWorker;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
/**
* Test for the {@link ManagementWorkerThread} class.
*
* @author Khai Do
*/
@PrepareForTest(GearmanWorker.class)
public class ManagementWorkerThreadTest {
/**
*/
@Before
public void setUp() {
GearmanWorker gearmanWorker = mock(GearmanWorker.class);
GearmanNIOJobServerConnection conn = new GearmanNIOJobServerConnection("localhost", 4730);
doNothing().when(gearmanWorker).work();
when(gearmanWorker.addServer(conn)).thenReturn(true);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testRegisterJobs() {
AbstractWorkerThread manager = new ManagementWorkerThread("GearmanServer", 4730, "MyWorker" );
manager.registerJobs();
Set<String> functions = manager.worker.getRegisteredFunctions();
assertEquals("stop:GearmanServer", functions.toArray()[0]);
}
}