📄 exprnavigationtest.java
字号:
MSystem system = ObjectCreation.getInstance().createModelWithObjectsAndLinkObject(); MAssociationClass job = system.model().getAssociationClass( "Job" ); MAssociationEnd personEnd = ( MAssociationEnd ) job .associationEndsAt( system.model().getClass( "Person" ) ).iterator().next(); // navigation from linkobject j1 to p1 MObject j1 = system.state().objectByName( "j1" ); List objects = system.state().getNavigableObjects( j1, job, personEnd ); assertEquals( 1, objects.size() ); assertEquals( "p1", ( ( MObject ) objects.get( 0 ) ).name() ); } catch ( Exception e ) { fail( "Exception was thrown: " + e.getMessage() ); } } /** * Test the navigation from an object to a specific object * respectively to the associationClass Job. */ public void testNavigableElementsWithAssocClass() { try { MSystem system = ObjectCreation.getInstance().createModelWithObjectsAndLinkObject(); MAssociationClass job = system.model().getAssociationClass( "Job" ); MAssociationEnd personEnd = ( MAssociationEnd ) job.associationEndsAt( system.model().getClass( "Person" ) ).iterator().next(); MAssociationEnd companyEnd = ( MAssociationEnd ) job.associationEndsAt( system.model().getClass( "Company" ) ).iterator().next(); // navigation from linkobject j1 to p1 MObject p1 = system.state().objectByName( "p1" ); List objects = system.state().getNavigableObjects( p1, personEnd, companyEnd ); assertEquals( 1, objects.size() ); assertEquals( "c1", ( ( MObject ) objects.get( 0 ) ).name() ); } catch ( Exception e ) { fail( "Exception was thrown: " + e.getMessage() ); } } /** * Test the navigation from a specific object respectively to the * association Job. */ public void testNavigableElementsWithNormalAssoc() { try { MSystem system = ObjectCreation.getInstance().createModelWithObjects(); MModel model = system.model(); // creation of an object (c2) of the class Company List names = new ArrayList(); names.add( "c2" ); ObjectType type = TypeFactory.mkObjectType( model.getClass( "Company" ) ); MCmd cmd = new MCmdCreateObjects( system.state(), names, type ); system.executeCmd( cmd ); // creation of a link between p1 and c2 of an association names.clear(); names.add( "p1" ); names.add( "c2" ); 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() ); } MAssociation assoc = model.getAssociation( "Job" ); cmd = new MCmdInsertLink( system.state(), exprs, assoc ); system.executeCmd( cmd ); MAssociation job = system.model().getAssociation( "Job" ); MAssociationEnd personEnd = ( MAssociationEnd ) job.associationEndsAt( system.model().getClass( "Person" ) ).iterator().next(); MAssociationEnd companyEnd = ( MAssociationEnd ) job.associationEndsAt( system.model().getClass( "Company" ) ).iterator().next(); // navigation from p1 to linkobject j1 MObject p1 = system.state().objectByName( "p1" ); List objects = system.state().getNavigableObjects( p1, personEnd, companyEnd ); assertEquals( 2, objects.size() ); assertEquals( "c2", ( ( MObject ) objects.get( 0 ) ).name() ); assertEquals( "c1", ( ( MObject ) objects.get( 1 ) ).name() ); } catch ( Exception e ) { fail( "Exception was thrown: " + e.getMessage() ); } }//-------------------------------------------------------------// Test the navigation by compiling an expression//------------------------------------------------------------- /** * Test the navigation from p1 to company (a normal association). */ public void testNavigationWithNormalAssoc() { MSystem system = ObjectCreation.getInstance().createModelWithObjectsAndLinkObject(); String expr = "p1.company->size"; PrintWriter pw = new PrintWriter( System.err ); Expression navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); Evaluator eval = new Evaluator(); Value value = eval.eval( navExpr, system.state(), system.varBindings() ); assertTrue( value.isInteger() ); IntegerValue val = ( IntegerValue ) value; assertEquals( 1, val.value() ); } /** * Test the navigation from j1 to company (from an linkObject to an * object). */ public void testNavigationFromLinkObjectToObject() { MSystem system = ObjectCreation.getInstance().createModelWithObjectsAndLinkObject(); MAssociationClass job = system.model().getAssociationClass( "Job" ); String expr = "j1.company->size"; PrintWriter pw = new PrintWriter( System.err ); Expression navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); Evaluator eval = new Evaluator(); Value value = eval.eval( navExpr, system.state(), system.varBindings() ); assertTrue( value.isInteger() ); IntegerValue val = ( IntegerValue ) value; assertEquals( 1, val.value() ); } /** * Test the navigation from c1 to job (from an object to an * linkObject). */ public void testNavigationFromObjectToLinkObject() { MSystem system = ObjectCreation.getInstance().createModelWithObjectsAndLinkObject(); MAssociationClass job = system.model().getAssociationClass( "Job" ); String expr = "c1.job->size"; PrintWriter pw = new PrintWriter( System.err ); Expression navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); Evaluator eval = new Evaluator(); Value value = eval.eval( navExpr, system.state(), system.varBindings() ); assertTrue( value.isInteger() ); IntegerValue val = ( IntegerValue ) value; assertEquals( 1, val.value() ); } /** * Test the navigation from c1 to job with an explicit rolename is given for * navigation (from an object to an linkObject). */ public void testNavigationFromObjectToLinkObjectWithExplicitRolename() { MSystem system = ObjectCreation.getInstance() .createModelWithObjectsOfSameClassAndLinkObject(); // test 1: navigation with explicit rolename String expr = "p1.job[boss]"; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter( sw ); //System.err ); Expression navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); Evaluator eval = new Evaluator(); Value value = eval.eval( navExpr, system.state(), system.varBindings() ); assertTrue( value.isObject() ); assertEquals( "@j1", value.toString() ); // test 2: ambigious navigation expr = "p1.job"; navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); assertNull( navExpr ); String expected = "<input>:1:4: The navigation path is ambiguous. " +"A qualification of the source association is required."; assertEquals(expected, sw.toString().trim()); // test 3: rolname is not `worker' is not avaliable expr = "p1.job[worker]"; navExpr = USECompiler.compileExpression( system.model(), new StringReader( expr ), "<input>", pw, system.topLevelBindings() ); eval = new Evaluator(); value = eval.eval( navExpr, system.state(), system.varBindings() ); assertTrue( value.isUndefined() ); } /** * Entry point */ public static void main( String[] args ) { junit.textui.TestRunner.run( new TestSuite( ExprNavigationTest.class ) ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -