org.assertj.core.api.AbstractCharSequenceAssert.hasSize()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(93)

本文整理了Java中org.assertj.core.api.AbstractCharSequenceAssert.hasSize()方法的一些代码示例,展示了AbstractCharSequenceAssert.hasSize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AbstractCharSequenceAssert.hasSize()方法的具体详情如下:
包路径:org.assertj.core.api.AbstractCharSequenceAssert
类名称:AbstractCharSequenceAssert
方法名:hasSize

AbstractCharSequenceAssert.hasSize介绍

[英]Verifies that the actual CharSequence has the expected length using the length() method.

This assertion will succeed:

String bookName = "A Game of Thrones" 
assertThat(bookName).hasSize(17);

Whereas this assertion will fail:

String bookName = "A Clash of Kings" 
assertThat(bookName).hasSize(4);

[中]使用length()方法验证实际CharSequence是否具有预期的长度。
此断言将成功:

String bookName = "A Game of Thrones" 
assertThat(bookName).hasSize(17);

而此断言将失败:

String bookName = "A Clash of Kings" 
assertThat(bookName).hasSize(4);

代码示例

代码示例来源:origin: SonarSource/sonarqube

@Test
public void create_error_msg_from_long_content() {
 String content = StringUtils.repeat("mystring", 1000);
 assertThat(ScannerWsClient.createErrorMessage(new HttpException("url", 400, content))).hasSize(15 + 128);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void shouldFormatDate() {
 assertThat(DateUtils.formatDate(new Date())).startsWith("20");
 assertThat(DateUtils.formatDate(new Date())).hasSize(10);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
 public void hash_token() {
  String hash = underTest.hash("1234567890123456789012345678901234567890");

  assertThat(hash)
   .hasSize(96)
   .isEqualTo("b2501fc3833ae6feba7dc8a973a22d709b7c796ee97cbf66db2c22df873a9fa147b1b630878f771457b7769efd9ffa0d")
   .matches("[0-9a-f]+");
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void test_content() throws IOException {
 Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH);
 Files.createDirectories(testFile.getParent());
 String content = "test é string";
 Files.write(testFile, content.getBytes(StandardCharsets.ISO_8859_1));
 assertThat(Files.readAllLines(testFile, StandardCharsets.ISO_8859_1).get(0)).hasSize(content.length());
 Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10);
 DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata))
  .setStatus(InputFile.Status.ADDED)
  .setCharset(StandardCharsets.ISO_8859_1);
 assertThat(inputFile.contents()).isEqualTo(content);
 try (InputStream inputStream = inputFile.inputStream()) {
  String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining());
  assertThat(result).isEqualTo(content);
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void test_content_exclude_bom() throws IOException {
 Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH);
 Files.createDirectories(testFile.getParent());
 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.toFile()), StandardCharsets.UTF_8))) {
  out.write('\ufeff');
 }
 String content = "test é string €";
 Files.write(testFile, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
 assertThat(Files.readAllLines(testFile, StandardCharsets.UTF_8).get(0)).hasSize(content.length() + 1);
 Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10);
 DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata))
  .setStatus(InputFile.Status.ADDED)
  .setCharset(StandardCharsets.UTF_8);
 assertThat(inputFile.contents()).isEqualTo(content);
 try (InputStream inputStream = inputFile.inputStream()) {
  String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining());
  assertThat(result).isEqualTo(content);
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void reformatParam_truncates_if_too_long() {
 String param = repeat("a", SqlLogFormatter.PARAM_MAX_WIDTH + 10);
 String formattedParam = SqlLogFormatter.reformatParam(param);
 assertThat(formattedParam)
  .hasSize(SqlLogFormatter.PARAM_MAX_WIDTH)
  .endsWith("...")
  .startsWith(repeat("a", SqlLogFormatter.PARAM_MAX_WIDTH - 3));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void prevent_too_long_messages() {
 assertThat(new DefaultIssueLocation()
  .on(inputFile)
  .message(StringUtils.repeat("a", 4000)).message()).hasSize(4000);
 assertThat(new DefaultIssueLocation()
  .on(inputFile)
  .message(StringUtils.repeat("a", 4001)).message()).hasSize(4000);
}

代码示例来源:origin: springside/springside4

@Test
public void generateString() {
  System.out.println(RandomUtil.randomStringFixLength(5));
  System.out.println(RandomUtil.randomStringRandomLength(5, 10));
  System.out.println(RandomUtil.randomStringFixLength(RandomUtil.threadLocalRandom(), 5));
  System.out.println(RandomUtil.randomStringRandomLength(RandomUtil.threadLocalRandom(), 5, 10));
  assertThat(RandomUtil.randomStringFixLength(5)).hasSize(5);
  assertThat(RandomUtil.randomStringFixLength(RandomUtil.threadLocalRandom(), 5)).hasSize(5);
  System.out.println(RandomUtil.randomLetterFixLength(5));
  System.out.println(RandomUtil.randomLetterRandomLength(5, 10));
  System.out.println(RandomUtil.randomLetterFixLength(RandomUtil.threadLocalRandom(), 5));
  System.out.println(RandomUtil.randomLetterRandomLength(RandomUtil.threadLocalRandom(), 5, 10));
  assertThat(RandomUtil.randomLetterFixLength(5)).hasSize(5);
  assertThat(RandomUtil.randomLetterFixLength(RandomUtil.threadLocalRandom(), 5)).hasSize(5);
  System.out.println(RandomUtil.randomAsciiFixLength(5));
  System.out.println(RandomUtil.randomAsciiRandomLength(5, 10));
  System.out.println(RandomUtil.randomAsciiFixLength(RandomUtil.threadLocalRandom(), 5));
  System.out.println(RandomUtil.randomAsciiRandomLength(RandomUtil.threadLocalRandom(), 5, 10));
  assertThat(RandomUtil.randomAsciiFixLength(5)).hasSize(5);
  assertThat(RandomUtil.randomAsciiFixLength(RandomUtil.threadLocalRandom(), 5)).hasSize(5);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void generate_different_tokens() {
 // this test is not enough to ensure that generated strings are unique,
 // but it still does a simple and stupid verification
 String firstToken = underTest.generate();
 String secondToken = underTest.generate();
 assertThat(firstToken)
  .isNotEqualTo(secondToken)
  .hasSize(40);
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void processSpanBuilderImpl_testNoExplicitLengthLimit() throws Exception {
  String value = "";
  for (int i = 0; i < 255; i++) {
    value += "0";
  }
  addServletParameter("m_tagWithoutExplicitLengthLimit", value);
  addMetadataDefinition("tagWithoutExplicitLengthLimit", "string");
  processSpanBuilderImpl(spanBuilder, servletParameters);
  assertThat(tracer.finishedSpans()).hasSize(1);
  MockSpan mockSpan = tracer.finishedSpans().get(0);
  assertThat(mockSpan.tags()).hasSize(1);
  assertThat(mockSpan.tags().get("tagWithoutExplicitLengthLimit").toString()).hasSize(MAX_LENGTH);
}

代码示例来源:origin: org.apache.james/james-server-jmap

@Test
public void computeShouldReturnStringWithoutTruncation() throws Exception {
  String body = StringUtils.leftPad("a", 100, "b");
  assertThat(testee.compute(Optional.of(body)))
      .hasSize(100)
      .isEqualTo(body);
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

@Test
public void slugifyRespectsAllowedLength() {
  final int allowedLength = 256;
  final String tooLongString = StringUtils.repeat("a", allowedLength - 1) + "bc";
  assertThat(tooLongString).hasSize(allowedLength + 1);
  final String actual = LocalizedString.ofEnglish(tooLongString).slugified().get(Locale.ENGLISH);
  assertThat(actual).hasSize(allowedLength).endsWith("ab");
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

@Test
public void slugifyUniqueRespectsAllowedLength() {
  final int allowedLength = 256;
  final String tooLongString = StringUtils.repeat("a", allowedLength + 1);
  assertThat(tooLongString).hasSize(allowedLength + 1);
  final String actual = LocalizedString.ofEnglish(tooLongString).slugifiedUnique().get(Locale.ENGLISH);
  assertThat(actual).hasSize(allowedLength).matches("a{247}-\\d{8}");
}

代码示例来源:origin: zanata/zanata-platform

@Test
  public void canChangeToDeletedSlugWithSuffixInPlaceIfOldSlugIsTooLong() {
    // 36 characters long
    SlugEntityBase slugEntityBase =
        new SlugClass("abcdefghijklmnopqrstuvwxyz1234567890");
    String newSlug = slugEntityBase.changeToDeletedSlug();
    assertThat(newSlug)
        .isEqualTo("abcdefghijklmnopqrstuvwxyz1" + DELETED_SUFFIX)
        .hasSize(40);
  }
}

代码示例来源:origin: org.apache.james/james-server-jmap

@Test
public void computeShouldReturnStringIsLimitedTo256Length() throws Exception {
  String body = StringUtils.leftPad("a", 300, "b");
  String expected = StringUtils.leftPad("b", MessagePreviewGenerator.MAX_PREVIEW_LENGTH, "b");
  assertThat(testee.compute(Optional.of(body)))
    .hasSize(MessagePreviewGenerator.MAX_PREVIEW_LENGTH)
    .isEqualTo(expected);
}

代码示例来源:origin: HubSpot/jinjava

@Test
public void itTruncatesText() throws Exception {
 assertThat(filter.filter(StringUtils.rightPad("", 256, 'x') + "y", interpreter, "255", "True").toString()).hasSize(258).endsWith("x...");
}

代码示例来源:origin: xing/xing-android-sdk

private static void assertRequestHasBody(Request request, TestMsg expected, int contentLength) throws IOException {
  RequestBody body = request.body();
  assertThat(body.contentLength()).isEqualTo(contentLength);
  assertThat(body.contentType().subtype()).isEqualTo("json");
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  assertThat(buffer.readUtf8())
     .contains("\"msg\":\"" + expected.msg + '"')
     .contains("\"code\":" + expected.code)
     .startsWith("{")
     .endsWith("}")
     .hasSize(contentLength);
}

代码示例来源:origin: com.hubspot.jinjava/jinjava

@Test
public void itTruncatesText() throws Exception {
 assertThat(filter.filter(StringUtils.rightPad("", 256, 'x') + "y", interpreter, "255", "True").toString()).hasSize(258).endsWith("x...");
}

代码示例来源:origin: org.apache.james/james-server-jmap-integration-testing

@Then("^the preview should not contain consecutive spaces or blank characters$")
public void assertPreviewShouldBeNormalized() {
  String actual = httpClient.jsonPath.read(FIRST_MESSAGE + ".preview");
  assertThat(actual).hasSize(MessagePreviewGenerator.MAX_PREVIEW_LENGTH)
    .doesNotMatch("  ")
    .doesNotContain(StringUtils.CR)
    .doesNotContain(StringUtils.LF);
}

代码示例来源:origin: tomasbjerre/git-changelog-lib

@Test
public void testThatTagsThatAreEmptyAfterCommitsHaveBeenIgnoredAreRemoved() throws Exception {
 final String templatePath = "templatetest/testAuthorsCommitsExtended.mustache";
 assertThat(
     gitChangelogApiBuilder() //
       .withFromCommit(ZERO_COMMIT) //
       .withToRef("test") //
       .withTemplatePath(templatePath) //
       .withIgnoreCommitsWithMessage(".*") //
       .render() //
       .trim())
   .hasSize(74);
}

相关文章

微信公众号

最新文章

更多