@Test public void createsOutputFilesWithDefaultEncodingReadFromMainTemplate() throws Exception { // prepare test final File templateFile = prepareArtefactDirectory("main.tpl", GENERATOR_PLUGIN_ID, "myNewArtefact"); MOGLiFileUtil.createNewFileWithContent( templateFile, "@CreateNew true" + FileUtil.getSystemLineSeparator() + "@TargetFileName UmlautTest.txt" + FileUtil.getSystemLineSeparator() + "@TargetDir " + MOGLiSystemConstants.APPLICATION_ROOT_IDENTIFIER + "/example" + FileUtil.getSystemLineSeparator() + "äüößÜÖÄ"); // call functionality under test executeMogliApplication(); // verify test result final File outputFile = new File(applicationOutputDir, GENERATOR_PLUGIN_ID + "/myNewArtefact/UmlautTest.txt"); assertStringEquals("outputFileContent", "äüößÜÖÄ", MOGLiFileUtil.getFileContent(outputFile)); }
@Test public void doesNotOverwriteExistingFilesInTargetDirAndcreatesCorrespondingGenerationReport() throws Exception { // prepare test final String artefactName = "testArtefact"; final File artefactDir = createTestArtefact(artefactName); final String targetFileName = "targetFile.txt"; final File targetDir = new File(applicationRootDir, artefactName); FileUtil.deleteDirWithContent(targetDir); targetDir.mkdirs(); final File targetFile = new File(targetDir, targetFileName); targetFile.createNewFile(); final VelocityGeneratorResultData resultData = buildVelocityGeneratorResultData(targetFileName, artefactName, "TargetFileContent", false); prepareResultData(resultData); // call functionality under test velocityClassBasedGenerator.doYourJob(); // cleanup FileUtil.deleteDirWithContent(artefactDir); // verify test result final String generationReport = velocityClassBasedGenerator.getGeneratorReport(); assertTrue( "unexpected generation result", generationReport.contains( "targetFile.txt did already exist and was NOT overwritten in testArtefact")); }
private List<String> readAllClassnamesFromModelfile() throws IOException { final HashMap<String, String> variableMap = new HashMap<String, String>(); final File modelDir = new File(testDir + "/" + DIR_INPUT_FILES + "/" + StandardModelProviderStarter.PLUGIN_ID); List<String> fileContentAsList = FileUtil.getFileContentAsList( new File(modelDir, StandardModelProviderStarter.FILENAME_STANDARD_MODEL_FILE)); final List<String> list = new ArrayList<String>(); for (String line : fileContentAsList) { line = line.trim(); if (line.startsWith(MetaModelConstants.CLASS_IDENTIFIER)) { final String[] splitResult = line.split(" "); if (splitResult.length < 2) { throw new MOGLiCoreException("Error reading model file"); } final String className = doVariableReplacement(splitResult[1], variableMap); final ClassNameData classNameData = new ClassNameData(className); list.add(classNameData.getSimpleClassName()); } if (line.startsWith(MetaModelConstants.VARIABLE_IDENTIFIER)) { final String[] splitResult = line.split(" "); if (splitResult.length < 3) { throw new MOGLiCoreException("Error reading model file"); } variableMap.put(splitResult[1], splitResult[2]); } } return list; }
@Override public void setup() { super.setup(); FileUtil.deleteDirWithContent(applicationRootDir); initForFirstUnitTest(); initPluginLibSubdir(); createMogliLogFile(); MOGLiLogUtil.setCoreLogfile(applicationLogfile); }
@Test public void unpacksInputDefaultData() throws MOGLiPluginException { // prepare test FileUtil.deleteDirWithContent(applicationInputDir); // call functionality under test velocityClassBasedGenerator.unpackDefaultInputData(); // verify test result assertFileExists(applicationInputDir); }
@Test public void returnsMetaInfoValidatorList() throws MOGLiPluginException { // prepare test final File conditionFile = new File( velocityClassBasedGenerator.getInfrastructure().getPluginInputDir(), "condition.txt"); MOGLiFileUtil.createNewFileWithContent( conditionFile, "|if MetaInfo| The \"other\" MetaInfo Name1 |exists.|" + FileUtil.getSystemLineSeparator() + "|if MetaInfo| The \"other\" MetaInfo Name2 |does not exist.|" + FileUtil.getSystemLineSeparator() + "|if MetaInfo| The \"other\" MetaInfo Name3 |with value| my value |does not exist.|"); final File validatorFile = new File( velocityClassBasedGenerator.getInfrastructure().getPluginInputDir(), MetaInfoValidationUtil.FILENAME_VALIDATION); MOGLiFileUtil.createNewFileWithContent( validatorFile, "|MetaInfo| MetaInfoTestName1 |is| optional |for| attributes |in| ModelName |.|" + FileUtil.getSystemLineSeparator() + "|MetaInfo| MetaInfoTestName2 |is| mandatory |for| attributes |in| ModelName |.|" + FileUtil.getSystemLineSeparator() + "|MetaInfo| MetaInfoTestName3 |is valid to occur| 0-4 |time(s) for| attributes |in| ModelName |.|" + FileUtil.getSystemLineSeparator() + "|MetaInfo| MetaInfoTestName4 |is valid to occur| 1-2 |time(s) for| attributes |in| ModelName |if| condition.txt |is true.|"); // call functionality under test final List<MetaInfoValidator> metaInfoValidatorList = velocityClassBasedGenerator.getMetaInfoValidatorList(); // verify test result assertEquals("validator number", 4, metaInfoValidatorList.size()); assertEquals( "condition number", 3, ((ConditionalMetaInfoValidator) metaInfoValidatorList.get(3)).getTotalNumberOfConditions()); }
@Test public void createsGeneratorResultFiles() throws IOException { // prepare test FileUtil.deleteDirWithContent(applicationOutputDir); assertFileDoesNotExist(applicationOutputDir); // call functionality under test executeMogliApplication(); // verify test result assertFileExists(applicationOutputDir); assertAllResultFileCreated(); }
@Test public void createsPluginLogFile() { // prepare test FileUtil.deleteDirWithContent(applicationLogDir); final File pluginLogFile = new File(applicationLogDir, GENERATOR_PLUGIN_ID + ".log"); assertFileDoesNotExist(pluginLogFile); // call functionality under test executeMogliApplication(); // verify test result assertFileExists(pluginLogFile); }
@Test public void createsHelpData() { // prepare test final File pluginHelpDir = new File(applicationHelpDir, GENERATOR_PLUGIN_ID); FileUtil.deleteDirWithContent(applicationHelpDir); assertFileDoesNotExist(pluginHelpDir); // call functionality under test executeMogliApplication(); // verify test result assertFileExists(pluginHelpDir); assertChildrenNumberInDirectory(pluginHelpDir, 1); }
protected void activatePluginsForTest(String... pluginNames) { final StringBuffer sb = new StringBuffer(); for (int i = 0; i < pluginNames.length; i++) { sb.append( pluginNames[i] + "=" + MOGLiTextConstants.TEXT_ACTIVATED_PLUGIN_PROPERTY + FileUtil.getSystemLineSeparator()); } try { initWorkspacePropertiesWith(sb.toString()); } catch (Exception e) { throw new MOGLiCoreException(e); } }
@Test public void doesNotOverwriteExistingTargetFile() throws Exception { // prepare test final String targetDir = applicationRootDir.getAbsolutePath() + "/example"; final String targetFileName = "targetFile.txt"; final VelocityGeneratorResultData resultData = buildVelocityGeneratorResultData(targetFileName, targetDir, "ContentToInsert", false); prepareResultData(resultData); final File targetFile = new File(targetDir, targetFileName); targetFile.getParentFile().mkdirs(); FileUtil.createNewFileWithContent(targetFile, "Test"); assertFileContainsEntry(targetFile, "Test"); velocityClassBasedGenerator.setTestDir(null); // call functionality under test velocityClassBasedGenerator.doYourJob(); // verify test result assertFileContainsEntry(targetFile, "Test"); }
@Test public void throwsExceptionIfMainTemplateIsNotFound() { // prepare test final File artefactDir = new File( infrastructure.getPluginInputDir(), VelocityClassBasedFileMakerStarter.ARTEFACT_JAVABEAN); final File mainTemplateFile = new File(artefactDir, MAIN_TEMPLATE); mainTemplateFile.delete(); // call functionality under test try { velocityClassBasedGenerator.findMainTemplate(artefactDir); fail("Expected exception not thrown!"); } catch (MOGLiPluginException e) { assertStringContains(e.getMessage(), TemplateUtil.NO_MAIN_TEMPLATE_FOUND); // cleanup FileUtil.deleteDirWithContent(generatorPluginInputDir); } }
@Test public void createsTargetFileWithRootDirDefinedInTemplateFile() throws MOGLiPluginException { // prepare test final String artefactName = "testArtefact"; createTestArtefact(artefactName); final String targetDir = applicationRootDir.getAbsolutePath() + "/temp"; final String targetFileName = "targetFile.txt"; final File targetFile = new File(targetDir, targetFileName); FileUtil.deleteDirWithContent(targetDir); assertFileDoesNotExist(targetFile); targetFile.getParentFile().mkdirs(); final VelocityGeneratorResultData resultData = buildVelocityGeneratorResultData(targetFileName, "temp", "Content", false); prepareResultData(resultData); // call functionality under test velocityClassBasedGenerator.doYourJob(); // verify test result assertFileExists(targetFile); }
@Test public void createsNotExistingTargetDirWithCreateNewInstructions() throws MOGLiPluginException { // prepare test final String artefactName = "testArtefact"; createTestArtefact(artefactName); final String targetDir = PROJECT_ROOT_DIR + TEST_SUBDIR + "/example"; final String targetFileName = "targetFile.txt"; final VelocityGeneratorResultData resultData = buildVelocityGeneratorResultData(targetFileName, targetDir, "Content", true); prepareResultData(resultData); final File dir = new File(targetDir); FileUtil.deleteDirWithContent(dir); assertFileDoesNotExist(dir); velocityClassBasedGenerator.setTestDir(null); // call functionality under test velocityClassBasedGenerator.doYourJob(); // verify test result assertFileExists(dir); }
@Test public void throwsExceptionIfConditionFileWasNotFound() throws MOGLiPluginException { // prepare test final File validatorFile = new File( velocityClassBasedGenerator.getInfrastructure().getPluginInputDir(), MetaInfoValidationUtil.FILENAME_VALIDATION); MOGLiFileUtil.createNewFileWithContent( validatorFile, "|MetaInfo| MetaInfoTestName1 |is| optional |for| attributes |in| ModelName |.|" + FileUtil.getSystemLineSeparator() + "|MetaInfo| MetaInfoTestName4 |is valid to occur| 1-2 |time(s) for| attributes |in| ModelName |if| notExistingConditionFile.txt |is true.|"); // call functionality under test try { velocityClassBasedGenerator.getMetaInfoValidatorList(); fail("Expected exception not thrown!"); } catch (MOGLiPluginException e) { assertStringContains(e.getMessage(), "Expected condition file does not exist: "); assertStringContains(e.getMessage(), "notExistingConditionFile.txt"); } }
@Test public void usesMetaInfoSupportMethod_DoesHaveAnyMetaInfosWithName() throws MOGLiPluginException, IOException { // prepare test final VelocityEngineData engineData = MockDataBuilder.buildVelocityEngineDataMock( buildModel(), velocityClassBasedFileMakerStarter.getId(), "artefact", "DoesHaveAnyMetaInfosWithName_TestTemplate.tpl", new File(getProjectTestResourcesDir())); velocityEngineProviderStarter.setEngineData(engineData); // call functionality under test final GeneratorResultData resultData = velocityEngineProviderStarter.startEngineWithModel(); // verify test result final File expectedFile = new File(getProjectTestResourcesDir(), "DoesHaveAnyMetaInfosWithName_expectedResult.txt"); final String expected = FileUtil.getFileContent(expectedFile); assertEquals("Generated Content", expected.trim(), resultData.getGeneratedContent().trim()); }