Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » JUnit Test UML Model setValue IllegalArgumentException
JUnit Test UML Model setValue IllegalArgumentException [message #1835325] Mon, 30 November 2020 17:41 Go to next message
Christoph Lehr is currently offline Christoph LehrFriend
Messages: 24
Registered: June 2020
Junior Member
Hi all,

I want to test my Papyrus plugin with JUnit and therefore I need to create a new UML model, apply a stereotype and load a library which contains a list of defined types.

While I managed to get everything running to a point, I'm facing some issues with creating References to other Elements. If I want to set the reference via setValue I get a IllegalArgumentException, but that does not happen if I execute the code via Eclipse/Papyrus.

Class varElement  = (Class) this.nodeIdMap.get(var.getNodeId());
		Stereotype uaInstance = getMatchingStereotype(var);
		
		Object dataTypeObject = getUmlNodeReference(var.getDataType());
		boolean success = true;
		
		if(dataTypeObject == null)
		{
			success = false;
		}
		else
		{				
			varElement.setValue(uaInstance, "dataType", dataTypeObject);
		}
		
		return success;


java.lang.IllegalArgumentException: org.eclipse.emf.ecore.impl.DynamicEObjectImpl@44dd5076 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@777c350f (name: UADataType) (instanceClassName: null) (abstract: false, interface: false))
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.setValue(ElementOperations.java:768)
	at org.eclipse.uml2.uml.internal.impl.ElementImpl.setValue(ElementImpl.java:307)
	at at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation.opcuatouml.OpcUaToUmlTransformer.transformUaDatatype(OpcUaToUmlTransformer.java:2436)
....


The following code is how I initialize my test model, as this is the part that I needed to code on my own, I guess that the Error is here.
public class ModelCreator {

	private static final String UML_PROFILE_PATH ="../at.ac.tuwien.auto.modelua.papyrus.opcua.profile/resources/profile/UANodeSet.profile.uml";
	private static final String BASENAMESPACE_LIBRARY_PATH ="../at.ac.tuwien.auto.modelua.papyrus.opcua.libraries/resources/libraries/Opc.Ua.NodeSet2/Opc.Ua.NodeSet2.uml";
	
	public static Model createEmptyTestModel()
	{
		Model umlTestModel = UMLFactory.eINSTANCE.createModel();
		
		ResourceSet owner_resource = new ResourceSetImpl();
		UMLResourcesUtil.init(owner_resource);
			
		initializeNodeSet(umlTestModel, owner_resource);
		loadBaseNodeSet(umlTestModel, owner_resource);
		
		return umlTestModel;
	}
	
	private static void initializeNodeSet(Model model, ResourceSet owner_resource)
	{
		URI uri_path = URI.createURI(UML_PROFILE_PATH);
		Profile basetypesProfile = (Profile) PackageUtil.loadPackage(uri_path, owner_resource);
		if (basetypesProfile != null) {
			PackageUtil.applyProfile(model, basetypesProfile, true);
		}
		Stereotype nodeSetType   = basetypesProfile.getOwnedStereotype("UANodeSetType");
		model.applyStereotype(nodeSetType);
	}
	
	private static void loadBaseNodeSet(Model model, ResourceSet owner_resource)
	{
		// TODO: do not import Base Namespace if base namespace is beeing updated
		URI lib_path = URI.createURI(BASENAMESPACE_LIBRARY_PATH);
		
		Map<URI,URI> uriMap = owner_resource.getURIConverter().getURIMap();
		
		// override wrong pathmap mapping in UMLResourcesUtil
		final URL UMLJarredFileLocation = ResourcesPlugin.class.getResource("ResourcesPlugin.class");
		String UMLJarPath = UMLJarredFileLocation.toString();
		UMLJarPath = UMLJarPath.substring(0, UMLJarPath.indexOf('!'));	
		
		
		URI jar_path;
		try {
			jar_path = URI.createURI(UMLJarredFileLocation.toURI().toString());
			uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),  URI.createURI(UMLJarPath+ "!/libraries/"));
			uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), URI.createURI(UMLJarPath+ "!/metamodels/"));
			uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), URI.createURI(UMLJarPath+ "!/profiles/"));
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		uriMap.put(URI.createURI("pathmap://OPC_UA_PROFILES/UANodeSet.profile.uml"), URI.createURI(UML_PROFILE_PATH));
		Package defaultNs = PackageUtil.loadPackage(lib_path, owner_resource);
		
		EList<Stereotype> ster = defaultNs.getOwnedElements().get(0).getAppliedStereotypes();
		
		if (defaultNs != null) {
			model.createPackageImport(defaultNs);
		}
	}
	
}




Is there something I missed or I did wrong?

Thanks for your help

Edit 1: fixed typo
Edit2: Added code that throws the excpetion

[Updated on: Tue, 01 December 2020 07:19]

Report message to a moderator

Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835747 is a reply to message #1835325] Fri, 11 December 2020 12:05 Go to previous messageGo to next message
Christoph Lehr is currently offline Christoph LehrFriend
Messages: 24
Registered: June 2020
Junior Member
Anybody has any idea what I could try?
I'd be happy for every point, every approach I could look at.

[Updated on: Fri, 11 December 2020 12:05]

Report message to a moderator

Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835752 is a reply to message #1835747] Fri, 11 December 2020 14:23 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 104
Registered: December 2015
Location: Grenoble, France
Senior Member
Hello,

Well, it is difficult to help with the information you provide, and without some artifacts to reproduce your problem.

There are so many things going on in your code, that one can only second guess what may go wrong.

You seem at the beginning to be loading a profile and a library. This seems ok, except for the weird code in loadBaseNodeSet where you override the URI mappings done by UMLResourcesUtil. Why are you doing that?

Then you create an empty model, import the library and apply the profile. That also seems ok.

Then you probably create some classes in the model and apply the stereotypes (guessing, not shown in your code). Finally, at the point of the exception you try to set a value to the applied stereotype.

At this point many things can go wrong :

- are you sure that you have applied the stereotype (uaInstance ) to the element before trying to set the value?
- what is the type of the property ""dataType"" of the stereotype ?
- are you sure you are passing a value of the expected class of the property?

This last point seems important, the exception is thrown in org.eclipse.uml2.uml.internal.operations.ElementOperations.setValue(ElementOperations.java:768) and if one looks at that code line (in UML version 5.4, I do not know which version are you using) there is a test
that the passed value is actually an instance of the EClass of the type of the property.

How are you creating "dataTypeObject "? you have to be sure that you are using the Eclass that is referenced from the defined package in your profile

As I said, difficult to help with so many guesses, have you tried to debug your code? sometimes the debugger is the best help you can get

If you can share a simplified version of your project, I can take a look, but without any way to reproduce the problem I can only point you to the things that may go wrong.

I am not a committer of the UML project, but I have experience with profile application in standalone applications, and never encountered this kind of problem



Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835835 is a reply to message #1835752] Mon, 14 December 2020 15:19 Go to previous messageGo to next message
Christoph Lehr is currently offline Christoph LehrFriend
Messages: 24
Registered: June 2020
Junior Member
Hi,

thanks for your reply German Vega.

Quote:
This seems ok, except for the weird code in loadBaseNodeSet where you override the URI mappings done by UMLResourcesUtil. Why are you doing that?

I got that from a forum post here, unfortunately I can't find it at the moment.

Quote:

At this point many things can go wrong :
- are you sure that you have applied the stereotype (uaInstance ) to the element before trying to set the value?
- what is the type of the property ""dataType"" of the stereotype ?
- are you sure you are passing a value of the expected class of the property?

- Yes, the correct stereotype is applied. Setting of all other values which are not References to other Class work
- The type is class with the Stereotype UANode applied to it ( or a Stereotype which inherits UANode)
- I compared what I handing over inside Papyrus and that matches

Quote:
(in UML version 5.4, I do not know which version are you using)

I'm using 5.5.

Quote:
How are you creating "dataTypeObject "? you have to be sure that you are using the Eclass that is referenced from the defined package in your profile

The underlying element behind "dataTypeObject" is a class created as new owned class and then the corresponding stereotype is applied to it. dataTypeObject is then the result of getStereotypeApplication.

Quote:
As I said, difficult to help with so many guesses, have you tried to debug your code? sometimes the debugger is the best help you can get

I already spent some time debugging that portion in Papyrus and when running the code as standalone, until know I didn't see any major difference in the objects.

Quote:
If you can share a simplified version of your project, I can take a look, but without any way to reproduce the problem I can only point you to the things that may go wrong.

That may take a little more time to create that, but for now my project is located here: https://github.com/model-UA/papyrus-opcua-plugin
The relevant code is in branch feature/#55-setup-junit.
The JUnit code is here https://github.com/model-UA/papyrus-opcua-plugin/tree/feature/%2355-setup-junit/at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation.tests where this is my test case that fails https://github.com/model-UA/papyrus-opcua-plugin/blob/63cf4672a9662771e904d3e94261479afcb36afc/at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation.tests/src/at/ac/tuwien/auto/modelua/papyrus/opcua/diagram/transformation/opcuatouml/simple/SimpleOpcUaToUmlTests.java#L289.
And this is where the model is created. at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation.tests/src/at/ac/tuwien/auto/modelua/papyrus/opcua/diagram/transformation/util/ModelCreator.java

The loaded UML Profile is located here : https://github.com/model-UA/papyrus-opcua-plugin/tree/63cf4672a9662771e904d3e94261479afcb36afc/at.ac.tuwien.auto.modelua.papyrus.opcua.profile/resources/profile

The loaded UML Library is located here: https://github.com/model-UA/papyrus-opcua-plugin/tree/63cf4672a9662771e904d3e94261479afcb36afc/at.ac.tuwien.auto.modelua.papyrus.opcua.libraries/resources/libraries/Opc.Ua.NodeSet2

The file where everything happens is at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation/src/at/ac/tuwien/auto/modelua/papyrus/opcua/diagram/transformation/opcuatouml/OpcUaToUmlTransformer.java
The relevant code for the error should start at this function call https://github.com/model-UA/papyrus-opcua-plugin/blob/63cf4672a9662771e904d3e94261479afcb36afc/at.ac.tuwien.auto.modelua.papyrus.opcua.diagram.transformation/src/at/ac/tuwien/auto/modelua/papyrus/opcua/diagram/transformation/opcuatouml/OpcUaToUmlTransformer.java#L432

I don't know if the linked code helps you, but I try to construct a minimal example to reproduce the bug.

BR Christoph
Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835858 is a reply to message #1835835] Tue, 15 December 2020 10:44 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 104
Registered: December 2015
Location: Grenoble, France
Senior Member
Hello Christoph,

I just cloned your repository from the github, I am trying to setup the environment (I do not use the latest versions of Eclipse, so I don't have in my target platform the same versions of UML, Papyrus, JUnit, etc,etc as you).

I will try to run your JUnit tests directly , but don't expect a quick response I am doing this in my out of work time that is limited this end of year, If you can build a minimal example that will be far easier.

G. Vega
Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835886 is a reply to message #1835835] Tue, 15 December 2020 18:38 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 104
Registered: December 2015
Location: Grenoble, France
Senior Member
Hello Christoph,

Finally I got the environment set faster than expected.

I debugged your test case, and I think all your problems arise because your profile is defined several times.

Take a look you look at the attached image.

index.php/fa/39562/0/

You will notice on the left of the figure that you have 66 definitions of your profile.

Every time you define a profile, a new EPackage is created that defines the EClasses that are instantiated when you apply the stereotype. For example, you can notice that there are two EPackages NodeSet (version 66 numbered 1 and version 65 numbered 2) in the figure, with two different EClasses UAnode (numbered 3 and 4 respectively in the figure).

I looked at your library (Opc.Ua.NodeSet2/Opc.Ua.NodeSet2.uml) and it applies version 63 of the stereotypes of the profile.

In your test case, you create your model using the latest version of the profile, then apply the stereotypes to the model. This will create instances of version 66 of the EClass UAVariable.

Then you load your library (in method ModelCreator.loadBaseNodeSet), this will create instances of version 63 of the EClass UADataType to represent the applied stereotypes.

Then, you start your transformation and at some point it tries to set the value of the 'datatype' reference (in method OpcUaToUmlTransformer.transformUaDatatype), EMF verifies that the value that you are trying to set (dataTypeObject) is of type UAnode. The problem is that EMF is validating against version 66 of UAnode and your object is an instance of version 63 of UADataType that inherits from version 63 of UAnode. And from the point of view of EMF they are not the same class.

This is for the diagnostic, how to fix it?

1) The first thing that I noticed is that you programatically define the profile (in method ModelCreator.initializeNodeSet see number 4 in the figure). Do not do that, this creates yet another version of the EPackage. I commented this part of the code, as you can see in the figure.

2) The second thing that I did was to manually upgrade the library (see number 5 in the figure) to use the latest version (66) of the defined profile.

Once I did that, I could run the Junit test and it passed (see number 6). I only tried this test case, and run it manually (without maven). It may be other problems, I did not tried to understand your code, I simply tried to get to the point of the exception.

The concept of versioning of the definition of a Profile is something that Papyrus added to enable loading of models that use old versions of the profile, it is something that IS NOT specified in the UML standard. I personally do not use it, so I can not give you any advice about the best way to keep synchronized the version in the profile and the version in the library. I also have no idea why it works when you do it in Papyrus by hand, perhaps when you import the library it automatically migrate it to the latest version of the definition. You could try to ask the question in the Papyrus forum.

Personally I only keep one definition for the profiles I distribute. When I do modifications to the profile that are backward compatible (adding stereotypes or attributes for instance) I delete the previous definition and define it again. If someone opens a model that uses the old definition in Papyrus, it will ask to upgrade to the latest version and help him do it automatically. In standalone programs, to be sure that I can load models with old definitions, I first load the profile in the ResourceSet and then add the definition of the profile to the package registry to be sure that it will be used when loading the models, like this:

	/**
	 * Get the defined versions of the profile and register the corresponding package in the ResourceSet, so that
	 * models that apply the profile will reference the specified profile when loaded
	 * 
	 */
	private static void init(ResourceSet resources, Profile profile) {
		
		EPackage definitionPackage = (EPackage) profile.getDefinition();
		resources.getPackageRegistry().put(definitionPackage.getNsURI(),definitionPackage);
		UMLPlugin.getEPackageNsURIToProfileLocationMap().put(definitionPackage.getNsURI(), EcoreUtil.getURI(profile)); 	
		
	}


It just popped up to my mind that this trick may also solve your problem. Try to do this in ModelCreator after you load the profile but before loading the library. I have never used it with a profile with several definitions, but it may work, and in that case you do not have to modify your library.

When I do backward incompatible modifications to the profile (removing or renaming stereotypes or attributes), I distribute a new version of the profile with a different nsURI and I provide a tool to help the users to manually migrate.

That is it, hope this helps you going on

G. vega

[Updated on: Wed, 16 December 2020 12:56]

Report message to a moderator

Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835916 is a reply to message #1835886] Wed, 16 December 2020 12:30 Go to previous messageGo to next message
Christoph Lehr is currently offline Christoph LehrFriend
Messages: 24
Registered: June 2020
Junior Member
Hi,

many thanks that you looked through my code.
I'm currently a little low on time to work on the project but I will check it as soon as I can.
Again many thanks for your time.

BR Christoph
Re: JUnit Test UML Model setValue IllegalArgumentException [message #1835971 is a reply to message #1835916] Thu, 17 December 2020 13:39 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
Messages: 104
Registered: December 2015
Location: Grenoble, France
Senior Member
Hi Christoph,

No problem spending some time on your problem, my two cents back to the community.

I tried the trick of forcing the latest defined package of the Profile when loading the library (bypassing the version used by the library) and it seems to work. But as mentioned in the previous message, you have to be sure that the latest definition is backwards compatible (no removing or renaming of stereotypes, classes or properties) with the one applied to the library (otherwise you will get inconsistent behavior, like some stereotypes applied while others not, or exceptions on loading).

With this approach, the only think you have to change is the ModelCreator, I attach you the version I modified.

I also commented a lot of code in ModelCreator that seemed weird or unnecessary to me (the commented sections are marked by @author vega). I ran the full testsuite and it seemed to work (there is one test that fails, but seems to be an unrelated problem). But I only ran it by hand from Eclipse (run as -> Junit) and not using the maven build.

If you need explanations when you will look at the project, don't hesitate to continue this thread

G. Vega

Re: JUnit Test UML Model setValue IllegalArgumentException [message #1836015 is a reply to message #1835971] Fri, 18 December 2020 09:30 Go to previous message
Christoph Lehr is currently offline Christoph LehrFriend
Messages: 24
Registered: June 2020
Junior Member
Hi,

I just implemented your fix from your prior post in my code and it works. I checked your modified version of my ModelCreator and as I expected most code you commented code was code I copied and adapted in desperation.

I'm so annoyed that I didn't think that the issue was caused by having different versions of the profile. I ran some times into the same issue in other places in Papyrus, but it had a different
manifestation.

BR Christoph
Previous Topic:problems saving stereotypes
Next Topic:Instance in class diagram
Goto Forum:
  


Current Time: Fri Apr 19 02:55:28 GMT 2024

Powered by FUDForum. Page generated in 0.02485 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top