/***************************************************************************
* Copyright (c) 2012-2015 VMware, Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
package com.vmware.bdd.utils;
import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.vmware.bdd.entity.Users;
public class TestFileUtils {
private static File file;
private static final String TestFile = "test.txt";
@BeforeClass
public static void createFile() {
file = new File(TestFileUtils.TestFile);
try {
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
}
}
@AfterClass
public static void deleteFile() {
if (file.exists()) {
file.delete();
}
}
@Test
public void testGetConfigFile() {
File testFile =
FileUtils.getConfigurationFile(TestFileUtils.TestFile, "Test file");
Assert.assertNotNull(testFile);
assertEquals(testFile.getName(), TestFileUtils.TestFile);
}
public static void createXMLFile(Users users, File output) throws JAXBException {
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(Users.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(users, output);
}
public static void deleteXMLFile(File output) {
if (output != null) {
output.delete();
}
}
}