Re: Write to disk in Platform based JUnit tests

Johannes Wahle <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
Where do you get the FileObject from, if not from 
FileUtil.toFileObject() or one of the createData() methods - which use 
toFileObject() internally?

I just created a new Maven NetBeans Module with the following Test class 
and POM.
The test works for me.

TEST:
package nb.filetestmodule;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;
import static org.junit.Assert.*;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;

public class FileTest {

     @Test
     public void testFileUtil() throws IOException {
         Path p = Files.createTempDirectory("tempDir");
         File tempDir = p.toAbsolutePath().toFile();
         FileObject tempDirFo = FileUtil.toFileObject(tempDir);
         File data1 = new File(tempDir, "data1");
         FileObject data1Fo = FileUtil.createData(data1);
         FileObject data2Fo = FileUtil.createData(tempDirFo, "data2");
         checkFile(tempDirFo);
         checkFile(data1Fo);
         checkFile(data2Fo);
     }

     private void checkFile(FileObject data) {
         assertNotNull(data);
         URL url = data.toURL();
         assertTrue(url.getProtocol().equals("file"));
         System.out.println(url);
     }

}

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>nb</groupId>
     <artifactId>FileTestModule</artifactId>
     <version>1.0</version>
     <packaging>nbm</packaging>
     <build>
         <plugins>
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
                 <version>3.13</version>
                 <extensions>true</extensions>
             </plugin>
             <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
                 <version>2.5.1</version>
                 <configuration>
                     <source>1.6</source>
                     <target>1.6</target>
                 </configuration>
             </plugin>
             <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
                 <version>2.4</version>
                 <configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
                 </configuration>
             </plugin>
         </plugins>
     </build>
     <repositories>
         <repository>
             <id>netbeans</id>
             <name>Repository hosting NetBeans modules</name>
<url>http://bits.netbeans.org/nexus/content/groups/netbeans</url>
             <snapshots>
                 <enabled>false</enabled>
             </snapshots>
         </repository>
     </repositories>
     <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-core</artifactId>
             <version>1.3</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.netbeans.api</groupId>
<artifactId>org-openide-filesystems</artifactId>
             <version>RELEASE81</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-masterfs</artifactId>
             <version>RELEASE81</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
     <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
</project>


On 02.12.2015 07:50, mhaslinger wrote:
> As mentioned before
>
>> (have tried it without - makes no difference)
>
>
> I Need to use FileUtil.toFile(..) not toFileObject(...), if at all... the file is not existing before, I am creating it. But the main problem is already no matter what I tried to file will not be created at all on disk but on virtual nbfs. In the end I need a URL starting with "file://"
>
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.