Test value of a HashSet in a FitNesse table cell? - hashset

Using FitNesse with FitSharp (.Net), I've got a property of an object that is a HashSet of an enum, e.g.
public enum Fubar { Foo, Bar }
public class Test
{
public HashSet<Fubar> SetOfFubar { get; set; }
}
I'd like to test it simply, for example in an array fixture like so:
|MyArrayFixture|
|SetOfFubar|
|Foo|
|Foo,Bar|
|Bar|
||
This simply results in all rows marked missing & red and a set of surplus rows showing something like:
System.Collections.Generic.HashSet`1[MyNamespace.Fubar] surplus
What's the easiest way to get FitNesse with FitSharp (.Net) to understand HashSets?

You can write a parse operator. Here's a little example from http://fitsharp.github.io/Fit/WriteOurOwnCellOperator.html that parses the string"pi" into a numeric value.
using fitSharp.Fit.Operators;
using fitSharp.Machine.Engine;
public class ParsePi: CellOperator, ParseOperator<Cell> {
The CanParse method override determines which cells are processed by
our operator. We check for "pi" in the cell and also that we are
working with a numeric type. This preserves normal behavior if we're
using "pi" as a string input or expected value.
public bool CanParse(Type type, TypedValue instance, Tree<Cell> parameters) {
return type == typeof(double) && parameters.Value.Text == "pi";
}
Overriding the Parse method changes the behavior when the cell is
parsed to create an input or an expected value.
public TypedValue Parse(Type type, TypedValue instance, TreeTree<Cell> parameters) {
return new TypedValue(System.Math.PI);
}
}

Related

Hashset to string [duplicate]

I have a class defined as follows:
public class Person {
private String name;
// constructor and getter/setter omitted
}
I tried to print an instance of my class:
System.out.println(myPerson);
but I got the following output: com.foo.Person#2f92e0f4.
A similar thing happened when I tried to print an array of Person objects:
Person[] people = //...
System.out.println(people);
I got the output: [Lcom.foo.Person;#28a418fc
What does this output mean? How do I change this output so it contains the name of my person? And how do I print collections of my objects?
Note: this is intended as a canonical Q&A about this subject.
Background
All Java objects have a toString() method, which is invoked when you try to print the object.
System.out.println(myObject); // invokes myObject.toString()
This method is defined in the Object class (the superclass of all Java objects). The Object.toString() method returns a fairly ugly looking string, composed of the name of the class, an # symbol and the hashcode of the object in hexadecimal. The code for this looks like:
// Code of Object.toString()
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
A result such as com.foo.MyType#2f92e0f4 can therefore be explained as:
com.foo.MyType - the name of the class, i.e. the class is MyType in the package com.foo.
# - joins the string together
2f92e0f4 the hashcode of the object.
The name of array classes look a little different, which is explained well in the Javadocs for Class.getName(). For instance, [Ljava.lang.String means:
[ - an single-dimensional array (as opposed to [[ or [[[ etc.)
L - the array contains a class or interface
java.lang.String - the type of objects in the array
Customizing the Output
To print something different when you call System.out.println(myObject), you must override the toString() method in your own class. Here's a simple example:
public class Person {
private String name;
// constructors and other methods omitted
#Override
public String toString() {
return name;
}
}
Now if we print a Person, we see their name rather than com.foo.Person#12345678.
Bear in mind that toString() is just one way for an object to be converted to a string. Typically this output should fully describe your object in a clear and concise manner. A better toString() for our Person class might be:
#Override
public String toString() {
return getClass().getSimpleName() + "[name=" + name + "]";
}
Which would print, e.g., Person[name=Henry]. That's a really useful piece of data for debugging/testing.
If you want to focus on just one aspect of your object or include a lot of jazzy formatting, you might be better to define a separate method instead, e.g. String toElegantReport() {...}.
Auto-generating the Output
Many IDEs offer support for auto-generating a toString() method, based on the fields in the class. See docs for Eclipse and IntelliJ, for example.
Several popular Java libraries offer this feature as well. Some examples include:
ToStringBuilder from Apache Commons Lang
MoreObjects.ToStringHelper from Google Guava
#ToString annotation from Project Lombok
Printing groups of objects
So you've created a nice toString() for your class. What happens if that class is placed into an array or a collection?
Arrays
If you have an array of objects, you can call Arrays.toString() to produce a simple representation of the contents of the array. For instance, consider this array of Person objects:
Person[] people = { new Person("Fred"), new Person("Mike") };
System.out.println(Arrays.toString(people));
// Prints: [Fred, Mike]
Note: this is a call to a static method called toString() in the Arrays class, which is different to what we've been discussing above.
If you have a multi-dimensional array, you can use Arrays.deepToString() to achieve the same sort of output.
Collections
Most collections will produce a pretty output based on calling .toString() on every element.
List<Person> people = new ArrayList<>();
people.add(new Person("Alice"));
people.add(new Person("Bob"));
System.out.println(people);
// Prints [Alice, Bob]
So you just need to ensure your list elements define a nice toString() as discussed above.
I think apache provides a better util class which provides a function to get the string
ReflectionToStringBuilder.toString(object)
Every class in Java has the toString() method in it by default, which is called if you pass some object of that class to System.out.println(). By default, this call returns the className#hashcode of that object.
{
SomeClass sc = new SomeClass();
// Class # followed by hashcode of object in Hexadecimal
System.out.println(sc);
}
You can override the toString method of a class to get different output. See this example
class A {
String s = "I am just a object";
#Override
public String toString()
{
return s;
}
}
class B {
public static void main(String args[])
{
A obj = new A();
System.out.println(obj);
}
}
In Eclipse,
Go to your class,
Right click->source->Generate toString();
It will override the toString() method and will print the object of that class.
I prefer to use a utility function which uses GSON to de-serialize the Java object into JSON string.
/**
* This class provides basic/common functionalities to be applied on Java Objects.
*/
public final class ObjectUtils {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private ObjectUtils() {
throw new UnsupportedOperationException("Instantiation of this class is not permitted in case you are using reflection.");
}
/**
* This method is responsible for de-serializing the Java Object into Json String.
*
* #param object Object to be de-serialized.
* #return String
*/
public static String deserializeObjectToString(final Object object) {
return GSON.toJson(object);
}
}
In intellij you can auto generate toString method by pressing alt+inset and then selecting toString() here is an out put for a test class:
public class test {
int a;
char b;
String c;
Test2 test2;
#Override
public String toString() {
return "test{" +
"a=" + a +
", b=" + b +
", c='" + c + '\'' +
", test2=" + test2 +
'}';
}
}
As you can see, it generates a String by concatenating, several attributes of the class, for primitives it will print their values and for reference types it will use their class type (in this case to string method of Test2).
By default, every Object in Java has the toString() method which outputs the ObjectType#HashCode.
If you want more meaningfull information then you need to override the toString() method in your class.
public class Person {
private String name;
// constructor and getter/setter omitted
// overridding toString() to print name
public String toString(){
return name;
}
}
Now when you print the person object using System.out.prtinln(personObj); it will print the name of the person instead of the classname and hashcode.
In your second case when you are trying to print the array, it prints [Lcom.foo.Person;#28a418fc the Array type and it's hashcode.
If you want to print the person names, there are many ways.
You could write your own function that iterates each person and prints
void printPersonArray(Person[] persons){
for(Person person: persons){
System.out.println(person);
}
}
You could print it using Arrays.toString(). This seems the simplest to me.
System.out.println(Arrays.toString(persons));
System.out.println(Arrays.deepToString(persons)); // for nested arrays
You could print it the java 8 way (using streams and method reference).
Arrays.stream(persons).forEach(System.out::println);
There might be other ways as well. Hope this helps. :)
If you Directly print any object of Person It will the ClassName#HashCode to the Code.
in your case com.foo.Person#2f92e0f4 is getting printed . Where Person is a class to which object belongs and 2f92e0f4 is hashCode of the Object.
public class Person {
private String name;
public Person(String name){
this.name = name;
}
// getter/setter omitted
#override
public String toString(){
return name;
}
}
Now if you try to Use the object of Person then it will print the name
Class Test
{
public static void main(String... args){
Person obj = new Person("YourName");
System.out.println(obj.toString());
}
}
If you look at the Object class (Parent class of all classes in Java) the toString() method implementation is
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
whenever you print any object in Java then toString() will be call. Now it's up to you if you override toString() then your method will call other Object class method call.
Using Lombok #Data annotation on class will provide getter, setter, toString and hashcode. Using Lombok is better as it handles boilerplate code.
For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:
System.out.println("My object: " +
ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle()));
Output examples:
// RecursiveToStringStyle
Person#7f54[name=Stephen,age=29,smoker=false,job=Job#43cd2[title=Manager]]
// MultilineRecursiveToStringStyle
Person#7f54[
name=Stephen,
age=29,
smoker=false,
job=Job#43cd2[
title=Manager
]
]
I managed to get this done using Jackson in Spring 5. Depending on the object it might not work in all cases.
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(yourObject));
the output would look like
{
"id" : 1,
"fieldOne" : "string"
}
Here are more examples using Jackson
If you use GSON instead It might look like
Gson gson = new Gson();
System.out.println(gson.toJson(yourObject));
If you are using project Lombok you could use the #ToString annotation and generate a standard toString() method without adding boilerplate.
import lombok.ToString;
#ToString
public class LoginDto {
private String user;
private String pass;
}
...
System.out.println(loginDto.toString());
// LoginDto(user=x#xxx.x, pass=xxxxx)

Camel set body to object with header expression

Is it possible to set a response body to an object and pass in a camel header property. I can achieve this in a processor but I'd rather do it in line of the route.
.setBody(constant(
new Foo()
.withOne("HelloWorld")
.withTwo(simple("Header property is ${header.uniqueProperty}").toString())
))
With the above code I get a response of:
<foo>
<one>HelloWorld</one>
<two>Header property is ${header.uniqueProperty}</two>
</foo>
Here is my POJO
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
private String one;
private String two;
public String getOne() {
return one;
}
public void setOne(final String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(final String two) {
this.two = two;
}
public Foo withOne(final String one) {
setOne(one);
return this;
}
public Foo withTwo(final String two) {
setTwo(two);
return this;
}
}
constant() probably won't work for you, since you probably want this dynamically evaluated for every exchange that passes through. Since you need to set the body to a newly instantiated object, you need a mechanism that's capable of this. You mentioned you want to avoid a processor, but I'd like to point out how simple this could be done in the route:
.setBody(exchange -> new Foo()
.withOne("HelloWorld")
.withTwo(simple("Header property is " + exchange.getIn().getHeader("uniqueProperty")))
)
Edit: Actually this is not a processor. We're just passing a lambda (Function) to setBody().
If you're in a Spring environment, you could use spring expression language:
.setBody().spel("#{new Foo()" +
".withOne('HelloWorld')" +
".withTwo(simple('Header property is ' + request.headers.uniqueProperty))}");

How to find a specific field from a genetic List object in wpf

I am trying to create a generic class for finding duplicate values for different class.
I am casting each list to an object and passing it as parameter like this
List<StudentModel1> a = new List<StudentModel1>();
List<StudentModel2> b = new List<StudentModel2>();
List<StudentModel3> c = new List<StudentModel3>();
List<object> obj = a.Cast<object>().ToList();//like this
public bool Duplicate(List<object> obj, string Fieldname, string Name)
{
if (obj.Any(x => x.Fieldname.Contains(Name))) { return true; } else { return false; }
}/// something like this;
here i am passing fieldname propery ,string name and object for finding duplicate and return a bool.How to access field name in linq.
please help how to acheive this.
thanks.
If I understand your question correctly, you have 3 different versions of a class: StudentModel1, StudentModel2 and StudentModel3 and you want to be able to compare lists of them. You are casting them to Object so that you can pass any any version of that class to your Duplicate method.
Assuming the above is correct, what you need is inheritance. If that's not something your familiar with you should definitely read up on it.
Consider the following:
class StudentModelBase
{
public string SomeProperty { get; set; }
}
class StudentModel1 : StudentModelBase
{
}
class StudentModel2 : StudentModelBase
{
}
class StudentModel3 : StudentModelBase
{
}
If your Duplicate method should be able to handle any of the "StudentModel" classes, then that means the information needed to tell if there are duplicates must be common to all three versions of that class. The properties and fields which store that information should be moved into StudentModelBase. Then, instead of casting to Object you should cast to StudentModelBase.
Your cast becomes:
List<StudentModelBase> base = a.Cast<StudentModelBase>().ToList();
And your Duplicate method becomes something like the following:
public bool Duplicate(List<StudentModelBase> obj, string Name)
Notice I left out Fieldname because if this is implemented correctly you shouldn't need to pass that as a parameter. You should just be able to access the need members of StudentModelBase like any normal variable, since they should be the same for all versions of "StudentModel".

Is it possible to pass Java-Enum as argument from cucumber feature file

I am currently using selenium with Java,And want to implement cucumber to make test script more readable.
Currently facing issue while passing argument to java method where Enum is expected as parameter.
I would also like to know if there are any other known limitations of cucumber-java before migrating current framework.
The answer is: Yes
You can use all kind of different types in your scenario: primitive types, own classes (POJOs), enums, ...
Scenario :
Feature: Setup Enum and Print value
In order to manage my Enum
As a System Admin
I want to get the Enum
Scenario: Verify Enum Print
When I supply enum value "GET"
Step definition code :
import cucumber.api.java.en.When;
public class EnumTest {
#When("^I supply enum value \"([^\"]*)\"$")
public void i_supply_enum_value(TestEnum arg1) throws Throwable {
testMyEnum(arg1);
}
public enum TestEnum {
GET,
POST,
PATCH
}
protected void testMyEnum(TestEnum testEnumValue) {
switch (testEnumValue) {
case GET:
System.out.println("Enum Value GET");
break;
case POST:
System.out.println("Enum Value POST");
break;
default:
System.out.println("Enum Value PATCH");
break;
}
}
}
Let me know how you are doing. I could try to help you.
This youtube lecture of about 11 minutes gives a good way of doing it.
https://www.youtube.com/watch?v=_N_ca6lStrU
For example,
// enum, obviously in a separate file,
public enum MessageBarButtonType {
Speak, Clear, Delete, Share
}
// method for parameter type. if you want to use a different method name, you could do #ParameterType(name="newMethodName", value="Speak|Clear|Delete|Share") according to the video.
#ParameterType("Speak|Clear|Delete|Share")
public MessageBarButtonType MessageBarButtonType(String buttonType) {
return MessageBarButtonType.valueOf(buttonType);
}
// use like this. the name inside {} should match the name of method, though I just used the type name.
#Then("Select message bar {MessageBarButtonType} button")
public void select_message_bar_button(MessageBarButtonType buttonType) {
...
}
First register a transformer based on an ObjectMapper, then you can just use enums as would be expected.
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
#DefaultParameterTransformer
#DefaultDataTableEntryTransformer
#DefaultDataTableCellTransformer
public Object defaultTransformer(Object fromValue, Type toValueType) {
JavaType javaType = objectMapper.constructType(toValueType);
return objectMapper.convertValue(fromValue, javaType);
}
Scenario: No.6 Parameter scenario enum
Given the professor level is ASSOCIATE
#Given("the professor level is {}")
public void theProfessorLevelIs(ProfLevels level) {
System.out.println(level);
System.out.println("");
}
public enum ProfLevels {
ASSISTANT, ASSOCIATE, PROFESSOR
}
Source
This is no more supported in latest io.cucumber maven group
https://github.com/cucumber/cucumber-jvm/issues/1393

How do deal with data type classes?

I have read this question about data types. How do I deal with such things in general and also in db? What I know/understand: compared to a regular class, two objects of a data type class should be considered equal if they have the same attribute value(s).
Example:
// Using Java here just as pseudo code. Personally, I don't use Java.
class Car {
public Color color;
}
// But here I'm not sure. Is this better?
class Color {
static String RED = 'red';
static String BLACK = 'black';
public String value; // gets a value from class constants
}
// Or this?
class Color {
static int RED = 1;
static int BLACK = 2;
public int value; // gets a value from class constants
}
And I also don't know if I need two tables (car, color) in db or just one. Should colors be hard-coded and not stored in the db?
Sorry, if I cannot describe my problem clearly. I hope anyone can provide an example. But I guess if I simply would add a Varchar column into the table so that each record gets a string 'red' or 'black' (or other colors), everyone would recommend me to use another table for the colors and make a relation between the tables. That's okay. But then again in code if I want to get the color I would always need to reference the value attribute - e.g. if I want to show the color (string) in the UI I always need to do carObj.color.value, instead of just carObj.color which would be more intuitive, IMO. ... there is something that I don't understand properly.
Can anyone show an example when and how to use data type classes/objects?
Well, what they say is correct, you should represent your types in two tables.
One table for the Car and one for the Color related by an ID.
//rapresent a row of your table Car, an Entity
public class CarDTO {
private int idCar;
private String type;
private int idColor;
public ...// getters and setters here
//For your first question, about if types are equals, you need to implement the method
#Override
public boolean equals(Object obj) {
if (!(obj instanceof CarDTO )) {
return false;
}
CarDTO carToCompare = (CarDTO ) obj;
return (this.getIdCar() == carToCompare.getIdCar()
&& this.getType().equals(carToCompare.getType())
&& this.getIdColor() == carToCompare.getIdColor());
}
//rapresent a row of your table Color, an Entity
public class ColorDTO {
private int idColor;
private String color;
public ...// getters and setters here
//For your first question, about if types are equals, you need to implement the method
#Override
public boolean equals(Object obj) {
if (!(obj instanceof ColorDTO )) {
return false;
}
ColorDTO colorToCompare = (ColorDTO ) obj;
return (this.getIdColor() == colorToCompare.getIdColor()
&& this.getColor().equals(colorToCompare.getColor()));
}
You have two entities that represents the rows of your tables, and idColor of Car is related to idColor of Color.
These of course are types not related in your code for now, but you can create an Entity that is the join of the informations that you need. for example the query:
SELECT c.idCar, c.type, co.colour FROM Car c, Colour co WHERE c.idColor = co.idColor
The Entity that will be the representation of your row, result of the join query is this:
public class CarAndColorDTO {
private int idCar;
private String type;
private String color;
public ...// getters and setters here
//...Equals here
}
You should avoid the hardcode of color in code, because you need these informations in your DB.
Of course this is a simple explanations, if you use Java you should see JPA Entity for better explanation and easy use (Join, Foreign Keys, ecc...)
Also watch out encapsulation of your variables in the code:)
I hope this can help you
Why don't you use an enumerated type for Color?
Please see https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Resources