@Test public void not_fail_with_valid_key() { validator.validate(createProjectReactor("foo")); validator.validate(createProjectReactor("123foo")); validator.validate(createProjectReactor("foo123")); validator.validate(createProjectReactor("1Z3")); validator.validate(createProjectReactor("a123")); validator.validate(createProjectReactor("123a")); validator.validate(createProjectReactor("1:2")); validator.validate(createProjectReactor("3-3")); validator.validate(createProjectReactor("-:")); }
public void validate(ProjectReactor reactor) { List<String> validationMessages = new ArrayList<>(); for (ProjectDefinition moduleDef : reactor.getProjects()) { if (mode.isIssues()) { validateModuleIssuesMode(moduleDef, validationMessages); } else { validateModule(moduleDef, validationMessages); } } String deprecatedBranchName = reactor.getRoot().getBranch(); if (isBranchFeatureAvailable()) { branchParamsValidator.validate(validationMessages, deprecatedBranchName); } else { validateBranchParamsWhenPluginAbsent(validationMessages); validatePullRequestParamsWhenPluginAbsent(validationMessages); } validateBranch(validationMessages, deprecatedBranchName); if (!validationMessages.isEmpty()) { throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages)); } }
@Before public void prepare() { mode = mock(AnalysisMode.class); settings = mock(GlobalConfiguration.class); when(settings.get(anyString())).thenReturn(Optional.empty()); validator = new ProjectReactorValidator(mode, settings); }
public void validate(ProjectReactor reactor) { List<String> validationMessages = new ArrayList<>(); for (ProjectDefinition moduleDef : reactor.getProjects()) { if (mode.isIssues()) { validateModuleIssuesMode(moduleDef, validationMessages); } else { validateModule(moduleDef, validationMessages); } } String deprecatedBranchName = reactor.getRoot().getBranch(); if (isBranchFeatureAvailable()) { branchParamsValidator.validate(validationMessages, deprecatedBranchName); } else { validateBranchParamsWhenPluginAbsent(validationMessages); validatePullRequestParamsWhenPluginAbsent(validationMessages); } validateBranch(validationMessages, deprecatedBranchName); if (!validationMessages.isEmpty()) { throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages)); } }
@Test public void not_fail_with_valid_branch() { validator.validate(createProjectReactor("foo", "branch")); validator.validate(createProjectReactor("foo", "Branch2")); validator.validate(createProjectReactor("foo", "bra.nch")); validator.validate(createProjectReactor("foo", "bra-nch")); validator.validate(createProjectReactor("foo", "1")); validator.validate(createProjectReactor("foo", "bra_nch")); }
public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator, ProjectReactor projectReactor, ScannerComponentIdGenerator scannerComponentIdGenerator) { if (project == null) { // 1 Apply project builders projectBuildersExecutor.execute(projectReactor); // 2 Validate final reactor validator.validate(projectReactor); // 3 Create project project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt()); LOG.info("Project key: {}", project.key()); LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString()); LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString()); LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault()); } return project; } }
@Test public void not_fail_with_colon_key() { ProjectReactor reactor = createProjectReactor("foo:bar"); validator.validate(reactor); }
@Test public void should_not_fail_with_dot_key() { ProjectReactor reactor = createProjectReactor("foo.bar"); validator.validate(reactor); }
@Test public void not_fail_with_alphanumeric_key() { ProjectReactor reactor = createProjectReactor("Foobar2"); validator.validate(reactor); }
@Test public void not_fail_with_dash_key() { ProjectReactor reactor = createProjectReactor("foo-bar"); validator.validate(reactor); }
@Test public void not_fail_with_underscore_key() { ProjectReactor reactor = createProjectReactor("foo_bar"); validator.validate(reactor); }
@Test public void not_fail_with_valid_version() { validator.validate(createProjectReactor("foo", def -> def.setVersion("1.0"))); validator.validate(createProjectReactor("foo", def -> def.setVersion("2017-10-16"))); validator.validate(createProjectReactor("foo", def -> def.setVersion(repeat("a", 100)))); }
@Test public void allow_slash_issues_mode() { when(mode.isIssues()).thenReturn(true); validator.validate(createProjectReactor("project/key")); when(mode.isIssues()).thenReturn(false); thrown.expect(MessageException.class); thrown.expectMessage("is not a valid project or module key"); validator.validate(createProjectReactor("project/key")); }
@Test public void fail_when_branch_name_is_specified_but_branch_plugin_not_present() { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo"); ProjectReactor reactor = new ProjectReactor(def); when(settings.get(eq(ScannerProperties.BRANCH_NAME))).thenReturn(Optional.of("feature1")); thrown.expect(MessageException.class); thrown.expectMessage("the branch plugin is required but not installed"); validator.validate(reactor); }
@Test public void fail_when_pull_request_id_specified_but_branch_plugin_not_present() { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo"); ProjectReactor reactor = new ProjectReactor(def); when(settings.get(eq(ScannerProperties.PULL_REQUEST_KEY))).thenReturn(Optional.of("#1984")); thrown.expect(MessageException.class); thrown.expectMessage("the branch plugin is required but not installed"); validator.validate(reactor); }
@Test public void fail_when_pull_request_branch_is_specified_but_branch_plugin_not_present() { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo"); ProjectReactor reactor = new ProjectReactor(def); when(settings.get(eq(ScannerProperties.PULL_REQUEST_BRANCH))).thenReturn(Optional.of("feature1")); thrown.expect(MessageException.class); thrown.expectMessage("the branch plugin is required but not installed"); validator.validate(reactor); }
@Test public void fail_when_branch_target_is_specified_but_branch_plugin_not_present() { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo"); ProjectReactor reactor = new ProjectReactor(def); when(settings.get(eq(ScannerProperties.BRANCH_TARGET))).thenReturn(Optional.of("feature1")); thrown.expect(MessageException.class); thrown.expectMessage("the branch plugin is required but not installed"); validator.validate(reactor); }
@Test public void fail_when_pull_request_base_specified_but_branch_plugin_not_present() { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo"); ProjectReactor reactor = new ProjectReactor(def); when(settings.get(eq(ScannerProperties.PULL_REQUEST_BASE))).thenReturn(Optional.of("feature1")); thrown.expect(MessageException.class); thrown.expectMessage("the branch plugin is required but not installed"); validator.validate(reactor); }
@Test public void fail_with_too_long_version() { ProjectReactor reactor = createProjectReactor("foo", def -> def.setVersion(repeat("a", 101))); thrown.expect(MessageException.class); thrown.expectMessage("\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" is not a valid version name for module \"foo\". " + "The maximum length for version numbers is 100 characters."); validator.validate(reactor); }
@Test public void fail_with_colon_in_branch() { ProjectReactor reactor = createProjectReactor("foo", "bran:ch"); thrown.expect(MessageException.class); thrown.expectMessage("\"bran:ch\" is not a valid branch name"); validator.validate(reactor); }