1 package org.apache.maven.continuum.management;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.continuum.store.AbstractContinuumStoreTestCase;
23 import org.apache.maven.continuum.store.ContinuumStoreException;
24 import org.codehaus.plexus.util.FileUtils;
25 import org.codehaus.plexus.util.IOUtil;
26 import org.custommonkey.xmlunit.Diff;
27 import org.jdom.Document;
28 import org.jdom.input.SAXBuilder;
29 import org.jdom.output.Format;
30 import org.jdom.output.XMLOutputter;
31
32 import java.io.File;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.io.StringReader;
36 import java.io.StringWriter;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.Locale;
40 import javax.xml.stream.XMLStreamException;
41
42
43
44
45 public class DataManagementToolTest
46 extends AbstractContinuumStoreTestCase
47 {
48 private DataManagementTool dataManagementTool;
49
50 private File targetDirectory;
51
52 private static final String BUILDS_XML = "builds.xml";
53
54 protected void setUp()
55 throws Exception
56 {
57 super.setUp();
58
59 dataManagementTool = (DataManagementTool) lookup( DataManagementTool.class.getName(), "continuum-jdo" );
60
61 targetDirectory = createBackupDirectory();
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public void testBackupBuilds()
81 throws IOException, ContinuumStoreException, XMLStreamException, Exception
82 {
83 createBuildDatabase( true );
84
85
86 assertBuildDatabase();
87
88 dataManagementTool.backupDatabase( targetDirectory );
89
90 File backupFile = new File( targetDirectory, BUILDS_XML );
91
92 assertTrue( "Check database exists", backupFile.exists() );
93
94 StringWriter sw = new StringWriter();
95
96 IOUtil.copy( getClass().getResourceAsStream( "/expected.xml" ), sw );
97
98
99
100 assertXmlSimilar( removeTimestampVariance( sw.toString() ), removeTimestampVariance( FileUtils.fileRead(
101 backupFile ) ) );
102 }
103
104 public void testEraseBuilds()
105 throws Exception
106 {
107 createBuildDatabase( false );
108
109 dataManagementTool.eraseDatabase();
110
111 assertEmpty( false );
112 }
113
114 public void testRestoreBuilds()
115 throws Exception
116 {
117 createBuildDatabase( false, true );
118
119 assertEmpty( true );
120
121 File backupFile = new File( targetDirectory, BUILDS_XML );
122
123 IOUtil.copy( getClass().getResourceAsStream( "/expected.xml" ), new FileWriter( backupFile ) );
124
125 dataManagementTool.restoreDatabase( targetDirectory, true );
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 }
142
143 private static File createBackupDirectory()
144 {
145 String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US ).format( new Date() );
146
147 File targetDirectory = getTestFile( "target/backups/" + timestamp );
148 targetDirectory.mkdirs();
149
150 return targetDirectory;
151 }
152
153 private static String removeTimestampVariance( String content )
154 {
155
156
157
158
159 return removeTagContent( removeTagContent( removeTagContent( removeTagContent( content, "startTime" ),
160 "endTime" ), "date" ), "id" );
161 }
162
163 public void assertXmlIdentical( String expected, String test )
164 throws Exception
165 {
166 String expectedXml = prettyXmlPrint( expected );
167 String testXml = prettyXmlPrint( test );
168 Diff diff = new Diff( expectedXml, testXml );
169 StringBuffer diffMessage = new StringBuffer();
170 assertTrue( " xml diff not identical " + diff.appendMessage( diffMessage ).toString(), diff.identical() );
171 }
172
173
174 public void assertXmlSimilar( String expected, String test )
175 throws Exception
176 {
177 String expectedXml = prettyXmlPrint( expected );
178 String testXml = prettyXmlPrint( test );
179 Diff diff = new Diff( expectedXml, testXml );
180 StringBuffer diffMessage = new StringBuffer();
181 assertTrue( " xml diff not similar " + diff.appendMessage( diffMessage ).toString(), diff.similar() );
182 }
183
184 public String prettyXmlPrint( String xml )
185 throws Exception
186 {
187 SAXBuilder saxBuilder = new SAXBuilder();
188 Document document = saxBuilder.build( new StringReader( xml ) );
189 XMLOutputter outp = new XMLOutputter();
190
191 outp.setFormat( Format.getPrettyFormat() );
192
193 StringWriter sw = new StringWriter();
194
195 outp.output( document, sw );
196 return sw.getBuffer().toString();
197
198 }
199
200 private static String removeTagContent( String content, String field )
201 {
202 return content.replaceAll( "<" + field + ">.*</" + field + ">", "<" + field + "></" + field + ">" );
203 }
204 }