@@ -22,57 +22,128 @@ public class TestNodeIds
2222
2323 #region Tests
2424 [ TestMethod ]
25- [ DataRow ( "GuidNodeIdWithAcutalGuidId" , "0EB66E95-DCED-415F-B8EC-43ED3F0C759B" , IdType . Guid ) ]
26- [ DataRow ( "NumericNodeIdWithActualNumericId" , "12345" , IdType . Numeric ) ]
27- [ DataRow ( "OpaqueNodeIdWithActualOpaqueId" , "T3BhcXVlTm9kZUlk" , IdType . Opaque ) ]
28- [ DataRow ( "StringNodeIdWithActualStringId" , "StringNodeId" , IdType . String ) ]
29- public void TestNodeIdentifierTypes ( string internelElemantName , string nodeId , IdType idType )
25+ [ DataRow ( "GuidNodeIdWithActualGuidId" , "0EB66E95-DCED-415F-B8EC-43ED3F0C759B" , IdType . Guid ) ]
26+ [ DataRow ( "NumericNodeIdWithActualNumericId" , "12345" , IdType . Numeric ) ]
27+ [ DataRow ( "OpaqueNodeIdWithActualOpaqueId" , "T3BhcXVlTm9kZUlk" , IdType . Opaque ) ]
28+ [ DataRow ( "StringNodeIdWithActualStringId" , "StringNodeId" , IdType . String ) ]
29+ [ DataRow ( "ThereIsNoValue" , "ThereIsNoValue" , IdType . String , false ) ]
30+ public void TestNodeIdentifierTypes ( string internalElementName , string nodeId , IdType idType , bool hasValue = true )
3031 {
31- InternalElementType testInternalElement = findInternalElementByName ( internelElemantName ) ;
32- Assert . IsNotNull ( testInternalElement , "Could not find test object" ) ;
33- var nodeIdAttribute = testInternalElement . Attribute . FirstOrDefault ( childElement => childElement . Name == "NodeId" ) ;
34- Assert . IsNotNull ( nodeIdAttribute , "Unable to find nodeId attribute" ) ;
35- var rootNodeIdAttribute = nodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "RootNodeId" ) ;
36- Assert . IsNotNull ( rootNodeIdAttribute , "Unable to find rootNodeId attribute" ) ;
37- switch ( idType )
32+ InternalElementType testInternalElement = findInternalElementByName ( internalElementName ) ;
33+ Assert . IsNotNull ( testInternalElement , "Could not find test object" ) ;
34+
35+ var nodeIdAttribute = testInternalElement . Attribute . FirstOrDefault ( childElement => childElement . Name == "NodeId" ) ;
36+ Assert . IsNotNull ( nodeIdAttribute , "Unable to find nodeId attribute" ) ;
37+ var rootNodeIdAttribute = nodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "RootNodeId" ) ;
38+ Assert . IsNotNull ( rootNodeIdAttribute , "Unable to find rootNodeId attribute" ) ;
39+
40+ string idName = Enum . GetName ( typeof ( IdType ) , idType ) + "Id" ;
41+
42+ AttributeType specificAttribute = rootNodeIdAttribute . Attribute . FirstOrDefault (
43+ childElement => childElement . Name == idName ) ;
44+ Assert . IsNotNull ( specificAttribute , "Unable to find " + idName + " attribute" ) ;
45+ Assert . AreEqual ( nodeId , specificAttribute . Value . ToString ( ) , true ) ;
46+ ValidateNodeId ( rootNodeIdAttribute , idType ) ;
47+
48+ AttributeType valueAttribute = testInternalElement . Attribute [ "Value" ] ;
49+ Assert . IsNotNull ( valueAttribute , "Could not find test value object" ) ;
50+ AttributeType valueRootNodeId = valueAttribute . Attribute [ "RootNodeId" ] ;
51+ Assert . IsNotNull ( valueRootNodeId , "Could not find test value object" ) ;
52+
53+ AttributeType specificValueAttribute = valueRootNodeId . Attribute . FirstOrDefault (
54+ childElement => childElement . Name == idName ) ;
55+ Assert . IsNotNull ( specificValueAttribute , "Unable to find " + idName + " value attribute" ) ;
56+ if ( hasValue )
3857 {
39- case IdType . Opaque :
40- var opaqueNodeIdAttribute = rootNodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "OpaqueId" ) ;
41- Assert . IsNotNull ( opaqueNodeIdAttribute , "Unable to find opaqueNodeId attribute" ) ;
42- Assert . AreEqual ( nodeId , opaqueNodeIdAttribute . Value . ToString ( ) , true ) ;
58+ Assert . AreEqual ( nodeId , specificValueAttribute . Value . ToString ( ) , true ) ;
59+ ValidateNodeId ( valueRootNodeId , idType ) ;
60+ }
61+ else
62+ {
63+ ValidNoNode ( valueRootNodeId ) ;
64+ }
65+ }
66+
67+ private void ValidateNodeId ( AttributeType rootNodeId , IdType idType )
68+ {
69+ AttributeType namespaceUri = rootNodeId . Attribute [ "NamespaceUri" ] ;
70+ Assert . IsNotNull ( namespaceUri ) ;
71+ Assert . IsNotNull ( namespaceUri . Value ) ;
72+ Assert . AreNotEqual ( 0 , namespaceUri . Value . Length ) ;
73+
74+ AttributeType numericId = rootNodeId . Attribute [ "NumericId" ] ;
75+ AttributeType stringId = rootNodeId . Attribute [ "StringId" ] ;
76+ AttributeType guidId = rootNodeId . Attribute [ "GuidId" ] ;
77+ AttributeType opaqueId = rootNodeId . Attribute [ "OpaqueId" ] ;
78+
79+ switch ( idType )
80+ {
81+ case IdType . Numeric :
82+ Assert . IsNotNull ( numericId ) ;
83+ Assert . IsNotNull ( numericId . Value ) ;
84+ Assert . AreNotEqual ( 0 , numericId . Value . Length ) ;
85+ Assert . IsNull ( stringId ) ;
86+ Assert . IsNull ( guidId ) ;
87+ Assert . IsNull ( opaqueId ) ;
4388 break ;
4489
4590 case IdType . String :
46- var stringNodeIdAttribute = rootNodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "StringId" ) ;
47- Assert . IsNotNull ( stringNodeIdAttribute , "Unable to find stringNodeId attribute" ) ;
48- Assert . AreEqual ( nodeId , stringNodeIdAttribute . Value . ToString ( ) ) ;
91+ Assert . IsNull ( numericId ) ;
92+ Assert . IsNotNull ( stringId ) ;
93+ Assert . IsNotNull ( stringId . Value ) ;
94+ Assert . AreNotEqual ( 0 , stringId . Value . Length ) ;
95+ Assert . IsNull ( guidId ) ;
96+ Assert . IsNull ( opaqueId ) ;
4997 break ;
5098
5199 case IdType . Guid :
52- var guidNodeIdAttribute = rootNodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "GuidId" ) ;
53- Assert . IsNotNull ( guidNodeIdAttribute , "Unable to find guidNodeId attribute" ) ;
54- Assert . AreEqual ( nodeId , guidNodeIdAttribute . Value . ToString ( ) , true ) ;
100+ Assert . IsNull ( numericId ) ;
101+ Assert . IsNull ( stringId ) ;
102+ Assert . IsNotNull ( guidId ) ;
103+ Assert . IsNotNull ( guidId . Value ) ;
104+ Assert . AreNotEqual ( 0 , guidId . Value . Length ) ;
105+ Assert . IsNull ( opaqueId ) ;
55106 break ;
56107
57- case IdType . Numeric :
58- var numericNodeIdAttribute = rootNodeIdAttribute . Attribute . FirstOrDefault ( childElement => childElement . Name == "NumericId" ) ;
59- Assert . IsNotNull ( numericNodeIdAttribute , "Unable to find numericNodeId attribute" ) ;
60- Assert . AreEqual ( nodeId , numericNodeIdAttribute . Value . ToString ( ) ) ;
108+ case IdType . Opaque :
109+ Assert . IsNull ( numericId ) ;
110+ Assert . IsNull ( stringId ) ;
111+ Assert . IsNull ( guidId ) ;
112+ Assert . IsNotNull ( opaqueId ) ;
113+ Assert . IsNotNull ( opaqueId . Value ) ;
114+ Assert . AreNotEqual ( 0 , opaqueId . Value . Length ) ;
61115 break ;
62116 }
63117 }
118+
119+ private void ValidNoNode ( AttributeType rootNodeId )
120+ {
121+ ValidateAttributeEmpty ( rootNodeId , "NamespaceUri" ) ;
122+ ValidateAttributeEmpty ( rootNodeId , "NumericId" ) ;
123+ ValidateAttributeEmpty ( rootNodeId , "StringId" ) ;
124+ ValidateAttributeEmpty ( rootNodeId , "GuidId" ) ;
125+ ValidateAttributeEmpty ( rootNodeId , "OpaqueId" ) ;
126+ }
127+
128+ private void ValidateAttributeEmpty ( AttributeType source , string attributeName )
129+ {
130+ AttributeType attribute = source . Attribute [ attributeName ] ;
131+ Assert . IsNotNull ( attribute ) ;
132+ Assert . IsNull ( attribute . Value ) ;
133+ }
134+
64135 #endregion
65136
66137 #region Helpers
67138
68- public InternalElementType ? findInternalElementByName ( string internelElemantName )
139+ public InternalElementType ? findInternalElementByName ( string internelElemantName )
69140 {
70- foreach ( var instanceHierarchy in GetDocument ( ) . CAEXFile . InstanceHierarchy )
141+ foreach ( var instanceHierarchy in GetDocument ( ) . CAEXFile . InstanceHierarchy )
71142 {
72143 // browse all InternalElements deep and find element with name "FxRoot"
73- foreach ( var internalElement in instanceHierarchy . Descendants < InternalElementType > ( ) )
144+ foreach ( var internalElement in instanceHierarchy . Descendants < InternalElementType > ( ) )
74145 {
75- if ( internalElement . Name . Equals ( internelElemantName ) ) return internalElement ;
146+ if ( internalElement . Name . Equals ( internelElemantName ) ) return internalElement ;
76147 }
77148 }
78149
@@ -90,7 +161,6 @@ private CAEXDocument GetDocument()
90161 return m_document ;
91162 }
92163
93-
94164 #endregion
95165 }
96166}
0 commit comments