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

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

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

AbstractFileAssert.hasContent介绍

[英]Verifies that the text content of the actual File is exactly equal to the given one.
The charset to use when reading the file should be provided with #usingCharset(Charset) or #usingCharset(String) prior to calling this method; if not, the platform's default charset (as returned by Charset#defaultCharset()) will be used.

Example:

// use the default charset 
File xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()).toFile(); 
// The following assertion succeeds (default charset is used): 
assertThat(xFile).hasContent("The Truth Is Out There"); 
// The following assertion fails: 
assertThat(xFile).hasContent("La Vérité Est Ailleurs"); 
// using a specific charset 
Charset turkishCharset = Charset.forName("windows-1254"); 
File xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek"), turkishCharset).toFile(); 
// The following assertion succeeds: 
assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek"); 
// The following assertion fails : 
assertThat(xFileTurkish).usingCharset(StandardCharsets.UTF_8).hasContent("Gerçek");

[中]验证实际文件的文本内容是否与给定文件完全相同。
在调用此方法之前,应为读取文件时使用的字符集提供#usingCharset(字符集)或#usingCharset(字符串);否则,将使用平台的默认字符集(由charset#defaultCharset()返回)。
例子:

// use the default charset 
File xFile = Files.write(Paths.get("xfile.txt"), "The Truth Is Out There".getBytes()).toFile(); 
// The following assertion succeeds (default charset is used): 
assertThat(xFile).hasContent("The Truth Is Out There"); 
// The following assertion fails: 
assertThat(xFile).hasContent("La Vérité Est Ailleurs"); 
// using a specific charset 
Charset turkishCharset = Charset.forName("windows-1254"); 
File xFileTurkish = Files.write(Paths.get("xfile.turk"), Collections.singleton("Gerçek"), turkishCharset).toFile(); 
// The following assertion succeeds: 
assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek"); 
// The following assertion fails : 
assertThat(xFileTurkish).usingCharset(StandardCharsets.UTF_8).hasContent("Gerçek");

代码示例

代码示例来源:origin: apache/geode

@Test
 public void filesToBytesAndThenBytesToFiles() throws IOException {
  File file1 = new File(temporaryFolder.getRoot(), "file1.txt");
  File file2 = new File(temporaryFolder.getRoot(), "file2.txt");

  FileUtils.write(file1, "file1-content", "UTF-8");
  FileUtils.write(file2, "file2-content", "UTF-8");

  List<String> fileNames = Arrays.asList(file1.getAbsolutePath(), file2.getAbsolutePath());
  Byte[][] bytes = CliUtil.filesToBytes(fileNames);

  File dir = temporaryFolder.newFolder("temp");
  List<String> filePaths = CliUtil.bytesToFiles(bytes, dir.getAbsolutePath());

  assertThat(filePaths).hasSize(2);
  assertThat(new File(filePaths.get(0))).hasContent("file1-content");
  assertThat(new File(filePaths.get(1))).hasContent("file2-content");
 }
}

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

@Test
public void test_generation_of_file() throws IOException {
 File yamlFile = temp.newFile();
 new EsYmlSettings(new HashMap<>()).writeToYmlSettingsFile(yamlFile);
 assertThat(yamlFile).exists();
 assertThat(yamlFile).hasContent("# This file has been automatically generated by SonarQube during startup.\n" +
  "\n" +
  "# DO NOT EDIT THIS FILE\n" +
  "\n" +
  "{\n" +
  "  }");
}

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

@Test
public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException {
 String taskUuid = "acme";
 when(ceTask.getUuid()).thenReturn(taskUuid);
 settings.setProperty("sonar.filemove.dumpCsv", "true");
 underTest.dumpAsCsv(A_SCORE_MATRIX);
 Collection<File> files = listDumpFilesForTaskUuid(taskUuid);
 assertThat(files).hasSize(1);
 assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';'));
}

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

@Test
public void unzip_report() throws Exception {
 logTester.setLevel(LoggerLevel.DEBUG);
 File reportFile = generateReport();
 try (InputStream input = FileUtils.openInputStream(reportFile)) {
  dbTester.getDbClient().ceTaskInputDao().insert(dbTester.getSession(), TASK_UUID, input);
 }
 dbTester.getSession().commit();
 dbTester.getSession().close();
 underTest.execute(new TestComputationStepContext());
 // directory contains the uncompressed report (which contains only metadata.pb in this test)
 File unzippedDir = reportDirectoryHolder.getDirectory();
 assertThat(unzippedDir).isDirectory().exists();
 assertThat(unzippedDir.listFiles()).hasSize(1);
 assertThat(new File(unzippedDir, "metadata.pb")).hasContent("{metadata}");
 assertThat(logTester.logs(LoggerLevel.DEBUG)).anyMatch(log -> log.matches("Analysis report is \\d+ bytes uncompressed"));
}

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

assertThat(file).hasContent(
 "# This file has been automatically generated by SonarQube during startup.\n" +
  "# Please use sonar.search.javaOpts and/or sonar.search.javaAdditionalOpts in sonar.properties to specify jvm options for Elasticsearch\n" +

代码示例来源:origin: diffplug/spotless

public void hasContent(String expected, Charset charset) {
  Assertions.assertThat(file).usingCharset(charset).hasContent(expected);
}

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

@Test
public void itAbsorbsFileAlreadyExistsExceptionsWhenCopyingDuplicateFiles() {
 List<String> lines = Arrays.asList("Testing", "1", "2", "3");
 Path originalPath = write("original.txt", lines);
 assertThat(originalPath.toFile()).hasContent(String.join(System.lineSeparator(), lines));
 Path copyPath = Paths.get(cacheDir.getRoot().toString() + "/copy.txt");
 assertThat(copyPath).doesNotExist();
 artifactManager.copy(originalPath, cacheDir.getRoot().toPath(), "copy.txt");
 assertThat(copyPath).exists();
 // A redundant copy operation should not throw.
 artifactManager.copy(originalPath, cacheDir.getRoot().toPath(), "copy.txt");
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void customSnippetTemplate() throws Exception {
  ClassLoader classLoader = new URLClassLoader(new URL[] {
      new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
      getClass().getClassLoader());
  ClassLoader previous = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(classLoader);
  try {
    given().port(tomcat.getPort()).accept("application/json")
        .filter(documentationConfiguration(this.restDocumentation))
        .filter(document("custom-snippet-template")).get("/").then()
        .statusCode(200);
  }
  finally {
    Thread.currentThread().setContextClassLoader(previous);
  }
  assertThat(new File(
      "build/generated-snippets/custom-snippet-template/curl-request.adoc"))
          .hasContent("Custom curl request");
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void customSnippetTemplate() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  ClassLoader classLoader = new URLClassLoader(new URL[] {
      new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
      getClass().getClassLoader());
  ClassLoader previous = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(classLoader);
  try {
    mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(document("custom-snippet-template"));
  }
  finally {
    Thread.currentThread().setContextClassLoader(previous);
  }
  assertThat(new File(
      "build/generated-snippets/custom-snippet-template/curl-request.adoc"))
          .hasContent("Custom curl request");
}

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

@Test
public void testConfProtocolSouldReturnConfFile() throws Exception {
  File file = fileSystem.getFile("file://conf/conf.txt");
  assertThat(file).hasContent("confcontent");
}

代码示例来源:origin: airlift/drift

private static void assertGenerated(File basedir, String name)
      throws IOException
  {
    URL expected = getResource(format("basic/%s.txt", name));

    name += META_SUFFIX;
    assertThat(new File(basedir, format("target/classes/its/%s.class", name))).isFile();
    assertThat(new File(basedir, format("target/generated-sources/annotations/its/%s.java", name)))
        .usingCharset(UTF_8).hasContent(asCharSource(expected, UTF_8).read());
  }
}

代码示例来源:origin: apache/james-project

@Test
public void testConfProtocolSouldReturnConfFile() throws Exception {
  File file = fileSystem.getFile("file://conf/conf.txt");
  assertThat(file).hasContent("confcontent");
}

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

@Test
public void testVarProtocolSouldReturnVarFile() throws Exception {
  File file = fileSystem.getFile("file://var/var.txt");
  assertThat(file).hasContent("varcontent");
}

代码示例来源:origin: apache/james-project

@Test
public void testVarProtocolSouldReturnVarFile() throws Exception {
  File file = fileSystem.getFile("file://var/var.txt");
  assertThat(file).hasContent("varcontent");
}

代码示例来源:origin: kolorobot/unit-testing-demo

@Test
public void writesContentToFile() throws IOException {
  // arrange
  File output = temporaryFolder.newFolder("reports")
      .toPath()
      .resolve("output.txt")
      .toFile();
  // act
  fileWriter.writeTo(output.getPath(), "test");
  // assert
  assertThat(output)
      .hasContent("test")
      .hasExtension("txt")
      .hasParent(resolvePath("reports"));
}

代码示例来源:origin: arquillian/arquillian-cube

@Test
public void should_move_recording_video() throws IOException, NoSuchMethodException {
  final File destination = temporaryFolder.newFolder("destination");
  final File video = temporaryFolder.newFile("file.flv");
  Files.write(video.toPath(), "Hello".getBytes());
  when(seleniumContainers.getVideoRecordingFile()).thenReturn(video.toPath());
  when(after.getTestClass()).thenReturn(new TestClass(VncRecorderLifecycleManagerTest.class));
  when(after.getTestMethod()).thenReturn(
    VncRecorderLifecycleManagerTest.class.getMethod("should_move_recording_video"));
  Map<String, String> conf = new HashMap<>();
  conf.put("videoOutput", destination.getAbsolutePath());
  TestResult testResult = TestResult.passed();
  VncRecorderLifecycleManager vncRecorderLifecycleManager = new VncRecorderLifecycleManager();
  vncRecorderLifecycleManager.vnc = cube;
  vncRecorderLifecycleManager.afterVideoRecordedEvent = event;
  vncRecorderLifecycleManager.stopRecording(after,
    testResult,
    CubeDroneConfiguration.fromMap(conf),
    seleniumContainers
  );
  assertThat(new File(destination,
    "org_arquillian_cube_docker_drone_VncRecorderLifecycleManagerTest_should_move_recording_video.flv"))
    .exists()
    .hasContent("Hello");
}

相关文章