public static void createFloppy(File f) throws Exception { GrubFatFormatter ff = new GrubFatFormatter(0, null, null); FileDevice newFd = new FileDevice(f, "rw"); newFd.setLength(1440 * 1024); ff.format(newFd); // newFd.start(); final FileSystemService fSS = InitialNaming.lookup(FileSystemService.NAME); FatFileSystemType type = fSS.getFileSystemType(FatFileSystemType.ID); FatFileSystem fs = new FatFileSystem(newFd, false, type); FSDirectory dir = fs.getRootEntry().getDirectory(); FSDirectory bDir = dir.addDirectory("boot").getDirectory(); FSDirectory bgDir = bDir.addDirectory("grub").getDirectory(); URLConnection urlConn = FatTest.class.getClassLoader().getResource("menu.lst").openConnection(); // byte[] buf = new byte[urlConn.getContentLength()]; ByteBuffer buf = ByteBuffer.allocate(urlConn.getContentLength()); FileUtils.copy(urlConn.getInputStream(), buf.array()); final FSFile fh1 = dir.addFile("test.lst").getFile(); fh1.setLength(urlConn.getContentLength()); fh1.write(0, buf); final FSFile fh2 = bgDir.addFile("menu.lst").getFile(); fh2.setLength(urlConn.getContentLength()); fh2.write(0, buf); fs.flush(); // newFd.stop(); newFd.close(); }
@Test public void testRemoveThenRemountFSAndGetEntry() throws Exception { if (!config.isReadOnly()) { setUp(); String filename = "a file to test.text"; FSDirectory rootDir = getFs().getRootEntry().getDirectory(); /*FSEntry entry =*/ rootDir.addFile(filename); FSEntry gotEntry = rootDir.getEntry(filename); assertNotNull("must contain the added file", gotEntry); assertEquals("returned bad entry", filename, gotEntry.getName()); rootDir.remove(filename); assertNull("must not contain the removed file", rootDir.getEntry(filename)); remountFS(config, config.isReadOnly()); FSDirectory rootDir2 = getFs().getRootEntry().getDirectory(); TestUtils.listEntries(rootDir2.iterator()); assertFalse("same ref (rootDir) after remount", rootDir == rootDir2); FSEntry gotEntry2 = rootDir2.getEntry(filename); assertNull("must not contain the removed file", gotEntry2); } }
@Test public void testAddFile() throws Exception { setUp(); FSDirectory rootDir = getFs().getRootEntry().getDirectory(); String fileName = "A new file.text"; log.debug("Root dir before testAddFile :"); TestUtils.listEntries(rootDir.iterator()); if (config.isReadOnly()) { try { rootDir.addFile(fileName); fail("addFile must fail in readOnly mode"); } catch (ReadOnlyFileSystemException e) { // success } assertContainsOnly("must be empty", rootDir.iterator(), getEmptyDirNames(config, true)); } else { try { FSEntry entry = rootDir.addFile(fileName); // success log.debug("added file entry=" + FSUtils.toString(entry, true)); } catch (ReadOnlyFileSystemException e) { fail("addFile must succeed in read/write mode"); } assertContainsOnly( "must contain " + fileName, rootDir.iterator(), TestUtils.append(getEmptyDirNames(config, true), new String[] {fileName})); FSEntry gotEntry = rootDir.getEntry(fileName); assertNotNull("must contain the added file", gotEntry); assertEquals("returned bad entry", fileName, gotEntry.getName()); } log.debug("Root dir after testAddFile :\n" + rootDir); TestUtils.listEntries(rootDir.iterator()); }