Add tests for IssueExtractor Change-Id: Ie62cba8382880e4b7aad4de38a21d6acea7dcdbe
diff --git a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/IssueExtractorTest.java b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/IssueExtractorTest.java new file mode 100644 index 0000000..f1290ea --- /dev/null +++ b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/IssueExtractorTest.java
@@ -0,0 +1,192 @@ +// Copyright (C) 2013 The Android Open Source Project +// +// 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 com.googlesource.gerrit.plugins.hooks.util; + +import static org.easymock.EasyMock.expect; + +import java.util.Arrays; +import java.util.List; + +import org.eclipse.jgit.lib.Config; + +import com.google.gerrit.server.config.FactoryModule; +import com.google.gerrit.server.config.GerritServerConfig; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.googlesource.gerrit.plugins.hooks.its.ItsName; +import com.googlesource.gerrit.plugins.hooks.testutil.LoggingMockingTestCase; + +public class IssueExtractorTest extends LoggingMockingTestCase { + private Injector injector; + private Config serverConfig; + + public void testPatternNullMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn(null).atLeastOnce(); + + replayMocks(); + + assertNull("Pattern for null match is not null", + issueExtractor.getPattern()); + } + + public void testPattern() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("TestPattern").atLeastOnce(); + + replayMocks(); + + assertEquals("Expected and generated pattern are not equal", + "TestPattern", issueExtractor.getPattern().pattern()); + } + + public void testIssueIdsNullPattern() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn(null).atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Test"); + assertEquals("Number of found ids do not match", 0, ret.length); + } + + public void testIssueIdsNoMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Test"); + assertEquals("Number of found ids do not match", 0, ret.length); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsFullMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("bug#4711"); + assertEquals("Number of found ids do not match", 1, ret.length); + assertEquals("First found issue id do not match", "4711", ret[0]); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Foo bug#4711 bar"); + assertEquals("Number of found ids do not match", 1, ret.length); + assertEquals("Found issue id does not match", "4711", ret[0]); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsGrouplessMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#\\d+").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Foo bug#4711 bar"); + assertEquals("Number of found ids do not match", 1, ret.length); + assertEquals("Found issue id does not match", "bug#4711", ret[0]); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsMultiGroupMatch() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d)(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Foo bug#4711 bar"); + assertEquals("Number of found ids do not match", 1, ret.length); + assertEquals("Found issue id does not match", "4", ret[0]); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsMulipleMatches() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Foo bug#4711 bug#42 bar bug#123"); + assertEquals("Number of found ids do not match", 3, ret.length); + List<String> retList = Arrays.asList(ret); + assertTrue("4711 not among the extracted ids", retList.contains("4711")); + assertTrue("42 not among the extracted ids", retList.contains("42")); + assertTrue("123 not among the extracted ids", retList.contains("123")); + + assertLogMessageContains("Matching"); + } + + public void testIssueIdsMulipleMatchesWithDuplicates() { + IssueExtractor issueExtractor = injector.getInstance(IssueExtractor.class); + expect(serverConfig.getString("commentLink", "ItsTestName", "match")) + .andReturn("bug#(\\d+)").atLeastOnce(); + + replayMocks(); + + String ret[] = issueExtractor.getIssueIds("Foo bug#4711 bug#42 bar\n" + + "bug#123 baz bug#42"); + assertEquals("Number of found ids do not match", 3, ret.length); + List<String> retList = Arrays.asList(ret); + assertTrue("4711 not among the extracted ids", retList.contains("4711")); + assertTrue("42 not among the extracted ids", retList.contains("42")); + assertTrue("123 not among the extracted ids", retList.contains("123")); + + assertLogMessageContains("Matching"); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + + injector = Guice.createInjector(new TestModule()); + } + + private class TestModule extends FactoryModule { + @Override + protected void configure() { + bind(String.class).annotatedWith(ItsName.class) + .toInstance("ItsTestName"); + + serverConfig = createMock(Config.class); + bind(Config.class).annotatedWith(GerritServerConfig.class) + .toInstance(serverConfig); + } + } +} \ No newline at end of file