类hudson.model.Failure源码实例Demo

下面列出了怎么用hudson.model.Failure的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: oic-auth-plugin   文件: OicSession.java
/**
 * When the identity provider is done with its thing, the user comes back here.
 * @return an {@link HttpResponse}
 */
public HttpResponse doFinishLogin(StaplerRequest request)  {
    StringBuffer buf = request.getRequestURL();
    if (request.getQueryString() != null) {
        buf.append('?').append(request.getQueryString());
    }
    AuthorizationCodeResponseUrl responseUrl = new AuthorizationCodeResponseUrl(buf.toString());
    if (!state.equals(responseUrl.getState())) {
        return new Failure("State is invalid");
    }
    String code = responseUrl.getCode();
    if (responseUrl.getError() != null) {
        return new Failure(
                "Error from provider: " + responseUrl.getError() + ". Details: " + responseUrl.getErrorDescription()
        );
    } else if (code == null) {
        return new Failure("Missing authorization code");
    } else {
        return onSuccess(code);
    }
}
 
@Test
public void validatePluginNoCredentialsFailure() {
    final String error = "AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified." + System.lineSeparator() +
            "The AWS credentials provided are not valid.";

    thrown.expect(Failure.class);
    thrown.expectMessage(error);

    Validation.validatePlugin(
            null,
            null,
            "us-east-1",
            CategoryType.Build.getName(),
            "Jenkins-Build",
            "1",
            "ProjectName",
            null);
}
 
@Test
public void validatePluginNoRegionFailure() {
    final String error = "AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified." + System.lineSeparator();
    final String regionError =  "The specified AWS region is not valid.";

    thrown.expect(Failure.class);
    thrown.expectMessage(error);
    thrown.expectMessage(regionError);

    Validation.validatePlugin(
            "",
            "",
            "",
            CategoryType.Build.getName(),
            "Jenkins-Build",
            "1",
            "ProjectName",
            null);
}
 
@Test
public void validatePluginInvalidActionTypeProviderFailure() {
    final String error = "AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified." + System.lineSeparator();

    thrown.expect(Failure.class);
    thrown.expectMessage(error);
    thrown.expectMessage("Category:");
    thrown.expectMessage("Version: 1");
    thrown.expectMessage("Provider:");

    Validation.validatePlugin(
            "",
            "",
            "us-east-1",
            CategoryType.Build.getName(),
            "",
            "1",
            "ProjectName",
            null);
}
 
@Test
public void validatePluginInvalidActionTypeCategoryFailure() {
    final String error = "AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified." + System.lineSeparator();

    thrown.expect(Failure.class);
    thrown.expectMessage(error);
    thrown.expectMessage("Category: Please Choose A Category");
    thrown.expectMessage("Version: 1");
    thrown.expectMessage("Provider: Jenkins-Build");

    Validation.validatePlugin(
            "",
            "",
            "us-east-1",
            CategoryType.PleaseChooseACategory.getName(),
            "Jenkins-Build",
            "1",
            "ProjectName",
            null);
}
 
@Test
public void validatePluginInvalidActionTypeVersionFailure() {
    final String error = "AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified." + System.lineSeparator();

    thrown.expect(Failure.class);
    thrown.expectMessage(error);
    thrown.expectMessage("Category: Build");
    thrown.expectMessage("Version:");
    thrown.expectMessage("Provider: Jenkins-Build");

    Validation.validatePlugin(
            "",
            "",
            "us-east-1",
            CategoryType.Build.getName(),
            "Jenkins-Build",
            "",
            "ProjectName",
            null);
}
 
@Test
public void validatePluginAllFieldsMissingFailure() {
    thrown.expect(Failure.class);
    thrown.expectMessage("AWS CodePipeline Jenkins plugin setup error. One or more required configuration parameters have not been specified.");
    thrown.expectMessage("The specified AWS region is not valid.");
    thrown.expectMessage("The AWS credentials provided are not valid.");

    Validation.validatePlugin(
            null,
            null,
            "",
            CategoryType.Build.getName(),
            "Jenkins-Build",
            "",
            "ProjectName",
            null);
}
 
private void validateInternal(String name, BlueScmConfig scmConfig, BlueOrganization organization) {

        checkUserIsAuthenticatedAndHasItemCreatePermission(organization);

        // If scmConfig is empty then we are missing the uri and name
        if (scmConfig == null) {
            throw fail(new Error("scmConfig", ErrorCodes.MISSING.toString(), "scmConfig is required"));
        }

        if (scmConfig.getUri() == null) {
            throw fail(new Error(ERROR_FIELD_SCM_CONFIG_URI, ErrorCodes.MISSING.toString(), ERROR_FIELD_SCM_CONFIG_URI + " is required"));
        }

        if (getName() == null) {
            throw fail(new Error(ERROR_FIELD_SCM_CONFIG_NAME, ErrorCodes.MISSING.toString(), ERROR_FIELD_SCM_CONFIG_NAME + " is required"));
        }

        List<Error> errors = Lists.newLinkedList(validate(name, scmConfig));

        // Validate that name matches rules
        try {
            Jenkins.getInstance().getProjectNamingStrategy().checkName(getName());
            Jenkins.checkGoodName(name);
        }catch (Failure f){
            errors.add(new Error(ERROR_FIELD_SCM_CONFIG_NAME, Error.ErrorCodes.INVALID.toString(),  getName() + " in not a valid name"));
        }

        ModifiableTopLevelItemGroup parent = getParent(organization);

        if (parent.getItem(name) != null) {
            errors.add(new Error(ERROR_NAME, Error.ErrorCodes.ALREADY_EXISTS.toString(), getName() + " already exists"));
        }

        if(!errors.isEmpty()){
            throw fail(errors);
        }
    }
 
 类所在包
 同包方法