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