aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test/java/org/sonar/batch/MavenProjectBuilderTest.java
blob: a35b692270a950f322aa1824b12aecad17bdefd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar 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.
 *
 * Sonar 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 Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.batch;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.CoreProperties;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MavenProjectBuilderTest extends AbstractDbUnitTestCase {

  private MavenProjectBuilder builder = null;

  @Before
  public void before() {
    builder = new MavenProjectBuilder(getSession());
  }

  @Test
  public void noExclusionPatterns() {
    Project project = new Project("key");
    builder.configure(project, new PropertiesConfiguration());

    assertThat(project.getExclusionPatterns().length, is(0));
  }

  @Test
  public void manyExclusionPatterns() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*,foo,*/bar");

    Project project = new Project("key");
    builder.configure(project, configuration);

    assertThat(project.getExclusionPatterns().length, is(3));
    assertThat(project.getExclusionPatterns()[0], is("**/*"));
    assertThat(project.getExclusionPatterns()[1], is("foo"));
    assertThat(project.getExclusionPatterns()[2], is("*/bar"));
  }

  @Test
  public void getLanguageFromConfiguration() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "foo");

    Project project = new Project("key");
    builder.configure(project, configuration);

    assertThat(project.getLanguageKey(), is("foo"));
  }

  @Test
  public void defaultLanguageIsJava() {
    Project project = new Project("key");
    builder.configure(project, new PropertiesConfiguration());

    assertThat(project.getLanguageKey(), is(Java.KEY));
  }

  @Test
  public void analysisIsTodayByDefault() {
    Project project = new Project("key");
    builder.configure(project, new PropertiesConfiguration());
    Date today = new Date();
    assertTrue(today.getTime() - project.getAnalysisDate().getTime() < 1000);
  }

  @Test
  public void analysisDateCouldBeExplicitlySet() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2005-01-30");
    Project project = new Project("key");
    builder.configure(project, configuration);

    assertEquals("30012005", new SimpleDateFormat("ddMMyyyy").format(project.getAnalysisDate()));
  }

  @Test(expected = RuntimeException.class)
  public void failIfAnalyisDateIsNotValid() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2005/30/01");
    Project project = new Project("key");
    builder.configure(project, configuration);

    project.getAnalysisDate();
  }

  @Test
  public void sonarLightIsDeprecated() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty("sonar.light", "true");
    Project project = new Project("key");
    builder.configure(project, configuration);

    assertThat(project.getAnalysisType(), is(Project.AnalysisType.STATIC));
  }

  @Test
  public void defaultAnalysisTypeIsDynamic() {
    Project project = new Project("key");
    builder.configure(project, new PropertiesConfiguration());
    assertThat(project.getAnalysisType(), is(Project.AnalysisType.DYNAMIC));
  }

  @Test
  public void explicitDynamicAnalysis() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY, "true");
    Project project = new Project("key");
    builder.configure(project, configuration);
    assertThat(project.getAnalysisType(), is(Project.AnalysisType.DYNAMIC));
  }

  @Test
  public void explicitStaticAnalysis() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY, "false");
    Project project = new Project("key");
    builder.configure(project, configuration);
    assertThat(project.getAnalysisType(), is(Project.AnalysisType.STATIC));
  }

  @Test
  public void explicitDynamicAnalysisReusingReports() {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY, "reuseReports");
    Project project = new Project("key");
    builder.configure(project, configuration);
    assertThat(project.getAnalysisType(), is(Project.AnalysisType.REUSE_REPORTS));
  }

  @Test
  public void isDynamicAnalysis() {
    assertThat(Project.AnalysisType.DYNAMIC.isDynamic(false), is(true));
    assertThat(Project.AnalysisType.DYNAMIC.isDynamic(true), is(true));

    assertThat(Project.AnalysisType.STATIC.isDynamic(false), is(false));
    assertThat(Project.AnalysisType.STATIC.isDynamic(true), is(false));

    assertThat(Project.AnalysisType.REUSE_REPORTS.isDynamic(false), is(false));
    assertThat(Project.AnalysisType.REUSE_REPORTS.isDynamic(true), is(true));
  }

  @Test
  public void isLatestAnalysis() {
    setupData("isLatestAnalysis");

    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2010-12-25");

    Project project = new Project("my:key");
    builder.configure(project, configuration);

    assertThat(project.isLatestAnalysis(), is(true));
  }

  @Test
  public void isLatestAnalysisIfNeverAnalysed() {
    setupData("isLatestAnalysisIfNeverAnalysed");

    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2010-12-25");

    Project project = new Project("my:key");
    builder.configure(project, configuration);

    assertThat(project.isLatestAnalysis(), is(true));
  }

  @Test
  public void isNotLatestAnalysis() {
    setupData("isNotLatestAnalysis");

    PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2005-12-25");

    Project project = new Project("my:key");
    builder.configure(project, configuration);

    assertThat(project.isLatestAnalysis(), is(false));
  }
}