View Javadoc

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