#!/usr/bin/env python
import unittest
import copy

from coral.exceptions import *
from coral.modeler import modeler,action
import coral
metamodel = coral.lang.UML14

TM = coral.getTransactionManager()

MW=modeler.ModelerWindow()
MW.main(loop=0)

class testWFR(unittest.TestCase):
    def testClass1(self):
        a=metamodel.Class()
        assert(a.isWellFormed())
        
    def testClass2(self):        
        a=metamodel.Class()
        a.isAbstract=0
        op=metamodel.Operation()
        me=metamodel.Method(specification=op)
        a.feature.insert(op)
        a.feature.insert(me)
        assert(a.isWellFormed())
        
    def testClass21(self):       
        a=metamodel.Class()
        a.isAbstract=0
        op=metamodel.Operation()
        a.feature.insert(op)
        self.failUnlessRaises(coral.WFRException,a.isWellFormed)

    
    def testClass3(self):
        st = action.newProject(["UML",1,4])
        a=metamodel.Class()
        a.__setProject__(st)
        a.isAbstract=1        
        TM.beginModifyCmd(1)
        try:
            a.isAbstract=0
            op=metamodel.Operation()
            a.feature.insert(op)
        finally:
            self.failUnlessRaises(coral.WFRException, TM.endModifyCmd)
        assert(a.isAbstract==1)
        
    def testState(self):
        st = action.newProject(["UML", 1, 4])
        sm=metamodel.StateMachine()
        sm.__setProject__(st)
        top=metamodel.CompositeState()
        sm.top=top
        top.subvertex.insert(metamodel.Pseudostate(kind=metamodel.PseudostateKind.pk_initial))
        try:
            TM.beginModifyCmd(1)
            try:
                top.subvertex.insert(metamodel.Pseudostate(kind=metamodel.PseudostateKind.pk_initial))
                print 
                assert(top.subvertex.size()==2)
            finally:
                TM.endModifyCmd()
        except coral.WFRException,e:
            print "You got an exception (it was expected): ",e
        assert(top.subvertex.size()==1)

       
if __name__ == '__main__':
    unittest.main()
