1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2021 Exiv2 authors
4  * This program is part of the Exiv2 distribution.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <exiv2/error.hpp>
22 #include <exiv2/properties.hpp>
23 #include "gtestwrapper.h"
24 using namespace Exiv2;
25 
26 namespace
27 {
28     const std::string expectedFamily("Xmp");
29     const std::string expectedPrefix("prefix");
30     const std::string expectedProperty("prop");
31     const std::string expectedKey(expectedFamily + "." + expectedPrefix + "." + expectedProperty);
32     const std::string notRegisteredValidKey("Xmp.noregistered.prop");
33 }
34 
35 // Test Fixture which register a namespace with a prefix. This is needed to test the correct
36 // behavior of the XmpKey class
37 class AXmpKey : public testing::Test
38 {
39 public:
SetUpTestCase()40     static void SetUpTestCase()
41     {
42         XmpProperties::registerNs(expectedFamily, expectedPrefix);
43     }
44 
TearDownTestCase()45     static void TearDownTestCase()
46     {
47         XmpProperties::unregisterNs();
48     }
49 
checkValidity(const XmpKey & key)50     void checkValidity(const XmpKey& key)
51     {
52         ASSERT_EQ(expectedKey, key.key());
53         ASSERT_EQ(expectedFamily, key.familyName());
54         ASSERT_EQ(expectedPrefix, key.groupName());
55         ASSERT_EQ(expectedProperty, key.tagName());
56         ASSERT_EQ(expectedProperty, key.tagLabel());
57         ASSERT_EQ(0, key.tag());
58         ASSERT_STREQ("Xmp/", key.ns().c_str());
59     }
60 };
61 
TEST_F(AXmpKey,correctlyInstantiateWithValidKey)62 TEST_F(AXmpKey, correctlyInstantiateWithValidKey)
63 {
64     XmpKey key(expectedKey);
65     checkValidity(key);
66 }
67 
TEST_F(AXmpKey,correctlyInstantiatedWithValidPrefixAndProperty)68 TEST_F(AXmpKey, correctlyInstantiatedWithValidPrefixAndProperty)
69 {
70     XmpKey key(expectedPrefix, expectedProperty);
71     checkValidity(key);
72 }
73 
TEST_F(AXmpKey,canBeCopiedConstructed)74 TEST_F(AXmpKey, canBeCopiedConstructed)
75 {
76     XmpKey key(expectedPrefix, expectedProperty);
77     XmpKey copiedKey(key);
78     checkValidity(copiedKey);
79 }
80 
TEST_F(AXmpKey,canBeCopied)81 TEST_F(AXmpKey, canBeCopied)
82 {
83     XmpKey key(expectedPrefix, expectedProperty);
84     XmpKey copiedKey("Xmp.prefix.prop2");
85     copiedKey = key;
86     checkValidity(copiedKey);
87 }
88 
TEST_F(AXmpKey,canBeCloned)89 TEST_F(AXmpKey, canBeCloned)
90 {
91     XmpKey key(expectedPrefix, expectedProperty);
92     XmpKey::AutoPtr clonedKey = key.clone();
93     checkValidity(*clonedKey);
94 }
95 
TEST_F(AXmpKey,throwsWithNotRegisteredWellFormedKey)96 TEST_F(AXmpKey, throwsWithNotRegisteredWellFormedKey)
97 {
98     ASSERT_THROW(XmpKey key(notRegisteredValidKey), std::exception);
99 }
100 
TEST_F(AXmpKey,throwsWithNotRegisteredPrefix)101 TEST_F(AXmpKey, throwsWithNotRegisteredPrefix)
102 {
103     ASSERT_THROW(XmpKey key("badPrefix", expectedProperty), std::exception);
104 }
105 
TEST_F(AXmpKey,throwsWithBadFormedKey)106 TEST_F(AXmpKey, throwsWithBadFormedKey)
107 {
108     ASSERT_THROW(XmpKey key(expectedProperty), std::exception);  // It should have the format ns.prefix.key
109 }
110