I just found a TestNG test case that uses Spring to provide its data source. As a result the code is quite clean and concise.
However, I need to expand the test cases so they can take a variable list of inputs.
Am I stuck using bean references for the list of lists as I've attempted below? Is there a way to do that and still be pretty (i.e. not breaking up the logical flow of input followed by output)? Is there a better way?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="stringPatternRegexMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="some input #1" value="expected output #1"/>
<entry key="some input #2" value="expected output #2"/>
<entry key="some input #3" value="expected output #3"/>
<entry key-ref="multi-list-1" value="expected output #3"/>
<entry key-ref="null-reference" value="null-reference"/>
</map>
</constructor-arg>
</bean>
<bean id="multi-list-1">
<list>
<value>apple</value>
<value>banana</value>
<value>orange</value>
</list>
</bean>
<bean id="null-reference">
<value>
<null/>
</value>
</bean>
</beans>
Note that the original code appears to be using a map instead of a list because it seems an easier way to provide a list of String[2].
No, you can use a #DataProvider to feed a test methods with a variable number of parameters:
#DataProvider
public Object[][] dp() {
return new Object[][] {
new Object[] { new Object[] { "a" } },
new Object[] { new Object[] { "b", "c" } },
};
}
#Test(dataProvider = "dp")
public void g1(Object... params) {
System.out.println("Received " + params.length + " parameters");
}
will print:
Received 1 parameters
Received 2 parameters
Note that your test method can declare either "Object..." or "Object[]" (it's the same to the compiler).
I would use TestNG and its DataSource construct as the right way to do this. You certainly can make this Spring configuration, but since it's test code I think TestNG is the more natural home for it.
Related
I am migrating an old NETCF 2.0 Application which uses webservices to NETCF 3.5. The webservice of the foreign server remains the same without changes. Also I newly generated Reference.cs using VS 2008 Command Prompt - and was excited to see the same result as using VS 2005.
My question relates to namespace definition at the root tag by a class which is defined by webservice. The java webservice serializes it with xmlns attribute and XmlSerializer.Deserialize() of NETCF 3.5 blames about this attribute throwing an InvalidOperationException. Using this XML and XmlSerializer.Deserialize() with NETCF 2.0 works as expected. The Object gets deserialized to memory.
Let a few code snippets make things clear.
Exception
InvalidOperationException-There is an error in XML document (2, 2).
InnerException: InvalidOperationException-<InstallDirective xmlns='java:my.foreign.namespace'> was not expected.
at System.Xml.Serialization.XmlSerializer.resolveDeserializingType(XmlReader reader, XmlSerializationReader serialReader, Boolean soap12)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at My.Neat.Software.Bug.Test()
generated Reference.cs (excerpt)
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="java:my.foreign.namespace")]
public partial class InstallDirective {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<System.DateTime> Date;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public JustAnotherThing JustAnotherThing;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<short> Oid;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string Filename;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string Version;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string Dir;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string Instructions;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Scripts", IsNullable=true)]
public Script[] Scripts;
}
the XML from webservice
<?xml version="1.0" encoding="utf-8"?>
<InstallDirective xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="java:my.foreign.namespace">
<Date>2014-02-11T13:31:49.238+01:00</Date>
<JustAnotherThing>
<FileName>MyFunnyFile.txt</CabFileName>
<Checksum>e759af8bd5787e3f8d62245a7d6aa73d</Checksum>
<FileExists xsi:nil="true" />
<Name>MyFunnyFile</Name>
<Version>1.2</Version>
<Path xsi:nil="true" />
<DependsOn xsi:nil="true" />
<RegistryPath xsi:nil="true" />
</JustAnotherThing>
<Oid>1</Oid>
<Filename>MyFunnyFile.txt</Filename>
<Version>1.2</Version>
<Dir>\My\Path\To\Files</Dir>
<Instructions xsi:nil="true" />
<Scripts xsi:nil="true" />
</InstallDirective>
the code snippet which throws the exception
FileInfo directiveFile = new FileInfo(#"\tmp\install\funnyInstallDirective.xml");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InstallDirective));
TextReader reader = new StreamReader(directiveFile.OpenRead());
InstallDirective installDirective = (InstallDirective)xmlSerializer.Deserialize(reader); // BAM!
Thanks for your help!
Indeed the XmlSerializer implementation of .Net Compact Framework 2.0 and 3.5 differs. The solution to this "new" behaviour is easy as well. Because the XmlSerializer which - in my case - serializes XML as a "fragment", we need to declare the XmlRootAttribute. This is due to missing XmlRootAttribute annotation of the type to be serialized within the Reference.cs.
To be backward compatible with XML serialized by NETCF 2.0 implementation, we need to add the namespace by definition of InstallDirective (Reference.cs). Unfortunately I found no programmatically way to get this.
Before:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InstallDirective));
FileStream file = File.Create(#"\tmp\install\funnyInstallDirective.xml");
TextWriter writer = new StreamWriter(file);
xmlSerializer.Serialize(writer, installDirective);
After:
XmlRootAttribute att = new XmlRootAttribute(typeof(InstallDirective).Name);
att.Namespace = "java:my.foreign.namespace";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InstallDirective), att);
FileStream file = File.Create(#"\tmp\install\funnyInstallDirective.xml");
TextWriter writer = new StreamWriter(file);
xmlSerializer.Serialize(writer, installDirective);
Credits to ta.speot.is who answered the question here.
[UPDATE]
Of course you can beautify it by using the annotated XmlTypeAttribute of the web service type. It will look this way.
XmlTypeAttribute ta = (XmlTypeAttribute)Attribute.GetCustomAttribute(typeof(InstallDirective), typeof(XmlTypeAttribute));
XmlRootAttribute att = new XmlRootAttribute(typeof(InstallDirective).Name);
att.Namespace = ta.Namespace;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InstallDirective), att);
In my Java-Mavan-Spring project I'm using cassandra unit to try and test my DAO.
I created 2 files in my classpath:
A simple XML that describes my initial data
A cassandra configuration file (cassandra.yaml)
Here is my test class:
public class UserProfilingCassandraDaoUTest extends BaseJunitTestCase {
#Rule
public CassandraUnit cassandraUnit = new CassandraUnit(new ClassPathXmlDataSet("cassandraTestValues.xml"), "cassandra.yaml", "127.0.0.1");
private HectorCassandraConnection connection;
#Before
public void init() throws Exception {
connection = Mockito.mock(HectorCassandraConnection.class);
Mockito.when(connection.getKeyspace()).thenReturn(cassandraUnit.keyspace);
}
#Test
public void shouldHaveLoadTestDataSet() throws Exception {
Assert.assertNotNull(cassandraUnit.keyspace);
Assert.assertEquals(cassandraUnit.keyspace.getKeyspaceName(), "rtb");
}
#Test
public void getUserStatsTest() {
// Some Test
}
}
This is my cassandraTestValues.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<keyspace xmlns="http://xml.dataset.cassandraunit.org">
<name>rtb</name>
<columnFamilies>
<columnFamily>
<name>my_column_family</name>
<keyType>UTF8Type</keyType>
<comparatorType>UTF8Type</comparatorType>
<defaultColumnValueType>UTF8Type</defaultColumnValueType>
<row>
<key>12345__678_910</key>
<column>
<name>Col1</name>
<value>6</value>
</column>
<column>
<name>Col2</name>
<value>6</value>
</column>
<column>
<name>Col3</name>
<value>3</value>
</column>
</row>
</columnFamily>
</columnFamilies>
</keyspace>
As I run my test I'm getting this log with this error.
I have tried for hours many different methods to overcome the issue with no success.
Any ideas?
This is a bit sad, but changing cassandra-unit version from 1.2.0.1 to 1.0.3.1 and it worked like a charm.
I got to the solution by importing this project to my workspace. The imported project worked just fine and after compering both found that the difference between the versions is what causing the issue.
In addition, non of the later maven versions worked, meaning that all the version that came out after 1.0.3.1 failed (1.2.0.1, 1.1.2.1, 1.1.1.3, 1.1.1.2, 1.1.1.1, 1.1.0.1).
I hope this could save some time to someone in the future.. it sure took me a while.
I have to expose an ejb service layer via jax-ws .
I have generated the web service using jax-ws and wsimport but I'm stopped by a strange things ; Date are being mapped to XmlGregorianCalendar .
Is it possible to use classic java Date instead ?
Can you show me the right way to proceed ?
Thanks .
Edit:
this the binding file i used :
thanks , I modified slightly your xml and attached it with netbeans to the client's webservice and it worked . This the binding I used :
<jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" wsdlLocation="../wsdl/localhost_8080/web_test/Testor.wsdl" >
<jaxb:globalBindings>
<jaxb:javaType name="java.util.Date"
xmlType="xsd:dateTime"
parseMethod="lol.XsdDateTimeConverter.unmarshal"
printMethod="lol.XsdDateTimeConverter.marshalDateTime"
/><jaxb:javaType
name="java.util.Date"
xmlType="xsd:date"
parseMethod="lol.XsdDateTimeConverter.unmarshal"
printMethod="lol.XsdDateTimeConverter.marshalDate"
/>
</jaxb:globalBindings>
</jaxws:bindings>
Not tested, but should work. First create such class:
import javax.xml.bind.DatatypeConverter;
public class XsdDateTimeConverter {
public static Date unmarshal(String dateTime) {
return DatatypeConverter.parseDate(dateTime).getTime();
}
public static String marshalDate(Date date) {
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return DatatypeConverter.printDate(calendar);
}
public static String marshalDateTime(Date dateTime) {
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(dateTime);
return DatatypeConverter.printDateTime(calendar);
}
}
Then add this to custom xjb file:
<javaType
name="java.util.Date"
xmlType="xs:dateTime"
parseMethod="XsdDateTimeConverter.unmarshal"
printMethod="XsdDateTimeConverter.marshalDateTime"
/>
<javaType
name="java.util.Date"
xmlType="xs:date"
parseMethod="XsdDateTimeConverter.unmarshal"
printMethod="XsdDateTimeConverter.marshalDate"
/>
</globalBindings>
Not tested, but should work. Based on my answer here: JAX-WS and Joda-Time?
Thanks Tomasz. The above solution works.
But wsimport also adds its set of Adapters like Adapter1.java and Adapter2.java with its package org.w3._2001.xmlschema, which really doesnot match my own package structure.
I found a way to change this package name using another jaxb binding. Actually, I searched for this a lot and could not find this easily, so I am adding it here for anyone looking for the same.
Add the following binding in the wsimport using '-b binding.xml'. Note that wsimport can work with multiple binding files.
binding.xml content below:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<annotation><appinfo>
<jaxb:schemaBindings>
<jaxb:package name="com.abc.xyz.utils"/>
</jaxb:schemaBindings>
</appinfo></annotation>
</schema>
I have a webservice in C#.NET with the following namespace:
[WebService (Namespace = "http://enterpriseName/wsName")]
The web service contains a WebMethod GetServiceObject and a class MyObject.
This web method returns a string whose content is a serialized instance of MyObject.
[WebMethod (MessageName = "GetServiceObjectXML" Description = "Get ServiceObject from Server to Client")]
public string GetServiceObjectXML ()
This method returns the following XML:
<? Xml version = "1.0" encoding = "utf-16"?>
<ServiceObject Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services>
<service>
<id>3</id>
<date>02/08/2010</date>
</service>
</Services>
</ServiceObject>
The problem that I encounter is that when I call this method from the client side and try to deserialize this xml to class MyObject and I get a NULL object.
After that I created a new WebMethod with the following signature:
[WebMethod (MessageName = "GetServiceObject" Description = "Get ServiceObject from Server to Client")]
public MyObject GetServiceObject ()
When I call this method from the client side I get the object filled correctly and I can also serialize the object without problems, but the result of serialization is the following xml:
<? Xml version = "1.0" encoding = "utf-16"?>
<ServiceObject Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services Xmlns="http://enterpriseName/wsName">
<service>
<id>3</id>
<date>02/08/2010</date>
</service>
</Services>
</ServiceObject>
which is different from the xml generated by the WebMethod GetServiceObjectXML.
How can I get around this, since I intend to use both methods on the same webservice and in the same customer?
The obvious answer would be, fix GetServiceObjectXML() to return the same XML as GetServiceObject(). The difference seems to be that the object as serialized by the framework has a different XML namespace specified. Whatever method you're using to serialize the object into XML in GetServiceObjectXML() isn't doing that.
I would like to create a service that accepts a complex nested type. In a sample asmx file I created:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ServiceNest : System.Web.Services.WebService
{
public class Block
{
[XmlElement(IsNullable = false)]
public int number;
}
public class Cell
{
[XmlElement(IsNullable = false)]
public Block block;
}
public class Head
{
[XmlElement(IsNullable = false)]
public Cell cell;
}
public class Nest
{
public Head head;
}
[WebMethod]
public void TakeNest(Nest nest)
{
}
}
When I view the asmx file in IE the test page shows the example SOAP post request as:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TakeNest xmlns="http://schemas.intellicorp.com/livecompare/">
<nest>
<head>
<cell>
<block xsi:nil="true" />
</cell>
</head>
</nest>
</TakeNest>
</soap:Body>
</soap:Envelope>
It hasn't expanded the <block> into its number member.
Looking at the WSDL, the types all look good. So is this just a limitation of the post demo page creator?
Thanks.
But those elements ARE null. You need to construct them before they show up otherwise they are just null.
As Kevin pointed out the example POST XML indicates that those elements are nil. I should have simply tried to consume the web service. Once I did that I could see that the importer (either .NET, Java or Ruby) correctly created all the types. So really there is no question here after all.
The .NET code did not give up after a certain number of levels.
If you look at the code generated by "Add Web Reference", you'll find that there's a bool numberSpecified field. Only if the client sets that to true will the number be serialized.
If you look at the XML Schema, you'll see that the number element might be absent. If it were of a reference type, then that could be represented in the client by a null value. Since it's an int, this additional flag is necessary to indicate whether or not to serialize this optional value.