View Javadoc

1   package sk.baka.ambient;
2   
3   /***
4    *     Ambient - A music player for the Android platform
5    Copyright (C) 2007 Martin Vysny
6   
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  import java.text.ParseException;
22  
23  import junit.framework.TestCase;
24  import sk.baka.ambient.collection.TrackMetadataBean;
25  import sk.baka.ambient.collection.TrackOriginEnum;
26  import sk.baka.ambient.collection.TrackMetadataBean.Builder;
27  import sk.baka.ambient.commons.TagFormatter;
28  
29  /***
30   * Tests the tag formatter.
31   * 
32   * @author Martin Vysny
33   */
34  public class TagFormatterTest extends TestCase {
35  	private final static TrackMetadataBean fullBean;
36  	static {
37  		final Builder b = TrackMetadataBean.newBuilder();
38  		b.setOrigin(TrackOriginEnum.LocalFs).setTitle("tit").setArtist("art");
39  		b.setComposer("com").setAlbum("alb").setGenre("gen");
40  		b.setTrackNumber("tnu").setLocation("loc").setLength(25);
41  		b.setBitrate(256).setFileSize(1000).setYearReleased("2000")
42  				.setFrequency(44100);
43  		fullBean = b.build(0);
44  	}
45  
46  	private final static TrackMetadataBean partiallyNullBean;
47  	static {
48  		final Builder b = TrackMetadataBean.newBuilder();
49  		b.setOrigin(TrackOriginEnum.LocalFs).setTitle("tit").setArtist("art");
50  		b.setComposer("com").setLocation("l").setLength(25);
51  		b.setBitrate(256).setFileSize(1000).setYearReleased("2000")
52  				.setFrequency(44100);
53  		partiallyNullBean = b.build(0);
54  	}
55  
56  	private final static TrackMetadataBean nullBean;
57  	static {
58  		nullBean = TrackMetadataBean.newBuilder().setOrigin(
59  				TrackOriginEnum.LocalFs).setLocation("l").build(0);
60  	}
61  
62  	/***
63  	 * @throws Exception
64  	 */
65  	public void testSimpleTagReplacement() throws Exception {
66  		TagFormatter formatter = new TagFormatter("%title - %album (%artist)");
67  		assertEquals("tit - alb (art)", formatter.format(fullBean));
68  		assertEquals("l -  ()", formatter.format(nullBean));
69  	}
70  
71  	/***
72  	 * @throws Exception
73  	 */
74  	public void testPartiallyEmptyGroups() throws Exception {
75  		TagFormatter formatter = new TagFormatter("{%title - %album (%artist)}");
76  		assertEquals("tit - alb (art)", formatter.format(fullBean));
77  		assertEquals("tit -  (art)", formatter.format(partiallyNullBean));
78  	}
79  
80  	/***
81  	 * @throws Exception
82  	 */
83  	public void testSiblingCollapsableGroups() throws Exception {
84  		TagFormatter formatter = new TagFormatter("{-}");
85  		assertEquals("", formatter.format(fullBean));
86  		assertEquals("", formatter.format(partiallyNullBean));
87  		assertEquals("", formatter.format(nullBean));
88  		formatter = new TagFormatter(
89  				"{%artist{, }%album{, }%year\n}{%track }%title{ (%length)}");
90  		String formatted = formatter.format(fullBean);
91  		assertEquals("art, alb, 2000\ntnu tit (0:25)", formatted);
92  		formatted = formatter.format(partiallyNullBean);
93  		assertEquals("art, 2000\ntit (0:25)", formatted);
94  		formatted = formatter.format(nullBean);
95  		assertEquals("l", formatted);
96  	}
97  
98  	/***
99  	 * @throws Exception
100 	 */
101 	public void testEmptyGroups() throws Exception {
102 		TagFormatter formatter = new TagFormatter("%title - %artist{ (%album)}");
103 		assertEquals("tit - art (alb)", formatter.format(fullBean));
104 		assertEquals("l - ", formatter.format(nullBean));
105 		assertEquals("tit - art", formatter.format(partiallyNullBean));
106 	}
107 
108 	/***
109 	 * @throws Exception
110 	 */
111 	public void testNestedGroups() throws Exception {
112 		TagFormatter formatter = new TagFormatter(
113 				"%title - %artist{ ({%year, }%album)}");
114 		assertEquals("tit - art (2000, alb)", formatter.format(fullBean));
115 		assertEquals("l - ", formatter.format(nullBean));
116 		assertEquals("tit - art (2000, )", formatter.format(partiallyNullBean));
117 		formatter = new TagFormatter(
118 				"{{%artist, }%album{, %year}\n}{%track }%title{ (%length)}");
119 		String formatted = formatter.format(fullBean);
120 		assertEquals("art, alb, 2000\ntnu tit (0:25)", formatted);
121 		formatted = formatter.format(partiallyNullBean);
122 		assertEquals("art, , 2000\ntit (0:25)", formatted);
123 	}
124 
125 	/***
126 	 * @throws Exception
127 	 */
128 	public void testInvalidFormat() throws Exception {
129 		TagFormatter formatter = new TagFormatter("%tit - %artist");
130 		assertEquals("%tit - art", formatter.format(fullBean));
131 		assertEquals("%tit - ", formatter.format(nullBean));
132 		try {
133 			formatter = new TagFormatter("%tit - {%artist)");
134 			fail("ParseException expected");
135 		} catch (ParseException ex) {
136 			// okay
137 		}
138 	}
139 
140 	/***
141 	 * @throws Exception
142 	 */
143 	public void testDelimiters() throws Exception {
144 		TagFormatter formatter = new TagFormatter(
145 				"{{%artist{, }%album{, }%year}\n}{%track }%title{ (%length)}");
146 		String formatted = formatter.format(fullBean);
147 		assertEquals("art, alb, 2000\ntnu tit (0:25)", formatted);
148 		formatted = formatter.format(partiallyNullBean);
149 		assertEquals("art, 2000\ntit (0:25)", formatted);
150 		formatted = formatter.format(nullBean);
151 		assertEquals("l", formatted);
152 		formatter = new TagFormatter("{%artist{, }%album{, }%track}\n");
153 		formatted = formatter.format(partiallyNullBean);
154 		assertEquals("art\n", formatted);
155 	}
156 }