Categories
discuss

Jacoco data file not readable for Android on TeamCity

I´m learning TeamCity Integration Server today and I´m trying yo enable Jococo Reports with my Android Gradle based Application.

This document shows me how to enable Jococo coverage, with the following warning:

Make sure your tests run in the fork=true mode. Otherwise the coverage data may not be properly collected.

I don´t know what should I do to “run my tests in fork=true mode”. TeamCity isn´t generating coverage reports and is warning me with the following log:

Jacoco data file path specified as C:TeamCitybuildAgenttempbuildTmpJACOCO5884661263301729570coveragejacoco.exec but is not readable. Coverage will not be collected.

I think that this warning is related to not running the test in fork=true mode.

So, my question is:

  1. What fork=true mode means and
  2. How to enable it at gradle?

Thanks!!!

Answer

After some research, I was able to instruct Teamcity to process coverage reports generated by jacoco using “services message” technique, explained on this:

Since TeamCity 9.0, TeamCity is able to parse JaCoCo coverage data and generate a report using a service message of the following format:

##teamcity[jacocoReport dataPath='<path to jacoco.exec file>']

So, I modified my build.gradle file adding the folowing lines to jacocoTestReport section:

if (project.hasProperty("teamcity")) {
    println '##teamcity[jacocoReport dataPath='app/build/jacoco/testDebug.exec' includes='com.mynamespace.myproject.*' excludes='**/R.class **/R$*.class **/*$ViewInjector*.* **/BuildConfig.* **/Manifest*.*']'
}

After that, the complere jacocoTestReport was:

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: '../app/build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)

    executionData = files('../app/build/jacoco/testDebug.exec')
    if (project.hasProperty("teamcity")) {
        println '##teamcity[jacocoReport dataPath='app/build/jacoco/testDebug.exec' includes='com.mynamespace.myproject.*' excludes='**/R.class **/R$*.class **/*$ViewInjector*.* **/BuildConfig.* **/Manifest*.*']'
    }

    reports {
        xml.enabled = true
        html.enabled = true
    }

}

And the Teamcity started to report CodeCoverage as belows:

enter image description here

Categories
discuss

autocomplete from php array

i wrote this code to do autocomplete from php array but it is not working, can anybody help?

php array

$cars = array("Volvo", "BMW", "Toyota");

my form

<form id="frm" method="post">
<input id="cartag" type="text" name="car">
</form>

script

$(function() {
var availableTags = [ <?php echo implode(',',$cars); ?>];
	$( "#cartag" ).autocomplete({
	source: availableTags
	});
});

Answer

if you want to use php array in jQuery you have to user json_encode.

like this:

var availableTags =  <?php echo json_encode($cars); ?>;

Working demo:

<?php

$cars = array("Volvo", "BMW", "Toyota");

?>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<form id="frm" method="post">
<input id="cartag" type="text" name="car">
</form>
<script>

$(function() {
var availableTags =  <?php echo json_encode($cars); ?>;
    $( "#cartag" ).autocomplete({
    source: availableTags
    });
});

</script>
Categories
discuss

Get timezone abbreviation using offset value

Using moment.js (with moment-timezone), I want to get the timezone abbreviation (e.g. PST) for the current locale.

var now = Date.now(); // 1423254073931
var zone = moment(now).zone(); // 480
var timezone = 

How do I get the timezone abbreviation? All of the examples I have seen in the docs and elsewhere pick a specific region like "America/New_York".

From the docs, it looks like I can get the information out of the Zone Object with zone.abbr(timestamp) but I’m not sure how to access the zone object.

JSFiddle

Answer

The title and the question are different. In the title, you ask how to get it using the offset – which would not be possible. There are many time zones that share the same offset, so it isn’t possible to distinguish a time zone abbreviation from an offset alone.

But in the question, you asked how to get the abbreviation for the current locale, for a specific timestamp.

The general problem is, there is no fully-reliable way to detect the current time zone. This is discussed in this answer. So moment-timezone can’t deterministically tell which time zone should be loaded by default.

There are some other options available though.

Current browsers / node

In current browsers, the ECMAScript Internationalization API extensions are supported on the toLocaleString function of the Date object. When supported, you can do this:

    var d = new Date(); // or whatever date you have
    var tzName = d.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop();

In current browsers, you’ll get a value like “EST”. You might want to do some sort of tests though, because it won’t work in all browsers.

Use jsTimeZoneDetect

You could use a script like jsTimeZoneDetect to guess at the local time zone. It’s usually correct, but not guaranteed. You could then pass that value to moment-timezone.

    var tzName = jstz.determine().name();
    var m = moment();
    var abbr = m.tz(tzName).zoneAbbr();  // or .format('z')

Use moment-timezone

There is also now built-in support for time zone detection/guessing in moment-timezone:

    var tzName = moment.tz.guess();
    var abbr = m.tz(tzName).zoneAbbr();  // or .format('z')
Categories
discuss

Spring – Programmatically generate a set of beans

I have a Dropwizard application that needs to generate a dozen or so beans for each of the configs in a configuration list. Things like health checks, quartz schedulers, etc.

Something like this:

@Component
class MyModule {
    @Inject
    private MyConfiguration configuration;

    @Bean
    @Lazy
    public QuartzModule quartzModule() {
        return new QuartzModule(quartzConfiguration());
    }


    @Bean
    @Lazy
    public QuartzConfiguration quartzConfiguration() {
        return this.configuration.getQuartzConfiguration();
    }

    @Bean
    @Lazy
    public HealthCheck healthCheck() throws SchedulerException {
        return this.quartzModule().quartzHealthCheck();
    }
}

I have multiple instances of MyConfiguration that all need beans like this. Right now I have to copy and paste these definitions and rename them for each new configuration.

Can I somehow iterate over my configuration classes and generate a set of bean definitions for each one?

I would be fine with a subclassing solution or anything that is type safe without making me copy and paste the same code and rename the methods ever time I have to add a new service.

EDIT: I should add that I have other components that depend on these beans (they inject Collection<HealthCheck> for example.)

Answer

The “best” approach I could come up with was to wrap all of my Quartz configuration and schedulers in 1 uber bean and wire it all up manually, then refactor the code to work with the uber bean interface.

The uber bean creates all the objects that I need in its PostConstruct, and implements ApplicationContextAware so it can auto-wire them. It’s not ideal, but it was the best I could come up with.

Spring simply does not have a good way to dynamically add beans in a typesafe way.

Categories
discuss

Android parse String to Date – unknown pattern character ‘X’

I have Service fetch date string from web and then I want to pare it to Date object. But somehow application crashes. This is my string that I’m parsing: 2015-02-05T05:20:02+00:00

onStartCommand()

String datetime = "2015-02-05T05:20:02+00:00";
Date new_date = stringToDate(datetime);

stringToDate()

private Date stringToDate(String s){
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    try{
        return df.parse(s);
    }catch(ParseException e){
        e.printStackTrace();
    }
    return null;
}

LogCat:

02-06 20:37:02.008: E/AndroidRuntime(28565): FATAL EXCEPTION: main
02-06 20:37:02.008: E/AndroidRuntime(28565): Process: com.dotmav.runescapenotifier, PID: 28565
02-06 20:37:02.008: E/AndroidRuntime(28565): java.lang.RuntimeException: Unable to start service com.dotmav.runescapenotifier.GEService@384655b5 with Intent { cmp=com.dotmav.runescapenotifier/.GEService }: java.lang.IllegalArgumentException: Unknown pattern character 'X'
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2881)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.app.ActivityThread.access$2100(ActivityThread.java:144)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.os.Looper.loop(Looper.java:135)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.lang.reflect.Method.invoke(Native Method)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.lang.reflect.Method.invoke(Method.java:372)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-06 20:37:02.008: E/AndroidRuntime(28565): Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'X'
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:314)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:303)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:356)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:249)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at com.dotmav.runescapenotifier.GEService.stringToDate(GEService.java:68)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at com.dotmav.runescapenotifier.GEService.onStartCommand(GEService.java:44)
02-06 20:37:02.008: E/AndroidRuntime(28565):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2864)
02-06 20:37:02.008: E/AndroidRuntime(28565):    ... 9 more

EDIT: onDestroy() set alarm for periodical update…

AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
    AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + (1000 * 60),
    PendingIntent.getService(this, 0, new Intent(this, GEService.class), 0)
);

Answer

Remove “XXX” from

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

and everything would work fine.

Go through the list of symbols that can be used inside a SimpleDateFormat constructor. Although the documentation shows the “XXX” format, this doesn’t work on Android and will throw an IllegalArgumentException.

Probably you are looking for "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

Change your code to

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); 

or

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // if timezone is required
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..