From: Simon Brandhof Date: Mon, 24 Feb 2014 15:17:03 +0000 (+0100) Subject: SONAR-926 Improve logs in ResourceKeyMigration X-Git-Tag: 4.2~59 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a7927c95bdaacf87b6a0f7d9611e77b1060d636;p=sonarqube.git SONAR-926 Improve logs in ResourceKeyMigration --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java index 21d10c38f01..b581c2ad8fe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java @@ -57,7 +57,6 @@ public class ResourceKeyMigration implements BatchComponent { public void checkIfMigrationNeeded(Project rootProject) { ResourceModel model = session.getSingleResult(ResourceModel.class, "key", rootProject.getEffectiveKey()); if (model != null && StringUtils.isBlank(model.getDeprecatedKey())) { - logger.info("Resources keys of project '" + rootProject.getName() + "' should be migrated"); this.migrationNeeded = true; } } @@ -68,16 +67,16 @@ public class ResourceKeyMigration implements BatchComponent { void migrateIfNeeded(Project module, Iterable inputFiles) { if (migrationNeeded) { - logger.info("Starting migration of resource keys"); + logger.info("Update component keys"); Map deprecatedFileKeyMapper = new HashMap(); Map deprecatedTestKeyMapper = new HashMap(); Map deprecatedDirectoryKeyMapper = new HashMap(); for (InputFile inputFile : inputFiles) { String deprecatedKey = ((DefaultInputFile) inputFile).deprecatedKey(); if (deprecatedKey != null) { - if (InputFile.Type.TEST == inputFile.type()) { + if (InputFile.Type.TEST == inputFile.type() && !deprecatedTestKeyMapper.containsKey(deprecatedKey)) { deprecatedTestKeyMapper.put(deprecatedKey, inputFile); - } else { + } else if (InputFile.Type.MAIN == inputFile.type() && !deprecatedFileKeyMapper.containsKey(deprecatedKey)) { deprecatedFileKeyMapper.put(deprecatedKey, inputFile); } } @@ -99,7 +98,7 @@ public class ResourceKeyMigration implements BatchComponent { .append(ResourceModel.class.getSimpleName()) .append(" where enabled = true ") .append(" and rootId = :rootId ") - .append(" and scope = '").append(Scopes.FILE).append("' order by key"); + .append(" and scope = '").append(Scopes.FILE).append("' order by qualifier, key"); List resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); for (ResourceModel resourceModel : resources) { String oldEffectiveKey = resourceModel.getKey(); @@ -125,9 +124,9 @@ public class ResourceKeyMigration implements BatchComponent { } resourceModel.setKey(newEffectiveKey); resourceModel.setDeprecatedKey(oldEffectiveKey); - logger.info("Migrated resource {} to {}", oldEffectiveKey, newEffectiveKey); + logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey); } else { - logger.warn("Unable to migrate resource {}. No match was found.", oldEffectiveKey); + logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey); } } } @@ -146,7 +145,7 @@ public class ResourceKeyMigration implements BatchComponent { .append(ResourceModel.class.getSimpleName()) .append(" where enabled = true ") .append(" and rootId = :rootId ") - .append(" and qualifier = '").append(Qualifiers.DIRECTORY).append("')"); + .append(" and qualifier = '").append(Qualifiers.DIRECTORY).append("'"); List resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); for (ResourceModel resourceModel : resources) { String oldEffectiveKey = resourceModel.getKey(); @@ -154,9 +153,9 @@ public class ResourceKeyMigration implements BatchComponent { String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey); resourceModel.setKey(newEffectiveKey); resourceModel.setDeprecatedKey(oldEffectiveKey); - logger.info("Migrated resource {} to {}", oldEffectiveKey, newEffectiveKey); + logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey); } else { - logger.warn("Unable to migrate resource {}. No match was found.", oldEffectiveKey); + logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey); } } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java index 826785021a4..540fcc6b64f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java @@ -99,17 +99,16 @@ public class ResourceKeyMigrationTest extends AbstractDbUnitTestCase { Logger logger = mock(Logger.class); ResourceKeyMigration migration = new ResourceKeyMigration(getSession(), logger); migration.checkIfMigrationNeeded(multiModuleProject); - verify(logger).info("Resources keys of project 'Root' should be migrated"); - + migration.migrateIfNeeded(javaModule, javaInputFiles); migration.migrateIfNeeded(phpModule, phpInputFiles); - verify(logger).info("Migrated resource {} to {}", "b:org.foo.Bar", "b:src/main/java/org/foo/Bar.java"); + verify(logger).info("Component {} changed to {}", "b:org.foo.Bar", "b:src/main/java/org/foo/Bar.java"); verify(logger).warn("Directory with key b:org/foo matches both b:src/main/java/org/foo and b:src/test/java/org/foo. First match is arbitrary chosen."); - verify(logger).info("Migrated resource {} to {}", "b:org.foo.BarTest", "b:src/test/java/org/foo/BarTest.java"); - verify(logger).info("Migrated resource {} to {}", "b:[default].RootBar", "b:src/main/java/RootBar.java"); - verify(logger).info("Migrated resource {} to {}", "b:org/foo", "b:src/main/java/org/foo"); - verify(logger).info("Migrated resource {} to {}", "b:[root]", "b:src/main/java"); + verify(logger).info("Component {} changed to {}", "b:org.foo.BarTest", "b:src/test/java/org/foo/BarTest.java"); + verify(logger).info("Component {} changed to {}", "b:[default].RootBar", "b:src/main/java/RootBar.java"); + verify(logger).info("Component {} changed to {}", "b:org/foo", "b:src/main/java/org/foo"); + verify(logger).info("Component {} changed to {}", "b:[root]", "b:src/main/java"); checkTables("shouldMigrateResourceKeys", new String[]{"build_date", "created_at"}, "projects"); }