1 |
| package photospace.meta; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import java.util.*; |
5 |
| import org.apache.commons.logging.*; |
6 |
| import com.drew.metadata.*; |
7 |
| import com.hp.hpl.jena.rdf.model.*; |
8 |
| import com.hp.hpl.jena.vocabulary.*; |
9 |
| import junit.framework.*; |
10 |
| import photospace.meta.rdf.*; |
11 |
| import photospace.vfs.*; |
12 |
| |
13 |
| public class TranslatorTest |
14 |
| extends TestCase |
15 |
| { |
16 |
| private static final Log log = LogFactory.getLog(TranslatorTest.class); |
17 |
| |
18 |
1
| public void testTranslateFromAsset() throws Exception
|
19 |
| { |
20 |
1
| Translator translator = new Translator();
|
21 |
| |
22 |
1
| PhotoMeta photo = new PhotoMeta();
|
23 |
1
| photo.setDevice("test device");
|
24 |
1
| photo.setLabels(new String[] { "k1", "k2"} );
|
25 |
| |
26 |
1
| Model rdf = translator.toRdf(photo);
|
27 |
| |
28 |
1
| assertEquals(photo.getDevice(), rdf.getProperty(null, TechVocab.CAMERA).getString());
|
29 |
| } |
30 |
| |
31 |
1
| public void testRoundTrip() throws Exception
|
32 |
| { |
33 |
1
| Translator translator = new Translator();
|
34 |
| |
35 |
1
| PhotoMeta photo = new PhotoMeta();
|
36 |
1
| photo.setLabels(new String[] { "k1", "k2"} );
|
37 |
| |
38 |
1
| Model rdf = translator.toRdf(photo);
|
39 |
1
| PhotoMeta fromRdf = (PhotoMeta) translator.fromRdf(rdf);
|
40 |
| |
41 |
1
| assertEquals(Arrays.asList(photo.getLabels()), Arrays.asList(fromRdf.getLabels()));
|
42 |
1
| assertEquals(photo, fromRdf);
|
43 |
| } |
44 |
| |
45 |
1
| public void testTranslateFromExif() throws Exception
|
46 |
| { |
47 |
1
| PersisterImpl persister = new PersisterImpl();
|
48 |
1
| Translator translator = new Translator();
|
49 |
1
| persister.setTranslator(translator);
|
50 |
| |
51 |
1
| File jpeg = new File(System.getProperty("project.root"), "build/test/exif-nordf.jpg");
|
52 |
| |
53 |
1
| Metadata exif = persister.getExif(jpeg);
|
54 |
| |
55 |
1
| PhotoMeta photo = translator.fromExif(exif);
|
56 |
1
| Model rdf = translator.toRdf(photo);
|
57 |
1
| PhotoMeta fromRdf = (PhotoMeta) translator.fromRdf(rdf);
|
58 |
| |
59 |
1
| assertEquals(photo, fromRdf);
|
60 |
1
| assertEquals(photo.getDevice(), fromRdf.getDevice());
|
61 |
| } |
62 |
| |
63 |
1
| public void testFolderMeta() throws Exception
|
64 |
| { |
65 |
| |
66 |
1
| PersisterImpl persister = new PersisterImpl();
|
67 |
1
| Translator translator = new Translator();
|
68 |
1
| persister.setTranslator(translator);
|
69 |
1
| FileSystem filesystem = new FileSystemImpl();
|
70 |
1
| filesystem.setRoot(new File(System.getProperty("project.root"), "build/test/"));
|
71 |
1
| persister.setFilesystem(filesystem);
|
72 |
| |
73 |
1
| File folder = new File(System.getProperty("project.root"), "build/test/").getCanonicalFile();
|
74 |
1
| FolderMeta meta = (FolderMeta) persister.getMeta(folder);
|
75 |
1
| assertEquals("/", meta.getPath());
|
76 |
1
| assertNotNull(meta);
|
77 |
1
| Model rdf = translator.toRdf(meta);
|
78 |
1
| assertNotNull(rdf);
|
79 |
1
| Meta fromRdf = translator.fromRdf(rdf);
|
80 |
1
| fromRdf.setName(meta.getName());
|
81 |
1
| assertEquals(meta.getPath(), fromRdf.getPath());
|
82 |
1
| assertEquals(meta, fromRdf);
|
83 |
| } |
84 |
| |
85 |
1
| public void testDeepToRdf() throws Exception
|
86 |
| { |
87 |
1
| PhotoMeta photo = new PhotoMeta();
|
88 |
1
| photo.setPath("/foo/photo.jpg");
|
89 |
1
| photo.setTitle("foo photo");
|
90 |
| |
91 |
1
| FolderMeta folder = new FolderMeta();
|
92 |
1
| folder.setPath("/foo/boo/");
|
93 |
1
| folder.setTitle("boo folder");
|
94 |
| |
95 |
1
| folder.addFile(photo);
|
96 |
| |
97 |
1
| Translator translator = new Translator();
|
98 |
1
| Model rdf = translator.toRdf(folder, 1);
|
99 |
1
| assertNotNull(rdf);
|
100 |
| |
101 |
1
| ResIterator subjects = rdf.listSubjects();
|
102 |
1
| assertTrue(subjects.hasNext());
|
103 |
1
| Resource r1 = subjects.nextResource();
|
104 |
1
| assertTrue(subjects.hasNext());
|
105 |
1
| Resource r2 = subjects.nextResource();
|
106 |
1
| assertFalse(subjects.hasNext());
|
107 |
| } |
108 |
| |
109 |
1
| public void testRss() throws Exception
|
110 |
| { |
111 |
1
| PhotoMeta photo = new PhotoMeta();
|
112 |
1
| photo.setPath("/foo/photo.jpg");
|
113 |
1
| photo.setTitle("foo photo");
|
114 |
| |
115 |
1
| Model rdf = ModelFactory.createDefaultModel();
|
116 |
1
| Resource channel = rdf.createResource(RSS.channel);
|
117 |
1
| channel.addProperty(RSS.title, "channel title");
|
118 |
1
| channel.addProperty(RSS.link, "http://foo.bar/channel/");
|
119 |
1
| channel.addProperty(RSS.description, "channel description");
|
120 |
1
| Seq items = rdf.createSeq();
|
121 |
1
| channel.addProperty(RSS.items, items);
|
122 |
1
| for (int i = 0; i < 6; i++)
|
123 |
| { |
124 |
6
| Resource item = rdf.createResource(RSS.item);
|
125 |
6
| items.add(item);
|
126 |
6
| item.addProperty(RSS.title, "item title " + i);
|
127 |
6
| item.addProperty(RSS.link, "http://foo.bar/channel/" + i);
|
128 |
6
| item.addProperty(RSS.description, "channel description " + i);
|
129 |
| } |
130 |
1
| log.info(RdfTools.toString(rdf));
|
131 |
| } |
132 |
| } |