Package org.sonar.batch.scan.filesystem

Source Code of org.sonar.batch.scan.filesystem.DefaultModuleFileSystemTest

/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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  02110-1301, USA.
*/
package org.sonar.batch.scan.filesystem;

import com.google.common.collect.Lists;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class DefaultModuleFileSystemTest {

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  @Rule
  public ExpectedException thrown = ExpectedException.none();

  Settings settings = new Settings();
  FileIndexer fileIndexer = mock(FileIndexer.class);
  ModuleFileSystemInitializer initializer = mock(ModuleFileSystemInitializer.class, Mockito.RETURNS_DEEP_STUBS);
  ComponentIndexer componentIndexer = mock(ComponentIndexer.class);
  ModuleInputFileCache moduleInputFileCache = mock(ModuleInputFileCache.class);

  @Test
  public void test_equals_and_hashCode() throws Exception {
    DefaultModuleFileSystem foo1 = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);
    DefaultModuleFileSystem foo2 = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);
    DefaultModuleFileSystem bar = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("bar"), settings, fileIndexer, initializer, componentIndexer);
    DefaultModuleFileSystem branch = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("bar", "branch", "My project"), settings, fileIndexer, initializer, componentIndexer);

    assertThat(foo1.moduleKey()).isEqualTo("foo");
    assertThat(branch.moduleKey()).isEqualTo("bar:branch");
    assertThat(foo1.equals(foo1)).isTrue();
    assertThat(foo1.equals(foo2)).isTrue();
    assertThat(foo1.equals(bar)).isFalse();
    assertThat(foo1.equals("foo")).isFalse();
    assertThat(foo1.hashCode()).isEqualTo(foo1.hashCode());
    assertThat(foo1.hashCode()).isEqualTo(foo2.hashCode());
  }

  @Test
  public void default_source_encoding() {
    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    assertThat(fs.sourceCharset()).isEqualTo(Charset.defaultCharset());
    assertThat(fs.isDefaultJvmEncoding()).isTrue();
  }

  @Test
  public void source_encoding_is_set() {
    settings.setProperty(CoreProperties.ENCODING_PROPERTY, "Cp1124");
    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    assertThat(fs.encoding()).isEqualTo(Charset.forName("Cp1124"));
    assertThat(fs.sourceCharset()).isEqualTo(Charset.forName("Cp1124"));

    // This test fails when default Java encoding is "IBM AIX Ukraine". Sorry for that.
    assertThat(fs.isDefaultJvmEncoding()).isFalse();
  }

  @Test
  public void test_dirs() throws IOException {
    File basedir = temp.newFolder("base");
    File buildDir = temp.newFolder("build");
    File workingDir = temp.newFolder("work");
    File additionalFile = temp.newFile("Main.java");
    File additionalTest = temp.newFile("Test.java");
    when(initializer.baseDir()).thenReturn(basedir);
    when(initializer.buildDir()).thenReturn(buildDir);
    when(initializer.workingDir()).thenReturn(workingDir);
    when(initializer.binaryDirs()).thenReturn(Arrays.asList(new File(basedir, "target/classes")));
    File javaSrc = new File(basedir, "src/main/java");
    javaSrc.mkdirs();
    File groovySrc = new File(basedir, "src/main/groovy");
    groovySrc.mkdirs();
    when(initializer.sources()).thenReturn(Arrays.asList(javaSrc, groovySrc, additionalFile));
    File javaTest = new File(basedir, "src/test/java");
    javaTest.mkdirs();
    when(initializer.tests()).thenReturn(Arrays.asList(javaTest, additionalTest));

    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
    assertThat(fs.workingDir().getCanonicalPath()).isEqualTo(workingDir.getCanonicalPath());
    assertThat(fs.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath());
    assertThat(fs.sourceDirs()).hasSize(2);
    assertThat(fs.testDirs()).hasSize(1);
    assertThat(fs.binaryDirs()).hasSize(1);
  }

  @Test
  public void should_reset_dirs() throws IOException {
    File basedir = temp.newFolder();
    when(initializer.baseDir()).thenReturn(basedir);
    when(initializer.workingDir()).thenReturn(basedir);
    when(initializer.sources()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));

    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    File existingDir = temp.newFolder("new_folder");
    File notExistingDir = new File(existingDir, "not_exist");

    fs.resetDirs(existingDir, existingDir,
      Lists.newArrayList(existingDir, notExistingDir), Lists.newArrayList(existingDir, notExistingDir), Lists.newArrayList(existingDir, notExistingDir));

    assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath());
    assertThat(fs.buildDir().getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath());
    assertThat(fs.sourceDirs()).hasSize(1);
    assertThat(fs.sourceDirs().get(0).getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath());
    assertThat(fs.testDirs()).hasSize(1);
    assertThat(fs.testDirs().get(0).getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath());
    assertThat(fs.binaryDirs()).hasSize(1);
    assertThat(fs.binaryDirs().get(0).getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath());
  }

  @Test
  public void should_search_input_files() throws Exception {
    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    File mainFile = temp.newFile();
    InputFile mainInput = new DeprecatedDefaultInputFile("foo", "Main.java").setFile(mainFile).setType(InputFile.Type.MAIN);
    InputFile testInput = new DeprecatedDefaultInputFile("foo", "Test.java").setFile(temp.newFile()).setType(InputFile.Type.TEST);
    when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput));

    fs.index();
    Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN));
    assertThat(inputFiles).containsOnly(mainInput);

    Iterable<File> files = fs.files(fs.predicates().hasType(InputFile.Type.MAIN));
    assertThat(files).containsOnly(mainFile);
  }

  @Test
  public void should_index() throws Exception {
    DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
      new Project("foo"), settings, fileIndexer, initializer, componentIndexer);

    verifyZeroInteractions(fileIndexer);

    fs.index();
    verify(fileIndexer).index(fs);
    verify(componentIndexer).execute(fs);
  }

}
TOP

Related Classes of org.sonar.batch.scan.filesystem.DefaultModuleFileSystemTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.