When running the following code on my Sonar, measure returns null. (Should return something on the lines of "1=author;2=author..."). However it works on nemo.sonarsource.org.
Other measures eg: violation work correctly on mine so this is probably not a question of faulty code. I suspect I need to configure Sonar somehow?
private final Sonar sonar;
public String getAuthors(String resourceKey){
return getMeasure(resourceKey, "authors_by_line").getData();
}
private Measure getMeasure(String resourceKey, String measureName){
Resource resource = sonar.find(ResourceQuery.createForMetrics(
resourceKey, measureName));
Measure measure = resource.getMeasure(measureName);
return measure;
}
You must have installed the SCM Activity plugin and enabled it (in Sonar settings, see the documentation on our Wiki) in order to be able to get this metric.
Then, once you have reanalysed your project, you'll get the expected behaviour.
Related
I have a java project with some test files in the following location:
src\test\resources\data\file\daily
I have some Junit test cases that check and assert based on the file modified time.
FileTime modFileTime = Files.getLastModifiedTime(Paths.get(classPathResource.getFile().getPath()));
when I execute the test cases using intellij without maven, my test passes and the modFileTime has time from the past e.g. 16/04/21 19:48
However, my test cases are failing when I run the tests using maven clean test as the file modified timings in target\test-classes\data\file\daily directory get updated timings.
How can I preserve the original file modified timings? or is there a common solution for this?
The method being called with test:
private boolean isFileAvailable(String file) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(file);
boolean exists = Files.exists(Paths.get(classPathResource.getFile().getPath()));
if (exists) {
FileTime modFileTime = Files.getLastModifiedTime(Paths.get(classPathResource.getFile().getPath()));
long modFileMinutes = modFileTime.to(TimeUnit.MINUTES);
long minutes = FileTime.from(Instant.now()).to(TimeUnit.MINUTES);
return minutes - modFileMinutes >= 5;
} else {
return false;
}
}
mvn clean is getting rid of everything in your target/ directory before running the tests, and repopulating it. Hence, the timestamp will change every run. But this also will be the case for an initial (clean) checkout of the project, which you should be doing before any release build, so ... this is a pretty normal thing to be happening.
However, I agree with all the comments -- your test (not posted) doesn't make a lot of sense. If you want to have your test check a file with a relative timestamp, then e.g. set the timestamp on the file to 4 minutes ago and confirm it's not loaded, then set it to 6 minutes ago and confirm it's loaded. You can set the last-modified value on your test file from within the test. This is much more reliable than relying on something in the test execution system (maven) itself, especially if you generate the test file as part of the test (a good idea)
Also: if you only want to load data files older than a certain time, then I doubt you really want to have those be classpath resources. They should probably be loaded from some other known location. I suspect you are trying to solve some problem with cleverness that would be better solved by something from, e.g., https://commons.apache.org/
I got some trouble with a sonar issue:
Remove this unused method parameter "messageId".
well my method looks like:
private static Optional<Status> getStatus(Member member, String messageId) {
return Optional.ofNullable(member)
.map(Member::getTraffic)
.map(Traffic::getSymlinks)
.map(messages -> messages
.stream()
.filter(message -> message.getMessageId().equals(messageId))
.findFirst()).flatMap(message -> message.map(Message::getStatus));
}
the messageId is used inside the equals of the filter.
It is no field. It is the method parameter.
Is that a sonar bug or have I overlooked something?
We are using Sonar 5.6
Yeah, looks like a Bug . Seems this got recently addressed in SonarJava 5.0 (compatible with SonarQube 6.7+) which got released last December
https://jira.sonarsource.com/browse/SONARJAVA-2115
I have this project that I'm doing and for whatever reason, whenever I execute the program and put in the given arguments required for it (that I set and all) and occasionally an IOException is thrown before anything else is executed. It seems to be true because I got loggers everywhere and none of them are being fired. However, it seems that just the loggers are not being fired cause when I look in the json file I output to, it shows that it did do the first step of the execution, just no loggers. I'm new to log4j2 so it may be that but I'm not sure (with the loggers not being fired) but it seems weird that an IOException occurs when it shouldn't at all. Cause when I execute it again right after the crash, it runs just fine.
(Side note: this is in kotlin/jvm, but this is pertaining to the use of the JDK File class)
The exception is thrown here: https://github.com/AlexCouch/projauto/blob/master/src/main/java/thinkingcouch/projauto/Save.kt#L114
I'm on MacOSX High Sierra using Intellij IDEA 2017.3.
So what ended up happening was I had this function here for isolating a certain part of the given path to be appended to a new path and also saved to json for later use
fun Path.splitPathWithContext(context: String): File{
val presplit = this.normalize()
logger.info("presplit: $presplit")
logger.info("context: $context")
if(presplit.toString() == context){
logger.info("Path and context are the same.")
return presplit.toFile()
}
val reg = Pattern.compile("\\b$context\\b")
val ret = presplit.toString().split(reg)[1]
logger.info("ret: $ret")
return File(ret)
}
The solution was to do a strict pattern check against the context variable so that it doesn't cut at a word that contains that string but isn't that string exactly, and it needed to be exact. This solved my issue. No more problems, and no more broken paths, and I also fixed my loggers. I don't know exactly what was causing it to not do any logging, but I fixed it by setting the root level to "all" and then removing all my other logger elements since that's all I needed to do.
I am trying to replace some of the enumerations in my source with IntDef annotation. I have been following this documentation.
I have a variable for holding a ViewMode which was previously an enumeration. Now I have changed it to some thing like below.
#Retention(RetentionPolicy.SOURCE)
#IntDef({ViewMode.VIEW_MODE_LIST_VIEW, ViewMode.VIEW_MODE_CARD_VIEW})
public #interface ViewMode {
int VIEW_MODE_LIST_VIEW = 0;
int VIEW_MODE_CARD_VIEW = 1;
}
#ViewMode
public int currentViewMode = ViewMode.VIEW_MODE_LIST_VIEW;
Now to test whether this is safe or not I have done the following in a method
this.currentViewMode = 987; //currentViewMode should be 0 or 1. Nothing else.
But this is now not giving me a compilation error. Am I missing something here?
You will not get a compilation error, because the enumerated annotations are just lint checks: see Improve Your Code with Lint
You should see the error-marker directly in Android Studio or when you run android lint checks from the command line: see Improve Your Code with Lint
Note: you can also configure your build to automatically run the lint checks:
see SO: Run lint when building android studio projects
But running the linter takes some time, so you may choose to run it only for your release builds or only on your CI server.
For testing a application with TestFX i need to get the actual primary stage of a running application. This means that i haven't the code, i can just run the application through a jar.
Is there any possible solution for this? Scenic View does this already, but i was not able to reproduce this functionallity, especially because it seems that they use the deprecated funtion
Windows.impl_getWindows
which is not working in my case.
Try this:
import com.sun.javafx.robot.impl.FXRobotHelper;
static Collection<Stage> getAllJavaFXStages() {
try {
return FXRobotHelper.getStages();
} catch ( NullPointerException npe ) {
// nasty NPE if no stages exist
return Collections.emptyList();
}
}
```
Based on my own testing framework code: Automaton.
EDIT:
If you want to get a Stage from a different JVM instance than where you're running your code, then there's no simple way.
You're right, ScenicView does it, but it uses tools.jar to do it. This is not a standard jar you get in your runtime, so you must add it manually (placing it in jre/lib/ext should do it, you'll normally find it in lib only).
I tracked down the code where ScenicView seems to be doing it in their BitBucket repo.
Check the function getRunningJavaFXApplications for example.
Have fun using that in your tests!