1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*******************************************************************************
* Copyright (c) 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ua.tests.help.preferences;
import java.util.Observable;
import java.util.Observer;
import junit.framework.TestCase;
import org.eclipse.help.IHelpResource;
import org.eclipse.help.internal.base.BookmarkManager;
import org.eclipse.help.internal.base.BookmarkManager.BookmarkEvent;
/*
* Test the BookmarkManager
*/
public class BookmarksTest extends TestCase {
private static final String ECLIPSE = "eclipse";
private static final String HTTP_ECLIPSE = "http://www.eclipse.org";
private static final String HELP = "help";
private static final String HTTP_HELP = "http://help.eclipse.org";
private static final String BUGZILLA = "bugzilla";
private static final String HTTP_BUGZILLA = "https://bugs.eclipse.org/bugs/";
private class BookmarkObserver implements Observer {
public Object o;
public Object arg;
public int eventCount = 0;
public void update(Observable o, Object arg) {
++eventCount;
this.o = o;
this.arg = arg;
}
public BookmarkManager.BookmarkEvent getEvent() {
return (BookmarkEvent) arg;
}
}
private BookmarkManager manager;
private BookmarkObserver observer;
protected void setUp() throws Exception {
manager = new BookmarkManager();
manager.removeAllBookmarks();
observer = new BookmarkObserver();
manager.addObserver(observer);
}
protected void tearDown() throws Exception {
manager = null;
observer = null;
}
public void testRemoveAll() {
manager.removeAllBookmarks();
assertEquals(1, observer.eventCount);
assertEquals(manager, observer.o);
assertEquals(BookmarkManager.REMOVE_ALL, observer.getEvent().getType());
assertNull(observer.getEvent().getBookmark());
IHelpResource[] bookmarks = manager.getBookmarks();
assertEquals(0, bookmarks.length);
assertEquals(1, observer.eventCount);
}
public void testAddBookmarks() {
manager.addBookmark(HTTP_ECLIPSE, ECLIPSE);
assertEquals(1, observer.eventCount);
assertTrue(observer.arg instanceof BookmarkManager.BookmarkEvent);
BookmarkManager.BookmarkEvent event = observer.getEvent();
assertEquals(event.getType(), BookmarkManager.ADD);
assertEquals(ECLIPSE, event.getBookmark().getLabel());
assertEquals(HTTP_ECLIPSE, event.getBookmark().getHref());
manager.addBookmark(HTTP_BUGZILLA, BUGZILLA);
BookmarkManager manager2 = new BookmarkManager();
IHelpResource[] bookmarks = manager2.getBookmarks();
assertEquals(2, bookmarks.length);
assertEquals(ECLIPSE, bookmarks[0].getLabel());
assertEquals(BUGZILLA, bookmarks[1].getLabel());
assertEquals(HTTP_ECLIPSE, bookmarks[0].getHref());
assertEquals(HTTP_BUGZILLA, bookmarks[1].getHref());
assertEquals(2, observer.eventCount);
assertEquals(manager, observer.o);
}
public void testRemoveBookmarks() {
manager.addBookmark(HTTP_ECLIPSE, ECLIPSE);
assertEquals(1, observer.eventCount);
manager.addBookmark(HTTP_BUGZILLA, BUGZILLA);
manager.addBookmark(HTTP_HELP, HELP);
assertEquals(3, observer.eventCount);
manager.removeBookmark(HTTP_ECLIPSE, BUGZILLA); // No such bookmark
assertEquals(observer.eventCount, 3);
manager.removeBookmark(HTTP_ECLIPSE, ECLIPSE);
assertEquals(4, observer.eventCount);
BookmarkManager.BookmarkEvent event = observer.getEvent();
assertEquals(event.getType(), BookmarkManager.REMOVE);
assertEquals(ECLIPSE, event.getBookmark().getLabel());
assertEquals(HTTP_ECLIPSE, event.getBookmark().getHref());
assertEquals(manager, observer.o);
manager.removeBookmark(new BookmarkManager.Bookmark(BUGZILLA, HTTP_BUGZILLA));
assertEquals(5, observer.eventCount);
event = observer.getEvent();
assertEquals(event.getType(), BookmarkManager.REMOVE);
assertEquals(BUGZILLA, event.getBookmark().getLabel());
assertEquals(HTTP_BUGZILLA, event.getBookmark().getHref());
assertEquals(1, manager.getBookmarks().length);
}
}
|