-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Development: Add test file for script to generate random results when…
… creating large courses locally (#9298)
- Loading branch information
1 parent
13a1e11
commit a014ab0
Showing
14 changed files
with
519 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
supporting_scripts/course-scripts/quick-course-setup/randomize_results_after.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
import shutil | ||
from logging_config import logging | ||
|
||
test_files_folder: str = "../../../src/main/resources/templates/java/test/testFiles" | ||
default_folder: str = "./testFiles-template/default" | ||
random_test_case_dest: str = os.path.join(test_files_folder, "RandomizedTestCases.java") | ||
|
||
def delete_random_test_case() -> None: | ||
if os.path.exists(random_test_case_dest): | ||
os.remove(random_test_case_dest) | ||
logging.info(f"Deleted file: {random_test_case_dest}") | ||
else: | ||
logging.info(f"RandomizedTestCases.java file not found at {random_test_case_dest}") | ||
|
||
def copy_default_folders() -> None: | ||
for folder_name in os.listdir(default_folder): | ||
src_folder_path: str = os.path.join(default_folder, folder_name) | ||
dest_folder_path: str = os.path.join(test_files_folder, folder_name) | ||
if os.path.isdir(src_folder_path): | ||
shutil.copytree(src_folder_path, dest_folder_path) | ||
logging.info(f"Copied folder {src_folder_path} to {dest_folder_path}") | ||
|
||
def run_cleanup() -> None: | ||
delete_random_test_case() | ||
copy_default_folders() | ||
|
||
if __name__ == "__main__": | ||
# Run this after running the script | ||
delete_random_test_case() | ||
copy_default_folders() |
29 changes: 29 additions & 0 deletions
29
supporting_scripts/course-scripts/quick-course-setup/randomize_results_before.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import shutil | ||
from logging_config import logging | ||
|
||
test_files_folder: str = "../../../src/main/resources/templates/java/test/testFiles" | ||
random_test_case_file: str = "./testFiles-template/randomized/RandomizedTestCases.java" | ||
random_test_case_dest: str = os.path.join(test_files_folder, "RandomizedTestCases.java") | ||
|
||
def delete_existing_folders() -> None: | ||
folders_to_delete: list[str] = ["behavior", "structural"] | ||
for folder in folders_to_delete: | ||
folder_path: str = os.path.join(test_files_folder, folder) | ||
if os.path.exists(folder_path) and os.path.isdir(folder_path): | ||
shutil.rmtree(folder_path) | ||
logging.info(f"Deleted folder: {folder_path}") | ||
else: | ||
logging.info(f"Folder not found: {folder_path}") | ||
|
||
def copy_random_test_case() -> None: | ||
if os.path.exists(random_test_case_file): | ||
shutil.copy(random_test_case_file, random_test_case_dest) | ||
logging.info(f"Copied {random_test_case_file} to {random_test_case_dest}") | ||
else: | ||
logging.info(f"RandomizedTestCases.java file not found at {random_test_case_file}") | ||
|
||
if __name__ == "__main__": | ||
# Run this before running the script | ||
delete_existing_folders() | ||
copy_random_test_case() |
98 changes: 98 additions & 0 deletions
98
...ts/quick-course-setup/testFiles-template/default/behavior/SortingExampleBehaviorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package ${packageName}; | ||
|
||
import org.junit.jupiter.api.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.text.*; | ||
import java.util.*; | ||
|
||
import static de.tum.in.test.api.util.ReflectionTestUtils.*; | ||
|
||
import de.tum.in.test.api.BlacklistPath; | ||
import de.tum.in.test.api.PathType; | ||
import de.tum.in.test.api.StrictTimeout; | ||
import de.tum.in.test.api.WhitelistPath; | ||
import de.tum.in.test.api.jupiter.Public; | ||
|
||
/** | ||
* @author Stephan Krusche (krusche@in.tum.de) | ||
* @version 5.1 (11.06.2021) | ||
*/ | ||
@Public | ||
@WhitelistPath("target") // mainly for Artemis | ||
@BlacklistPath("target/test-classes") // prevent access to test-related classes and resources | ||
class SortingExampleBehaviorTest { | ||
|
||
private List<Date> dates; | ||
private List<Date> datesWithCorrectOrder; | ||
|
||
@BeforeEach | ||
void setup() throws ParseException { | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); | ||
Date date1 = dateFormat.parse("08.11.2018"); | ||
Date date2 = dateFormat.parse("15.04.2017"); | ||
Date date3 = dateFormat.parse("15.02.2016"); | ||
Date date4 = dateFormat.parse("15.09.2017"); | ||
|
||
this.dates = Arrays.asList(date1, date2, date3, date4); | ||
this.datesWithCorrectOrder = Arrays.asList(date3, date2, date4, date1); | ||
} | ||
|
||
@Test | ||
@StrictTimeout(1) | ||
void testBubbleSort() { | ||
BubbleSort bubbleSort = new BubbleSort(); | ||
bubbleSort.performSort(dates); | ||
if (!datesWithCorrectOrder.equals(dates)) { | ||
fail("BubbleSort does not sort correctly"); | ||
} | ||
} | ||
|
||
@Test | ||
@StrictTimeout(1) | ||
void testMergeSort() { | ||
MergeSort mergeSort = new MergeSort(); | ||
mergeSort.performSort(dates); | ||
if (!datesWithCorrectOrder.equals(dates)) { | ||
fail("MergeSort does not sort correctly"); | ||
} | ||
} | ||
|
||
@Test | ||
@StrictTimeout(1) | ||
void testUseMergeSortForBigList() throws ReflectiveOperationException { | ||
List<Date> bigList = new ArrayList<Date>(); | ||
for (int i = 0; i < 11; i++) { | ||
bigList.add(new Date()); | ||
} | ||
Object chosenSortStrategy = configurePolicyAndContext(bigList); | ||
if (!(chosenSortStrategy instanceof MergeSort)) { | ||
fail("The sort algorithm of Context was not MergeSort for a list with more than 10 dates."); | ||
} | ||
} | ||
|
||
@Test | ||
@StrictTimeout(1) | ||
void testUseBubbleSortForSmallList() throws ReflectiveOperationException { | ||
List<Date> smallList = new ArrayList<Date>(); | ||
for (int i = 0; i < 3; i++) { | ||
smallList.add(new Date()); | ||
} | ||
Object chosenSortStrategy = configurePolicyAndContext(smallList); | ||
if (!(chosenSortStrategy instanceof BubbleSort)) { | ||
fail("The sort algorithm of Context was not BubbleSort for a list with less or equal than 10 dates."); | ||
} | ||
} | ||
|
||
private Object configurePolicyAndContext(List<Date> dates) throws ReflectiveOperationException { | ||
Object context = newInstance("${packageName}.Context"); | ||
invokeMethod(context, getMethod(context, "setDates", List.class), dates); | ||
|
||
Object policy = newInstance("${packageName}.Policy", context); | ||
invokeMethod(policy, getMethod(policy, "configure")); | ||
|
||
Object chosenSortStrategy = invokeMethod(context, getMethod(context, "getSortAlgorithm")); | ||
return chosenSortStrategy; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ourse-scripts/quick-course-setup/testFiles-template/default/structural/AttributeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ${packageName}; | ||
|
||
import java.net.URISyntaxException; | ||
|
||
import org.junit.jupiter.api.DynamicContainer; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import de.tum.in.test.api.BlacklistPath; | ||
import de.tum.in.test.api.PathType; | ||
import de.tum.in.test.api.StrictTimeout; | ||
import de.tum.in.test.api.WhitelistPath; | ||
import de.tum.in.test.api.jupiter.Public; | ||
import de.tum.in.test.api.structural.AttributeTestProvider; | ||
|
||
/** | ||
* @author Stephan Krusche (krusche@in.tum.de) | ||
* @version 5.1 (11.06.2021) | ||
* <br><br> | ||
* This test evaluates if the specified attributes in the structure oracle are correctly implemented with the expected type, visibility modifiers and annotations, | ||
* based on its definition in the structure oracle (test.json). | ||
*/ | ||
@Public | ||
@WhitelistPath("target") // mainly for Artemis | ||
@BlacklistPath("target/test-classes") // prevent access to test-related classes and resources | ||
class AttributeTest extends AttributeTestProvider { | ||
|
||
/** | ||
* This method collects the classes in the structure oracle file for which attributes are specified. | ||
* These classes are then transformed into JUnit 5 dynamic tests. | ||
* @return A dynamic test container containing the test for each class which is then executed by JUnit. | ||
*/ | ||
@Override | ||
@StrictTimeout(10) | ||
@TestFactory | ||
protected DynamicContainer generateTestsForAllClasses() throws URISyntaxException { | ||
structureOracleJSON = retrieveStructureOracleJSON(this.getClass().getResource("test.json")); | ||
return super.generateTestsForAllClasses(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ts/course-scripts/quick-course-setup/testFiles-template/default/structural/ClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ${packageName}; | ||
|
||
import java.net.URISyntaxException; | ||
|
||
import org.junit.jupiter.api.DynamicContainer; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import de.tum.in.test.api.BlacklistPath; | ||
import de.tum.in.test.api.PathType; | ||
import de.tum.in.test.api.StrictTimeout; | ||
import de.tum.in.test.api.WhitelistPath; | ||
import de.tum.in.test.api.jupiter.Public; | ||
import de.tum.in.test.api.structural.ClassTestProvider; | ||
|
||
/** | ||
* @author Stephan Krusche (krusche@in.tum.de) | ||
* @version 5.1 (11.06.2021) | ||
* <br><br> | ||
* This test evaluates the hierarchy of the class, i.e. if the class is abstract or an interface or an enum and also if the class extends another superclass and if | ||
* it implements the interfaces and annotations, based on its definition in the structure oracle (test.json). | ||
*/ | ||
@Public | ||
@WhitelistPath("target") // mainly for Artemis | ||
@BlacklistPath("target/test-classes") // prevent access to test-related classes and resources | ||
class ClassTest extends ClassTestProvider { | ||
|
||
/** | ||
* This method collects the classes in the structure oracle file for which at least one class property is specified. | ||
* These classes are then transformed into JUnit 5 dynamic tests. | ||
* @return A dynamic test container containing the test for each class which is then executed by JUnit. | ||
*/ | ||
@Override | ||
@StrictTimeout(10) | ||
@TestFactory | ||
protected DynamicContainer generateTestsForAllClasses() throws URISyntaxException { | ||
structureOracleJSON = retrieveStructureOracleJSON(this.getClass().getResource("test.json")); | ||
return super.generateTestsForAllClasses(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...rse-scripts/quick-course-setup/testFiles-template/default/structural/ConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ${packageName}; | ||
|
||
import java.net.URISyntaxException; | ||
|
||
import org.junit.jupiter.api.DynamicContainer; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import de.tum.in.test.api.BlacklistPath; | ||
import de.tum.in.test.api.PathType; | ||
import de.tum.in.test.api.StrictTimeout; | ||
import de.tum.in.test.api.WhitelistPath; | ||
import de.tum.in.test.api.jupiter.Public; | ||
import de.tum.in.test.api.structural.ConstructorTestProvider; | ||
|
||
/** | ||
* @author Stephan Krusche (krusche@in.tum.de) | ||
* @version 5.1 (11.06.2021) | ||
* <br><br> | ||
* This test evaluates if the specified constructors in the structure oracle are correctly implemented with the expected parameter types and annotations, | ||
* based on its definition in the structure oracle (test.json). | ||
*/ | ||
@Public | ||
@WhitelistPath("target") // mainly for Artemis | ||
@BlacklistPath("target/test-classes") // prevent access to test-related classes and resources | ||
class ConstructorTest extends ConstructorTestProvider { | ||
|
||
/** | ||
* This method collects the classes in the structure oracle file for which constructors are specified. | ||
* These classes are then transformed into JUnit 5 dynamic tests. | ||
* @return A dynamic test container containing the test for each class which is then executed by JUnit. | ||
*/ | ||
@Override | ||
@StrictTimeout(10) | ||
@TestFactory | ||
protected DynamicContainer generateTestsForAllClasses() throws URISyntaxException { | ||
structureOracleJSON = retrieveStructureOracleJSON(this.getClass().getResource("test.json")); | ||
return super.generateTestsForAllClasses(); | ||
} | ||
} |
Oops, something went wrong.