java - Encoding of DBUnit Blob Column on JPA Entity -


i creating unit tests existing functionality.

i using dbunit embedded h2 database test jpa entities. our production environment uses sql server.

the problem having need perform operations on blob column on 1 of entities seems if copy contents of blob data 1 of sql server rows dbunit xml dataset, when instantiate string bytes not represent text expect.

snippet of entity:

@entity @table(name = "mn_gateway_template") public class gatewaytemplate implements serializable {  @lob @column(name = "config_file_bytes", length = 500000) private byte[] configfilebytes;  } 

to save bytes this:

gatewaytemplate template = entitymanager.find(gatewaytemplate.class, 1l); byte[] bytes = postedstringcontent.getbytes(); template.setconfigfilebytes(bytes); entitymanager.persist(template); 

my dataset:

 <?xml version="1.0" encoding="utf-8"?>     <dataset>       <mn_gateway_template id="4" disabled="0" description="a config file" config_file_bytes="00101111  01101001  01101110  01110100" />     </dataset> 

my spring-test-mvc test:

@test @withmockuser public void testsaveeditedtemplate() throws exception {      account account = new account();     account.setid(1l);       mvc.perform(                 post("/admin/gateway/config/template/save")                         .sessionattr("account", account)                         .param("configtemplatefilename", "testconfig.txt")                         .param("configfiletext", "/log :info \"this config file \"")                         .param("configurationowneraccount","1")                         .param("model", "1")                         .param("termsaccepted", "true")                         .param("mastertemplateid", "1")                      );      entitymanager.gettransaction().commit();     entitymanager.gettransaction().begin();      gatewaytemplate editedtemplate = entitymanager.find(gatewaytemplate.class, 1l );     assert.assertequals("/log :info \"this config file \"", editedtemplate.getconfigfiletext());   } 

the test simulating post of string. call string.getbytes() method blob data , save it. in real application when retrieve blob data , instantiate string it, string represents posted on ui, when bytes provided in dataset dbunit assertion fails.see below.

org.junit.comparisonfailure: expected:<[/log :info "this config file "]> was:<[Ѯ7�n9�^x㝴�5�n㝴�n�﮹�Μ��_�~�뾟�n�����������}�ѭ�ѭ��m��޸�n��޻�ޞ��獴��y���nt�n=��5�n}�n�}���m��n��^�띴﮵�n��ε�m��m�ߝ��n��x�n������ѭ�~v�5�9�nx�^6��9�m�뎶�������랜�Ο����޵������m���m��n�덴�n�뾽����nt�n=��5�n}�n�}�n��x�n�]> @ org.junit.assert.assertequals(assert.java:115) @ org.junit.assert.assertequals(assert.java:144) @ za.co.wifire.admin.api.controller.gateway.gatewaytemplatecontrollertest.testsaveeditedtemplate(gatewaytemplatecontrollertest.java:133)

i assume due encoding difference...

turns out dbunit supports binary data in form of base64 encoded string.

think problem more in design else. blob column should simple string , blob accommodate file uploads , cant changed contains data need.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -