package _VisualDVM.Passes.All; import Common.Passes.Pass; import Common.Passes.PassException; import Common.Utils.Utils_; import _VisualDVM.Global; import _VisualDVM.ProjectData.Files.FileType; import _VisualDVM.TestingSystem.Common.Group.Group; import _VisualDVM.TestingSystem.Common.Test.Test; import _VisualDVM.TestingSystem.Common.TestFile.TestFile; import _VisualDVM.Utils; import org.apache.commons.io.FileUtils; import java.io.File; import java.util.Vector; public class CreateTestFromFile extends Pass { //---- Group group; TestFile testFile; File file; //---- @Override protected boolean needsAnimation() { return true; } @Override public String getIconPath() { return "/icons/AddFile.png"; } @Override public String getButtonText() { return ""; } @Override protected boolean canStart(Object... args) throws Exception { testFile = null; file = (File) args[0]; group = (Group) args[1]; //-- if (Utils_.ContainsCyrillic(file.getName()) || Utils_.ContainsForbiddenName(file.getName())) { Log.Writeln_("Имя файла " + Utils_.Brackets(file.getName()) + " содержит запрещённые символы " + Utils_.printAllForbiddenCharacters() + ", или кириллицу."); return false; } //-- testFile = new TestFile(file); if (!testFile.fileType.equals(FileType.program) || !testFile.languageName.equals(group.language)) { Log.Writeln_("Не удалось распознать файл " + Utils_.Brackets(file.getName()) + " как программу на языке " + group.language.getDescription()); return false; } //-- target = new Test(); target.sender_address = Global.mainModule.getAccount().email; target.sender_name = Global.mainModule.getAccount().name; target.group_id = group.id; target.description = Utils_.getNameWithoutExtension(file.getName()); target.files = new Vector<>(); target.files.add(testFile); //-- return true; } public File packTestCode() throws Exception { target.temp_project_name = Utils_.getDateName("test"); //- File tempProject = target.getTempProject(); File tempArchive = target.getTempArchive(); //- создать бд. FileUtils.forceMkdir(tempProject); //-- File dst = new File(tempProject, testFile.name); FileUtils.copyFile(file, dst); //--- Utils.ClearProjectData(tempProject); //-- ZipFolderPass zip = new ZipFolderPass(); if (zip.Do(tempProject.getAbsolutePath(), tempArchive.getAbsolutePath())) { target.project_archive_bytes = Utils_.fileToBytes(tempArchive); } else throw new PassException("Не удалось создать архив папки с кодом."); return tempProject; } @Override protected void body() throws Exception { ShowMessage1(testFile.name); packTestCode(); } }