public void WriteXmlTest() { var fs = new FeatureSchema("Foo", "Bar"); Assert.AreEqual("Foo", fs.Name); Assert.AreEqual("Bar", fs.Description); Assert.AreEqual(0, fs.Classes.Count); var cls = new ClassDefinition("Class1", "Test Class"); var id = new DataPropertyDefinition("ID", ""); id.DataType = DataPropertyType.Int32; id.IsAutoGenerated = true; var name = new DataPropertyDefinition("Name", ""); cls.AddProperty(id, true); cls.AddProperty(name); fs.AddClass(cls); var doc = new XmlDocument(); fs.WriteXml(doc, doc); string xml = doc.ToXmlString(); Assert.False(String.IsNullOrEmpty(xml)); }
public void CloneTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; var prop2 = new DataPropertyDefinition("Prop1", ""); Assert.Null(cls.FindProperty("ID")); cls.AddProperty(prop, true); Assert.Null(cls.FindProperty("Prop1")); cls.AddProperty(prop2); Assert.AreEqual(cls, prop.Parent); Assert.AreEqual(cls, prop2.Parent); Assert.AreEqual(2, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); var cls2 = ClassDefinition.Clone(cls); Assert.AreEqual(cls.Name, cls2.Name); Assert.AreEqual(cls.Description, cls2.Description); Assert.AreEqual(cls.Properties.Count, cls2.Properties.Count); Assert.AreEqual(cls.IdentityProperties.Count, cls2.IdentityProperties.Count); Assert.AreNotSame(cls, cls2); }
public void RemovePropertyAtTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; var prop2 = new DataPropertyDefinition("Prop1", ""); cls.AddProperty(prop, true); cls.AddProperty(prop2); Assert.AreEqual(cls, prop.Parent); Assert.AreEqual(cls, prop2.Parent); Assert.AreEqual(2, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); int idx1 = cls.GetOrdinal("Prop1"); Assert.GreaterOrEqual(idx1, 0); cls.RemovePropertyAt(idx1); Assert.AreEqual(1, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.Less(cls.IndexOfProperty(prop2), 0); int idx2 = cls.GetOrdinal("ID"); Assert.GreaterOrEqual(idx2, 0); cls.RemovePropertyAt(idx2); Assert.AreEqual(0, cls.Properties.Count); Assert.AreEqual(0, cls.IdentityProperties.Count); Assert.Less(cls.IndexOfProperty(prop), 0); }
public void RemovePropertyTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; var prop2 = new DataPropertyDefinition("Prop1", ""); cls.AddProperty(prop, true); cls.AddProperty(prop2); Assert.Equal(cls, prop.Parent); Assert.Equal(cls, prop2.Parent); Assert.Equal(2, cls.Properties.Count); Assert.Single(cls.IdentityProperties); cls.RemoveProperty("Prop1"); Assert.Single(cls.Properties); Assert.Single(cls.IdentityProperties); Assert.True(cls.IndexOfProperty(prop2) < 0); cls.RemoveProperty(prop); Assert.Empty(cls.Properties); Assert.Empty(cls.IdentityProperties); Assert.True(cls.IndexOfProperty(prop) < 0); }
public void CloneTest() { var fs = new FeatureSchema("Foo", "Bar"); Assert.AreEqual("Foo", fs.Name); Assert.AreEqual("Bar", fs.Description); Assert.AreEqual(0, fs.Classes.Count); var cls = new ClassDefinition("Class1", "Test Class"); var id = new DataPropertyDefinition("ID", ""); id.DataType = DataPropertyType.Int32; id.IsAutoGenerated = true; var name = new DataPropertyDefinition("Name", ""); cls.AddProperty(id, true); cls.AddProperty(name); fs.AddClass(cls); var fs2 = FeatureSchema.Clone(fs); Assert.AreEqual(fs.Name, fs2.Name); Assert.AreEqual(fs.Description, fs2.Description); Assert.AreEqual(fs.Classes.Count, fs2.Classes.Count); Assert.AreNotSame(fs, fs2); }
public void Build_CreatesClassWithCompleteContentType() { var classDefinition = new ClassDefinition("Complete content type"); classDefinition.AddProperty(Property.FromContentType("text", "text")); classDefinition.AddProperty(Property.FromContentType("rich_text", "rich_text")); classDefinition.AddProperty(Property.FromContentType("rich_text_structured", "rich_text(structured)")); classDefinition.AddProperty(Property.FromContentType("number", "number")); classDefinition.AddProperty(Property.FromContentType("multiple_choice", "multiple_choice")); classDefinition.AddProperty(Property.FromContentType("date_time", "date_time")); classDefinition.AddProperty(Property.FromContentType("asset", "asset")); classDefinition.AddProperty(Property.FromContentType("modular_content", "modular_content")); classDefinition.AddProperty(Property.FromContentType("taxonomy", "taxonomy")); classDefinition.AddProperty(Property.FromContentType("url_slug", "url_slug")); classDefinition.AddSystemProperty(); var classCodeGenerator = new ClassCodeGenerator(classDefinition, classDefinition.ClassName); string compiledCode = classCodeGenerator.GenerateCode(); string executingPath = AppContext.BaseDirectory; string expectedCode = File.ReadAllText(executingPath + "/Assets/CompleteContentType_CompiledCode.txt"); Assert.Equal(expectedCode, compiledCode, ignoreWhiteSpaceDifferences: true, ignoreLineEndingDifferences: true); }
protected void CreateTestDataStore(IServerConnection conn, string fsId, ref FeatureSchema schema, ref ClassDefinition cls) { schema = new FeatureSchema("Default", ""); cls = new ClassDefinition("Class1", ""); try { if (conn.ResourceService.ResourceExists(fsId)) { conn.ResourceService.DeleteResource(fsId); } cls.DefaultGeometryPropertyName = "GEOM"; cls.AddProperty(new DataPropertyDefinition("KEY", "") { DataType = DataPropertyType.Int32, IsAutoGenerated = true, IsReadOnly = true, IsNullable = false }, true); cls.AddProperty(new DataPropertyDefinition("NAME", "") { DataType = DataPropertyType.String, Length = 255, IsNullable = true, IsReadOnly = false }); cls.AddProperty(new GeometricPropertyDefinition("GEOM", "") { GeometricTypes = FeatureGeometricType.Point, SpatialContextAssociation = "Default" }); schema.AddClass(cls); ICreateDataStore create = (ICreateDataStore)conn.CreateCommand((int)CommandType.CreateDataStore); CoordinateSystemDefinitionBase coordSys = conn.CoordinateSystemCatalog.FindCoordSys("LL84"); create.FeatureSourceId = fsId; create.CoordinateSystemWkt = coordSys.WKT; create.Name = "Default"; create.ExtentType = OSGeo.MapGuide.ObjectModels.Common.FdoSpatialContextListSpatialContextExtentType.Dynamic; create.FileName = "Test.sdf"; create.Provider = "OSGeo.SDF"; create.Schema = schema; create.XYTolerance = 0.001; create.ZTolerance = 0.001; create.Execute(); } catch { schema = null; cls = null; throw; } }
public void AddProperty_DuplicateElementCodenames_Throws() { var classDefinition = new ClassDefinition("Class name"); classDefinition.AddProperty(Property.FromContentTypeElement("element", "text")); Assert.Throws <InvalidOperationException>(() => classDefinition.AddProperty(Property.FromContentTypeElement("element", "text"))); Assert.Single(classDefinition.Properties); }
public void AddElement_ThrowsAnExceptionWhenAddingElementWithSameCodename() { var definition = new ClassDefinition("Class name"); definition.AddProperty(Property.FromContentType("element", "text")); Assert.Throws <InvalidOperationException>(() => definition.AddProperty(Property.FromContentType("element", "text"))); Assert.Single(definition.Properties); }
public static ClassDefinition ConvertClassDefinition(MgClassDefinition mgClass) { var cls = new ClassDefinition(mgClass.Name, mgClass.Description); //foreach (var prop in mgClass.GetProperties()) var props = mgClass.GetProperties(); int pcount = props.GetCount(); for (int i = 0; i < pcount; i++) { var prop = props.GetItem(i); switch (prop.PropertyType) { case MgFeaturePropertyType.DataProperty: MgDataPropertyDefinition mgData = (MgDataPropertyDefinition)prop; var dp = ConvertDataProperty(mgData); //API Bug? passing object reference gives incorrect result for identity //properties bool identity = (mgClass.GetIdentityProperties().Contains(prop.Name)); cls.AddProperty(dp, identity); break; case MgFeaturePropertyType.GeometricProperty: MgGeometricPropertyDefinition mgGeom = (MgGeometricPropertyDefinition)prop; var geom = ConvertGeometricProperty(mgGeom); cls.AddProperty(geom); break; case MgFeaturePropertyType.RasterProperty: MgRasterPropertyDefinition mgRaster = (MgRasterPropertyDefinition)prop; var raster = ConvertRasterProperty(mgRaster); cls.AddProperty(raster); break; case MgFeaturePropertyType.ObjectProperty: break; case MgFeaturePropertyType.AssociationProperty: break; } } cls.DefaultGeometryPropertyName = mgClass.DefaultGeometryPropertyName; return(cls); }
public void TestFeatureArrayReader() { var fcount = 5; var clsDef = new ClassDefinition("Test", ""); clsDef.AddProperty(new DataPropertyDefinition("ID", "") { DataType = DataPropertyType.Int32 }, true); var features = Enumerable.Range(0, fcount).Select(i => { var feat = new Mock <IFeature>(); feat.Setup(f => f.ClassDefinition).Returns(clsDef); feat.Setup(f => f.FieldCount).Returns(1); feat.Setup(f => f.GetInt32(It.Is <string>(arg => arg == "ID"))).Returns(i); feat.Setup(f => f.GetInt32(It.Is <int>(arg => arg == 0))).Returns(i); return(feat.Object); }); var iterations = 0; var reader = new FeatureArrayReader(features.ToArray()); while (reader.ReadNext()) { Assert.Equal(1, reader.FieldCount); Assert.Equal(iterations, reader.GetInt32("ID")); Assert.Equal(iterations, reader.GetInt32(0)); Assert.Equal(PropertyValueType.Int32, reader.GetPropertyType(0)); Assert.Equal(PropertyValueType.Int32, reader.GetPropertyType("ID")); iterations++; } reader.Close(); Assert.Equal(fcount, iterations); }
public void IntegrationTest_GeneratedCodeCompilesWithoutErrors() { var definition = new ClassDefinition("Complete content type"); definition.AddProperty(Property.FromContentType("text", "text")); definition.AddProperty(Property.FromContentType("rich_text", "rich_text")); definition.AddProperty(Property.FromContentType("rich_text_structured", "rich_text(structured)")); definition.AddProperty(Property.FromContentType("number", "number")); definition.AddProperty(Property.FromContentType("multiple_choice", "multiple_choice")); definition.AddProperty(Property.FromContentType("date_time", "date_time")); definition.AddProperty(Property.FromContentType("asset", "asset")); definition.AddProperty(Property.FromContentType("modular_content", "modular_content")); definition.AddProperty(Property.FromContentType("taxonomy", "taxonomy")); definition.AddProperty(Property.FromContentType("custom", "custom")); var classCodeGenerator = new ClassCodeGenerator(definition, definition.ClassName); string compiledCode = classCodeGenerator.GenerateCode(); CSharpCompilation compilation = CSharpCompilation.Create( assemblyName: Path.GetRandomFileName(), syntaxTrees: new[] { CSharpSyntaxTree.ParseText(compiledCode) }, references: new[] { MetadataReference.CreateFromFile(typeof(Object).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(Delivery.DeliveryClientBuilder).GetTypeInfo().Assembly.Location) }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var ms = new MemoryStream()) { EmitResult result = compilation.Emit(ms); string compilationErrors = "Compilation errors:\n"; if (!result.Success) { IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (Diagnostic diagnostic in failures) { compilationErrors += String.Format("{0}: {1}\n", diagnostic.Id, diagnostic.GetMessage()); } } Assert.True(result.Success, compilationErrors); } }
public void AddElement_AddsValidElement() { var classDefinition = new ClassDefinition("Class name"); classDefinition.AddProperty(Property.FromContentType("element_1", "text")); Assert.Single(classDefinition.Properties); }
public void AddProperty_AddCustomProperty_PropertyIsAdded() { var propertyCodename = "element_1"; var classDefinition = new ClassDefinition("Class name"); classDefinition.AddProperty(Property.FromContentTypeElement(propertyCodename, "text")); Assert.Single(classDefinition.Properties, property => property.Codename == propertyCodename); }
public void AddElement_RewritesSystemFieldWithUsersCustomOne() { var definition = new ClassDefinition("Class name"); var userDefinedSystemProperty = Property.FromContentType("system", "text"); definition.AddProperty(userDefinedSystemProperty); Assert.Equal(userDefinedSystemProperty, definition.Properties.First()); }
public void AddProperty_CustomSystemField_SystemFieldIsReplaced() { var classDefinition = new ClassDefinition("Class name"); var userDefinedSystemProperty = Property.FromContentTypeElement("system", "text"); classDefinition.AddProperty(userDefinedSystemProperty); Assert.Equal(userDefinedSystemProperty, classDefinition.Properties.First()); }
private WmsConfigurationDocument BuildDefaultWmsDocument() { var doc = new WmsConfigurationDocument(); var contexts = _service.CurrentConnection.FeatureService.GetSpatialContextInfo(_fs.ResourceID, false); var schemaName = _service.CurrentConnection.FeatureService.GetSchemas(_fs.ResourceID)[0]; var clsNames = _service.CurrentConnection.FeatureService.GetClassNames(_fs.ResourceID, schemaName); var schema = new FeatureSchema(schemaName, string.Empty); doc.AddSchema(schema); foreach (var sc in contexts.SpatialContext) { doc.AddSpatialContext(sc); } var defaultSc = contexts.SpatialContext[0]; foreach (var clsName in clsNames) { var className = clsName.Split(':')[1]; //NOXLATE var cls = new ClassDefinition(className, string.Empty); cls.AddProperty(new DataPropertyDefinition("Id", string.Empty) //NOXLATE { DataType = DataPropertyType.String, Length = 256, IsNullable = false }, true); cls.AddProperty(new RasterPropertyDefinition("Image", string.Empty) //NOXLATE { DefaultImageXSize = 1024, DefaultImageYSize = 1024, SpatialContextAssociation = defaultSc.Name }); schema.AddClass(cls); var item = CreateDefaultItem(schema.Name, cls.Name, "Image", defaultSc); //NOXLATE doc.AddRasterItem(item); } return(doc); }
public void FindPropertyTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; var prop2 = new DataPropertyDefinition("Prop1", ""); Assert.Null(cls.FindProperty("ID")); cls.AddProperty(prop, true); Assert.Null(cls.FindProperty("Prop1")); cls.AddProperty(prop2); Assert.AreEqual(cls, prop.Parent); Assert.AreEqual(cls, prop2.Parent); Assert.AreEqual(2, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.NotNull(cls.FindProperty("ID")); Assert.NotNull(cls.FindProperty("Prop1")); }
public void GetOrdinalTest() { var cls = new ClassDefinition("Foo", "Bar"); Assert.True(cls.GetOrdinal("Prop1") < 0); var prop = new DataPropertyDefinition("Prop1", ""); prop.DataType = DataPropertyType.String; cls.AddProperty(prop); Assert.True(cls.GetOrdinal("Prop1") >= 0); }
public void GetOrdinalTest() { var cls = new ClassDefinition("Foo", "Bar"); Assert.Less(cls.GetOrdinal("Prop1"), 0); var prop = new DataPropertyDefinition("Prop1", ""); prop.DataType = DataPropertyType.String; cls.AddProperty(prop); Assert.GreaterOrEqual(cls.GetOrdinal("Prop1"), 0); }
public void ClearIdentityPropertiesTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; cls.AddProperty(prop, true); Assert.Equal(cls, prop.Parent); Assert.Single(cls.Properties); Assert.Single(cls.IdentityProperties); Assert.True(cls.GetOrdinal("ID") >= 0); cls.ClearIdentityProperties(); Assert.Empty(cls.IdentityProperties); }
public void ClearIdentityPropertiesTest() { var cls = new ClassDefinition("Foo", "Bar"); var prop = new DataPropertyDefinition("ID", ""); prop.DataType = DataPropertyType.Int32; prop.IsAutoGenerated = true; cls.AddProperty(prop, true); Assert.AreEqual(cls, prop.Parent); Assert.AreEqual(1, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.GreaterOrEqual(cls.GetOrdinal("ID"), 0); cls.ClearIdentityProperties(); Assert.AreEqual(0, cls.IdentityProperties.Count); }
public virtual void TestApplySchema() { Skip.If(_fixture.Skip, _fixture.SkipReason); var fsId = "Library://UnitTests/Data/TestMaestro" + GetTestPrefix() + "ApplySchema.FeatureSource"; var conn = _fixture.CreateTestConnection(); if (conn.ResourceService.ResourceExists(fsId)) { conn.ResourceService.DeleteResource(fsId); } ClassDefinition cls = null; FeatureSchema schema = null; CreateTestDataStore(conn, fsId, ref schema, ref cls); cls.AddProperty(new DataPropertyDefinition("ExtraProp", "") { DataType = DataPropertyType.String, IsNullable = false, Length = 255 }); //Apply changes IApplySchema cmd = (IApplySchema)conn.CreateCommand((int)CommandType.ApplySchema); cmd.Schema = schema; cmd.FeatureSourceId = fsId; cmd.Execute(); ClassDefinition cls2 = conn.FeatureService.GetClassDefinition(cmd.FeatureSourceId, "Class1"); Assert.NotNull(cls2); Assert.False(ClassDefinition.ReferenceEquals(cls, cls2)); Assert.Equal(cls.Name, cls2.Name); Assert.Equal(cls.DefaultGeometryPropertyName, cls2.DefaultGeometryPropertyName); Assert.Equal(cls.Properties.Count, cls2.Properties.Count); Assert.Equal(cls.IdentityProperties.Count, cls2.IdentityProperties.Count); foreach (var prop in cls.Properties) { var prop2 = cls2.FindProperty(prop.Name); Assert.Equal(prop.Name, prop2.Name); Assert.Equal(prop.Type, prop2.Type); } }
protected static void AddProperty(Property property, ref ClassDefinition classDefinition) { classDefinition.AddPropertyCodenameConstant(property.Codename); classDefinition.AddProperty(property); }
public override void ProcessClass(Type classType, ClassDefinition classDef) { MethodInfo[] remoteMethods = remoteType.GetMethods(); bool isServer = !classType.IsSubclassOf(typeof(ClientScope)); // create and setup writer ClassDefinition fakeRemoteClass = new ClassDefinition(string.Format("Remote{0}", remoteType.Name)); { fakeRemoteClass.imports.Add("System.Collections.Generic"); fakeRemoteClass.imports.Add("UnityEngine.Networking"); // add interface sender field fakeRemoteClass.AddField("_netSender", "INetworkSender", false); // add constructor method MethodDefinition ctor = new MethodDefinition(fakeRemoteClass.Name); ctor.IsConstructor = true; ctor.parameters.Add(new ParameterDefinition("netSender", "INetworkSender")); fakeRemoteClass.methods.Add(ctor); // assign it in the body ctor.instructions.AddInstruction("_netSender = netSender;"); } // find remote methods with "Signal" attribute; the SEND methods bool didCreateAnySendMethods = false; foreach (MethodInfo remoteMethod in remoteMethods) { if (ReflectionUtility.ContainsAttribute <Signal>(remoteMethod)) { // create send remote method MethodDefinition fakeMethodDef = CreateSendMethod(remoteMethod); // if method call wasn't found, mark it invalid if (fakeMethodDef == null) { classDef.IsInvalid = true; continue; } // otherwise add it to the list of methods in the inner remote class fakeRemoteClass.methods.Add(fakeMethodDef); // flag that send methods were created to allow the class to be created in a future check didCreateAnySendMethods = true; } } // find local methods with "Signal" attribute; the RECEIVE methods foreach (MethodInfo localMethod in classType.GetMethods()) { if (ReflectionUtility.ContainsAttribute <Signal>(localMethod)) { // create send remote method MethodDefinition localMethodDef = CreateReceiveMethod(localMethod); // if method call wasn't found, mark it invalid if (localMethodDef == null) { classDef.IsInvalid = true; } // otherwise add it to the lsit of methods within the partial class else { classDef.methods.Add(localMethodDef); } } } // add SEND-specific code - ONLY if there's any any send methods generated earlier if (didCreateAnySendMethods) { classDef.classes.Add(fakeRemoteClass); if (isServer) { string peerTypeName = classType.BaseType.GetGenericArguments()[0].FullName; classDef.AddField("_Remote", fakeRemoteClass.Name, false); // send method (single-peer) MethodDefinition sendPeerMethod = new MethodDefinition("SendToPeer"); sendPeerMethod.ReturnType = fakeRemoteClass.Name; sendPeerMethod.parameters.Add(new ParameterDefinition("targetPeer", peerTypeName)); sendPeerMethod.instructions.AddInlineIfCheck("_Remote == null", string.Format("_Remote = new {0}(this);", fakeRemoteClass.Name)); sendPeerMethod.instructions.AddInstruction("TargetPeer = targetPeer;"); sendPeerMethod.instructions.AddInstruction("return _Remote;"); classDef.methods.Add(sendPeerMethod); // reply method (single-peer - last sender) sendPeerMethod = new MethodDefinition("ReplyToPeer"); sendPeerMethod.ReturnType = fakeRemoteClass.Name; sendPeerMethod.instructions.AddInlineIfCheck("_Remote == null", string.Format("_Remote = new {0}(this);", fakeRemoteClass.Name)); sendPeerMethod.instructions.AddInstruction("TargetPeer = SenderPeer;"); sendPeerMethod.instructions.AddInstruction("return _Remote;"); classDef.methods.Add(sendPeerMethod); // send method (multi-peer) sendPeerMethod = new MethodDefinition("SendToPeers"); sendPeerMethod.ReturnType = fakeRemoteClass.Name; sendPeerMethod.parameters.Add(new ParameterDefinition("targetPeerGroup", string.Format("IEnumerable<{0}>", peerTypeName))); sendPeerMethod.instructions.AddInlineIfCheck("_Remote == null", string.Format("_Remote = new {0}(this);", fakeRemoteClass.Name)); sendPeerMethod.instructions.AddInstruction("TargetPeerGroup = targetPeerGroup;"); sendPeerMethod.instructions.AddInstruction("return _Remote;"); classDef.methods.Add(sendPeerMethod); } else { // add the 'Server' property by that type classDef.AddField("_Remote", fakeRemoteClass.Name, false); var prop = classDef.AddProperty("SendToServer", fakeRemoteClass.Name); prop.getterBody = new MethodBodyDefinition(); prop.getterBody.AddInlineIfCheck("_Remote == null", string.Format("_Remote = new {0}(this);", fakeRemoteClass.Name)); prop.getterBody.AddInstruction("return _Remote;"); } } }
public void TestCreate() { var schema = new FeatureSchema("Default", "Default Schema"); Assert.AreEqual("Default", schema.Name); Assert.AreEqual("Default Schema", schema.Description); var cls = new ClassDefinition("Class1", "My Class"); Assert.AreEqual("Class1", cls.Name); Assert.AreEqual("My Class", cls.Description); Assert.IsTrue(string.IsNullOrEmpty(cls.DefaultGeometryPropertyName)); Assert.AreEqual(0, cls.Properties.Count); Assert.AreEqual(0, cls.IdentityProperties.Count); var prop = new DataPropertyDefinition("ID", "identity"); Assert.AreEqual("ID", prop.Name); Assert.AreEqual("identity", prop.Description); Assert.AreEqual(false, prop.IsAutoGenerated); Assert.AreEqual(false, prop.IsReadOnly); Assert.IsTrue(string.IsNullOrEmpty(prop.DefaultValue)); prop.DataType = DataPropertyType.Int32; Assert.AreEqual(DataPropertyType.Int32, prop.DataType); prop.IsAutoGenerated = true; Assert.IsTrue(prop.IsAutoGenerated); prop.IsReadOnly = true; Assert.IsTrue(prop.IsReadOnly); cls.AddProperty(prop, true); Assert.AreEqual(1, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(cls, prop.Parent); Assert.NotNull(cls.FindProperty(prop.Name)); cls.RemoveProperty(prop); Assert.AreEqual(0, cls.Properties.Count); Assert.AreEqual(0, cls.Properties.Count); Assert.IsNull(prop.Parent); Assert.IsNull(cls.FindProperty(prop.Name)); cls.AddProperty(prop, true); Assert.AreEqual(1, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(cls, prop.Parent); Assert.NotNull(cls.FindProperty(prop.Name)); cls.AddProperty(new DataPropertyDefinition("Name", "") { DataType = DataPropertyType.String, Length = 255, IsNullable = true }); Assert.AreEqual(2, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); cls.AddProperty(new GeometricPropertyDefinition("Geom", "") { HasMeasure = false, HasElevation = false, GeometricTypes = FeatureGeometricType.All, SpecificGeometryTypes = (SpecificGeometryType[])Enum.GetValues(typeof(SpecificGeometryType)) }); Assert.AreEqual(3, cls.Properties.Count); Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.IsTrue(string.IsNullOrEmpty(cls.DefaultGeometryPropertyName)); var geom = cls.FindProperty("Geom"); Assert.NotNull(geom); cls.DefaultGeometryPropertyName = geom.Name; Assert.False(String.IsNullOrEmpty(cls.DefaultGeometryPropertyName)); schema.AddClass(cls); Assert.AreEqual(schema, cls.Parent); }
public void TestFeatureSchemaRoundtrip() { FeatureSchema schema = new FeatureSchema("Default", "Default Schema"); ClassDefinition cls = new ClassDefinition("Class1", "Test Class"); cls.AddProperty(new DataPropertyDefinition("ID", "ID Property") { IsAutoGenerated = true, DataType = DataPropertyType.Int64, IsNullable = false, }, true); var prop = cls.FindProperty("ID") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(1, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.Int64, prop.DataType); Assert.IsTrue(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsFalse(prop.IsNullable); cls.AddProperty(new DataPropertyDefinition("Name", "The name") { DataType = DataPropertyType.String, Length = 255 }); prop = cls.FindProperty("Name") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(2, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.String, prop.DataType); Assert.IsFalse(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsFalse(prop.IsNullable); cls.AddProperty(new DataPropertyDefinition("Date", "The date") { DataType = DataPropertyType.DateTime, IsNullable = true }); prop = cls.FindProperty("Date") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(3, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.DateTime, prop.DataType); Assert.IsFalse(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsTrue(prop.IsNullable); schema.AddClass(cls); Assert.AreEqual(1, schema.Classes.Count); XmlDocument doc = new XmlDocument(); schema.WriteXml(doc, doc); string path = Path.GetTempFileName(); doc.Save(path); FeatureSourceDescription fsd = new FeatureSourceDescription(Utils.OpenFile(path)); Assert.AreEqual(1, fsd.Schemas.Length); schema = fsd.Schemas[0]; Assert.NotNull(schema); cls = schema.GetClass("Class1"); Assert.NotNull(cls); prop = cls.FindProperty("ID") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(3, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.Int64, prop.DataType); Assert.IsTrue(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsFalse(prop.IsNullable); prop = cls.FindProperty("Name") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(3, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.String, prop.DataType); Assert.IsFalse(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsFalse(prop.IsNullable); prop = cls.FindProperty("Date") as DataPropertyDefinition; Assert.AreEqual(1, cls.IdentityProperties.Count); Assert.AreEqual(3, cls.Properties.Count); Assert.NotNull(prop); Assert.AreEqual(DataPropertyType.DateTime, prop.DataType); Assert.IsFalse(prop.IsAutoGenerated); Assert.IsFalse(prop.IsReadOnly); Assert.IsTrue(prop.IsNullable); }
public void IntegrationTest_GeneratedCodeCompilesWithoutErrors() { var classDefinition = new ClassDefinition("Complete content type"); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("6712e528-8504-4a36-b716-a28327d6205f"), "text"))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("014d2125-923d-4428-93b4-ad1590274912"), "rich_text", ElementMetadataType.RichText))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("9d23ff46-117c-432c-8fb2-3273acfbbbf5"), "number", ElementMetadataType.Number))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("2115b9ad-5df5-45b8-aa0f-490b5119afa6"), "multiple_choice", ElementMetadataType.MultipleChoice))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("66756a72-6af8-44a4-b58c-485425586a90"), "date_time", ElementMetadataType.DateTime))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("af569649-ee18-4d6a-a095-ea6ffa012546"), "asset", ElementMetadataType.Asset))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("4fa6bad6-d984-45e8-8ebb-f6be25626ee5"), "modular_content", ElementMetadataType.LinkedItems))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("44924563-44d4-4272-a20f-b8745698b082"), "subpages", ElementMetadataType.Subpages))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("83011da2-559d-458c-a4b5-c81a001f4139"), "taxonomy", ElementMetadataType.Taxonomy))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("14390f27-213e-4f8d-9c31-cbf9a7c0a0d8"), "url_slug", ElementMetadataType.UrlSlug))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("23154ba2-73fc-450c-99d4-c18ba45bb743"), "custom", ElementMetadataType.Custom))); var classCodeGenerator = new ManagementClassCodeGenerator(classDefinition, classDefinition.ClassName); var compiledCode = classCodeGenerator.GenerateCode(); var compilation = CSharpCompilation.Create( assemblyName: Path.GetRandomFileName(), syntaxTrees: new[] { CSharpSyntaxTree.ParseText(compiledCode) }, references: new[] { MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location), MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location), MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location), MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(Management.Modules.ModelBuilders.IModelProvider).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(Management.Models.LanguageVariants.Elements.BaseElement).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(Newtonsoft.Json.IJsonLineInfo).GetTypeInfo().Assembly.Location) }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using var ms = new MemoryStream(); var result = compilation.Emit(ms); var compilationErrors = "Compilation errors:\n"; if (!result.Success) { var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (var diagnostic in failures) { compilationErrors += $"{diagnostic.Id}: {diagnostic.GetMessage()}\n"; } } Assert.True(result.Success, compilationErrors); }
public void Build_CreatesClassWithCompleteContentType() { var classDefinition = new ClassDefinition("Complete content type"); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("6712e528-8504-4a36-b716-a28327d6205f"), "text"))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("014d2125-923d-4428-93b4-ad1590274912"), "rich_text", ElementMetadataType.RichText))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("9d23ff46-117c-432c-8fb2-3273acfbbbf5"), "number", ElementMetadataType.Number))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("2115b9ad-5df5-45b8-aa0f-490b5119afa6"), "multiple_choice", ElementMetadataType.MultipleChoice))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("66756a72-6af8-44a4-b58c-485425586a90"), "date_time", ElementMetadataType.DateTime))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("af569649-ee18-4d6a-a095-ea6ffa012546"), "asset", ElementMetadataType.Asset))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("4fa6bad6-d984-45e8-8ebb-f6be25626ee5"), "modular_content", ElementMetadataType.LinkedItems))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("44924563-44d4-4272-a20f-b8745698b082"), "subpages", ElementMetadataType.Subpages))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("83011da2-559d-458c-a4b5-c81a001f4139"), "taxonomy", ElementMetadataType.Taxonomy))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("14390f27-213e-4f8d-9c31-cbf9a7c0a0d8"), "url_slug", ElementMetadataType.UrlSlug))); classDefinition.AddProperty(Property.FromContentTypeElement( TestHelper.GenerateElementMetadataBase(Guid.Parse("23154ba2-73fc-450c-99d4-c18ba45bb743"), "custom", ElementMetadataType.Custom))); var classCodeGenerator = new ManagementClassCodeGenerator(classDefinition, classDefinition.ClassName); var compiledCode = classCodeGenerator.GenerateCode(); var executingPath = AppContext.BaseDirectory; var expectedCode = File.ReadAllText(executingPath + "/Assets/CompleteContentType_CompiledCode_ManagementApi.txt"); Assert.Equal(expectedCode, compiledCode, ignoreWhiteSpaceDifferences: true, ignoreLineEndingDifferences: true); }
public void TestLoadFromStream() { var spatialContext = new FdoSpatialContextListSpatialContext() { Name = "Default", Description = "Test Spatial Context", CoordinateSystemName = "Default", CoordinateSystemWkt = "", ExtentType = FdoSpatialContextListSpatialContextExtentType.Static, Extent = new FdoSpatialContextListSpatialContextExtent { LowerLeftCoordinate = new FdoSpatialContextListSpatialContextExtentLowerLeftCoordinate { X = "-180.0", Y = "-90.0" }, UpperRightCoordinate = new FdoSpatialContextListSpatialContextExtentUpperRightCoordinate { X = "180.0", Y = "90" } }, XYTolerance = 0.00001, ZTolerance = 0.00001 }; var doc = new GenericConfigurationDocument(); var schema = new FeatureSchema("Default", ""); var cls = new ClassDefinition("Test", ""); var id = new DataPropertyDefinition("ID", "") { DataType = DataPropertyType.Int32 }; var name = new DataPropertyDefinition("Name", "") { IsNullable = true }; var geom = new GeometricPropertyDefinition("Geometry", "") { SpatialContextAssociation = spatialContext.Name, GeometricTypes = FeatureGeometricType.Point }; cls.AddProperty(id, true); cls.AddProperty(name); cls.AddProperty(geom); cls.DefaultGeometryPropertyName = geom.Name; schema.AddClass(cls); doc.AddSchema(schema); doc.AddSpatialContext(spatialContext); var xmldoc = new XmlDocument(); using (var ms = new MemoryStream()) { var xml = doc.ToXml(); xmldoc.LoadXml(xml); using (var xw = XmlWriter.Create(ms)) { xmldoc.WriteTo(xw); } //Rewind ms.Position = 0L; var fsd = new FeatureSourceDescription(ms); Assert.Single(fsd.SchemaNames); foreach (var sn in fsd.SchemaNames) { var sch1 = fsd.GetSchema(sn); Assert.NotNull(sch1); Assert.Equal(schema.Name, sch1.Name); foreach (var klass in schema.Classes) { var c1 = fsd.GetClass($"{schema.Name}:{klass.Name}"); var c2 = fsd.GetClass(schema.Name, klass.Name); Assert.Equal(klass.Name, c1.Name); Assert.Equal(c1.Name, c2.Name); Assert.Equal(3, klass.Properties.Count); Assert.Equal(klass.Properties.Count, c1.Properties.Count); Assert.Equal(c1.Properties.Count, c2.Properties.Count); } } } }