source: josm/trunk/test/unit/org/openstreetmap/josm/gui/history/HistoryBrowserModelTest.java @ 13509

Last change on this file since 13509 was 13509, checked in by Don-vip, 8 months ago

fix #16060 - NPE + minor fixes

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotEquals;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertNull;
8import static org.junit.Assert.assertTrue;
9
10import org.junit.Rule;
11import org.junit.Test;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
14import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
15import org.openstreetmap.josm.data.osm.User;
16import org.openstreetmap.josm.data.osm.history.History;
17import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
18import org.openstreetmap.josm.data.osm.history.HistoryNode;
19import org.openstreetmap.josm.testutils.JOSMTestRules;
20
21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23/**
24 * Unit tests of {@link HistoryBrowserModel} class.
25 */
26public class HistoryBrowserModelTest {
27
28    /**
29     * Setup test.
30     */
31    @Rule
32    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
33    public JOSMTestRules test = new JOSMTestRules().preferences().devAPI().timeout(30000);
34
35    /**
36     * Test for {@link HistoryBrowserModel#HistoryBrowserModel}.
37     */
38    @Test
39    public void testHistoryBrowserModel() {
40        HistoryBrowserModel model = new HistoryBrowserModel();
41        assertNotNull(model.getVersionTableModel());
42        assertNull(model.getHistory());
43        Node n = new Node(1, 1);
44        n.setUser(User.getAnonymous());
45        n.setChangesetId(1);
46        HistoryDataSet.getInstance().put(new HistoryNode(n));
47        History history = HistoryDataSet.getInstance().getHistory(1, OsmPrimitiveType.NODE);
48        assertNotNull(history);
49        model.setHistory(history);
50        assertEquals(history, model.getHistory());
51        model = new HistoryBrowserModel(history);
52        assertEquals(history, model.getHistory());
53    }
54
55    /**
56     * Unit test of {@link HistoryBrowserModel#getTagTableModel}.
57     */
58    @Test
59    public void testGetTagTableModel() {
60        HistoryBrowserModel model = new HistoryBrowserModel();
61        TagTableModel t1 = model.getTagTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
62        TagTableModel t2 = model.getTagTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
63        assertNotNull(t1);
64        assertNotNull(t2);
65        assertNotEquals(t1, t2);
66    }
67
68    /**
69     * Unit test of {@link HistoryBrowserModel#getNodeListTableModel}.
70     */
71    @Test
72    public void testGetNodeListTableModel() {
73        HistoryBrowserModel model = new HistoryBrowserModel();
74        DiffTableModel t1 = model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
75        DiffTableModel t2 = model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
76        assertNotNull(t1);
77        assertNotNull(t2);
78        assertNotEquals(t1, t2);
79    }
80
81    /**
82     * Unit test of {@link HistoryBrowserModel#getRelationMemberTableModel}.
83     */
84    @Test
85    public void testGetRelationMemberTableModel() {
86        HistoryBrowserModel model = new HistoryBrowserModel();
87        DiffTableModel t1 = model.getRelationMemberTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
88        DiffTableModel t2 = model.getRelationMemberTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
89        assertNotNull(t1);
90        assertNotNull(t2);
91        assertNotEquals(t1, t2);
92    }
93
94    /**
95     * Unit test of {@link HistoryBrowserModel#setCurrentPointInTime} and {@link HistoryBrowserModel#setReferencePointInTime} - null history.
96     */
97    @Test
98    public void testSetPointsInTimeNullHistory() {
99        HistoryBrowserModel model = new HistoryBrowserModel();
100        VersionTableModel tableModel = model.getVersionTableModel();
101        tableModel.setValueAt(false, 0, 0); // code coverage
102        tableModel.setValueAt(true, 0, 1);  // reference point
103        tableModel.setValueAt(true, 1, 2);  // current point
104        tableModel.setValueAt(true, 3, 3);  // code coverage
105    }
106
107    /**
108     * Unit test of {@link HistoryBrowserModel#setCurrentPointInTime} and {@link HistoryBrowserModel#setReferencePointInTime} - node history.
109     */
110    @Test
111    public void testSetPointsInTimeNodeHistory() {
112        SimplePrimitiveId id = new SimplePrimitiveId(2, OsmPrimitiveType.NODE);
113        new HistoryLoadTask().add(id).run();
114        History history = HistoryDataSet.getInstance().getHistory(id);
115        assertTrue(history.getNumVersions() >= 4);
116        HistoryBrowserModel model = new HistoryBrowserModel(history);
117        VersionTableModel tableModel = model.getVersionTableModel();
118        tableModel.setValueAt(false, 0, 0); // code coverage
119        tableModel.setValueAt(true, 0, 1);  // reference point
120        tableModel.setValueAt(true, 3, 2);  // current point
121        tableModel.setValueAt(true, 3, 3);  // code coverage
122        // nodes only for ways
123        assertEquals(0, model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
124        assertEquals(0, model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
125        // members only for relations
126        assertEquals(0, model.getRelationMemberTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
127        assertEquals(0, model.getRelationMemberTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
128    }
129
130    /**
131     * Unit test of {@link HistoryBrowserModel#setCurrentPointInTime} and {@link HistoryBrowserModel#setReferencePointInTime} - way history.
132     */
133    @Test
134    public void testSetPointsInTimeWayHistory() {
135        SimplePrimitiveId id = new SimplePrimitiveId(2, OsmPrimitiveType.WAY);
136        new HistoryLoadTask().add(id).run();
137        History history = HistoryDataSet.getInstance().getHistory(id);
138        assertTrue(history.getNumVersions() >= 2);
139        HistoryBrowserModel model = new HistoryBrowserModel(history);
140        VersionTableModel tableModel = model.getVersionTableModel();
141        tableModel.setValueAt(false, 0, 0); // code coverage
142        tableModel.setValueAt(true, 0, 1);  // reference point
143        tableModel.setValueAt(true, 3, 2);  // current point
144        tableModel.setValueAt(true, 3, 3);  // code coverage
145        // nodes only for ways
146        assertEquals(2, model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
147        assertEquals(2, model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
148        // members only for relations
149        assertEquals(0, model.getRelationMemberTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
150        assertEquals(0, model.getRelationMemberTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
151    }
152
153    /**
154     * Unit test of {@link HistoryBrowserModel#setCurrentPointInTime} and {@link HistoryBrowserModel#setReferencePointInTime} - relation history.
155     */
156    @Test
157    public void testSetPointsInTimeRelationHistory() {
158        SimplePrimitiveId id = new SimplePrimitiveId(2, OsmPrimitiveType.RELATION);
159        new HistoryLoadTask().add(id).run();
160        History history = HistoryDataSet.getInstance().getHistory(id);
161        assertTrue(history.getNumVersions() >= 2);
162        HistoryBrowserModel model = new HistoryBrowserModel(history);
163        VersionTableModel tableModel = model.getVersionTableModel();
164        tableModel.setValueAt(false, 0, 0); // code coverage
165        tableModel.setValueAt(true, 0, 1);  // reference point
166        tableModel.setValueAt(true, 3, 2);  // current point
167        tableModel.setValueAt(true, 3, 3);  // code coverage
168        // nodes only for ways
169        assertEquals(0, model.getNodeListTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
170        assertEquals(0, model.getNodeListTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
171        // members only for relations
172        assertEquals(1, model.getRelationMemberTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME).getRowCount());
173        assertEquals(1, model.getRelationMemberTableModel(PointInTimeType.CURRENT_POINT_IN_TIME).getRowCount());
174    }
175}
Note: See TracBrowser for help on using the repository browser.