1   /**
2    * JSpiff
3    * -----------------
4    * Copyright 2005-2006 Emil A. Lefkof III
5    *
6    * I always give it my best shot to make a program useful and solid, but
7    * remeber that there is absolutely no warranty for using this program as
8    * stated in the following terms:
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  package com.melloware.jspiff.jaxb;
23  
24  import java.io.InputStream;
25  import java.math.BigInteger;
26  import java.util.Iterator;
27  
28  import javax.xml.bind.JAXBContext;
29  import javax.xml.bind.JAXBElement;
30  import javax.xml.bind.JAXBException;
31  import javax.xml.bind.Marshaller;
32  import javax.xml.bind.PropertyException;
33  import javax.xml.bind.Unmarshaller;
34  import javax.xml.datatype.DatatypeConfigurationException;
35  
36  import junit.framework.TestCase;
37  
38  /**
39   * Test case to test JAXB playlists.
40   * <p>
41   * Copyright (c) 2006
42   * Melloware, Inc. <http://www.melloware.com>
43   * @author Emil A. Lefkof III <elefkof@ksmpartners.com>
44   * @version 4.0
45   */
46  public class XspfJAXBTest
47      extends TestCase {
48  
49      /**
50       * Constructor for XspfJAXBTest.
51       * @param arg0
52       */
53      public XspfJAXBTest(String arg0) {
54          super(arg0);
55      }
56  
57      /**
58       * Tests an invalid playlist file
59       */
60      public void testInvalidPlaylistFile() {
61          try {
62              // create the file
63              final InputStream stream = getClass().getResourceAsStream("/playlist-invalid.xml");
64  
65              JAXBContext jc = JAXBContext.newInstance("com.melloware.jspiff.jaxb");
66  
67              Unmarshaller u = jc.createUnmarshaller();
68              JAXBElement element = (JAXBElement)u.unmarshal(stream);
69              PlaylistType playlist = (PlaylistType)element.getValue();
70              System.out.println(playlist.getTitle());
71          } catch (PropertyException ex) {
72              fail(ex.getMessage());
73          } catch (JAXBException ex) {
74              // don't fail,should get a JAXBExcption
75              ;
76  
77          }
78      }
79  
80      /**
81       * Tests the creation of an XspfPlaylist object.
82       */
83      public void testPlaylist() {
84          try {
85              final ObjectFactory factory = new ObjectFactory();
86              final PlaylistType playlist = factory.createPlaylistType();
87              playlist.setTitle("Melloware XSPF Playlist");
88              playlist.setCreator("Melloware User");
89              playlist.setDate(javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(2006, 9, 27, 6, 1,
90                                                                                                        1, 1, 1));
91              playlist.setInfo("http://melloware.com/");
92              playlist.setVersion("1");
93              playlist.setImage("http://melloware.com/images/header.jpg");
94              playlist.setIdentifier(Integer.toString(super.hashCode()));
95              playlist.setLicense("http://www.apache.org/licenses/LICENSE-2.0.txt");
96  
97              final JAXBElement element = factory.createPlaylist(playlist);
98              // make an xml document out of it
99              final JAXBContext jc = JAXBContext.newInstance("com.melloware.jspiff.jaxb");
100             final Marshaller m = jc.createMarshaller();
101             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
102             m.marshal(element, System.out);
103             assertNotNull(element);
104         } catch (PropertyException ex) {
105             fail(ex.getMessage());
106         } catch (DatatypeConfigurationException ex) {
107             fail(ex.getMessage());
108         } catch (JAXBException ex) {
109             fail(ex.getMessage());
110         }
111 
112     }
113 
114     /**
115      * Tests the creation of an XspfTrack objects and appending them to a
116      * playlist.
117      */
118     public void testTracks() {
119         try {
120             final ObjectFactory factory = new ObjectFactory();
121             final PlaylistType playlist = factory.createPlaylistType();
122             playlist.setTitle("Melloware XSPF Playlist");
123             playlist.setCreator("Melloware User");
124             playlist.setDate(javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(2006, 9, 27, 6, 1,
125                                                                                                       1, 1, 1));
126             playlist.setInfo("http://melloware.com/");
127             playlist.setVersion("1");
128             playlist.setImage("http://melloware.com/images/header.jpg");
129             playlist.setIdentifier(Integer.toString(super.hashCode()));
130             playlist.setLicense("http://www.apache.org/licenses/LICENSE-2.0.txt");
131 
132             // create track list first
133             final TrackListType tracks = factory.createTrackListType();
134 
135             // now create track 1 and add to list
136             TrackType track = factory.createTrackType();
137             track.getIdentifier().add("135c3af5-526f-4d87-9757-1b404b51e31d");
138             track.getLocation().add("C:\\music\\01 - She Talks To Angels.mp3");
139             track.setCreator("Black Crowes");
140             track.setAlbum("Shake Your Moneymaker");
141             track.setTitle("She Talks To Angels");
142             track.setAnnotation("This is a classic song");
143             track.setTrackNum(BigInteger.valueOf(1));
144             track.setDuration(BigInteger.valueOf(314));
145             tracks.getTrack().add(track);
146 
147             // now create track 2 and add to list
148             track = factory.createTrackType();
149             track.getIdentifier().add("e113c56f-c4d4-4bfb-b9f0-6f90a172b5a9");
150             track.getLocation().add("C:\\music\\02 - Come Together.mp3");
151             track.setCreator("Beatles");
152             track.setAlbum("Abbey Road");
153             track.setTitle("Come Together");
154             track.setAnnotation("Love the Beatles");
155             track.setTrackNum(BigInteger.valueOf(2));
156             track.setDuration(BigInteger.valueOf(226));
157             tracks.getTrack().add(track);
158 
159             // add track to playlist
160             playlist.setTrackList(tracks);
161 
162             final JAXBElement element = factory.createPlaylist(playlist);
163             // make an xml document out of it
164             final JAXBContext jc = JAXBContext.newInstance("com.melloware.jspiff.jaxb");
165             final Marshaller m = jc.createMarshaller();
166             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
167             m.marshal(element, System.out);
168             assertNotNull(element);
169         } catch (PropertyException ex) {
170             fail(ex.getMessage());
171         } catch (DatatypeConfigurationException ex) {
172             fail(ex.getMessage());
173         } catch (JAXBException ex) {
174             fail(ex.getMessage());
175         }
176     }
177 
178     /**
179      * Tests a valid playlist file version 0.
180      */
181     public void testValidPlaylistFileVersion0() {
182         try {
183             // create the file
184             final InputStream stream = getClass().getResourceAsStream("/playlist-valid-ver0.xml");
185 
186             final JAXBContext jc = JAXBContext.newInstance("com.melloware.jspiff.jaxb");
187             final Unmarshaller u = jc.createUnmarshaller();
188             final JAXBElement element = (JAXBElement)u.unmarshal(stream);
189             final PlaylistType playlist = (PlaylistType)element.getValue();
190 
191             assertEquals(playlist.getTitle(), "XSPlF it up!");
192 
193             for (Iterator iter = playlist.getTrackList().getTrack().iterator(); iter.hasNext();) {
194                 TrackType track = (TrackType)iter.next();
195                 assertNotNull(track.getTitle());
196                 assertEquals(track.getMeta().size(), 1);
197             }
198 
199             assertEquals(playlist.getAttribution().getLocation().size(), 1);
200             assertEquals(playlist.getTrackList().getTrack().size(), 3);
201 
202             // make an xml document out of it
203             final Marshaller m = jc.createMarshaller();
204             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
205             m.marshal(element, System.out);
206         } catch (PropertyException ex) {
207             fail(ex.getMessage());
208         } catch (JAXBException ex) {
209             fail(ex.getMessage());
210         }
211     }
212 
213     /**
214     * Tests a valid playlist file version 1.
215     */
216     public void testValidPlaylistFileVersion1() {
217         try {
218             // create the file
219             final InputStream stream = getClass().getResourceAsStream("/playlist-valid-ver1.xml");
220 
221             JAXBContext jc = JAXBContext.newInstance("com.melloware.jspiff.jaxb");
222             Unmarshaller u = jc.createUnmarshaller();
223             JAXBElement element = (JAXBElement)u.unmarshal(stream);
224             PlaylistType playlist = (PlaylistType)element.getValue();
225 
226             assertEquals(playlist.getTitle(), "Track Test Playlist");
227 
228             for (Iterator iter = playlist.getTrackList().getTrack().iterator(); iter.hasNext();) {
229                 TrackType track = (TrackType)iter.next();
230                 assertNotNull(track.getTitle());
231             }
232 
233             assertEquals(playlist.getTrackList().getTrack().size(), 2);
234 
235             // make an xml document out of it
236             Marshaller m = jc.createMarshaller();
237             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
238             m.marshal(element, System.out);
239         } catch (PropertyException ex) {
240             fail(ex.getMessage());
241         } catch (JAXBException ex) {
242             fail(ex.getMessage());
243         }
244     }
245 
246     /*
247      * @see TestCase#setUp()
248      */
249     protected void setUp()
250                   throws Exception {
251         super.setUp();
252     }
253 
254     /*
255      * @see TestCase#tearDown()
256      */
257     protected void tearDown()
258                      throws Exception {
259         super.tearDown();
260     }
261 
262 }