|
| 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.assertNotNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 18 | +import org.eclipse.jdt.core.IJavaProject; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import de.cognicrypt.codegenerator.generator.CodeGenerator; |
| 22 | +import de.cognicrypt.codegenerator.generator.CrySLBasedCodeGenerator; |
| 23 | +import de.cognicrypt.codegenerator.testutilities.TestUtils; |
| 24 | +import de.cognicrypt.codegenerator.wizard.CrySLConfiguration; |
| 25 | +import de.cognicrypt.core.Constants; |
| 26 | +import de.cognicrypt.utils.DeveloperProject; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author André Sonntag |
| 30 | + */ |
| 31 | +public class TwoTimesCogniCryptRunTests { |
| 32 | + |
| 33 | + /** |
| 34 | + * Scenario: User runs CogniCrypt two times without selecting a specific class |
| 35 | + * or package. |
| 36 | + * |
| 37 | + * @throws Exception |
| 38 | + */ |
| 39 | + @Test |
| 40 | + public void runCCTwoTimesNoSpecificSelection() throws Exception { |
| 41 | + // task template |
| 42 | + String templateSecEnc = "secretkeyencryption"; |
| 43 | + String templateSecPwd = "securepassword"; |
| 44 | + |
| 45 | + // create Java project without any package or class |
| 46 | + IJavaProject generatedProject = TestUtils.createJavaProject("TestProject1"); |
| 47 | + |
| 48 | + // setup for code generation |
| 49 | + CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource()); |
| 50 | + DeveloperProject developerProject = codeGenerator.getDeveloperProject(); |
| 51 | + CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc, |
| 52 | + generatedProject.getResource(), codeGenerator, developerProject); |
| 53 | + |
| 54 | + // first generation run |
| 55 | + boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 56 | + assertTrue(secEncCheck); // check if code generation is successful for the first run |
| 57 | + |
| 58 | + // setup for second generation |
| 59 | + chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, generatedProject.getResource(), codeGenerator, |
| 60 | + developerProject); |
| 61 | + |
| 62 | + // second generation run |
| 63 | + boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 64 | + assertTrue(secPwdCheck); // check if code generation is successful for the second run |
| 65 | + |
| 66 | + ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 67 | + "SecureEncryptor.java"); |
| 68 | + assertNotNull(encClass); // check if SecureEncryptor.java is created |
| 69 | + |
| 70 | + ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 71 | + "PasswordHasher.java"); |
| 72 | + assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created |
| 73 | + |
| 74 | + ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 75 | + "Output.java"); |
| 76 | + assertNotNull(outputClass); // check if Output.java is created |
| 77 | + assertEquals(1, TestUtils.countMethods(outputClass)); |
| 78 | + |
| 79 | + TestUtils.deleteProject(generatedProject.getProject()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Scenario: User runs CogniCrypt two times and selects the previous generated |
| 84 | + * output class. |
| 85 | + * |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void runCCTwoTimesOutputClassSelection() throws Exception { |
| 90 | + // task template |
| 91 | + String templateSecEnc = "secretkeyencryption"; |
| 92 | + String templateSecPwd = "securepassword"; |
| 93 | + |
| 94 | + // create Java project without any package or class |
| 95 | + IJavaProject generatedProject = TestUtils.createJavaProject("TestProject2"); |
| 96 | + |
| 97 | + // setup for first generation |
| 98 | + CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource()); |
| 99 | + DeveloperProject developerProject = codeGenerator.getDeveloperProject(); |
| 100 | + CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc, |
| 101 | + generatedProject.getResource(), codeGenerator, developerProject); |
| 102 | + |
| 103 | + // first generation run |
| 104 | + boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 105 | + assertTrue(secEncCheck); // check if code generation is successful for the first run |
| 106 | + |
| 107 | + ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 108 | + "SecureEncryptor.java"); |
| 109 | + assertNotNull(encClass); // check if SecureEncryptor.java is created |
| 110 | + |
| 111 | + ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 112 | + "Output.java"); |
| 113 | + assertNotNull(outputClass); |
| 114 | + |
| 115 | + // setup for second generation |
| 116 | + codeGenerator = new CrySLBasedCodeGenerator(outputClass.getResource()); |
| 117 | + developerProject = codeGenerator.getDeveloperProject(); |
| 118 | + chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, outputClass.getResource(), codeGenerator, |
| 119 | + developerProject); |
| 120 | + |
| 121 | + // second generation run |
| 122 | + boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 123 | + assertTrue(secPwdCheck); // check if code generation is successful for the second run |
| 124 | + |
| 125 | + ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 126 | + "PasswordHasher.java"); |
| 127 | + assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created |
| 128 | + |
| 129 | + outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, "Output.java"); |
| 130 | + assertNotNull(outputClass); |
| 131 | + |
| 132 | + int outputMethodCount = TestUtils.countMethods(outputClass); |
| 133 | + assertEquals(1, outputMethodCount); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Scenario: User runs CogniCrypt two times and selects a previous generated |
| 138 | + * "logic" class. |
| 139 | + * |
| 140 | + * @throws Exception |
| 141 | + */ |
| 142 | + @Test |
| 143 | + public void runCCTwoTimesLogicClassSelection() throws Exception { |
| 144 | + // task template |
| 145 | + String templateSecEnc = "secretkeyencryption"; |
| 146 | + String templateSecPwd = "securepassword"; |
| 147 | + |
| 148 | + // create Java project without any package or class |
| 149 | + IJavaProject generatedProject = TestUtils.createJavaProject("TestProject3"); |
| 150 | + |
| 151 | + // setup for first generation |
| 152 | + CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource()); |
| 153 | + DeveloperProject developerProject = codeGenerator.getDeveloperProject(); |
| 154 | + CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc, |
| 155 | + generatedProject.getResource(), codeGenerator, developerProject); |
| 156 | + |
| 157 | + // first generation run |
| 158 | + boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 159 | + assertTrue(secEncCheck); // check if code generation is successful for the first run |
| 160 | + |
| 161 | + ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 162 | + "SecureEncryptor.java"); |
| 163 | + assertNotNull(encClass); // check if SecureEncryptor.java is created |
| 164 | + |
| 165 | + ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 166 | + "Output.java"); |
| 167 | + assertNotNull(outputClass); |
| 168 | + |
| 169 | + // setup for second generation |
| 170 | + codeGenerator = new CrySLBasedCodeGenerator(encClass.getResource()); |
| 171 | + developerProject = codeGenerator.getDeveloperProject(); |
| 172 | + chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, encClass.getResource(), codeGenerator, |
| 173 | + developerProject); |
| 174 | + |
| 175 | + // second generation run |
| 176 | + boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, ""); |
| 177 | + assertTrue(secPwdCheck); // check if code generation is successful for the second run |
| 178 | + |
| 179 | + ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, |
| 180 | + "PasswordHasher.java"); |
| 181 | + assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created |
| 182 | + |
| 183 | + encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, "SecureEncryptor.java"); |
| 184 | + assertNotNull(encClass); |
| 185 | + |
| 186 | + int secureEncryptorMethodCount = TestUtils.countMethods(encClass); |
| 187 | + |
| 188 | + assertEquals(4, secureEncryptorMethodCount); |
| 189 | + |
| 190 | + } |
| 191 | +} |
0 commit comments