public static ExceptionCauseMatcher hasType(Class<? extends Throwable> type) { return new ExceptionCauseMatcher(type, null); }
@Test public void fail_if_invalid_remediation_function() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to load the rule with key [squid:1]"); expectedException.expectCause(hasType(IllegalArgumentException.class) .andMessage("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN")); load("" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>" + " <description>Desc</description>" + " <remediationFunction>UNKNOWN</remediationFunction>" + " </rule>" + "</rules>"); }
@Test public void input_stream_should_be_closed() throws Exception { logInAsSystemAdministrator(); InputStream inputStream = newInputStream(plugin.toPath()); call(inputStream, PLUGIN_NAME); // As the same InputStream is used, it will fail as it should have been called during the first execution of the WS expectedException.expectCause(hasType(ClosedChannelException.class)); call(inputStream, PLUGIN_NAME); }
@Test public void fail_if_unsupported_description_format() { String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>" + " <description>Desc</description>" + " <descriptionFormat>UNKNOWN</descriptionFormat>" + " </rule>" + "</rules>"; expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to load the rule with key [squid:1]"); expectedException.expectCause(hasType(IllegalArgumentException.class) .andMessage("No enum constant org.sonar.api.server.rule.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN")); load(xml).rule("1"); }
@Test public void writeToJvmOptionFile_throws_ISE_in_case_of_IOException() throws IOException { File notAFile = temporaryFolder.newFolder(); EsJvmOptions underTest = new EsJvmOptions(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Cannot write Elasticsearch jvm options file"); expectedException.expectCause(ExceptionCauseMatcher.hasType(IOException.class)); underTest.writeToJvmOptionFile(notAFile); } }
@Test public void loads_duplication_with_otherFileRef_throws_IAE_if_references_itself() { int line = 2; reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(FILE_1_REF, line + 1))); expectedException.expect(VisitException.class); expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("file and otherFile references can not be the same")); underTest.execute(new TestComputationStepContext()); }
@Test public void rethrow_error_as_ISE_when_error_writing_message() { TestRequest request = new TestRequest(); request.setMediaType(MediaTypes.PROTOBUF); Permissions.Permission message = Permissions.Permission.newBuilder().setName("permission-name").build(); expectedException.expect(IllegalStateException.class); expectedException.expectCause(hasType(NullPointerException.class)); expectedException.expectMessage("Error while writing protobuf message"); // provoke NullPointerException WsUtils.writeProtobuf(message, null, new DumbResponse()); }
public ExceptionCauseMatcher andNoMessage() { return new ExceptionCauseMatcher(type, EXPECT_NO_MESSAGE_CONSTANT); }
@Test public void retrieveSystemInfo_throws_ISE_if_http_error() { server.enqueue(new MockResponse().setResponseCode(500)); // initialize registration of process setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE); expectedException.expectCause(hasType(IOException.class) .andMessage(format("Server returned HTTP response code: 500 for URL: http://%s:%d/systemInfo", server.getHostName(), server.getPort()))); underTest.retrieveSystemInfo(); }
@Test public void fail_to_insert_same_row_twice() { underTest.insert(db.getSession(), new QProfileEditUsersDto() .setUuid("UUID-1") .setUserId(100) .setQProfileUuid("QPROFILE") ); expectedException.expectCause(hasType(SQLException.class)); underTest.insert(db.getSession(), new QProfileEditUsersDto() .setUuid("UUID-2") .setUserId(100) .setQProfileUuid("QPROFILE") ); }
public ExceptionCauseMatcher andMessage(String expectedMessage) { return new ExceptionCauseMatcher(type, Objects.requireNonNull(expectedMessage)); }
@Test public void loads_duplication_with_otherFileRef_throws_IAE_if_component_does_not_exist() { int line = 2; reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(line), createInProjectDuplicate(666, line + 1))); expectedException.expect(VisitException.class); expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("Component with ref '666' can't be found")); underTest.execute(new TestComputationStepContext()); }
public ExceptionCauseMatcher andNoMessage() { return new ExceptionCauseMatcher(type, EXPECT_NO_MESSAGE_CONSTANT); }
@Test public void changeLogLevel_throws_ISE_if_http_error() { String message = "blah"; server.enqueue(new MockResponse().setResponseCode(500).setBody(message)); // initialize registration of process setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE); expectedException.expectCause(hasType(IOException.class) .andMessage(format("Failed to change log level in Compute Engine. Code was '500' and response was 'blah' for url " + "'http://%s:%s/changeLogLevel'", server.getHostName(), server.getPort()))); underTest.changeLogLevel(LoggerLevel.DEBUG); }
public static ExceptionCauseMatcher hasType(Class<? extends Throwable> type) { return new ExceptionCauseMatcher(type, null); }
@Test public void fail_on_project_without_children_already_having_computed_measure() { ReportComponent root = builder(PROJECT, ROOT_REF).build(); treeRootHolder.setRoot(root); measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(10)); expectedException.expectCause(hasType(UnsupportedOperationException.class) .andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", ROOT_REF, NCLOC_KEY))); new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) .visit(root); }
public ExceptionCauseMatcher andMessage(String expectedMessage) { return new ExceptionCauseMatcher(type, Objects.requireNonNull(expectedMessage)); }
@Test public void fail_when_trying_to_compute_file_measure_already_existing_in_report() { ReportComponent root = builder(PROJECT, ROOT_REF) .addChildren( builder(Component.Type.FILE, FILE_1_REF).build()) .build(); treeRootHolder.setRoot(root); measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(2)); expectedException.expectCause(hasType(UnsupportedOperationException.class) .andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", FILE_1_REF, NCLOC_KEY))); new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) .visit(root); }