Exemple #1
0
  /** Tests the parsing the def expression for static attributes and operations. */
  public void test_defExpression_static() {
    try {
      Environment<
              Package,
              Classifier,
              Operation,
              Property,
              EnumerationLiteral,
              Parameter,
              State,
              CallOperationAction,
              SendSignalAction,
              Constraint,
              Class,
              EObject>
          env = ocl.getEnvironment();
      UMLReflection<
              Package,
              Classifier,
              Operation,
              Property,
              EnumerationLiteral,
              Parameter,
              State,
              CallOperationAction,
              SendSignalAction,
              Constraint>
          umlReflection = env.getUMLReflection();

      ParsingOptions.setOption(ocl.getEnvironment(), ParsingOptions.SUPPORT_STATIC_FEATURES, true);
      parseDef(
          "package ocltest context Fruit "
              + "def: bestColor1() : Color = null "
              + "static def: bestColor2() : Color = null "
              + "def: goodColor1 : Color = null "
              + "static def: goodColor2 : Color = null "
              + "endpackage");
      Operation operation1 = env.lookupOperation(fruit, "bestColor1", null);
      assertNotNull(operation1);
      assertEquals(false, umlReflection.isStatic(operation1));
      Operation operation2 = env.lookupOperation(fruit, "bestColor2", null);
      assertNotNull(operation2);
      assertEquals(true, umlReflection.isStatic(operation2));
      Property property1 = env.lookupProperty(fruit, "goodColor1");
      assertNotNull(property1);
      assertEquals(false, umlReflection.isStatic(property1));
      Property property2 = env.lookupProperty(fruit, "goodColor2");
      assertNotNull(property2);
      assertEquals(true, umlReflection.isStatic(property2));

      ParsingOptions.setOption(ocl.getEnvironment(), ParsingOptions.SUPPORT_STATIC_FEATURES, false);
      try {
        ocl.parse(
            new OCLInput(
                "package ocltest context Fruit "
                    + "def: bestColor3() : Color = null "
                    + "static def: bestColor4() : Color = null "
                    + "endpackage"));
        fail("Should have failed to parse the unsupported static");
      } catch (ParserException e) {
        // success!
        assertEquals(OCLMessages.UnsupportedStatic_ERROR_, e.getMessage());
        System.out.println("Got the expected exception: " + e.getLocalizedMessage());
      }
    } catch (Exception e) {
      fail("Failed to parse or evaluate: " + e.getLocalizedMessage());
    }
  }
Exemple #2
0
  public void test_dotNotationForSymbolicOperationNames() {
    ParsingOptions.setOption(
        helper.getEnvironment(),
        ProblemOption.CONCEPTUAL_OPERATION_NAME,
        ProblemHandler.Severity.OK);
    helper.setContext(getUMLInteger());

    Integer minusOne = new Integer(-1);
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Double doubleTwo = new Double(2.0);
    Integer three = new Integer(3);
    Integer six = new Integer(6);

    try {
      // new NUMERIC_OPERATION token
      assertEquals(one, evaluate(helper, one, "3.-(2)"));
      assertEquals(three, evaluate(helper, one, "1.+(2)"));
      assertEquals(doubleTwo, evaluate(helper, one, "6./(3)"));
      assertEquals(six, evaluate(helper, one, "2.*(3)"));
      assertTrue(check(helper, one, "1.<(2)"));
      assertTrue(check(helper, one, "1.<=(2)"));
      assertTrue(check(helper, one, "2.>=(1)"));
      assertTrue(check(helper, one, "2.>(1)"));

      // new operationCallExpCS rule
      assertEquals(one, evaluate(helper, three, "self.-(2)"));
      assertEquals(three, evaluate(helper, one, "self.+(2)"));
      assertEquals(doubleTwo, evaluate(helper, six, "self./(3)"));
      assertEquals(six, evaluate(helper, two, "self.*(3)"));
      assertTrue(check(helper, one, "self.<(2)"));
      assertTrue(check(helper, one, "self.<=(2)"));
      assertTrue(check(helper, two, "self.>=(1)"));
      assertTrue(check(helper, two, "self.>(1)"));

      // unary minus
      assertEquals(minusOne, evaluate(helper, one, "-1"));
      assertEquals(minusOne, evaluate(helper, one, "-self"));
      assertEquals(minusOne, evaluate(helper, one, "self.\"-\"()"));
      assertEquals(minusOne, evaluate(helper, one, "self.-()"));
      assertEquals(one, evaluate(helper, one, "- self.\"-\"()"));
      assertEquals(one, evaluate(helper, one, "- self.-()"));
      assertEquals(one, evaluate(helper, one, "- -1"));
      assertEquals(one, evaluate(helper, one, "- -self"));

      // unary not
      helper.setContext(getUMLBoolean());
      assertEquals(Boolean.FALSE, evaluate(helper, Boolean.TRUE, "not self"));
      assertEquals(Boolean.FALSE, evaluate(helper, Boolean.TRUE, "self.\"not\"()"));
      assertEquals(Boolean.FALSE, evaluate(helper, Boolean.TRUE, "self.not()"));
      assertEquals(Boolean.TRUE, evaluate(helper, Boolean.TRUE, "not not self"));
      assertEquals(Boolean.TRUE, evaluate(helper, Boolean.TRUE, "not self.\"not\"()"));
      assertEquals(Boolean.TRUE, evaluate(helper, Boolean.TRUE, "not self.not()"));

    } catch (Exception e) {
      fail("Failed to parse or evaluate: " + e.getLocalizedMessage());
    }
    ParsingOptions.setOption(
        helper.getEnvironment(),
        ProblemOption.CONCEPTUAL_OPERATION_NAME,
        ProblemHandler.Severity.ERROR);
    try {
      assertEquals(one, evaluate(helper, one, "3.-(2)"));
      fail("Missing exception");
    } catch (Exception e) {
    }
  }