Force Cucumber to not call the wrong constructor for DataTable - cucumber-jvm

What is the easiest way to do this without changing the Data class?
Feature: My feature
Scenario: My Scenario
Given some data table:
| a | b | c |
| | true | true |
public class MySteps {
#Given("^some data table:$")
public void some_data_table (
final List<ThirdPartyEntity> rows) throws Throwable {
for (ThirdPartyEntity row : rows) {
// I get 6 rows here :(
}
}
}
public class ThirdPartyEntity extends ... {
private String a;
private boolean b;
private boolean c;
public SpecificBenefitRuleRequest() {
this("x");
}
public SpecificBenefitRuleRequest(String x) {
super(x); // cucumber calls this, but I don't want it to :(
}
// getters and setters for a,b,c
}

The best way I found was to hide the constructors that I don't want cucumber to call by overriding the entity in my test class:
public class MySteps {
#Given("^some data table:$")
public void some_data_table (
final List<ThirdPartyEntityCucumberHatesMe> rows) throws Throwable {
for (ThirdPartyEntityCucumberHatesMe row : rows) {
// I get 1 rows here \o/
}
}
}
public static class ThirdPartyEntityCucumberHatesMe extends ThirdPartyEntity {
// this was the easiest way to get cucumber to not call the wrong constructor :'(
}
}

Related

How to find all dependencies between two classes using VS Enterprise Code Map?

If I pull a class A and class B onto a Code Map, VSE (Visual Studio Enterprise) will map the direct calls of class A calling methods in class B.
So,
public class A
{
public void DoSomething()
{
b.DoSomethingElse();
}
}
This will map. But if it's something like:
public class A
{
public void DoSomething()
{
d.DoManyThings();
}
}
public class D
{
public void DoManyThings()
{
c.DoThings();
}
}
public class C
{
public void DoThings()
{
b.DoSomethingElse();
}
}
public class B
{
public void DoSomethingElse()
{
// imagine code here
}
}
Then the Code Map won't map between class A and class B automatically. The only way I've found to show those dependencies is to go to each method and click "Show Methods This Calls".
Is there a way to get VSE make the Code Map all those dependencies initially without having to investigate every method?

eclipselink/jpa inheritance problems

I am using spring boot (2.0.0) with eclipse link to persist data (over 500 entity classes) to a postgres db (6.5). Thats works very well. For receiving the data over REST I build an other spring boot application. Here I have some inheriance problem with JPA (sorry for my drawing):
Class C and class D (abstract) inherit from class B. Class A have a reference (attribute1) to class B. This attribute is an instance of entity class E, which inherit from abstract class D. I am using inheritance strategy table per class. Every class using the annotation Entity with the table name. In the database, table from class A have a correct foreign key to table from class E, but if I want to read the data the attribute1 is null. I see from the log level that eclipse link only look inside table from class C. How can I resolve this problem?
Greets Benjamin
here are the classes, class E:
#Entity(name="ep_core_voltagelevel")
public class VoltageLevel extends EquipmentContainer {
#Embedded
#AttributeOverrides(#AttributeOverride(name="value", column=#Column(name="highVoltageLimit_value")
)
)
private myPackage.DomainProfile.Voltage highVoltageLimit;
public myPackage.DomainProfile.Voltage getHighVoltageLimit() {
return highVoltageLimit;
}
public void setHighVoltageLimit(myPackage.DomainProfile.Voltage parameter) {
this.highVoltageLimit = parameter;
}
#Embedded
#AttributeOverrides(#AttributeOverride(name="value", column=#Column(name="lowVoltageLimit_value")
)
)
private myPackage.DomainProfile.Voltage lowVoltageLimit;
public myPackage.DomainProfile.Voltage getLowVoltageLimit() {
return lowVoltageLimit;
}
public void setLowVoltageLimit(myPackage.DomainProfile.Voltage parameter) {
this.lowVoltageLimit = parameter;
}
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="basevoltage_id")
private BaseVoltage baseVoltage;
public BaseVoltage getBaseVoltage() {
return baseVoltage;
}
public void setBaseVoltage(BaseVoltage parameter) {
this.baseVoltage = parameter;
}
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="substation_id")
private Substation substation;
public Substation getSubstation() {
return substation;
}
public void setSubstation(Substation parameter) {
this.substation = parameter;
}
}
Class D:
#Entity(name = "ep_core_equipmentcontainer")
public abstract class EquipmentContainer extends ConnectivityNodeContainer {
}
Class B:
#Entity(name="ep_core_connectivitynodecontainer")
public abstract class ConnectivityNodeContainer extends PowerSystemResource {
}
Class A:
public class ConnectivityNode extends IdentifiedObject {
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="connectivitynodecontainer_id")
private ConnectivityNodeContainer connectivityNodeContainer;
public ConnectivityNodeContainer getConnectivityNodeContainer() {
return connectivityNodeContainer;
}
public void setConnectivityNodeContainer(ConnectivityNodeContainer parameter) {
this.connectivityNodeContainer = parameter;
}
}

Intellij reports code duplication while actually it's not

Here's the code. The code in method test and test2 are different because the parameter passed to Test constructor are different. Actually, if I change any parameter to null, intellij stops reporting the duplication. Is there any way to fix this?
---- Updated --------
I pass 2 functions doing totally different things but intellij still reports duplication
public class TestMain {
public void test(int a)
{
System.out.println("haha");
System.out.println("hahaa");
TestMain testMain = new TestMain();
new Test(testMain::test3);
System.out.println("hahaaa");
}
public void test2(int a)
{
System.out.println("haha");
System.out.println("hahaa");
TestMain testMain = new TestMain();
new Test(testMain::still_dup);
System.out.println("hahaaa");
}
public void test3(int a) {
System.out.println("abc");
}
public void still_dup(int a) {
String b = "edf";
b.toLowerCase();
}
public class Test {
Test(handler h) {
}
}
public interface handler<M> {
void entitySelector(int a);
}
public static void main(String[] args) {
TestMain test = new TestMain();
test.test(1);
System.out.println("-------");
test.test2(2);
}
}
I think the best way to fix this is to replace test and test2 by a single method. You don't have to distinguish what to pass the constructor because it's the current method. This might be the reason why code duplication is reported. The methods can be replaced by a single one without problems.

OO design problem

Suppose there's 2 classes : A and B.
A can operate on B.
I need to be able to query all B instances that A has operated on.
And for a specific B instance, I need to be able to query all A instances that have operated on it.
What's the elegant(in the OO taste..) solution for this kind of problem?
In a language like Java I would do something like:
package com.whatever.blah;
public class A {
private Set<B> patients = new HashSet<B>;
public void operateOn(B patient) {
patient.startRecoveringFromOperation(this);
patients.add(patient);
}
public List<B> getPatients() {
return patients;
}
}
public class B {
private Set<A> surgeons = new HashSet<A>;
//this has package access to `A` can access it but other classes can't
void startRecoveringFromOperation(A theSurgeon) {
surgeons.add(theSurgeon);
}
public List<A> getSurgeons() {
return surgeons;
}
}
This really isn't doing anything special, beyond using package access to allow A access to B's startRecoveringFromOperation() method while hiding the method from most other classes. In other languages you might use a different approach to accomplish this. For instance in C++ you might declare A as a friend of B instead.
import java.util.*;
class A {
void operate(B b) {
operatedOn.add(b);
b.operatedOnBy.add(this);
}
final Set<B> operatedOn = new HashSet<B>();
}
class B {
final Set<A> operatedOnBy = new HashSet<A>();
}
public class Main {
public static void main(String[] args) {
A a=new A();
B b=new B();
a.operate(b);
System.out.println(a+" "+a.operatedOn);
System.out.println(b+" "+b.operatedOnBy);
}
}

How to design around lack of multiple inheritance?

Using interfaces won't work because I want a single implementation. Using this solution would end in a lot of redundant code because I plan on having quite a few sub classes (composition vs inheritance). I've decided that a problem-specific design solution is what I'm looking for, and I can't think of anything elegant.
Basically I want classes to have separate properties, and for those properties to be attached at design time to any sub class I choose. Say, I have class 'ninja'. I would like to be able to make arbitrary sub classes such as 'grayNinja' where a gray ninja will always have a sword and throwing stars. Then possibly 'redNinja' who will always have a sword and a cape. Obviously swords, stars, and capes will each have their own implementation - and this is where I can't use interfaces. The closest solution I could find was the decorator pattern, but I don't want that functionality at runtime. Is the best solution an offshoot of that? Where inside the Black Ninja class constructor, I pass it through the constructors of sword and throwingStar? (those being abstract classes)
haven't coded in a while and reading hasn't gotten me too far - forgive me if the answer is simple.
Edit: Answered my own question. I can't mark it as 'answer' until tomorrow. Please let me know if there's a problem with it that I didn't catch. All the reading this problem forced me to do has been awesome. Learned quite a bit.
You want classes to have separate properties. Have you considered coding exactly that?
For example, you want a RedNinja that is-a Ninja that has-a sword and cape. Okay, so define Ninja to have an inventory, make it accessible through Ninja, and pass in an inventory through RedNinja's constructor. You can do the same thing for behaviors.
I've done once a similar app. with a earlier "C++" compiler that supported only single inheritance and no interfaces, at all.
// base class for all ninjas
public class Ninja {
// default constructor
public Ninja() { ... }
// default destructor
public ~Ninja() { ... }
} // class
public class StarNinja: public Ninja {
// default constructor
public StarNinja() { ... }
// default destructor
public ~StarNinja() { ... }
public void throwStars() { ... }
} // class
public class KatannaNinja: public Ninja {
// default constructor
public KatannaNinja() { ... }
// default destructor
public ~KatannaNinja() { ... }
public void useKatanna() { ... }
} // class
public class InvisibleNinja: public Ninja {
// default constructor
public InvisibleNinja() { ... }
// default destructor
public ~InvisibleNinja() { ... }
public void becomeVisible() { ... }
public void becomeInvisible() { ... }
} // class
public class FlyNinja: public Ninja {
// default constructor
public FlyNinja() { ... }
// default destructor
public ~FlyNinja() { ... }
public void fly() { ... }
public void land() { ... }
} // class
public class InvincibleNinja: public Ninja {
// default constructor
public InvincibleNinja() { ... }
// default destructor
public ~InvincibleNinja() { ... }
public void turnToStone() { ... }
public void turnToHuman() { ... }
} // class
// --> this doesn't need to have the same superclass,
// --> but, it helps
public class SuperNinja: public Ninja {
StarNinja* LeftArm;
InvincibleNinja* RightArm;
FlyNinja* LeftLeg;
KatannaNinja* RightLeg;
InvisibleNinja* Body;
// default constructor
public SuperNinja() {
// -> there is no rule to call composed classes,
LeftArm = new StarNinja();
RightArm = new InvincibleNinja();
LeftLeg = new FlyNinja();
RightLeg = new KatannaNinja();
Body = new InvisibleNinja();
}
// default destructor
public ~SuperNinja() {
// -> there is no rule to call composed classes
delete LeftArm();
delete RightArm();
delete LeftLeg();
delete RightLeg();
delete Body();
}
// --> add all public methods from peers,
// --> to main class
public void throwStars() { LeftArm->throwStars(); }
public void useKatanna() { RightLeg->useKatanna(); }
public void becomeVisible() { Body->becomeVisible() }
public void becomeInvisible() { Body->becomeInvisible() }
public void fly() { LeftLeg->fly() }
public void land() { LeftLeg->land() }
public void turnToStone() { RightArm->turnToStone(); }
public void turnToHuman() { RightArm->turnToHuman(); }
} // class
Im afraid, that the most close example is the composition design pattern. In order, to become more similar to inheritance, I make a generic base class that all composite classes share, and I make a main class that will be the result of the multiple inheritance, that has a copy of all the public methods of the component classes.
If you want to use interfaces, to enforce that main class have all important methods,
then make an interface that matches each composing class, and implemented in the main class.
public interface INinja {
public void NinjaScream() { ... }
} // class
public interface IStarNinja {
void throwStars();
} // class
public interface IKatannaNinja {
void useKatanna();
} // class
public interface IInvisibleNinja {
void becomeVisible();
void becomeInvisible();
} // class
public interface CFlyNinja {
void fly();
void land();
} // class
public interface IInvincibleNinja {
void turnToStone() { ... }
void turnToHuman() { ... }
} // class
// base class for all ninjas
public class CNinja: public INinja {
// default constructor
public CNinja() { ... }
// default destructor
public ~CNinja() { ... }
public void NinjaScream() { ... }
} // class
public class CStarNinja: public CNinja, INinja {
// default constructor
public CStarNinja() { ... }
// default destructor
public ~CStarNinja() { ... }
public void NinjaScream() { ... }
public void throwStars() { ... }
} // class
public class CKatannaNinja: public CNinja, IKatannaNinja {
// default constructor
public CKatannaNinja() { ... }
// default destructor
public ~CKatannaNinja() { ... }
public void NinjaScream() { ... }
public void useKatanna() { ... }
} // class
public class CInvisibleNinja: public CNinja, IInvisibleNinja {
// default constructor
public CInvisibleNinja() { ... }
// default destructor
public ~CInvisibleNinja() { ... }
public void becomeVisible() { ... }
public void becomeInvisible() { ... }
} // class
public class CFlyNinja: public CNinja, IFlyNinja {
// default constructor
public CFlyNinja() { ... }
// default destructor
public ~CFlyNinja() { ... }
public void fly() { ... }
public void land() { ... }
} // class
public class CInvincibleNinja: public CNinja, IInvincibleNinja {
// default constructor
public CInvincibleNinja() { ... }
// default destructor
public ~CInvincibleNinja() { ... }
public void turnToStone() { ... }
public void turnToHuman() { ... }
} // class
// --> this doesn't need to have the same superclass,
// --> but, it helps
public class CSuperNinja: public CNinja,
IKatannaNinja,
IInvisibleNinja,
IFlyNinja,
IInvincibleNinja
{
CStarNinja* LeftArm;
CInvincibleNinja* RightArm;
CFlyNinja* LeftLeg;
CKatannaNinja* RightLeg;
CInvisibleNinja* Body;
// default constructor
public CSuperNinja() {
// -> there is no rule to call composed classes
LeftArm = new CStarNinja();
RightArm = new CInvincibleNinja();
LeftLeg = new CFlyNinja();
RightLeg = new CKatannaNinja();
Body = new CInvisibleNinja();
}
// default destructor
public ~SuperNinja() {
// -> there is no rule to call composed classes
delete LeftArm();
delete RightArm();
delete LeftLeg();
delete RightLeg();
delete Body();
}
// --> add all public methods from peers,
// --> to main class
public void throwStars() { LeftArm->throwStars(); }
public void useKatanna() { RightLeg->useKatanna(); }
public void becomeVisible() { Body->becomeVisible() }
public void becomeInvisible() { Body->becomeInvisible() }
public void fly() { LeftLeg->fly() }
public void land() { LeftLeg->land() }
public void turnToStone() { RightArm->turnToStone(); }
public void turnToHuman() { RightArm->turnToHuman(); }
} // class
I know this solution is complex, but, seems that there is not another way.
Cheers.
Alright so mix-ins through extension methods are going to be my preferred route. I couldn't figure out how to use dynamic proxies in vb.net (seemed to require libraries with lots of documentation that didn't cover specifically what I needed). Dynamic proxies also seems to be a bit dirtier of a solution than using extension methods. Composition would have been what I defaulted to if the previous two didn't work.
So one problem with extension methods, is that the code gets a little dirtier if you want to hold variables. Not much though. Another problem is that all the extension methods must be defined in modules, so the code might look a little goofy to a new eye. I will solve this by defining my interface and module with the corresponding extension method in the same file.
finally, here's some sample vb.net code if you don't want to see a full fledged example through the link.
Imports System.Runtime.CompilerServices 'for extension methods
Public Interface ISword
End Interface
Public Interface IThrowingStar
End Interface
Module ExtensionMethods
<Extension()>
Public Sub swingSword(ByVal hasASword As ISword)
Console.WriteLine("Sword has been swung")
End Sub
<Extension()>
Public Sub throwStar(ByVal hasAStar As IThrowingStar)
Console.WriteLine("Star has been thrown")
End Sub
End Module
Public Class RedNinja
Inherits Ninja
Implements IThrowingStar, ISword
Public Sub New()
End Sub
End Class
Public MustInherit Class Ninja
private curHealth as Integer
Public Sub New()
curHealth = 100
End Sub
Public Function getHP() As Integer
Return curHealth
End Function
End Class
Module Module1
Sub main()
Console.WriteLine("Type any character to continue.")
Console.ReadKey()
Dim a As New RedNinja
a.swingSword() 'prints "Sword has been swung"
a.throwStar() 'prints "Star has been thrown"
Console.WriteLine("End of program - Type any key to exit")
Console.ReadKey()
End Sub
End Module
Dirty solution, if you simply must have multiple inheritance, is using something like dynamic proxies in Java.
But I guess you're probably programming in C#, and this is language agnostic question, so here goes language agnostic answer: check out composite and factory design patterns, that should give you some ideas.
Also, it might not be needed to pass everything in constructor. Check out IoC pattern as well.