View Javadoc

1   package org.apache.maven.continuum.management;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
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   * Test the database management tool.
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      protected ContinuumStore createStore()
63          throws Exception
64      {
65          DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE );
66  
67          File database = getTestFile( "target/database/" + getName());
68          FileUtils.deleteDirectory( database );
69  
70          jdoFactory.setDriverName( "org.apache.derby.jdbc.EmbeddedDriver");
71          jdoFactory.setUrl( "jdbc:derby:"+database.getAbsolutePath() + ";create=true" );
72  
73          return (ContinuumStore) lookup( ContinuumStore.ROLE );
74      }
75  */
76  
77      public void testBackupBuilds()
78          throws IOException, ContinuumStoreException, XMLStreamException, Exception
79      {           
80          createBuildDatabase( true );
81  
82          // test sanity check
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          //assertEquals( "Check database content", removeTimestampVariance( sw.toString() ),
96          //              removeTimestampVariance( FileUtils.fileRead( backupFile ) ) );        
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         // TODO: why is this wrong?
124         assertBuildDatabase();
125 
126         // Test that it worked. Relies on BackupBuilds having worked
127         dataManagementTool.backupDatabase( targetDirectory );
128 
129         StringWriter sw = new StringWriter();
130 
131         IOUtil.copy( getClass().getResourceAsStream( "/expected.xml" ), sw );
132 
133         //assertEquals( "Check database content", removeTimestampVariance( sw.toString() ),
134         //              removeTimestampVariance( FileUtils.fileRead( backupFile ) ) );
135         assertXmlSimilar( removeTimestampVariance( sw.toString() ), removeTimestampVariance( FileUtils
136             .fileRead( backupFile ) ) );*/
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         /*return fixXmlQuotes( removeTagContent( removeTagContent( removeTagContent( removeTagContent( content,
152                                                                                                      "startTime" ),
153                                                                                    "endTime" ), "date" ), "id" ) );*/
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 }