|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2015-2019 TU Darmstadt, Paderborn University |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | + |
| 11 | +package de.cognicrypt.codegenerator.generator.test; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import org.eclipse.core.resources.IResource; |
| 18 | +import org.eclipse.core.runtime.CoreException; |
| 19 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 20 | +import org.eclipse.jdt.core.IJavaProject; |
| 21 | +import org.eclipse.jdt.core.JavaModelException; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import de.cognicrypt.codegenerator.generator.CodeGenerator; |
| 26 | +import de.cognicrypt.codegenerator.generator.CrySLBasedCodeGenerator; |
| 27 | +import de.cognicrypt.codegenerator.tasks.Task; |
| 28 | +import de.cognicrypt.codegenerator.testutilities.TestUtils; |
| 29 | +import de.cognicrypt.codegenerator.wizard.Configuration; |
| 30 | +import de.cognicrypt.core.Constants; |
| 31 | +import de.cognicrypt.utils.DeveloperProject; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author André Sonntag |
| 35 | + */ |
| 36 | +public class GenerationTest { |
| 37 | + |
| 38 | + /** |
| 39 | + * In the following tests we check for the right number of methods in the appropriate classes. We choose this approach, because a comparing of the source code/bytes leads to |
| 40 | + * problems when some changes happen in the XSLTemplate. |
| 41 | + */ |
| 42 | + |
| 43 | + Logger log = Logger.getLogger(GenerationTest.class.getName()); |
| 44 | + IJavaProject testJavaProject; |
| 45 | + CodeGenerator generatorEnc; |
| 46 | + CodeGenerator generatorSecPassword; |
| 47 | + Task encTask; |
| 48 | + Task secPasswordTask; |
| 49 | + Configuration configEnc; |
| 50 | + Configuration configSecPassword; |
| 51 | + DeveloperProject developerProject; |
| 52 | + static int counter = 0; |
| 53 | + IResource targetFile; |
| 54 | + |
| 55 | + @After |
| 56 | + public void tearDown() throws CoreException { |
| 57 | + TestUtils.deleteProject(this.testJavaProject.getProject()); |
| 58 | + } |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() throws Exception { |
| 62 | + GenerationTest.counter++; |
| 63 | + this.testJavaProject = TestUtils.createJavaProject("TestProject_" + counter); |
| 64 | + targetFile = TestUtils.generateJavaClassInJavaProject(this.testJavaProject, "testPackage", "Test"); |
| 65 | + this.encTask = TestUtils.getTask("Encryption"); |
| 66 | + this.generatorEnc = new CrySLBasedCodeGenerator(targetFile); |
| 67 | + this.secPasswordTask = TestUtils.getTask("SecurePassword"); |
| 68 | + this.generatorSecPassword = new CrySLBasedCodeGenerator(targetFile); |
| 69 | + this.developerProject = this.generatorEnc.getDeveloperProject(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test if the codegeneration for SymmetricEncrytion works, without any open class. |
| 74 | + * @throws IOException |
| 75 | + * @throws CoreException |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void testCodeGeneration() throws CoreException, IOException { |
| 79 | + this.configEnc = TestUtils.createCrySLConfiguration("encryption", targetFile, generatorEnc, developerProject); |
| 80 | + final boolean encCheck = this.generatorEnc.generateCodeTemplates(this.configEnc, this.encTask.getAdditionalResources()); |
| 81 | + assertTrue(encCheck); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test if the codegeneration for SymmetricEncrytion works with an open Test class. |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testCodeGenerationInTestClass() throws CoreException, IOException { |
| 89 | + |
| 90 | + final ICompilationUnit testClassUnit = TestUtils.getICompilationUnit(this.developerProject, "testPackage", "Test.java"); |
| 91 | + TestUtils.openJavaFileInWorkspace(this.developerProject, "testPackage", testClassUnit); |
| 92 | + |
| 93 | + this.configEnc = TestUtils.createCrySLConfiguration("encryption", testClassUnit.getResource(), generatorEnc, this.developerProject); |
| 94 | + this.generatorEnc.generateCodeTemplates(this.configEnc, this.encTask.getAdditionalResources()); |
| 95 | + assertEquals(1, countMethods(testClassUnit)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test if the Output class has the right methods, after the codegeneration runs two times (different tasks), without any open class. |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void testCodeGenerationTwoTimesNoClassOpen() throws CoreException, IOException { |
| 103 | + |
| 104 | + this.configEnc = TestUtils.createCrySLConfiguration("encryption", targetFile, generatorEnc, this.developerProject); |
| 105 | + this.generatorEnc.generateCodeTemplates(this.configEnc, this.encTask.getAdditionalResources()); |
| 106 | + |
| 107 | + this.configSecPassword = TestUtils.createCrySLConfiguration("securePassword", targetFile, generatorSecPassword, this.developerProject); |
| 108 | + this.generatorSecPassword.generateCodeTemplates(this.configSecPassword, this.secPasswordTask.getAdditionalResources()); |
| 109 | + |
| 110 | + final ICompilationUnit outputUnit = TestUtils.getICompilationUnit(this.developerProject, Constants.PackageNameAsName, "Output.java"); |
| 111 | + assertEquals(2, countMethods(outputUnit)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test if the codegeneration puts the templageUsage-method in the open Enc class. |
| 116 | + */ |
| 117 | + // @Test |
| 118 | + public void testCodeGenerationInEncClass() throws CoreException, IOException { |
| 119 | + this.configEnc = TestUtils.createCrySLConfiguration("encryption", targetFile, generatorEnc, this.developerProject); |
| 120 | + this.generatorEnc.generateCodeTemplates(this.configEnc, this.encTask.getAdditionalResources()); |
| 121 | + final ICompilationUnit encUnit = TestUtils.getICompilationUnit(this.developerProject, Constants.PackageName, "Enc.java"); |
| 122 | + TestUtils.openJavaFileInWorkspace(this.developerProject, Constants.PackageName, encUnit); |
| 123 | + |
| 124 | + this.configSecPassword = TestUtils.createCrySLConfiguration("securePassword", targetFile, generatorSecPassword, this.developerProject); |
| 125 | + this.generatorSecPassword.generateCodeTemplates(this.configSecPassword, this.secPasswordTask.getAdditionalResources()); |
| 126 | + |
| 127 | + final ICompilationUnit outputUnit = TestUtils.getICompilationUnit(this.developerProject, Constants.PackageName, "Output.java"); |
| 128 | + assertEquals(2, countMethods(outputUnit)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * This method counts methods in ICompilationUnits |
| 133 | + * |
| 134 | + * @param unit |
| 135 | + * @return |
| 136 | + * @throws JavaModelException |
| 137 | + */ |
| 138 | + private int countMethods(final ICompilationUnit unit) throws JavaModelException { |
| 139 | + return unit.getAllTypes()[0].getMethods().length; |
| 140 | + } |
| 141 | +} |
0 commit comments