require 'test/unit'
require 'ontomde-core'
require 'logger'
SRCDIR=File.dirname(__FILE__)
TargetFileType=FileTypeJava.instance
@@log=Logger.new("log")
@@log.level=Logger::Severity::DEBUG
Test_string_protected1="file tmp1.java\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected2="file tmp2.java\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected3="file tmp3.java\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected40="file tmp4.java\n zone aaaaa zone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected41="file tmp4.java\n zone bbbbb zone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected5A="file tmp5A\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected5B="file tmp5B\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected5C="file tmp5C\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
Test_string_protected5D="file tmp5D\nzone protege a reprendre a la main (ligne 1)\nzone protege a reprendre a la main (ligne 2)"
module Mrdf_Resource
def test1
mtk_writeSession("build/tmp1.java"){#,REVERSE) {
write("Hello!\n")
write("Hello!\n")
write("Hello!\n")
write("Hello!\n")
mtk_protected(true) {
write Test_string_protected1
}
write("Bye!\n")
write("Bye!\n")
write("Bye!\n")
write("Bye!\n")
}
end
def test2
mtk_writeSession("build/tmp2.java"){#,REVERSE) {
write("Hello!")
mtk_protected(true) {
write Test_string_protected2
}
write("Bye!")
}
end
def test3
mtk_writeSession("build/tmp3.java"){#,REVERSE) {
write("Hello!")
mtk_protected(false) {
write Test_string_protected3
}
}
end
def test4
mtk_writeSession("build/tmp4.java"){#,REVERSE) {
write("Hello!")
mtk_protected(true,'zone40') {
write Test_string_protected40
}
mtk_protected(true,'zone41') {
write Test_string_protected41
}
}
end
def test5a
mtk_writeSession("build/tmp5.java"){}#,REVERSE) {}
File.delete("build/tmp5.java")
mtk_writeSession("build/tmp5.java"){#,REVERSE) {
write("Hello! : phase 1")
mtk_protected(false,"A") {
write "KO KO KO * phase 2- zone A"
}
mtk_protected(true,"B") {
write Test_string_protected5B
}
mtk_protected(false,"C") {
write "KO KO KO * phase 2- zone C"
}
mtk_protected(true,"D") {
write Test_string_protected5D
}
}
end
def test5b
mtk_writeSession("build/tmp5.java"){#,REVERSE) {
write("Hello! : phase 2")
mtk_protected(false,"A") {
write Test_string_protected5A
}
mtk_protected(true,"B") {
#on remplace
#normalement non appel�
write "KO KO KO * phase 2- zone B"
}
mtk_protected(true,"C") {
write Test_string_protected5C
}
mtk_protected(false,"D") {
#on ne remplace pas.
#normalement non appel�
write "phase 2 - zoneD"
write "KO KO KO * phase 2- zone D"
}
}
end
end
class TC_protected_tc1 < Test::Unit::TestCase
def setup
@@model=Crdf_Repository.new
end
def test_write_protected
@@model.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
@@model['rdf_Resource'].test1
@@model['rdf_Resource'].test2
@@model.mtk_retrieveProtected("build/tmp1.java",TargetFileType)
@@model.mtk_retrieveProtected("build/tmp2.java",TargetFileType)
s1=@@model['rdf_Resource__0__build/tmp1.java']
s2=@@model['rdf_Resource__0__build/tmp2.java']
assert(!s1.nil?,"1 non revers�")
assert(!s2.nil?,"2 non revers�")
assert(s1==Test_string_protected1,"Contenu s1 different valeur injecte");
assert(s2==Test_string_protected2,"Contenu s2 different valeur injecte");
}
end
def test_no_reverse
@@model['rdf_Resource'].test3
s3=@@model['rdf_Resource__0__build/tmp3.java']
assert( s3.nil?,"3 n'aurai pas du �tre recupere")
#assert(s3==Test_string_protected3);
@@model.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
@@model.mtk_retrieveProtected("build/tmp3.java",TargetFileType)
}
end
def test_avec_zone
@@model['rdf_Resource'].test4
@@model.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
@@model.mtk_retrieveProtected("build/tmp4.java",TargetFileType)
}
s40=@@model['rdf_Resource__zone40__build/tmp4.java']
s41=@@model['rdf_Resource__zone41__build/tmp4.java']
assert(!s40.nil?,"40 non revers�")
assert(!s41.nil?,"41 non revers�")
# On verifie que le texte de chaque zone se retrouve au bon endroit.
assert(s40==Test_string_protected40,"Contenu s40 different valeur injecte");
assert(s41==Test_string_protected41,"Contenu s41 different valeur injecte");
end
def test_multi_write
@@model['rdf_Resource'].test5a
m=Crdf_Repository.new
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
m.mtk_retrieveProtected("build/tmp5.java",TargetFileType)
}
#m.each{ |k,val| puts "5a k=#{k} " }
s5a=m['rdf_Resource__A__build/tmp5.java']
s5b=m['rdf_Resource__B__build/tmp5.java']
s5c=m['rdf_Resource__C__build/tmp5.java']
s5d=m['rdf_Resource__D__build/tmp5.java']
assert(s5a.nil? ,"5a ne devrait pas etre remonte")
assert(!s5b.nil?,"5b non revers�")
assert(s5c.nil? ,"5c revers�")
assert(!s5d.nil?,"5d non revers�")
@@model['rdf_Resource'].test5b
m=Crdf_Repository.new
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
m.mtk_retrieveProtected("build/tmp5.java",TargetFileType)
}
#m.each{ |k,val| puts "5b k=#{k} " }
s5a=m['rdf_Resource__A__build/tmp5.java']
s5b=m['rdf_Resource__B__build/tmp5.java']
s5c=m['rdf_Resource__C__build/tmp5.java']
s5d=m['rdf_Resource__D__build/tmp5.java']
assert(s5a.nil?,"5a ne devrait pas etre remonte")
assert(!s5b.nil?,"5b non revers�")
assert(!s5c.nil?,"5c non revers�")
assert(!s5d.nil?,"5d non revers�")
#assert(s5a==Test_string_protected5A,"Contenu 5A different valeur injecte");
assert(s5b==Test_string_protected5B,"Contenu 5B different valeur injecte");
assert(s5c==Test_string_protected5C,"Contenu 5C different valeur injecte");
assert(s5d==Test_string_protected5D,"Contenu 5D different valeur injecte");
#verification du fichier sur disque
#pour les donn�es non remont�es (et les autres tant qu'on y est)
a=b=c=d=false
File.open("build/tmp5.java") { |f| f.each_line { |line|
a=true if line.include? "file tmp5A"
b=true if line.include? "file tmp5B"
c=true if line.include? "file tmp5C"
d=true if line.include? "file tmp5D"
}}
assert(a,"tmp5A absent du fichier")
assert(b,"tmp5B absent du fichier")
assert(c,"tmp5C absent du fichier")
assert(d,"tmp5D absent du fichier")
end
def test_compatibilite_ascendante
m=Crdf_Repository.new
f="#{SRCDIR}/unit_test_protected_sample"
#f="#{SRCDIR}/unit_test_protected_sample"
processed=nil
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
processed=m.mtk_retrieveProtected(f,TargetFileType)
}
assert(processed,"fichier probablement non trouve:#{f}")
#m.each{|k,res| puts "#{k}=>#{res}##########" }
s5a=m["rdf_Resource__A__#{SRCDIR}/unit_test_protected_sample"]
s5b=m["rdf_Resource__B__#{SRCDIR}/unit_test_protected_sample"]
s5c=m["rdf_Resource__C__#{SRCDIR}/unit_test_protected_sample"]
s5d=m["rdf_Resource__D__#{SRCDIR}/unit_test_protected_sample"]
assert(s5a.nil?,"5a ne devrait pas etre remonte")
assert(!s5b.nil?,"5b non revers�")
assert(!s5c.nil?,"5c non revers�")
assert(!s5d.nil?,"5d non revers�")
#assert(s5a==Test_string_protected5A,"Contenu 5A different valeur injecte");
assert(s5b==Test_string_protected5B,"Contenu 5B different valeur injecte");
assert(s5c==Test_string_protected5C,"Contenu 5C different valeur injecte");
assert(s5d==Test_string_protected5D,"Contenu 5D different valeur injecte");
end
def test_OK_si_fichier_existe_pas
m=Crdf_Repository.new
x=nil
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
x=m.mtk_retrieveProtected("nom fichier qui n'existe pas mjdfljqs mljfdkq fqdj qf",TargetFileType)
}
assert(!x,"retrieveProtected doit renvoyer false si le fichier n'est pas trouv�")
end
def test_END_with_bad_uri
m=Crdf_Repository.new
begin
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
m.mtk_retrieveProtected("#{SRCDIR}/unit_test_protected_wrongEnd",TargetFileType)
}
assert(false,"une erreur ne s'est pas d�clench�e")
rescue WarningReverseBadOrMissingEnd => w
end
end
def test_atomic_write
end
def test_allowAutomaticCodeDeletion
m=Crdf_Repository.new
r=m['rdf_Resource']
r.mtk_context(:allowAutomaticCodeDeletion => false) {
f="build/tmp10.fjqmjfmkqs2.java"
File.delete(f) if File.exists?(f)
m.mtk_writeSession(f){
r.mtk_protected(false,"a") {
m.write("xyz")
}
}
m.mtk_writeSession(f){
r.mtk_protected(true,"b") {
m.write("xyz")
}
#zone "a" was lost but it is ok since reverse=false
}
begin
m.mtk_writeSession(f){
r.mtk_protected(true,"c") {
m.write("xyz")
}
#zone "c" was lost and it is not ok since reverse=true in file
}
assert(false,"exception expected")
rescue Exception => e
puts "exception OK"
end
}
end
def test_mtkstss
f="build/tmp10.fjqmjfmkqs2.java"
2.times {
m=Crdf_Repository.new
r=m['rdf_Resource']
r.mtk_context(:allowAutomaticCodeDeletion => false) {
File.delete(f) if File.exists?(f)
m.mtk_writeSession(f){
r.mtk_protected(false,"aa") {
m.write("xyz")
}
r.write(r.mtk_stss{
r.mtk_protected(true,"b") {
m.write("xyz")
}
})
}}}
end
def test_unknown_extension_without_protected
m=Crdf_Repository.new
m.mtk_writeSession("build/tmp6.fjqmjfmkqs1"){
m.write("foo")
}
end
def test_unknown_extension_with_protected
m=Crdf_Repository.new
r=m['rdf_Resource']
m.mtk_writeSession("build/tmp6.fjqmjfmkqs2"){
begin
r.mtk_protected(false) {
m.write("xyz")
}
assert(false,"exception expected")
rescue WarningUnknownFileType => e
end
}
end
def test_writeXML
f=FileTypeXML.instance
#m=f.re_begin
#s=f.beginMarker("1234",true)
##s=%{}
#s=%{}
#puts "m=#{m}"
#puts "s=#{s}"
#puts "match=#{m.match(s)}\n"
#puts "uri=#{m.match(s)[2]}\n"
#puts "rev=#{m.match(s)[1]}\n"
#
#s=%{}
#m=f.re_end
##s=f.beginMarker("1234",true)
##s=%{}
#s=%{}
#puts "m=#{m}"
#puts "s=#{s}"
#puts "match=#{m.match(s)}\n"
#puts "uri=#{m.match(s)[1]}\n"
File.delete("build/tmp7.xml") if File.exists?("build/tmp7.xml")
m=Crdf_Repository.new
r=m['rdf_Resource']
r.mtk_writeSession("build/tmp7.xml"){
r.write("\n")
r.mtk_protected(true) {
r.write("xyz")
}
r.mtk_protected(false,"a") {
r.write("efg")
}
r.write("\n")
#writeComment("hello!")
}
r.mtk_writeSession("build/tmp7.xml"){
r.write("\n")
r.mtk_protected(true) {
r.write("\n")
r.write("")
}
r.mtk_protected(true,"a") {
r.write("123")
}
r.write("\n")
#writeComment("hello!")
}
#puts "m.length=#{m.length}"
m.mtk_context(:mtk_fileType=>FileTypeJava.instance) {
z=m.mtk_retrieveProtected("build/tmp7.xml",FileTypeXML.instance)
}
#puts "m.length=#{m.length}"
s7=m['rdf_Resource__0__build/tmp7.xml']
#puts "s7=#{s7}"
assert(s7=="xyz")
zoneA=m['rdf_Resource__a__build/tmp7.xml']
#puts "zoneA=#{zoneA}"
assert(zoneA=="123")
end
end
require 'test/unit/ui/console/testrunner'
Test::Unit::UI::Console::TestRunner.run(TC_protected_tc1)