下面列出了org.junit.rules.TemporaryFolder#newFile() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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());
}
@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));
}
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);
}
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);
}
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();
}
@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));
}
@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));
}
@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));
}
@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());
}
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());
}
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();
}
@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()));
}
@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);
}
@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());
}
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 ;
}
@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));
}
@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()));
}
@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));
}
@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;
}
@Before
public void setup() throws IOException {
TemporaryFolder tmpFolder = new TemporaryFolder();
tmpFolder.create();
dbStore = new SqliteDBStore(tmpFolder.newFile("dbStore.db"));
}