-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b3eba5
commit 7dbe8ce
Showing
3 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...checks-test/src/test/java/com/exxeta/iss/sonar/esql/checks/verifier/CheckMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Sonar ESQL Plugin | ||
* Copyright (C) 2013-2020 Thomas Pohl and EXXETA AG | ||
* http://www.exxeta.com | ||
* | ||
* 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.exxeta.iss.sonar.esql.checks.verifier; | ||
|
||
import org.junit.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CheckMessageTest { | ||
@Test | ||
public void testFormatDefaultMessage() { | ||
CheckMessage message = new CheckMessage(null, "Value is {0,number,integer}, expected value is {1,number,integer}.", 3, 7); | ||
assertThat(message.formatDefaultMessage()).isEqualTo("Value is 3, expected value is 7."); | ||
} | ||
@Test | ||
public void testNotFormatMessageWithoutParameters() { | ||
CheckMessage message = new CheckMessage(null, "public void main(){."); // This message can't be used as a pattern by the MessageFormat | ||
// class | ||
assertThat(message.formatDefaultMessage()).isEqualTo("public void main(){."); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
...st/src/test/java/com/exxeta/iss/sonar/esql/checks/verifier/CheckMessagesVerifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Sonar ESQL Plugin | ||
* Copyright (C) 2013-2020 Thomas Pohl and EXXETA AG | ||
* http://www.exxeta.com | ||
* | ||
* 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.exxeta.iss.sonar.esql.checks.verifier; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.core.StringContains.containsString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
public class CheckMessagesVerifierTest { | ||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void next() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage("\nExpected violation"); | ||
CheckMessagesVerifier.verify(Collections.EMPTY_LIST) | ||
.next(); | ||
} | ||
|
||
@Test | ||
public void noMore() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage("\nNo more violations expected\ngot: at line 1"); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void line() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage("\nExpected: 2\ngot: 1"); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.next().atLine(2); | ||
} | ||
|
||
@Test | ||
public void line_withoutHasNext() { | ||
thrown.expect(IllegalStateException.class); | ||
thrown.expectMessage("Prior to this method you should call next()"); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.atLine(2); | ||
} | ||
|
||
@Test | ||
public void withMessage() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage(containsString("Expected: \"bar\"")); | ||
thrown.expectMessage(containsString("got: \"foo\"")); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.next().atLine(1).withMessage("bar"); | ||
} | ||
|
||
@Test | ||
public void withMessage_withoutHasNext() { | ||
thrown.expect(IllegalStateException.class); | ||
thrown.expectMessage("Prior to this method you should call next()"); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.withMessage("foo"); | ||
} | ||
|
||
@Test | ||
public void withMessageThat() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage("\nExpected: a string containing \"bar\"\n but: was \"foo\""); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo")); | ||
CheckMessagesVerifier.verify(messages) | ||
.next().atLine(1).withMessageThat(containsString("bar")); | ||
} | ||
|
||
@Test | ||
public void withCost() { | ||
thrown.expect(AssertionError.class); | ||
thrown.expectMessage(containsString("Expected: 1.0")); | ||
thrown.expectMessage(containsString("got: 0.0")); | ||
Collection<CheckMessage> messages = Arrays.asList(mockCheckMessage(1, "foo", 0d)); | ||
CheckMessagesVerifier.verify(messages) | ||
.next().withCost(1d); | ||
} | ||
|
||
@Test | ||
public void ok() { | ||
Collection<CheckMessage> messages = Arrays.asList( | ||
mockCheckMessage(null, "b"), | ||
mockCheckMessage(2, "a", 1d), | ||
mockCheckMessage(null, "a"), | ||
mockCheckMessage(1, "b"), | ||
mockCheckMessage(1, "a")); | ||
CheckMessagesVerifier.verify(messages) | ||
.next().atLine(null).withMessage("a") | ||
.next().atLine(null).withMessageThat(containsString("b")) | ||
.next().atLine(1).withMessage("a") | ||
.next().atLine(1).withMessage("b") | ||
.next().atLine(2).withMessage("a").withCost(1d) | ||
.noMore(); | ||
} | ||
|
||
private static final CheckMessage mockCheckMessage(Integer line, String message, Double cost) { | ||
CheckMessage checkMessage = mock(CheckMessage.class); | ||
when(checkMessage.getLine()).thenReturn(line); | ||
when(checkMessage.getDefaultMessage()).thenReturn(message); | ||
when(checkMessage.getText()).thenReturn(message); | ||
when(checkMessage.getCost()).thenReturn(cost); | ||
return checkMessage; | ||
} | ||
|
||
private static final CheckMessage mockCheckMessage(Integer line, String message) { | ||
return mockCheckMessage(line, message, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters