/* * Copyright (c) 2013 ulno (http://contact.ulno.net) * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * The Software shall be used for Good, not Evil. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.sdmlib.test.examples.studyrightWithAssignments; import org.sdmlib.SimpleSDMLib; import org.sdmlib.codegen.Gradle; import org.sdmlib.models.YamlIdMap; import org.sdmlib.models.classes.ClassModel; import org.sdmlib.storyboards.Goal; import org.sdmlib.storyboards.MikadoLog; import org.sdmlib.storyboards.Storyboard; import de.uniks.networkparser.ext.SimpleController; import de.uniks.networkparser.graph.Association; import de.uniks.networkparser.graph.AssociationTypes; import de.uniks.networkparser.graph.Clazz; import de.uniks.networkparser.graph.DataType; import de.uniks.networkparser.graph.Parameter; public class StudyRightWithAssignmentsModel { public static final String ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL = "org.sdmlib.test.codegen.studyright.model"; public static final String SRC_TEST_JAVA = "src/test/java"; /** * *
*
* ClassModel model = new ClassModel("org.sdmlib.test.examples.studyrightWithAssignments.model");
*
* Clazz universityClass = model.createClazz("University")
* .withAttribute("name", DataType.STRING);
*
*
*
*
*
*
* Clazz studentClass = model.createClazz("Student")
* .withAttribute("name", DataType.STRING)
* .withAttribute("id", DataType.STRING)
* .withAttribute("assignmentPoints", DataType.INT)
* .withAttribute("motivation", DataType.INT)
* .withAttribute("credits", DataType.INT);
*
*
*
*
* * 3. add University --> Student association *
* *
*
* universityClass.withBidirectional(studentClass, "students", Association.MANY, "university", Association.ONE);
*
*
*
*
* * 4. add University --> Room association *
* *
*
* Clazz roomClass = model.createClazz("Room")
* .withAttribute("name", DataType.STRING)
* .withAttribute("topic", DataType.STRING)
* .withAttribute("credits", DataType.INT);
*
* roomClass.withMethod("findPath", DataType.STRING, new Parameter(DataType.INT).with("motivation"));
*
* //Association universityToRoom =
* universityClass.createBidirectional(roomClass, "rooms", Association.MANY, "university", Association.ONE).with(AssociationTypes.AGGREGATION);
*
* // Association doors =
* roomClass.createBidirectional(roomClass, "doors", Association.MANY, "doors", Association.MANY);
*
* // Association studentsInRoom =
* studentClass.createBidirectional(roomClass, "in", Association.ONE, "students", Association.MANY);
* studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", Association.MANY);
*
*
*
*
* * 5. add assignments: *
* *
*
* Clazz assignmentClass = model.createClazz("Assignment")
* .withAttribute("content", DataType.STRING)
* .withAttribute("points", DataType.INT);
*
* assignmentClass.createBidirectional(roomClass, "room", Association.ONE, "assignments", Association.MANY);
*
* studentClass.createBidirectional(assignmentClass, "done", Association.MANY, "students", Association.MANY);
*
*
*
*
* * 6. generate class source files. *
* *
*
* model.generate("src/test/java"); // usually don't specify anything here, then it goes into src
*
*
* * Check: compile result after all 0 actual 0 *
*/ // @Test public void testStudyRightWithAssignmentsClassGeneration() { /* * This file will generate necessary classes and class diagram for the StudyRight with Assignments * example in the Story Driven Modeling book */ if (SimpleSDMLib.ENABLE() == false) { return; } // remove old code? // Gradle.removeDir("src/test/java", "org.sdmlib.test.examples.studyrightWithAssignments.model"); Storyboard story = new Storyboard(); // project compiles? int gradleResult = 0; // ============================================================ story.addStep("Build model for class University"); story.markCodeStart(); ClassModel model = new ClassModel("org.sdmlib.test.examples.studyrightWithAssignments.model"); Clazz universityClass = model.createClazz("University") .withAttribute("name", DataType.STRING); story.addCode(); story.addClassDiagram(model); // ============================================================ story.addStep("Add class Student"); story.markCodeStart(); Clazz studentClass = model.createClazz("Student") .withAttribute("name", DataType.STRING) .withAttribute("id", DataType.STRING) .withAttribute("assignmentPoints", DataType.INT) .withAttribute("motivation", DataType.INT) .withAttribute("credits", DataType.INT); story.addCode(); story.addClassDiagram(model); // ============================================================ story.add("3. add University --> Student association"); // Association universityToStudent = story.markCodeStart(); universityClass.withBidirectional(studentClass, "students", Association.MANY, "university", Association.ONE); story.addCode(); story.addClassDiagram(model); // ============================================================ story.add("4. add University --> Room association"); story.markCodeStart(); Clazz roomClass = model.createClazz("Room") .withAttribute("name", DataType.STRING) .withAttribute("topic", DataType.STRING) .withAttribute("credits", DataType.INT); roomClass.withMethod("findPath", DataType.STRING, new Parameter(DataType.INT).with("motivation")); // Association universityToRoom = universityClass.createBidirectional(roomClass, "rooms", Association.MANY, "university", Association.ONE).with(AssociationTypes.AGGREGATION); // Association doors = roomClass.createBidirectional(roomClass, "doors", Association.MANY, "doors", Association.MANY); // Association studentsInRoom = studentClass.createBidirectional(roomClass, "in", Association.ONE, "students", Association.MANY); studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", Association.MANY); story.addCode(); story.addClassDiagram(model); // ============================================================ story.add("5. add assignments:"); story.markCodeStart(); Clazz assignmentClass = model.createClazz("Assignment") .withAttribute("content", DataType.STRING) .withAttribute("points", DataType.INT); assignmentClass.createBidirectional(roomClass, "room", Association.ONE, "assignments", Association.MANY); studentClass.createBidirectional(assignmentClass, "done", Association.MANY, "students", Association.MANY); story.addCode(); story.addClassDiagram(model); studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", Association.MANY); // some more classes for model navigation tests studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", Association.MANY); model.createClazz("TeachingAssistant") .withSuperClazz(studentClass) .withBidirectional(roomClass, "room", Association.ONE, "tas", Association.MANY) .withAttribute("certified", DataType.BOOLEAN); Clazz presidentClass = model.createClazz("President"); universityClass.createBidirectional(presidentClass, "president", Association.ONE, "university", Association.ONE).with(AssociationTypes.AGGREGATION); story.addStep("Some more classes for extended navigation demo"); story.addClassDiagram(model); // ============================================================ story.add("6. generate class source files."); // model.removeAllGeneratedCode("src/test/java"); model.setAuthorName("zuendorf"); story.markCodeStart(); model.generate("src/test/java"); // usually don't specify anything here, then it goes into src story.addCode(); SimpleController controller = SimpleController.create(); controller.withPackageName(SRC_TEST_JAVA + "/" + "org.sdmlib.test.examples.studyrightWithAssignments.model".replaceAll("\\.", "/")); gradleResult = controller.start(); story.assertEquals("compile result after all", 0, gradleResult); // story.dumpJavaDoc(ClassModel.class.getName()); story.dumpHTML(); } /** * ** Check: javac result 0 actual 0 *
** Check: compile after manuel insertion, expected 0 actual 0 *
** Check: gradle compileTestJava result after adding University.name 0 actual 0 *
*
*
* Clazz studentClass = model.createClazz("Student")
* .withAttribute("name", DataType.STRING)
* .withAttribute("id", DataType.STRING)
* .withAttribute("assignmentPoints", DataType.INT)
* .withAttribute("motivation", DataType.INT)
* .withAttribute("credits", DataType.INT);
*
*
*
*
* * Check: compile result after adding class Student 0 actual 0 *
** 3. add University --> Student association *
* *
*
* universityClass.withBidirectional(studentClass, "students", Association.MANY, "university", Association.ONE);
*
*
*
*
* * Check: compile result after adding students assoc 0 actual 0 *
** 6. generate class source files. *
* *
*
* model.generate("src/test/java"); // usually don't specify anything here, then it goes into src
*
*
* * Check: compile result after all 0 actual 0 *
*/ // @Test public void testNetworkParserCodeGen() { if (SimpleSDMLib.ENABLE() == false) { return; } /* * This file will generate that necessary classes and class diagram for the StudyRight with * Assignments example in the Story Driven Modeling book */ Storyboard story = new Storyboard().withDocDirName("doc/internal"); // remove old code // Gradle.removeDir(SRC_TEST_JAVA, ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL); // project compiles? int result = -1; // ============================================================ story.addStep("Build model for class University"); ClassModel model = new ClassModel(ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL); Clazz universityClass = model.createClazz("University"); // ============================================================ model.generate(SRC_TEST_JAVA); SimpleController controller = SimpleController.create(); controller.withPackageName(SRC_TEST_JAVA + "/" + ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL.replaceAll("\\.", "/")); result = controller.start(); story.assertEquals("javac result", 0, result); // ============================================================ // add manual code Gradle.insertCode(SRC_TEST_JAVA, ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL + ".University", " public void selfTest()\n" + " {\n" + " System.out.println();\n" + " }\n" + "\n"); result = controller.start(); story.assertEquals("compile after manuel insertion, expected", 0, result); // ============================================================ universityClass.withAttribute("name", DataType.STRING); model.generate(SRC_TEST_JAVA); result = controller.start(); story.assertEquals("gradle compileTestJava result after adding University.name", 0, result); // ============================================================ story.addStep("Add class Student"); story.markCodeStart(); Clazz studentClass = model.createClazz("Student") .withAttribute("name", DataType.STRING) .withAttribute("id", DataType.STRING) .withAttribute("assignmentPoints", DataType.INT) .withAttribute("motivation", DataType.INT) .withAttribute("credits", DataType.INT); story.addCode(); story.addClassDiagram(model); // project compiles? model.generate(SRC_TEST_JAVA); result = controller.start(); story.assertEquals("compile result after adding class Student", 0, result); // ============================================================ story.add("3. add University --> Student association"); // Association universityToStudent = story.markCodeStart(); universityClass.withBidirectional(studentClass, "students", Association.MANY, "university", Association.ONE); story.addCode(); story.addClassDiagram(model); // project compiles? model.generate(SRC_TEST_JAVA); result = controller.start(); story.assertEquals("compile result after adding students assoc", 0, result); // // // //============================================================ // story.add("4. add University --> Room association"); // // story.markCodeStart(); // Clazz roomClass = model.createClazz("Room") // .withAttribute("name", DataType.STRING) // .withAttribute("topic", DataType.STRING) // .withAttribute("credits", DataType.INT); // // roomClass.withMethod("findPath", DataType.STRING, new // Parameter(DataType.INT).with("motivation")); // // //Association universityToRoom = // universityClass.createBidirectional(roomClass, "rooms", Association.MANY, "university", // Association.ONE).with(AssociationTypes.AGGREGATION); // // // Association doors = // roomClass.createBidirectional(roomClass, "doors", Association.MANY, "doors", Association.MANY); // // // Association studentsInRoom = // studentClass.createBidirectional(roomClass, "in", Association.ONE, "students", Association.MANY); // studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", // Association.MANY); // // story.addCode(); // // // story.addClassDiagramAsImage(model, 400, 500); // // // // //============================================================ // story.add("5. add assignments:"); // // story.markCodeStart(); // Clazz assignmentClass = model.createClazz("Assignment") // .withAttribute("content", DataType.STRING) // .withAttribute("points", DataType.INT); // // assignmentClass.createBidirectional(roomClass, "room", Association.ONE, "assignments", // Association.MANY); // // studentClass.createBidirectional(assignmentClass, "done", Association.MANY, "students", // Association.MANY); // story.addCode(); // // // story.addClassDiagramAsImage(model, 450, 600); // // studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", // Association.MANY); // // // // some more classes for model navigation tests // studentClass.createBidirectional(studentClass, "friends", Association.MANY, "friends", // Association.MANY); // // model.createClazz("TeachingAssistant") // .withSuperClazz(studentClass) // .withBidirectional(roomClass, "room", Association.ONE, "tas", Association.MANY) // .withAttribute("certified", DataType.BOOLEAN); // // // Clazz presidentClass = model.createClazz("President"); // universityClass.createBidirectional(presidentClass, "president", Association.ONE, "university", // Association.ONE).with(AssociationTypes.AGGREGATION); // ============================================================ story.add("6. generate class source files."); // model.removeAllGeneratedCode("src/test/java"); model.setAuthorName("zuendorf"); story.markCodeStart(); model.generate("src/test/java"); // usually don't specify anything here, then it goes into src story.addCode(); result = controller.start(); story.assertEquals("compile result after all", 0, result); // remove old code // Gradle.removeDir(SRC_TEST_JAVA, ORG_SDMLIB_TEST_CODEEGEN_STUDYRIGHT_MODEL); story.dumpHTML(); } /** * *