aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java14
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java11
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java3
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java7
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java4
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java5
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java14
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java31
-rw-r--r--sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/sonar-project.properties6
l---------sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/testx1
l---------sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/xources1
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java4
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java25
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java3
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java57
17 files changed, 143 insertions, 53 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java
index 8caec63784f..c10586fda05 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java
@@ -19,8 +19,7 @@
*/
package org.sonar.plugins.core.issue;
-import java.util.Collection;
-
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.resources.Resource;
import org.sonar.batch.scan.LastSnapshots;
@@ -28,7 +27,7 @@ import org.sonar.plugins.core.issue.tracking.HashedSequence;
import org.sonar.plugins.core.issue.tracking.StringText;
import org.sonar.plugins.core.issue.tracking.StringTextComparator;
-
+import java.util.Collection;
public class SourceHashHolder {
@@ -66,15 +65,15 @@ public class SourceHashHolder {
}
public String getSource() {
- if (! sourceInitialized) {
- source = index.getSource(resource);
+ if (!sourceInitialized) {
+ source = StringUtils.defaultString(index.getSource(resource), "");
sourceInitialized = true;
}
return source;
}
public String getReferenceSource() {
- if (! referenceSourceInitialized) {
+ if (!referenceSourceInitialized) {
if (resource != null) {
referenceSource = lastSnapshots.getSource(resource);
}
@@ -88,7 +87,7 @@ public class SourceHashHolder {
}
private void initHashesIfNull(Object required) {
- if(required == null) {
+ if (required == null) {
initHashes();
}
}
@@ -97,4 +96,3 @@ public class SourceHashHolder {
return getHashedSource().getLinesForHash(getHashedReference().getHash(originLine));
}
}
-
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
index 86130986d1f..0acc0a94232 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
@@ -36,7 +36,9 @@ import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import java.net.URI;
+import java.net.URLEncoder;
/**
* Replace the deprecated org.sonar.batch.ServerMetadata
@@ -144,4 +146,13 @@ public class ServerClient implements BatchComponent {
private String getPassword() {
return props.property(CoreProperties.PASSWORD);
}
+
+ public static String encodeForUrl(String url) {
+ try {
+ return URLEncoder.encode(url, "UTF-8");
+
+ } catch (UnsupportedEncodingException e) {
+ throw new IllegalStateException("Encoding not supported", e);
+ }
+ }
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
index fbf973640ed..b665d9d3279 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
@@ -36,7 +36,7 @@ public class TempFolderProvider extends ProviderAdapter {
public TempFolder provide(BootstrapProperties bootstrapProps) {
if (tempFolder == null) {
String workingDirPath = StringUtils.defaultIfBlank(bootstrapProps.property(CoreProperties.WORKING_DIRECTORY), CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE);
- File workingDir = new File(workingDirPath);
+ File workingDir = new File(workingDirPath).getAbsoluteFile();
File tempDir = new File(workingDir, ".sonartmp");
try {
FileUtils.forceMkdir(tempDir);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
index b86dd4e1cf7..f2519a186eb 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
@@ -44,8 +44,6 @@ import org.sonar.core.source.db.SnapshotDataDto;
import javax.persistence.Query;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@@ -77,11 +75,7 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad
if (taskProperties.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) {
LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP
+ "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server.");
- try {
- url += "&profile=" + URLEncoder.encode(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("Unable to encode URL", e);
- }
+ url += "&profile=" + ServerClient.encodeForUrl(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP));
}
url += "&preview=" + analysisMode.isPreview();
ProjectReferentials ref = ProjectReferentials.fromJson(serverClient.request(url));
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
index 980129c0311..9f04b206fb3 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
@@ -62,7 +62,8 @@ public class LastSnapshots implements BatchComponent {
private String loadSourceFromWs(Resource resource) {
TimeProfiler profiler = new TimeProfiler(LOG).start("Load previous source code of: " + resource.getEffectiveKey()).setLevelToDebug();
try {
- return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", "GET", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
+ return server
+ .request("/api/sources?resource=" + ServerClient.encodeForUrl(resource.getEffectiveKey()) + "&format=txt", "GET", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
} catch (HttpDownloader.HttpException he) {
if (he.getResponseCode() == 404) {
return "";
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java
index 1e185487a05..65a7585555e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java
@@ -22,6 +22,8 @@ package org.sonar.batch.scan.filesystem;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.io.Files;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.SonarIndex;
@@ -44,6 +46,8 @@ import org.sonar.batch.util.DeprecatedKeyUtils;
*/
public class ComponentIndexer implements BatchComponent {
+ private static final Logger LOG = LoggerFactory.getLogger(ComponentIndexer.class);
+
private final Languages languages;
private final Settings settings;
private final SonarIndex sonarIndex;
@@ -62,6 +66,9 @@ public class ComponentIndexer implements BatchComponent {
migration.migrateIfNeeded(module, fs);
boolean shouldImportSource = settings.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY);
+ if (!shouldImportSource) {
+ LOG.warn("Not importing source will prevent issues to be properly tracked between consecutive analyses");
+ }
for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) {
String languageKey = inputFile.language();
boolean unitTest = InputFile.Type.TEST == inputFile.type();
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java
index 2c9c2473194..3f25a94c6c7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java
@@ -109,6 +109,10 @@ public class ModuleFileSystemInitializer implements BatchComponent {
return testDirsOrFiles;
}
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
List<File> binaryDirs() {
return binaryDirs;
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
index 34060a4ee30..9c9b930f99d 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
@@ -135,6 +135,11 @@ public class ServerClientTest {
newServerClient().request("/foo");
}
+ @Test
+ public void testEncode() {
+ assertThat(ServerClient.encodeForUrl("my value")).isEqualTo("my+value");
+ }
+
private ServerClient newServerClient() {
when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort());
return new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4"));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
index 08ac9d7e3cd..5429cbd860c 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
@@ -29,6 +29,7 @@ import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.System2;
import org.sonar.batch.mediumtest.BatchMediumTester;
import org.sonar.batch.mediumtest.BatchMediumTester.TaskResult;
import org.sonar.batch.protocol.input.ActiveRule;
@@ -38,6 +39,7 @@ import java.io.File;
import java.io.IOException;
import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assume.assumeFalse;
public class FileSystemMediumTest {
@@ -169,4 +171,16 @@ public class FileSystemMediumTest {
}
+ // SONAR-5330
+ @Test
+ public void scanProjectWithSourceSymlink() throws Exception {
+ assumeFalse(System2.INSTANCE.isOsWindows());
+ File projectDir = new File("src/test/resources/mediumtest/xoo/sample-with-symlink");
+ TaskResult result = tester
+ .newScanTask(new File(projectDir, "sonar-project.properties"))
+ .start();
+
+ assertThat(result.inputFiles()).hasSize(3);
+ }
+
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java
index 145332981d4..66acae88fa3 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java
@@ -37,7 +37,10 @@ import java.net.URISyntaxException;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+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 LastSnapshotsTest {
@@ -88,7 +91,21 @@ public class LastSnapshotsTest {
String source = lastSnapshots.getSource(newFile());
assertThat(source).isEqualTo("downloaded source of Bar.c");
- verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", "GET", false, 30 * 1000);
+ verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", "GET", false, 30 * 1000);
+ }
+
+ @Test
+ public void should_download_source_with_space_from_ws_if_preview_mode() {
+ db.prepareDbUnit(getClass(), "last_snapshot.xml");
+ ServerClient server = mock(ServerClient.class);
+ when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Foo Bar.c");
+
+ when(mode.isPreview()).thenReturn(true);
+ LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server);
+
+ String source = lastSnapshots.getSource(newFile());
+ assertThat(source).isEqualTo("downloaded source of Foo Bar.c");
+ verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", "GET", false, 30 * 1000);
}
@Test
@@ -113,9 +130,9 @@ public class LastSnapshotsTest {
when(mode.isPreview()).thenReturn(true);
LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server);
- String source = lastSnapshots.getSource(newFile());
+ String source = lastSnapshots.getSource(newFileWithSpace());
assertThat(source).isEqualTo("");
- verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", "GET", false, 30 * 1000);
+ verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FFoo+Bar.c&format=txt", "GET", false, 30 * 1000);
}
@Test
@@ -134,4 +151,10 @@ public class LastSnapshotsTest {
file.setEffectiveKey("myproject:org/foo/Bar.c");
return file;
}
+
+ private File newFileWithSpace() {
+ File file = new File("org/foo", "Foo Bar.c");
+ file.setEffectiveKey("myproject:org/foo/Foo Bar.c");
+ return file;
+ }
}
diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/sonar-project.properties b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/sonar-project.properties
new file mode 100644
index 00000000000..8810e376701
--- /dev/null
+++ b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/sonar-project.properties
@@ -0,0 +1,6 @@
+sonar.projectKey=sample
+sonar.projectName=Sample
+sonar.projectVersion=0.1-SNAPSHOT
+sonar.sources=xources
+sonar.tests=testx
+sonar.language=xoo
diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/testx b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/testx
new file mode 120000
index 00000000000..7385ebd51cf
--- /dev/null
+++ b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/testx
@@ -0,0 +1 @@
+../sample/testx/ \ No newline at end of file
diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/xources b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/xources
new file mode 120000
index 00000000000..15dca9d90d2
--- /dev/null
+++ b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-symlink/xources
@@ -0,0 +1 @@
+../sample/xources/ \ No newline at end of file
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
index bcce4d8668b..1fdb243b393 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
@@ -105,9 +105,11 @@ public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Depe
public abstract void setSource(Resource reference, String source);
/**
- * @return source code associated with a specified resource, <code>null</code> if not available
+ * @return source code associated with a specified resource, <code>null</code> if not available
+ * (for example when sonar.importSources=false)
* @since 2.9
*/
+ @CheckForNull
public abstract String getSource(Resource resource);
public abstract Project getProject();
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
index a5fa7ab0d93..ec3bb5efa07 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java
@@ -62,7 +62,15 @@ public class ProjectDefinition {
*/
@Deprecated
public static final String TEST_FILES_PROPERTY = "sonar.testFiles";
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
public static final String BINARIES_PROPERTY = "sonar.binaries";
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
public static final String LIBRARIES_PROPERTY = "sonar.libraries";
public static final String BUILD_DIR_PROPERTY = "sonar.buildDir";
@@ -450,6 +458,10 @@ public class ProjectDefinition {
return tests();
}
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
public List<String> getBinaries() {
String sources = properties.getProperty(BINARIES_PROPERTY, "");
return trim(StringUtils.split(sources, SEPARATOR));
@@ -458,17 +470,26 @@ public class ProjectDefinition {
/**
* @param path path to directory with compiled source. In case of Java this is directory with class files.
* It can be absolute or relative to project directory.
- * TODO currently Sonar supports only one such directory due to dependency on MavenProject
+ * @deprecated since 4.5.1 use SonarQube Java specific API
*/
+ @Deprecated
public ProjectDefinition addBinaryDir(String path) {
appendProperty(BINARIES_PROPERTY, path);
return this;
}
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
public ProjectDefinition addBinaryDir(File f) {
return addBinaryDir(f.getAbsolutePath());
}
+ /**
+ * @deprecated since 4.5.1 use SonarQube Java specific API
+ */
+ @Deprecated
public List<String> getLibraries() {
String sources = properties.getProperty(LIBRARIES_PROPERTY, "");
return trim(StringUtils.split(sources, SEPARATOR));
@@ -477,7 +498,9 @@ public class ProjectDefinition {
/**
* @param path path to file with third-party library. In case of Java this is path to jar file.
* It can be absolute or relative to project directory.
+ * @deprecated since 4.5.1 use SonarQube Java specific API
*/
+ @Deprecated
public void addLibrary(String path) {
appendProperty(LIBRARIES_PROPERTY, path);
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
index 1dbaeaeca11..6da18e4a049 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
@@ -26,6 +26,7 @@ import org.sonar.api.BatchComponent;
import org.sonar.api.utils.PathUtils;
import javax.annotation.CheckForNull;
+
import java.io.File;
import java.util.Collection;
import java.util.List;
@@ -40,7 +41,7 @@ public class PathResolver implements BatchComponent {
File file = new File(path);
if (!file.isAbsolute()) {
try {
- file = new File(dir, path).getCanonicalFile();
+ file = new File(dir, path).getAbsoluteFile();
} catch (Exception e) {
throw new IllegalStateException("Fail to resolve path '" + path + "' relative to: " + dir.getAbsolutePath(), e);
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
index 54a34757d10..2f2cde7b5e7 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
@@ -30,9 +30,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class KeyValueFormatTest {
@@ -42,7 +40,8 @@ public class KeyValueFormatTest {
map.put("lucky", "luke");
map.put("aste", "rix");
String s = KeyValueFormat.format(map);
- assertThat(s, is("lucky=luke;aste=rix"));// same order
+ // same order
+ assertThat(s).isEqualTo("lucky=luke;aste=rix");
}
@Test
@@ -51,7 +50,7 @@ public class KeyValueFormatTest {
map.put(3, "three");
map.put(5, "five");
String s = KeyValueFormat.formatIntString(map);
- assertThat(s, is("3=three;5=five"));// same order
+ assertThat(s).isEqualTo("3=three;5=five");
}
@Test
@@ -60,7 +59,7 @@ public class KeyValueFormatTest {
map.put(13, 2.0);
map.put(5, 5.75);
String s = KeyValueFormat.formatIntDouble(map);
- assertThat(s, is("13=2.0;5=5.75"));// same order
+ assertThat(s).isEqualTo("13=2.0;5=5.75");
}
@Test
@@ -69,14 +68,14 @@ public class KeyValueFormatTest {
map.put(13, null);
map.put(5, 5.75);
String s = KeyValueFormat.formatIntDouble(map);
- assertThat(s, is("13=;5=5.75"));// same order
+ assertThat(s).isEqualTo("13=;5=5.75");
}
@Test
public void shouldFormatBlank() {
Map<Integer, String> map = Maps.newTreeMap();
String s = KeyValueFormat.formatIntString(map);
- assertThat(s, is(""));
+ assertThat(s).isEqualTo("");
}
@Test
@@ -86,46 +85,46 @@ public class KeyValueFormatTest {
map.put(20, new SimpleDateFormat("yyyy-MM-dd").parse("2009-05-28"));
map.put(12, null);
String s = KeyValueFormat.formatIntDate(map);
- assertThat(s, is("4=2010-12-25;20=2009-05-28;12="));
+ assertThat(s).isEqualTo("4=2010-12-25;20=2009-05-28;12=");
}
@Test
public void shouldParseStrings() {
Map<String, String> map = KeyValueFormat.parse("one=un;two=deux");
- assertThat(map.size(), is(2));
- assertThat(map.get("one"), is("un"));
- assertThat(map.get("two"), is("deux"));
- assertThat(map.keySet().iterator().next(), is("one"));// same order as in string
+ assertThat(map.size()).isEqualTo(2);
+ assertThat(map.get("one")).isEqualTo("un");
+ assertThat(map.get("two")).isEqualTo("deux");
+ assertThat(map.keySet().iterator().next()).isEqualTo("one");// same order as in string
}
@Test
public void shouldParseBlank() {
Map<String, String> map = KeyValueFormat.parse("");
- assertThat(map.size(), is(0));
+ assertThat(map.size()).isEqualTo(0);
}
@Test
public void shouldParseNull() {
Map<String, String> map = KeyValueFormat.parse(null);
- assertThat(map.size(), is(0));
+ assertThat(map.size()).isEqualTo(0);
}
@Test
public void shouldParseEmptyFields() {
Map<Integer, Double> map = KeyValueFormat.parseIntDouble("4=4.2;2=;6=6.68");
- assertThat(map.size(), is(3));
- assertThat(map.get(4), is(4.2));
- assertThat(map.get(2), nullValue());
- assertThat(map.get(6), is(6.68));
+ assertThat(map.size()).isEqualTo(3);
+ assertThat(map.get(4)).isEqualTo(4.2);
+ assertThat(map.get(2)).isNull();
+ assertThat(map.get(6)).isEqualTo(6.68);
}
@Test
public void shouldConvertPriority() {
- assertThat(KeyValueFormat.newPriorityConverter().format(RulePriority.BLOCKER), is("BLOCKER"));
- assertThat(KeyValueFormat.newPriorityConverter().format(null), is(""));
+ assertThat(KeyValueFormat.newPriorityConverter().format(RulePriority.BLOCKER)).isEqualTo("BLOCKER");
+ assertThat(KeyValueFormat.newPriorityConverter().format(null)).isEqualTo("");
- assertThat(KeyValueFormat.newPriorityConverter().parse("MAJOR"), is(RulePriority.MAJOR));
- assertThat(KeyValueFormat.newPriorityConverter().parse(""), nullValue());
+ assertThat(KeyValueFormat.newPriorityConverter().parse("MAJOR")).isEqualTo(RulePriority.MAJOR);
+ assertThat(KeyValueFormat.newPriorityConverter().parse("")).isNull();
}
@Test
@@ -134,16 +133,16 @@ public class KeyValueFormatTest {
set.add("foo");
set.add("foo");
set.add("bar");
- assertThat(KeyValueFormat.format(set), is("foo=2;bar=1"));
+ assertThat(KeyValueFormat.format(set)).isEqualTo("foo=2;bar=1");
}
@Test
public void shouldParseMultiset() {
Multiset<String> multiset = KeyValueFormat.parseMultiset("foo=2;bar=1;none=");
- assertThat(multiset.count("foo"), is(2));
- assertThat(multiset.count("bar"), is(1));
- assertThat(multiset.count("none"), is(0));
- assertThat(multiset.contains("none"), is(false));
+ assertThat(multiset.count("foo")).isEqualTo(2);
+ assertThat(multiset.count("bar")).isEqualTo(1);
+ assertThat(multiset.count("none")).isEqualTo(0);
+ assertThat(multiset.contains("none")).isFalse();
}
@Test
@@ -151,6 +150,6 @@ public class KeyValueFormatTest {
Multiset<String> multiset = KeyValueFormat.parseMultiset("foo=2;bar=1");
// first one is foo
- assertThat(multiset.iterator().next(), is("foo"));
+ assertThat(multiset.iterator().next()).isEqualTo("foo");
}
}