📄 linktest.java
字号:
// Look here first if the test fails. Hint: Check the names. (UniqueNameGenerator) assertEquals( "Job1", system.state().objectByName( "Job1" ).name() ); Set objects = new HashSet(); objects.add( system.state().objectByName( "p1" ) ); objects.add( system.state().objectByName( "c1" ) ); assertTrue( system.state().hasLinkBetweenObjects( system.model().getAssociation( "Job" ), objects ) ); } catch ( MSystemException e ) { throw ( new Error( e ) ); } } /** * Tries to create Linkobject with command * <code>MCmdCreateObjects</code>. */ public void testCreationLinkObjectWithCmdCreateObjects() { MSystem system = null; try { system = createModelWithoutLinkObject(); List names = new ArrayList(); names.add( "j1" ); ObjectType type = TypeFactory.mkObjectType( system.model().getClass( "Job" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); } catch ( MSystemException e ) { // tests if the object j1 does not exists assertEquals( null, system.state().objectByName( "j1" ) ); // tests if the link between p1 (Person) and c1 (Company) exists Set objects = new HashSet(); objects.add( system.state().objectByName( "p1" ) ); objects.add( system.state().objectByName( "c1" ) ); assertFalse( system.state().hasLinkBetweenObjects( system.model() .getAssociation( "Job" ), objects ) ); } } /** * Tests that two AssociationClasses cannot be defiened between * two other objects with the command create-between . */ public void testTwoLinkObjectsBetweenTwoOtherObjects() { MSystem system = null; try { system = createModelWithoutLinkObject(); List names = new ArrayList(); names.add( "p1" ); names.add( "c1" ); MAssociationClass assocClass = system.model().getAssociationClass( "Job" ); // Insert the first LinkObject MCmd cmd = new MCmdCreateInsertObjects( system.state(), "j1", assocClass, names ); system.executeCmd( cmd ); // Insert the second LinkObject cmd = new MCmdCreateInsertObjects( system.state(), "j2", assocClass, names ); system.executeCmd( cmd ); } catch ( MSystemException e ) { // wanted. } finally { // tests if the object j2 does not exists assertEquals( null, system.state().objectByName( "j2" ) ); } } /** * Tests that two AssociationClasses cannot be defiened between * two other objects with the command insert. */ public void testTwoLinkObjectsBetweenTwoOtherObjectsWithInsert() { MSystem system = null; MAssociationClass assocClass = null; try { system = createModelWithoutLinkObject(); List names = new ArrayList(); names.add( "p1" ); names.add( "c1" ); Expression[] exprs = new Expression[names.size()]; Iterator it = names.iterator(); int i = 0; while (it.hasNext() ) { MObject obj = system.state().objectByName( (String) it.next() ); exprs[i++] = new ExpVariable( obj.name(), obj.type() ); } assocClass = system.model().getAssociationClass( "Job" ); // Insert the first LinkObject MCmd cmd = new MCmdInsertLink( system.state(), exprs, assocClass ); system.executeCmd( cmd ); // Insert the second LinkObject cmd = new MCmdInsertLink( system.state(), exprs, assocClass ); system.executeCmd( cmd ); } catch ( MSystemException e ) { // wanted. } finally { // for counting the number of Instances between names int cnt = 0; Iterator linkSet = system.state().linksOfAssociation( assocClass ).links().iterator(); while ( linkSet.hasNext() ) { MLink link = ( MLink ) linkSet.next(); Iterator it = link.linkedObjects().iterator(); MObject first = ( MObject ) it.next(); MObject second = ( MObject ) it.next(); if ( ( first.name().equals( "p1" ) && second.name().equals( "c1" ) ) || ( first.name().equals( "c1" ) && second.name().equals( "p1" ) ) ) { cnt++; } } // tests if the object j2 does not exists assertEquals( 1, cnt ); } } /** * Creates a model with two classes and an associationclass. It * creates instances of those as well. * * @return returns the actual System. */ private MSystem createModelWithObject() { try { // creation of the system MModel model = TestModelUtil.getInstance() .createModelWithClassAndAssocClass(); MSystem system = new MSystem( model ); // creation of an object (p1) of the class Person List names = new ArrayList(); names.add( "p1" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (c1) of the class Company names.clear(); names.add( "c1" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an linkobject (j1) of the associationclass Job names.clear(); names.add( "p1" ); names.add( "c1" ); MAssociationClass assocClass = model.getAssociationClass( "Job" ); cmd = new MCmdCreateInsertObjects( system.state(), "j1", assocClass, names ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } /** * Creates a model with two classes and an associationclass. It * creates instances of those as well. * * @return returns the actual System. */ private MSystem createModelWithoutLinkObject() { try { // creation of the system MModel model = TestModelUtil.getInstance() .createModelWithClassAndAssocClass(); MSystem system = new MSystem( model ); // creation of an object (p1) of the class Person List names = new ArrayList(); names.add( "p1" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Person" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of an object (c1) of the class Company names.clear(); names.add( "c1" ); type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); return system; } catch ( Exception e ) { throw ( new Error( e ) ); } } /** * Entry point */ public static void main( String[] args ) { junit.textui.TestRunner.run( new TestSuite( LinkTest.class ) ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -