Is there a way to tell eclipse to organize import A, import A.X and import B in that order? - eclipse

Please see the sample program below (just to illustrate the problem and the code as such does nothing):
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
Map map;
Properties props;
Entry entry = new Entry<String, String>() {
#Override
public String setValue(String value) {
// TODO Auto-generated method stub
return null;
}
#Override
public String getValue() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getKey() {
// TODO Auto-generated method stub
return null;
}
};
}
}
Eclipse organizes them as above and not as follows:
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
thanks.

everithing you can do about the "organize import" function is defined in the following preferences page:
preferences -> java -> code style -> organize imports

I realized that it's a bug in solaris eclipse v 3.5.0. This doesn't seem to happen on eclipse helios on windows.

Related

Fetch all Form tags from HTML from console in eclipse selenium java

I got the HTML in console using getpagesource in selenium java .
Now i need only the tag 'Forms' in the console result.
How do i do that?
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://''/tandem/login/?");
String pagesource = driver.getPageSource();
System.out.println(pagesource);
}
You can use following code snippet for this :
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//Sample Java class to fetch list of all tag with tagname forms
public class Test {
public static void main(String[] args) {
String geckoDriverPath=Class1.class.getResource("/firefox/geckodriver.exe").getPath();
System.setProperty("webdriver.gecko.driver",geckoDriverPath);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.URL.com");
List<WebElement>list =driver.findElements(By.tagName("form"));
for(WebElement ele:list) {
System.out.println(ele.getText());
}
}
}
Once you get the lists you can fetch individual form element and perform action like getting text.Hope, this helps

How to set a global custom Jackson deserializer in Camel w/o spring using REST

I would like to set a global custom date/Time deserializer on a camel route that is configured with rest.
What I already found is Camel + Jackson : Register a module for all deserialization
But I do not have the unmarshal() method in the route, but use the
RestDefinition rest(String path)
method from
org.apache.camel.builder.RouteBuilder.
We do not use Spring, but plain Camel with Scala and REST all configuration done programmatically (no xml).
My current solution is to use
#JsonDeserialize(using = classOf[MyDeserializer])
annotation on every date/time field, but that is not a satisfying solution.
Does anybody have a clue how to configure Camel to use the custom deserializer everywhere?
By default camel use DATES_AS_TIMESTAMPS feature. to disable this featuer just add .dataFormatProperty("json.out.disableFeatures", "WRITE_DATES_AS_TIMESTAMPS")
Also you can add custom serializer:
module.addSerializer(Date.class, sample.new DateSerializer());
and binding with name json-jackson
jndiContext.bind("json-jackson", jackson);
example:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.util.jndi.JndiContext;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class Samlpe3 {
public static void main(String[] args) throws Exception {
Samlpe3 sample = new Samlpe3();
//custom mapper
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("DefaultModule", new Version(0, 0, 1, null, null, null));
module.addSerializer(Date.class, sample.new DateSerializer());
module.addSerializer(Person.class, sample.new PersonSerializer());
objectMapper.registerModule(module);
JacksonDataFormat jackson = new JacksonDataFormat(objectMapper, null);
JndiContext jndiContext = new JndiContext();
jndiContext.bind("json-jackson", jackson);
CamelContext context = new DefaultCamelContext(jndiContext);
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
restConfiguration().component("jetty").bindingMode(RestBindingMode.json)
.host("0.0.0.0").contextPath("/test").port(8080)
//disableFeatures WRITE_DATES_AS_TIMESTAMPS
.dataFormatProperty("json.out.disableFeatures", "WRITE_DATES_AS_TIMESTAMPS")
;
rest("/v1/").produces("application/json")
.get("persons")
.to("direct:getPersons");
from("direct:getPersons")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(new Person("Sergey", new GregorianCalendar().getTime()));
}
})
;
}
});
context.start();
Thread.sleep(60000);
context.stop();
}
public class DateSerializer extends JsonSerializer<Date> {
#Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(value);
gen.writeString(date);
}
}
public class PersonSerializer extends JsonSerializer<Person> {
#Override
public void serialize(Person value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeFieldName("Changed_n");
gen.writeObject(value.getName() + " Changed");
gen.writeFieldName("Changed_b");
gen.writeObject(value.getBirthday());
gen.writeEndObject();
}
}
}
Person.java:
import java.util.Date;
public class Person {
private String name;
private Date birthday;
Person(String name, Date birhday){
System.out.println("Person");
this.setBirthday(birhday);
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

How to call a class from another activity?

I want to call MainActivity class to another activity. This is my code for the MainActivity.java:
package com.blinkedup.geolocationchat;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
LocationManager locationManager;
MyLocationListener locationListener = new MyLocationListener();
Criteria criteria;
String bestProvider;
String listOfBestProviders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
criteria = new Criteria();
textView = (TextView) findViewById(R.id.textView1);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
bestProvider = locationManager.getBestProvider(criteria, true);
Toast.makeText(getApplicationContext(), bestProvider, 3).show();
}
protected void onPause(){
super.onPause();
locationManager.removeUpdates(locationListener);
}
private class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
textView.setText("Latitude: " + location.getLatitude() +
"Longitude: " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
In another activity I wanted to call the lat and long of the above code but don't want to rewrite the code to the activity. I just want to call it and display the result in another activity. please help. thanks
make your variable static and define it before onCreate() method use that variable in another activity by call like this.
YourMainActivity.yourstaticvariable

Using Testng in Eclipse Juno

I am using Eclipse Juno. I installed TestNG plugins properly. But when I want to run test I can't find a TestNG option in Run As. What's the matter?
My Class is
package com.oasisdigital.rental.client;
import static javax.ws.rs.core.Response.Status.CREATED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import javax.ws.rs.core.Response;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.oasisdigital.rental.test.AbstractIT;
#Test
public class ClientResourceIT extends AbstractIT {
private ClientApi clientApi;
#Override
#BeforeMethod
public void setUp() {
super.setUp();
this.clientApi = new ClientApi(api);
}
#Test
public void shouldReturnEmptyListWhenNoProviders() {
assertThat(clientApi.getClients(), is(empty()));
}
#Test
public void shouldReturnClientAfterCreation() {
Response resp = clientApi.postClient("Jimmy");
assertThat(resp.getStatus(), is(CREATED.getStatusCode()));
ClientDto client = resp.readEntity(ClientDto.class);
assertThat(client.getId(), is(notNullValue()));
assertThat(client.getName(), is("Jimmy"));
assertThat(clientApi.getClients(), contains(client));
}
}

accessing array with object

hi i have two classes in android and in one class i have write an array and i want to access it in the main class but the error is give me that "force closed" here is my code
package com.semanticnotion.DAO;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DAO extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WordsDAO DAO = new WordsDAO(new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"});
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), WordsDAO.class);
startActivity(myIntent);
}
});
}
}
and the second class code is
package com.semanticnotion.DAO;
public class WordsDAO
{
String[] words = new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"};
public WordsDAO(String[] words )
{
this.words=words;
}
}
please any one tell what well be the error in this code thaks
First of all: the constructor in your second class would not be used. The way to pass parameters to another activity is to use Intent.putExtra in the code calling the other activity and in your other activity use
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
to get the data in onCreate.
That said, I guess the problem arises from your second class not providing an explicit parameterless constructor.