org.sonar.plugins.plsqltoad.PlSqlToadRulesRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.plsqltoad.PlSqlToadRulesRepositoryTest.java

Source

/*
 * Sonar PL/SQL Toad Plugin
 * Copyright (C) 2012 SonarSource
 * dev@sonar.codehaus.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.plugins.plsqltoad;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;

import java.util.List;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

public class PlSqlToadRulesRepositoryTest {

    private PlSqlToadRulesRepository repository;
    private List<Rule> rules;

    @Before
    public void init() {
        repository = new PlSqlToadRulesRepository();
        rules = repository.createRules();
    }

    @Test
    public void repositoryImmutableSettingsTest() {
        assertThat(repository.getLanguage(), is("plsql"));
        assertThat(repository.getKey(), is("toad"));
    }

    @Test
    public void createRulesTotalRulesTest() {
        assertThat(rules.size(), is(89));
    }

    @Test
    public void createRulesNoNullFieldTest() {
        for (Rule rule : rules) {
            assertThat(rule.getKey(), notNullValue());
            assertThat(rule.getName(), notNullValue());
            assertThat(rule.getDescription(), notNullValue());
        }
    }

    @Test
    public void createRulesSpecificRuleTest() {
        String key = "4201";

        for (Rule rule : rules) {
            if (rule.getKey().equals(key)) {
                assertThat(rule.getName(), is("IF - Limit a CASE statement or expression to 128 choices"));
                assertThat(
                        rule.getDescription().replace(IOUtils.LINE_SEPARATOR_WINDOWS, " ")
                                .replace(IOUtils.LINE_SEPARATOR_UNIX, " "),
                        is("<p>Although not enforced by Oracle it is a good idea to limit the number of choices to avoid performance penalties.</p>"));
                assertThat(rule.getSeverity(), is(RulePriority.BLOCKER));
                return;
            }
        }

        throw new IllegalArgumentException("Unable to find a rule with " + key + " as key in the repository!");
    }

}