Java Code Examples for org.apache.commons.io.FileUtils#cleanDirectory()

The following examples show how to use org.apache.commons.io.FileUtils#cleanDirectory() . 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: DumpData.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private <T> void dump(Class<T> cls, EntityManager em) throws Exception {
	/** 创建最终存储文件的目录 */
	File directory = new File(dir, cls.getName());
	FileUtils.forceMkdir(directory);
	FileUtils.cleanDirectory(directory);
	int count = 0;
	int size = Config.dumpRestoreData().getBatchSize();
	String id = "";
	List<T> list = null;
	do {
		list = this.list(em, cls, id, size);
		if (ListTools.isNotEmpty(list)) {
			count = count + list.size();
			id = BeanUtils.getProperty(list.get(list.size() - 1), JpaObject.id_FIELDNAME);
			File file = new File(directory, count + ".json");
			FileUtils.write(file, pureGsonDateFormated.toJson(list), DefaultCharset.charset);
		}
		em.clear();
		Runtime.getRuntime().gc();
	} while (ListTools.isNotEmpty(list));
	this.catalog.put(cls.getName(), count);
}
 
Example 2
Source File: RunnerImplTest.java    From stagen with Apache License 2.0 6 votes vote down vote up
/**
 * Test of run method, of class RunnerImpl.
 */
@Test
public void testRun() throws Exception {
    System.out.println("run");
    File tmplDir = new File("src/test/resources");
    File baseDir = new File("build/test-site");
    baseDir.mkdirs();
    FileUtils.cleanDirectory(baseDir);
    Util.copyDirectory(tmplDir.toPath(), baseDir.toPath());

    Runner instance = new RunnerGen();
    instance.run(baseDir);

    if(!Constants.getOutDir(baseDir).exists()) {
        fail("Out dir does not exist.");
    }
}
 
Example 3
Source File: GitHubSCMProbeTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    // Clear all caches before each test
    File cacheBaseDir = new File(j.jenkins.getRootDir(),
        GitHubSCMProbe.class.getName() + ".cache");
    if (cacheBaseDir.exists()) {
        FileUtils.cleanDirectory(cacheBaseDir);
    }

    githubApi.stubFor(
        get(urlEqualTo("/repos/cloudbeers/yolo"))
            .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBodyFile("body-cloudbeers-yolo-PucD6.json"))
    );
}
 
Example 4
Source File: StarTreeIndexCombiner.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Combines the index files inside the given directory into the single index file, then cleans the directory.
 */
public Map<IndexKey, IndexValue> combine(StarTreeV2BuilderConfig builderConfig, File starTreeIndexDir)
    throws IOException {
  Map<IndexKey, IndexValue> indexMap = new HashMap<>();

  // Write star-tree index
  File starTreeIndexFile = new File(starTreeIndexDir, STAR_TREE_INDEX_FILE_NAME);
  indexMap.put(STAR_TREE_INDEX_KEY, writeFile(starTreeIndexFile));

  // Write dimension indexes
  for (String dimension : builderConfig.getDimensionsSplitOrder()) {
    File dimensionIndexFile = new File(starTreeIndexDir, dimension + UNSORTED_SV_FORWARD_INDEX_FILE_EXTENSION);
    indexMap.put(new IndexKey(IndexType.FORWARD_INDEX, dimension), writeFile(dimensionIndexFile));
  }

  // Write metric (function-column pair) indexes
  for (AggregationFunctionColumnPair functionColumnPair : builderConfig.getFunctionColumnPairs()) {
    String metric = functionColumnPair.toColumnName();
    File metricIndexFile = new File(starTreeIndexDir, metric + RAW_SV_FORWARD_INDEX_FILE_EXTENSION);
    indexMap.put(new IndexKey(IndexType.FORWARD_INDEX, metric), writeFile(metricIndexFile));
  }

  FileUtils.cleanDirectory(starTreeIndexDir);
  return indexMap;
}
 
Example 5
Source File: TestUnnecessaryBlockingOnHistoryFileInfo.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
  if(USER_DIR.exists()) {
    FileUtils.cleanDirectory(USER_DIR);
  }
  USER_DIR.mkdirs();
}
 
Example 6
Source File: InputFileTest.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initDir() throws IOException {
  if (!TEST_DIR.exists()) {
    TEST_DIR.mkdir();
  }
  FileUtils.cleanDirectory(TEST_DIR);
}
 
Example 7
Source File: AbstractDataBridgeTest.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Uploads and registers a version if of the test business object data that will be used as a parent.
 *
 * @param s3KeyPrefix the destination S3 key prefix that must comply with the S3 naming conventions including the expected data version value
 * @param dataBridgeWebClient the databridge web client instance
 */
protected void uploadAndRegisterTestDataParent(String s3KeyPrefix, DataBridgeWebClient dataBridgeWebClient) throws Exception
{
    uploadTestDataFilesToS3(s3KeyPrefix, testManifestFiles, new ArrayList<String>());
    UploaderInputManifestDto uploaderInputManifestDto = getTestUploaderInputManifestDto(TEST_PARENT_PARTITION_VALUE, TEST_SUB_PARTITION_VALUES, false);
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = getTestS3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix + "/");
    BusinessObjectData businessObjectData =
        dataBridgeWebClient.preRegisterBusinessObjectData(uploaderInputManifestDto, StorageEntity.MANAGED_STORAGE, true);
    BusinessObjectDataKey businessObjectDataKey = businessObjectDataHelper.getBusinessObjectDataKey(businessObjectData);
    dataBridgeWebClient.addStorageFiles(businessObjectDataKey, uploaderInputManifestDto, s3FileTransferRequestParamsDto, StorageEntity.MANAGED_STORAGE);
    dataBridgeWebClient.updateBusinessObjectDataStatus(businessObjectDataKey, BusinessObjectDataStatusEntity.VALID);
    // Clean up the local input directory used for the test data files upload.
    FileUtils.cleanDirectory(LOCAL_TEMP_PATH_INPUT.toFile());
}
 
Example 8
Source File: ExternalTableIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testPureEmptyDirectory() throws  Exception{
    String tablePath = getExternalResourceDirectory()+"pure_empty_directory";
    File path =  new File(tablePath);
    path.mkdir();
    methodWatcher.executeUpdate(String.format("create external table pure_empty_directory (col1 varchar(24), col2 varchar(24), col3 varchar(24))" +
                " STORED AS PARQUET LOCATION '%s'",tablePath));
    FileUtils.cleanDirectory(path);

    ResultSet rs = methodWatcher.executeQuery("select * from pure_empty_directory");
    String actual = TestUtils.FormattedResult.ResultFactory.toString(rs);
    String expected = "";
    Assert.assertEquals(actual, expected, actual);
}
 
Example 9
Source File: AbstractTestBase.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
/**
 * clear directory
 *
 * @throws IOException
 */
public static void cleanLogDirectory() throws IOException {
    if (!logDirectory.exists()) {
        return;
    }
    FileUtils.cleanDirectory(logDirectory);
}
 
Example 10
Source File: GFileUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void cleanDirectory(File directory) {
    try {
        FileUtils.cleanDirectory(directory);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 11
Source File: RegisterPackageServletIT.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeAll() throws Exception
{

  if ( getBaseTempDir().exists() ) {
    FileUtils.cleanDirectory(getBaseTempDir());
  }

}
 
Example 12
Source File: TestFSPackageRegistry.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    if (DIR_REGISTRY_HOME.exists()) {
        FileUtils.cleanDirectory(DIR_REGISTRY_HOME);
    } else {
        DIR_REGISTRY_HOME.mkdir();
    }
    
    getFreshRegistry();
}
 
Example 13
Source File: ClientGenerator.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
public static void makeCleanDir(String dirPath) throws IOException {
  File dir = new File(dirPath);
  if (dir.exists()) {
    FileUtils.cleanDirectory(dir);
  } else {
    dir.mkdirs();
  }
}
 
Example 14
Source File: TestSlaveStandaloneRunner.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws Exception {
  TestUtil.EMPTY_OFFSET = false;
  File dataDir = new File(System.getProperty(RuntimeModule.SDC_PROPERTY_PREFIX + RuntimeInfo.DATA_DIR));
  try {
    FileUtils.cleanDirectory(dataDir);
  } catch (IOException e) {
    // ignore
  }
  Assert.assertTrue(dataDir.isDirectory());
}
 
Example 15
Source File: CachedContentCleanupJobTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws IOException
{
    cachingStore = (CachingContentStore) ctx.getBean("cachingContentStore");
    cache = (ContentCacheImpl) ctx.getBean("contentCache");
    cacheRoot = cache.getCacheRoot();
    cleaner = (CachedContentCleaner) ctx.getBean("cachedContentCleaner");
    cleaner.setMinFileAgeMillis(0);
    cleaner.setMaxDeleteWatchCount(0);

    // Clear the cache from disk and memory
    cache.removeAll();
    FileUtils.cleanDirectory(cacheRoot);
}
 
Example 16
Source File: ClientOptionsE2ETest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * rmdir normdir  Makes 'p4 sync' attempt to delete a client
 * directory when all files are removed.
 */
@Test
public void testRmdir() {
	try {
		debugPrintTestName();
		
		//get the default options
		ClientOptions tClient = new ClientOptions();
		assertFalse("Default setting for isRmdir() should be false.", tClient.isRmdir());
		tClient.setRmdir(true);
		assertTrue("Setting for isRmdir() should be true.", tClient.isRmdir());
		String clientRoot = client.getRoot();
		assertNotNull("clientRoot should not be Null.", clientRoot);
		
		//create the files
		String newFile = clientRoot + File.separator + clientDir + File.separator + "RMDIRTEST" + File.separator + "testfileCO.txt";
		String newFilePath = clientRoot + File.separator + clientDir + File.separator + "RMDIRTEST";
		File parentDir = new File(newFilePath);
		SysFileHelperBridge.getSysFileCommands().setWritable(parentDir.getCanonicalPath(), true);
           if (parentDir.exists() && parentDir.isDirectory()) {
               FileUtils.cleanDirectory(parentDir);
           }
		
		File file1 = new File(newFilePath + File.separator + prepareTestFile(sourceFile, newFile, true));	
		File file2 = new File(newFilePath + File.separator + prepareTestFile(sourceFile, newFile, true));	
		File file3 = new File(newFilePath + File.separator + prepareTestFile(sourceFile, newFile, true));	
		
		//create the filelist
		final String[] filePaths = {
				file1.getAbsolutePath(),
				file2.getAbsolutePath(),
				file3.getAbsolutePath(),
		};

		//add the files, submit them, reopen them
		IChangelist changelistImpl  = getNewChangelist(server, client, 
				"Changelist to submit files for " + getName());		
		IChangelist changelist = client.createChangelist(changelistImpl);
		
		List<IFileSpec> fileSpecs = FileSpecBuilder.makeFileSpecList(filePaths);
		assertNotNull("FileSpecs should not be Null.", fileSpecs);
		List<IFileSpec> addedFileSpecs = client.addFiles(fileSpecs, false, 0, P4JTEST_FILETYPE_TEXT, false);
		assertEquals("Number built & added fileSpecs should be equal.", fileSpecs.size(), addedFileSpecs.size());
		
		//submit files. Check if added files are in the correct changelist.
		List<IFileSpec> reopenedFileSpecs = client.reopenFiles(fileSpecs, changelist.getId(), P4JTEST_FILETYPE_TEXT);			
		List<IFileSpec> submittedFileSpecs = changelist.submit(false);
		int numSubmitted = FileSpecBuilder.getValidFileSpecs(submittedFileSpecs).size(); 			
		assertEquals("numSubmitted should equal number of files created.", filePaths.length, numSubmitted);
		
		List<IFileSpec> deletedFileSpecs = client.deleteFiles(fileSpecs, 0, false);
		int numDeleted = FileSpecBuilder.getValidFileSpecs(deletedFileSpecs).size(); 
		assertEquals("numSubmitted should equal number of files created.", filePaths.length, numDeleted);
		//submit the deleted files.
		changelistImpl  = getNewChangelist(server, client, 
				"Changelist to submit files for " + getName());		
		changelist = client.createChangelist(changelistImpl);
		reopenedFileSpecs.clear();
		reopenedFileSpecs = client.reopenFiles(fileSpecs, changelist.getId(), P4JTEST_FILETYPE_TEXT);
		assertEquals("Number built & reopened fileSpecs should be equal.", fileSpecs.size(), reopenedFileSpecs.size());
		
		submittedFileSpecs.clear();
		submittedFileSpecs = changelist.submit(false);
		
		List<IFileSpec> syncFileSpecs = client.sync(fileSpecs, true, false, false, false);
		dumpFileSpecInfo(syncFileSpecs, "Sync'ed files:");

		assertTrue(parentDir.listFiles() == null || parentDir.listFiles().length == 0);

		tClient.setRmdir(false);
		assertFalse("Setting for isRmdir() should be false.", tClient.isRmdir());

	} catch (Exception exc){
		fail("Unexpected Exception: " + exc + " - " + exc.getLocalizedMessage());
	}
}
 
Example 17
Source File: JettySeverTools.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
protected static void cleanDirectory(File dir) throws Exception {
	FileUtils.forceMkdir(dir);
	FileUtils.cleanDirectory(dir);
}
 
Example 18
Source File: DumpData.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
public boolean execute(String path) throws Exception {
	if (StringUtils.isEmpty(path)) {
		this.dir = new File(Config.base(), "local/dump/dumpData_" + DateTools.compact(this.start));
	} else {
		this.dir = new File(path);
		if (dir.getAbsolutePath().startsWith(Config.base())) {
			logger.print("path can not in base directory.");
			return false;
		}
	}
	FileUtils.forceMkdir(this.dir);
	FileUtils.cleanDirectory(this.dir);
	this.catalog = new DumpRestoreDataCatalog();

	/* 初始化完成 */

	List<String> containerEntityNames = new ArrayList<>();
	containerEntityNames.addAll((List<String>) Config.resource(Config.RESOURCE_CONTAINERENTITYNAMES));
	List<String> classNames = ListTools.includesExcludesWildcard(containerEntityNames,
			Config.dumpRestoreData().getIncludes(), Config.dumpRestoreData().getExcludes());
	logger.print("dump data find {} data to dump, start at {}.", classNames.size(), DateTools.format(start));
	File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
	PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
	for (int i = 0; i < classNames.size(); i++) {
		Class<JpaObject> cls = (Class<JpaObject>) Class.forName(classNames.get(i));
			EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
					persistence.getName(),
					PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
			EntityManager em = emf.createEntityManager();
			try {
				logger.print("dump data({}/{}): {}, count: {}.", (i + 1), classNames.size(), cls.getName(),
						this.estimateCount(em, cls));
				this.dump(cls, em);
			} finally {
				em.close();
				emf.close();
			}
	}
	FileUtils.write(new File(dir, "catalog.json"), pureGsonDateFormated.toJson(this.catalog),
			DefaultCharset.charset);
	logger.print("dump data completed, directory: {}, count: {}, elapsed: {} minutes.", dir.getAbsolutePath(),
			this.count(), (System.currentTimeMillis() - start.getTime()) / 1000 / 60);
	return true;
}
 
Example 19
Source File: DumpStorage.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
public boolean execute(String path) throws Exception {
	if (StringUtils.isEmpty(path)) {
		this.dir = new File(Config.base(), "local/dump/dumpStorage_" + DateTools.compact(this.start));
	} else {
		this.dir = new File(path);
		if (dir.getAbsolutePath().startsWith(Config.base())) {
			logger.print("path can not in base directory.");
			return false;
		}
	}
	FileUtils.forceMkdir(this.dir);
	FileUtils.cleanDirectory(this.dir);
	this.catalog = new DumpRestoreStorageCatalog();

	List<String> storageContainerEntityNames = new ArrayList<>();
	storageContainerEntityNames.addAll((List<String>) Config.resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES));
	List<String> classNames = ListTools.includesExcludesWildcard(storageContainerEntityNames,
			Config.dumpRestoreStorage().getIncludes(), Config.dumpRestoreStorage().getExcludes());
	logger.print("dump storage find {} data to dump, start at {}.", classNames.size(), DateTools.format(start));
	StorageMappings storageMappings = Config.storageMappings();
	File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
	PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
	for (int i = 0; i < classNames.size(); i++) {
		Class<StorageObject> cls = (Class<StorageObject>) Class.forName(classNames.get(i));
		EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
				persistence.getName(), PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
		EntityManager em = emf.createEntityManager();
		try {
			logger.print("dump storage({}/{}): {}, estimate count: {}, estimate size: {}M.", (i + 1),
					classNames.size(), cls.getName(), this.estimateCount(em, cls),
					(this.estimateSize(em, cls) / 1024 / 1024));
			this.dump(cls, em, storageMappings);
		} finally {
			em.close();
			emf.close();
		}
	}
	FileUtils.write(new File(dir, "catalog.json"), XGsonBuilder.instance().toJson(this.catalog),
			DefaultCharset.charset);
	logger.print(
			"dump storage completed, directory: {}, count: {}, normal: {}, empty: {}, invalidStorage: {}, size: {}M, elapsed: {} minutes.",
			dir.getAbsolutePath(), this.count(), this.normal(), this.empty(), this.invalidStorage(),
			(this.size() / 1024 / 1024), (System.currentTimeMillis() - start.getTime()) / 1000 / 60);
	return true;
}
 
Example 20
Source File: GraphReceivePackTest.java    From p4ic4idea with Apache License 2.0 2 votes vote down vote up
/**
 * Cleans up the expanded git repo at the end of the tests
 *
 * @throws IOException
 */
@AfterClass
public static void afterClass() throws IOException {
	FileUtils.cleanDirectory(new File(PACK_FILE_PATH + "unpacked/scm-api-plugin.git"));
}