예제 #1
0
        public void RulePluginGen_Simple()
        {
            string inputDir        = TestUtils.CreateTestDirectory(this.TestContext, "input");
            string outputDir       = TestUtils.CreateTestDirectory(this.TestContext, "output");
            string fullJarFilePath = Path.Combine(outputDir, "myPlugin.jar");

            string language         = "xx";
            string rulesXmlFilePath = TestUtils.CreateTextFile("rules.xml", inputDir, "<xml Rules />");

            IJdkWrapper        jdkWrapper = new JdkWrapper();
            RulesPluginBuilder generator  = new RulesPluginBuilder(jdkWrapper, new TestLogger());

            PluginManifest defn = new PluginManifest()
            {
                Key          = "MyPlugin",
                Name         = "My Plugin",
                Description  = "Generated plugin",
                Version      = "0.1-SNAPSHOT",
                Organization = "ACME Software Ltd",
                License      = "Commercial",
                Developers   = typeof(RulesPluginBuilder).FullName
            };

            generator.GeneratePlugin(defn, language, rulesXmlFilePath, fullJarFilePath);
            if (File.Exists(fullJarFilePath))
            {
                this.TestContext.AddResultFile(fullJarFilePath);
            }

            new JarChecker(this.TestContext, fullJarFilePath)
            .JarContainsFiles(
                "resources\\*rules.xml",
                "org\\sonarqube\\plugin\\sdk\\MyPlugin\\Plugin.class",
                "org\\sonarqube\\plugin\\sdk\\MyPlugin\\PluginRulesDefinition.class");
        }
        public void RulesPluginBuilder_CreateSimplePlugin_Succeeds()
        {
            string inputDir        = TestUtils.CreateTestDirectory(this.TestContext, "input");
            string outputDir       = TestUtils.CreateTestDirectory(this.TestContext, "output");
            string fullJarFilePath = Path.Combine(outputDir, "myPlugin.jar");

            string language         = "xx";
            string rulesXmlFilePath = TestUtils.CreateTextFile("rules.xml", inputDir, "<xml Rules />");
            string sqaleXmlFilePath = TestUtils.CreateTextFile("sqale.xml", inputDir, "<xml sqale />");

            PluginManifest defn = new PluginManifest()
            {
                Key          = "MyPlugin",
                Name         = "My Plugin",
                Description  = "Generated plugin",
                Version      = "0.1-SNAPSHOT",
                Organization = "ACME Software Ltd",
                License      = "Commercial",
                Developers   = typeof(RulesPluginBuilder).FullName
            };

            MockJdkWrapper mockJdkWrapper = new MockJdkWrapper();

            RulesPluginBuilder builder = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());

            builder.SetLanguage(language)
            .SetRulesFilePath(rulesXmlFilePath)
            .SetSqaleFilePath(sqaleXmlFilePath)
            .SetJarFilePath(fullJarFilePath)
            .SetProperties(defn);

            builder.Build(); // should not fail
            mockJdkWrapper.AssertJarBuilt();
        }
        public void RulesPluginBuilder_RulesFileValidation()
        {
            // Arrange
            MockJdkWrapper     mockJdkWrapper = new MockJdkWrapper();
            RulesPluginBuilder builder        = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());

            SetValidCoreProperties(builder);
            builder.SetLanguage("aLanguage");

            // 1. Rules file not specified -> error
            AssertException.Expect <InvalidOperationException>(() => builder.Build());

            // 2. Non-existent rules file specified -> error
            string testDir   = TestUtils.EnsureTestDirectoryExists(this.TestContext);
            string rulesFile = Path.Combine(testDir, "missingFile.txt");

            mockJdkWrapper.AssertCodeNotCompiled();

            builder.SetRulesFilePath(rulesFile);
            FileNotFoundException ex = AssertException.Expect <FileNotFoundException>(() => builder.Build());

            Assert.AreEqual(ex.FileName, rulesFile);
            mockJdkWrapper.AssertCodeNotCompiled();

            // 3. Rules file exists -> succeeds
            AddValidDummyRulesFiles(builder);
            builder.Build(); // should succeed
            mockJdkWrapper.AssertJarBuilt();
        }
        public void RulesPluginBuilder_CreateSimplePlugin_Succeeds()
        {
            string inputDir = TestUtils.CreateTestDirectory(this.TestContext, "input");
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, "output");
            string fullJarFilePath = Path.Combine(outputDir, "myPlugin.jar");

            string language = "xx";
            string rulesXmlFilePath = TestUtils.CreateTextFile("rules.xml", inputDir, "<xml Rules />");
            string sqaleXmlFilePath = TestUtils.CreateTextFile("sqale.xml", inputDir, "<xml sqale />");

            PluginManifest defn = new PluginManifest()
            {
                Key = "MyPlugin",
                Name = "My Plugin",
                Description = "Generated plugin",
                Version = "0.1-SNAPSHOT",
                Organization = "ACME Software Ltd",
                License = "Commercial",
                Developers = typeof(RulesPluginBuilder).FullName
            };

            MockJdkWrapper mockJdkWrapper = new MockJdkWrapper();

            RulesPluginBuilder builder = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());

            builder.SetLanguage(language)
                .SetRulesFilePath(rulesXmlFilePath)
                .SetSqaleFilePath(sqaleXmlFilePath)
                .SetJarFilePath(fullJarFilePath)
                .SetProperties(defn);

            builder.Build(); // should not fail
            mockJdkWrapper.AssertJarBuilt();
        }
        public void RulePluginGen_Simple()
        {
            string inputDir = TestUtils.CreateTestDirectory(this.TestContext, "input");
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, "output");
            string fullJarFilePath = Path.Combine(outputDir, "myPlugin.jar");

            string language = "xx";
            string rulesXmlFilePath = TestUtils.CreateTextFile("rules.xml", inputDir, "<xml Rules />");

            IJdkWrapper jdkWrapper = new JdkWrapper();
            RulesPluginBuilder generator = new RulesPluginBuilder(jdkWrapper, new TestLogger());

            PluginManifest defn = new PluginManifest()
            {
                Key = "MyPlugin",
                Name = "My Plugin",
                Description = "Generated plugin",
                Version = "0.1-SNAPSHOT",
                Organization = "ACME Software Ltd",
                License = "Commercial",
                Developers = typeof(RulesPluginBuilder).FullName
            };

            generator.GeneratePlugin(defn, language, rulesXmlFilePath, fullJarFilePath);
            if (File.Exists(fullJarFilePath))
            {
                this.TestContext.AddResultFile(fullJarFilePath);
            }

            new JarChecker(this.TestContext, fullJarFilePath)
                .JarContainsFiles(
                    "resources\\rules.xml",
                    "org\\sonarqube\\plugin\\sdk\\MyPlugin\\Plugin.class",
                    "org\\sonarqube\\plugin\\sdk\\MyPlugin\\PluginRulesDefinition.class");
        }
예제 #6
0
        private void BuildPlugin(IPackage package, string language, string rulesFilePath, string sqaleFilePath, PluginManifest pluginDefn, string baseTempDirectory, string outputDirectory)
        {
            this.logger.LogInfo(UIResources.APG_GeneratingPlugin);

            // Make the .jar name match the format [artefactid]-[version].jar
            // i.e. the format expected by Maven
            Directory.CreateDirectory(outputDirectory);
            string fullJarPath = Path.Combine(outputDirectory,
                                              package.Id + "-plugin-" + pluginDefn.Version + ".jar");

            RulesPluginBuilder builder = new RulesPluginBuilder(logger);

            builder.SetLanguage(language)
            .SetRulesFilePath(rulesFilePath)
            .SetProperties(pluginDefn)
            .SetJarFilePath(fullJarPath);

            if (!string.IsNullOrWhiteSpace(sqaleFilePath))
            {
                builder.SetSqaleFilePath(sqaleFilePath);
            }

            AddRoslynMetadata(baseTempDirectory, builder, package);

            builder.Build();

            this.logger.LogInfo(UIResources.APG_PluginGenerated, fullJarPath);
        }
        private void AddValidDummyRulesFiles(RulesPluginBuilder builder)
        {
            string testDir   = TestUtils.EnsureTestDirectoryExists(this.TestContext);
            string rulesFile = TestUtils.CreateTextFile("rules.txt", testDir, "dummy rules file");

            builder.SetRulesFilePath(rulesFile);
        }
        public void RulesPluginBuilder_SqaleFileValidation()
        {
            // Arrange
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);

            MockJdkWrapper     mockJdkWrapper = new MockJdkWrapper();
            RulesPluginBuilder builder        = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());

            SetValidCoreProperties(builder);
            builder.SetLanguage("aLanguage");
            AddValidDummyRulesFiles(builder);

            // 1. Sqale file not specified -> ok
            builder.Build();

            // 2. Non-existent Sqale file specified -> error
            mockJdkWrapper.ClearCalledMethodList();

            string sqaleFile = Path.Combine(testDir, "missingFile.txt");

            builder.SetSqaleFilePath(sqaleFile);
            FileNotFoundException ex = AssertException.Expect <FileNotFoundException>(() => builder.Build());

            Assert.AreEqual(ex.FileName, sqaleFile);
            mockJdkWrapper.AssertCodeNotCompiled();

            // 3. Sqale file exists -> succeeds
            sqaleFile = TestUtils.CreateTextFile("sqale.txt", testDir, "dummy sqale file");
            builder.SetSqaleFilePath(sqaleFile);
            builder.Build(); // should succeed
            mockJdkWrapper.AssertJarBuilt();
        }
        private void SetValidCoreProperties(RulesPluginBuilder builder)
        {
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);

            builder.SetPluginKey("dummy.key")
            .SetPluginName("dummy name")
            .SetJarFilePath(Path.Combine(testDir, "dummy.jar.txt"));
        }
        public void RulesPluginBuilder_SqaleFileSetToNull_Fails()
        {
            // Arrange
            RulesPluginBuilder builder = new RulesPluginBuilder(new TestLogger());

            // Act and assert
            AssertException.Expect <ArgumentNullException>(() => builder.SetSqaleFilePath(null));
            AssertException.Expect <ArgumentNullException>(() => builder.SetSqaleFilePath(""));
        }
        public void RulesPluginBuilder_SqaleFileSetToNull_Fails()
        {
            // Arrange
            RulesPluginBuilder builder = new RulesPluginBuilder(new TestLogger());

            // Act and assert
            AssertException.Expect<ArgumentNullException>(() => builder.SetSqaleFilePath(null));
            AssertException.Expect<ArgumentNullException>(() => builder.SetSqaleFilePath(""));
        }
        public void RulesPluginBuilder_LanguageIsRequired()
        {
            // Arrange
            RulesPluginBuilder builder = new RulesPluginBuilder(new TestLogger());

            SetValidCoreProperties(builder);
            AddValidDummyRulesFiles(builder);

            // Act and assert
            AssertException.Expect <InvalidOperationException>(() => builder.Build());
        }
        private void BuildPlugin(IPackage package, string language, string rulesFilePath, string sqaleFilePath, PluginManifest pluginDefn, string baseTempDirectory)
        {
            this.logger.LogInfo(UIResources.APG_GeneratingPlugin);

            // Make the .jar name match the format [artefactid]-[version].jar
            // i.e. the format expected by Maven
            string fullJarPath = Path.Combine(Directory.GetCurrentDirectory(),
                                              package.Id + "-plugin-" + pluginDefn.Version + ".jar");

            PluginBuilder builder = new PluginBuilder(logger);

            RulesPluginBuilder.ConfigureBuilder(builder, pluginDefn, language, rulesFilePath, sqaleFilePath);

            AddRoslynMetadata(baseTempDirectory, builder, package);

            builder.SetJarFilePath(fullJarPath);
            builder.Build();

            this.logger.LogInfo(UIResources.APG_PluginGenerated, fullJarPath);
        }
예제 #14
0
        static int Main(string[] args)
        {
            ILogger logger = new ConsoleLogger();

            Utilities.LogAssemblyVersion(typeof(Program).Assembly, UIResources.AssemblyDescription, logger);

            if (args.Length != 2 && args.Length != 3)
            {
                logger.LogError(UIResources.Cmd_Error_IncorrectArguments);
                return(ERROR_CODE);
            }

            string pluginDefnFilePath = args[0];
            string rulesFilePath      = args[1];

            PluginManifest defn = PluginManifest.Load(pluginDefnFilePath);
            string         fullNewJarFilePath = Path.Combine(Directory.GetCurrentDirectory(),
                                                             Path.GetFileNameWithoutExtension(pluginDefnFilePath) + ".jar");

            string sqaleFilePath = null;

            if (args.Length == 3)
            {
                sqaleFilePath = args[2];
            }

            //TODO: support multiple languages
            string language = "cs";

            RulesPluginBuilder builder = new RulesPluginBuilder(logger);

            builder.SetLanguage(language)
            .SetRulesFilePath(rulesFilePath)
            .SetSqaleFilePath(sqaleFilePath)
            .SetProperties(defn)
            .SetJarFilePath(fullNewJarFilePath);
            builder.Build();

            return(SUCCESS_CODE);
        }
예제 #15
0
        static int Main(string[] args)
        {
            ILogger logger = new ConsoleLogger();

            Utilities.LogAssemblyVersion(typeof(Program).Assembly, UIResources.AssemblyDescription, logger);

            if (args.Length != 2 && args.Length != 3)
            {
                logger.LogError(UIResources.Cmd_Error_IncorrectArguments);
                return ERROR_CODE;
            }

            string pluginDefnFilePath = args[0];
            string rulesFilePath = args[1];
                        
            PluginManifest defn = PluginManifest.Load(pluginDefnFilePath);
            string fullNewJarFilePath = Path.Combine(Directory.GetCurrentDirectory(),
                Path.GetFileNameWithoutExtension(pluginDefnFilePath) + ".jar");

            string sqaleFilePath = null;
            if (args.Length == 3)
            {
                sqaleFilePath = args[2];
            }

            //TODO: support multiple languages
            string language = "cs";

            RulesPluginBuilder builder = new RulesPluginBuilder(logger);
            builder.SetLanguage(language)
                .SetRulesFilePath(rulesFilePath)
                .SetSqaleFilePath(sqaleFilePath)
                .SetProperties(defn)
                .SetJarFilePath(fullNewJarFilePath);
            builder.Build();

            return SUCCESS_CODE;
        }
        public void RulesPluginBuilder_SqaleFileValidation()
        {
            // Arrange
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);

            MockJdkWrapper mockJdkWrapper = new MockJdkWrapper();
            RulesPluginBuilder builder = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());
            SetValidCoreProperties(builder);
            builder.SetLanguage("aLanguage");
            AddValidDummyRulesFiles(builder);

            // 1. Sqale file not specified -> ok
            builder.Build();

            // 2. Non-existent Sqale file specified -> error
            mockJdkWrapper.ClearCalledMethodList();

            string sqaleFile = Path.Combine(testDir, "missingFile.txt");
            builder.SetSqaleFilePath(sqaleFile);
            FileNotFoundException ex = AssertException.Expect<FileNotFoundException>(() => builder.Build());
            Assert.AreEqual(ex.FileName, sqaleFile);
            mockJdkWrapper.AssertCodeNotCompiled();

            // 3. Sqale file exists -> succeeds
            sqaleFile = TestUtils.CreateTextFile("sqale.txt", testDir, "dummy sqale file");
            builder.SetSqaleFilePath(sqaleFile);
            builder.Build(); // should succeed
            mockJdkWrapper.AssertJarBuilt();
        }
        private void SetValidCoreProperties(RulesPluginBuilder builder)
        {
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);

            builder.SetPluginKey("dummy.key")
                .SetPluginName("dummy name")
                .SetJarFilePath(Path.Combine(testDir, "dummy.jar.txt"));
        }
        private void AddValidDummyRulesFiles(RulesPluginBuilder builder)
        {
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);
            string rulesFile = TestUtils.CreateTextFile("rules.txt", testDir, "dummy rules file");

            builder.SetRulesFilePath(rulesFile);
        }
        public void RulesPluginBuilder_RulesFileValidation()
        {
            // Arrange
            MockJdkWrapper mockJdkWrapper = new MockJdkWrapper();
            RulesPluginBuilder builder = new RulesPluginBuilder(mockJdkWrapper, new MockMavenArtifactHandler(), new TestLogger());
            SetValidCoreProperties(builder);
            builder.SetLanguage("aLanguage");

            // 1. Rules file not specified -> error
            AssertException.Expect<InvalidOperationException>(() => builder.Build());

            // 2. Non-existent rules file specified -> error
            string testDir = TestUtils.EnsureTestDirectoryExists(this.TestContext);
            string rulesFile = Path.Combine(testDir, "missingFile.txt");
            mockJdkWrapper.AssertCodeNotCompiled();

            builder.SetRulesFilePath(rulesFile);
            FileNotFoundException ex = AssertException.Expect<FileNotFoundException>(() => builder.Build());
            Assert.AreEqual(ex.FileName, rulesFile);
            mockJdkWrapper.AssertCodeNotCompiled();

            // 3. Rules file exists -> succeeds
            AddValidDummyRulesFiles(builder);
            builder.Build(); // should succeed
            mockJdkWrapper.AssertJarBuilt();
        }
        private void BuildPlugin(IPackage package, string language, string rulesFilePath, string sqaleFilePath, PluginManifest pluginDefn, string baseTempDirectory, string outputDirectory)
        {
            this.logger.LogInfo(UIResources.APG_GeneratingPlugin);

            // Make the .jar name match the format [artefactid]-[version].jar
            // i.e. the format expected by Maven
            Directory.CreateDirectory(outputDirectory);
            string fullJarPath = Path.Combine(outputDirectory,
                package.Id + "-plugin-" + pluginDefn.Version + ".jar");

            RulesPluginBuilder builder = new RulesPluginBuilder(logger);
            builder.SetLanguage(language)
                            .SetRulesFilePath(rulesFilePath)
                            .SetProperties(pluginDefn)
                            .SetJarFilePath(fullJarPath);

            if (!string.IsNullOrWhiteSpace(sqaleFilePath))
            {
                builder.SetSqaleFilePath(sqaleFilePath);
            }

            AddRoslynMetadata(baseTempDirectory, builder, package);
            
            builder.Build();

            this.logger.LogInfo(UIResources.APG_PluginGenerated, fullJarPath);
        }
        public void RulesPluginBuilder_LanguageIsRequired()
        {
            // Arrange
            RulesPluginBuilder builder = new RulesPluginBuilder(new TestLogger());
            SetValidCoreProperties(builder);
            AddValidDummyRulesFiles(builder);

            // Act and assert
            AssertException.Expect<InvalidOperationException>(() => builder.Build());
        }