Java Code Examples for org.junit.rules.TemporaryFolder#newFile()

The following examples show how to use org.junit.rules.TemporaryFolder#newFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddToSerializedListNoList() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();
  serializedList.delete();

  NodeInterfacePair additionalInterface = NodeInterfacePair.of("n2", "iface2");

  addToSerializedList(
      serializedListPath,
      ImmutableList.of(additionalInterface),
      new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm the additional interface shows up in the serialized list, even if the serialized list
  // didn't exist in the first place
  assertThat(
      BatfishObjectMapper.mapper()
          .readValue(
              CommonUtil.readFile(serializedListPath),
              new TypeReference<List<NodeInterfacePair>>() {}),
      containsInAnyOrder(additionalInterface));
}
 
Example 2
Source File: MessageTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeWithURL() throws Exception {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();
	File file = folder.newFile();
	writeContentsToFile(file, testString);
	URL source = file.toURL();

	Message in = new Message(source);
	byte[] wire = serializationTester.serialize(in);
	writeContentsToFile(file, "fakeContentAsReplacementOfThePrevious");
	Message out = serializationTester.deserialize(wire);
	
	assertTrue(out.isBinary());
	assertEquals(testString,out.asString());
}
 
Example 3
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddToSerializedListNullAddition() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();

  NodeInterfacePair baseInterface = NodeInterfacePair.of("n1", "iface1");

  // Write base serialized list
  List<NodeInterfacePair> interfaces = new ArrayList<>();
  interfaces.add(baseInterface);
  writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces));

  addToSerializedList(serializedListPath, null, new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm original interface shows up in the merged list, even if addition is null
  assertThat(
      BatfishObjectMapper.mapper()
          .readValue(
              CommonUtil.readFile(serializedListPath),
              new TypeReference<List<NodeInterfacePair>>() {}),
      containsInAnyOrder(baseInterface));
}
 
Example 4
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddToSerializedListNoAddition() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();

  NodeInterfacePair baseInterface = NodeInterfacePair.of("n1", "iface1");

  // Write base serialized list
  List<NodeInterfacePair> interfaces = new ArrayList<>();
  interfaces.add(baseInterface);
  writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces));

  addToSerializedList(
      serializedListPath, ImmutableList.of(), new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm original interface shows up in the merged list, even if there are no additions
  assertThat(
      BatfishObjectMapper.mapper()
          .readValue(
              CommonUtil.readFile(serializedListPath),
              new TypeReference<List<NodeInterfacePair>>() {}),
      containsInAnyOrder(baseInterface));
}
 
Example 5
Source File: EndpointProxiesGenTaskTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
TestDir(TemporaryFolder testDir) throws IOException {
  String sourcePath = "ml-modules/root/dbfunctiondef/positive/sessions/";

  srcDir = testDir.newFolder("src");
  serviceDir  = new File(srcDir, sourcePath);
  javaBaseDir = new File(srcDir, "main/java");
  buildFile = testDir.newFile("build.gradle");
  propsFile = testDir.newFile("gradle.properties");
  outClass  = new File(javaBaseDir, "com/marklogic/client/test/dbfunction/positive/SessionsBundle.java");

  serviceDir.mkdirs();

  GradleTestUtil.copyFiles(new File("src/test/" + sourcePath), serviceDir);

  javaBaseDir.mkdirs();
}
 
Example 6
Source File: CoverageSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void test() throws Throwable {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());
	String tempName = "GetStatusMessage.sql";
	String covReport = "test.xml";
	File f = folder.newFile(tempName);
	File coverage = folder.newFile(covReport);

	FileUtils.copyInputStreamToFile(this.getClass().getResourceAsStream("/coverage/Coverage.opencoverxml"),
			coverage);

	FileUtils.copyInputStreamToFile(this.getClass().getResourceAsStream("/coverage/TestCode.sql"), f);
	DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
			.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
	ctxTester.fileSystem().add(file1);
	ctxTester.settings().setProperty(Constants.COVERAGE_FILE, coverage.getAbsolutePath());
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_COVERAGE, false);
	CoverageSensor sut = new CoverageSensor(new SqlCoverCoverageProvider(ctxTester.settings(), ctxTester.fileSystem()));
	sut.execute(ctxTester);
	Assert.assertEquals((int) 2, (int) ctxTester.lineHits(file1.key(), 7));

}
 
Example 7
Source File: ModuleInitTaskTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
TestDir(TemporaryFolder testDir) throws IOException {
  String sourcePath = "ml-modules/root/dbfunctiondef/positive/sessions/";
  String apiFilename = baseName + ".api";

  buildFile = testDir.newFile("build.gradle");
  propsFile = testDir.newFile("gradle.properties");
  srcDir = testDir.newFolder("src");

  sjsOutDir = new File(srcDir, "sjs");
  xqyOutDir = new File(srcDir, "xqy");

  sjsOutDir.mkdirs();
  xqyOutDir.mkdirs();

  sjsAPIFile = new File(sjsOutDir, apiFilename);
  xqyAPIFile = new File(xqyOutDir, apiFilename);

  File srcAPIFile = new File("src/test/" + sourcePath + apiFilename);
  GradleTestUtil.copyTextFile(srcAPIFile, sjsAPIFile);
  GradleTestUtil.copyTextFile(srcAPIFile, xqyAPIFile);
}
 
Example 8
Source File: ServiceCompareTaskTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
TestDir(TemporaryFolder testDir) throws IOException {
    String baseSourcePath   = "ml-modules/root/dbfunctiondef/positive/decoratorBase/";
    String customSourcePath  = "ml-modules/root/dbfunctiondef/positive/decoratorCustom/";
    String otherSourcePath   = "ml-modules/root/dbfunctiondef/positive/mimetype/";

    srcDir = testDir.newFolder("src");

    baseServiceDir   = new File(srcDir, baseSourcePath);
    customServiceDir  = new File(srcDir, customSourcePath);
    otherServiceDir   = new File(srcDir, otherSourcePath);

    buildFile = testDir.newFile("build.gradle");
    propsFile = testDir.newFile("gradle.properties");

    baseServiceDir.mkdirs();
    customServiceDir.mkdirs();
    otherServiceDir.mkdirs();

    GradleTestUtil.copyFiles(new File("src/test/" + baseSourcePath),   baseServiceDir);
    GradleTestUtil.copyFiles(new File("src/test/" + customSourcePath), customServiceDir);
    GradleTestUtil.copyFiles(new File("src/test/" + otherSourcePath),  otherServiceDir);
}
 
Example 9
Source File: TestUtils.java    From swift-explorer with Apache License 2.0 5 votes vote down vote up
public static File getTestFile (TemporaryFolder tmpFolder, String fileName, long fileSize) throws IOException
{
    byte data [] = new byte[(int) fileSize] ;
    for (int i = 0 ; i < fileSize ; ++i)
    	data[i] = (byte)(Math.random() * 256) ;

    File file = tmpFolder.newFile(fileName) ;
    
	// generate test file
	FileOutputStream out = new FileOutputStream(file);
  	out.write(data);
	out.close();
    
    return file ;
}
 
Example 10
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFromSerializedList() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();

  NodeInterfacePair baseInterface1 = NodeInterfacePair.of("n1", "iface1");
  NodeInterfacePair baseInterface2 = NodeInterfacePair.of("n2", "iface2");

  // Write base serialized list
  List<NodeInterfacePair> interfaces = new ArrayList<>();
  interfaces.add(baseInterface1);
  interfaces.add(baseInterface2);
  writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces));

  removeFromSerializedList(
      serializedListPath,
      ImmutableList.of(baseInterface1),
      new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm only one interface shows up
  assertThat(
      BatfishObjectMapper.mapper()
          .readValue(
              CommonUtil.readFile(serializedListPath),
              new TypeReference<List<NodeInterfacePair>>() {}),
      contains(baseInterface2));
}
 
Example 11
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddToSerializedListNoListNoAddition() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();
  serializedList.delete();

  addToSerializedList(
      serializedListPath, ImmutableList.of(), new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm no file was created (since there was no list to begin with and nothing was added)
  assertThat(serializedList, not(FileMatchers.anExistingFile()));
}
 
Example 12
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddToSerializedList() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();

  NodeInterfacePair baseInterface = NodeInterfacePair.of("n1", "iface1");
  NodeInterfacePair additionalInterface = NodeInterfacePair.of("n2", "iface2");

  // Write base serialized list
  List<NodeInterfacePair> interfaces = new ArrayList<>();
  interfaces.add(baseInterface);
  writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces));

  addToSerializedList(
      serializedListPath,
      ImmutableList.of(additionalInterface),
      new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm the additional and original interfaces show up in the merged list
  assertThat(
      BatfishObjectMapper.mapper()
          .readValue(
              CommonUtil.readFile(serializedListPath),
              new TypeReference<List<NodeInterfacePair>>() {}),
      containsInAnyOrder(baseInterface, additionalInterface));
}
 
Example 13
Source File: MessageTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeWithFile() throws Exception {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();
	File source = folder.newFile();
	writeContentsToFile(source, testString);
	
	Message in = new Message(source);
	byte[] wire = serializationTester.serialize(in);
	writeContentsToFile(source, "fakeContentAsReplacementOfThePrevious");
	Message out = serializationTester.deserialize(wire);
	
	assertTrue(out.isBinary());
	assertEquals(testString,out.asString());
}
 
Example 14
Source File: CustomChecksSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testTSQLGrammarFiles() throws IOException {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();
	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM_RULES, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_MAX_FILE_SIZE, 100);
	String dirPath = "..\\grammars\\tsql";
	File dir = new File(dirPath);
	Collection<File> files = FileUtils.listFiles(dir, new String[] { "sql" }, true);
	for (File f : files) {
		String tempName = f.getName() + System.nanoTime();
		File dest = folder.newFile(tempName);
		FileUtils.copyFile(f, dest);

		DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
				.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
		ctxTester.fileSystem().add(file1);

	}
	CustomChecksSensor sensor = new CustomChecksSensor();
	sensor.execute(ctxTester);
	Collection<Issue> issues = ctxTester.allIssues();
	Assert.assertEquals(183, issues.size());

}
 
Example 15
Source File: GradleCommandLineTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void testJVMArgs2() throws IOException {
    TemporaryFolder root = new TemporaryFolder();
    root.create();
    File props = root.newFile("gradle.properties");
    Files.write(props.toPath(), Arrays.asList("org.gradle.jvmargs=\"-Dfile.encoding=UTF-8\" -Dsomething=\"space value\""));
    List<String> jvmargs = new ArrayList<>();
    GradleCommandLine.addGradleSettingJvmargs(root.getRoot(), jvmargs);
    assertEquals(Arrays.asList("-Dfile.encoding=UTF-8", "-Dsomething=space value"), jvmargs);
}
 
Example 16
Source File: WorkMgrTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFromSerializedListMissingBaseList() throws IOException {
  TemporaryFolder tmp = new TemporaryFolder();
  tmp.create();
  File serializedList = tmp.newFile();
  Path serializedListPath = serializedList.toPath();
  Files.delete(serializedListPath);

  // Remove nothing from a non-existent list
  removeFromSerializedList(
      serializedListPath, ImmutableList.of(), new TypeReference<List<NodeInterfacePair>>() {});

  // Confirm no issue if base list didn't exist
  assertThat(serializedList, not(anExistingFile()));
}
 
Example 17
Source File: TestUtils.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public static InferenceConfiguration getConfig(TemporaryFolder trainDir) throws Exception {
    Pair<MultiLayerNetwork, DataNormalization> multiLayerNetwork = TrainUtils.getTrainedNetwork();
    File modelSave = trainDir.newFile("model.zip");
    ModelSerializer.writeModel(multiLayerNetwork.getFirst(), modelSave, false);

    Schema.Builder schemaBuilder = new Schema.Builder();
    schemaBuilder.addColumnDouble("petal_length")
            .addColumnDouble("petal_width")
            .addColumnDouble("sepal_width")
            .addColumnDouble("sepal_height");
    Schema inputSchema = schemaBuilder.build();

    Schema.Builder outputSchemaBuilder = new Schema.Builder();
    outputSchemaBuilder.addColumnDouble("setosa");
    outputSchemaBuilder.addColumnDouble("versicolor");
    outputSchemaBuilder.addColumnDouble("virginica");
    Schema outputSchema = outputSchemaBuilder.build();

    ServingConfig servingConfig = ServingConfig.builder()
            .createLoggingEndpoints(true)
            .build();

    Dl4jStep modelPipelineStep = Dl4jStep.builder()
            .inputName("default")
            .inputColumnName("default", SchemaTypeUtils.columnNames(inputSchema))
            .inputSchema("default", SchemaTypeUtils.typesForSchema(inputSchema))
            .outputSchema("default", SchemaTypeUtils.typesForSchema(outputSchema))
            .path(modelSave.getAbsolutePath())
            .outputColumnName("default", SchemaTypeUtils.columnNames(outputSchema))
            .build();

    return InferenceConfiguration.builder()
            .servingConfig(servingConfig)
            .step(modelPipelineStep)
            .build();
}
 
Example 18
Source File: IsolatedPluginClassLoaderUtil.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
public static @NotNull IsolatedPluginClassloader buildClassLoader(
        final @NotNull TemporaryFolder temporaryFolder, final @NotNull Class[] classes) throws Exception {

    final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).addClasses(classes);

    final File jarFile = temporaryFolder.newFile();
    javaArchive.as(ZipExporter.class).exportTo(jarFile, true);

    return new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()},
            IsolatedPluginClassLoaderUtil.class.getClassLoader());
}
 
Example 19
Source File: SqliteDBStoreTest.java    From writelatex-git-bridge with MIT License 4 votes vote down vote up
@Before
public void setup() throws IOException {
    TemporaryFolder tmpFolder = new TemporaryFolder();
    tmpFolder.create();
    dbStore = new SqliteDBStore(tmpFolder.newFile("dbStore.db"));
}
 
Example 20
Source File: TestAuthorizerUtil.java    From hivemq-community-edition with Apache License 2.0 4 votes vote down vote up
@NotNull
public static SubscriptionAuthorizer getIsolatedSubscriptionAuthorizer(final TemporaryFolder temporaryFolder, @NotNull final ClassLoader classLoader) throws Exception {

    final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class)
            .addClass("util.TestAuthorizerUtil$TestSubscriptionAuthorizer");

    final File jarFile = temporaryFolder.newFile();
    javaArchive.as(ZipExporter.class).exportTo(jarFile, true);

    //This classloader contains the classes from the jar file
    final IsolatedPluginClassloader cl = new IsolatedPluginClassloader(new URL[]{jarFile.toURI().toURL()}, classLoader);

    final List<Authorizer> authorizers = new ArrayList<>();

    final Class<?> subscriptionAuthorizerClass = cl.loadClass("util.TestAuthorizerUtil$TestSubscriptionAuthorizer");

    final SubscriptionAuthorizer subscriptionAuthorizer = (SubscriptionAuthorizer) subscriptionAuthorizerClass.newInstance();

    return subscriptionAuthorizer;

}