Changeset 10200 in josm


Ignore:
Timestamp:
2016-05-13T03:11:46+02:00 (8 years ago)
Author:
Don-vip
Message:

add more unit tests

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r10093 r10200  
    237237    }
    238238
    239     private static Map<Option, Collection<String>> buildCommandLineArgumentMap(String[] args) {
     239    /**
     240     * Builds the command-line argument map.
     241     * @param args command-line arguments array
     242     * @return command-line argument map
     243     */
     244    public static Map<Option, Collection<String>> buildCommandLineArgumentMap(String[] args) {
    240245
    241246        List<LongOpt> los = new ArrayList<>();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r10199 r10200  
    211211        TreeSet<String> props = new TreeSet<>();
    212212        for (Entry<String, Object> entry : prop.entrySet()) {
    213             StringBuilder sb = new StringBuilder(entry.getKey()+':');
     213            StringBuilder sb = new StringBuilder(entry.getKey()).append(':');
    214214            Object val = entry.getValue();
    215215            if (val instanceof float[]) {
  • trunk/src/org/openstreetmap/josm/tools/WindowGeometry.java

    r10115 r10200  
    2424 * This is a helper class for persisting the geometry of a JOSM window to the preference store
    2525 * and for restoring it from the preference store.
    26  *
     26 * @since 2008
    2727 */
    2828public class WindowGeometry {
     29
     30    /** the top left point */
     31    private Point topLeft;
     32    /** the size */
     33    private Dimension extent;
     34
     35    /**
     36     * Creates a window geometry from a position and dimension
     37     *
     38     * @param topLeft the top left point
     39     * @param extent the extent
     40     */
     41    public WindowGeometry(Point topLeft, Dimension extent) {
     42        this.topLeft = topLeft;
     43        this.extent = extent;
     44    }
     45
     46    /**
     47     * Creates a window geometry from a rectangle
     48     *
     49     * @param rect the position
     50     */
     51    public WindowGeometry(Rectangle rect) {
     52        this(rect.getLocation(), rect.getSize());
     53    }
     54
     55    /**
     56     * Creates a window geometry from the position and the size of a window.
     57     *
     58     * @param window the window
     59     */
     60    public WindowGeometry(Window window)  {
     61        this(window.getLocationOnScreen(), window.getSize());
     62    }
     63
     64    /**
     65     * Creates a window geometry from the values kept in the preference store under the
     66     * key <code>preferenceKey</code>
     67     *
     68     * @param preferenceKey the preference key
     69     * @throws WindowGeometryException if no such key exist or if the preference value has
     70     * an illegal format
     71     */
     72    public WindowGeometry(String preferenceKey) throws WindowGeometryException {
     73        initFromPreferences(preferenceKey);
     74    }
     75
     76    /**
     77     * Creates a window geometry from the values kept in the preference store under the
     78     * key <code>preferenceKey</code>. Falls back to the <code>defaultGeometry</code> if
     79     * something goes wrong.
     80     *
     81     * @param preferenceKey the preference key
     82     * @param defaultGeometry the default geometry
     83     *
     84     */
     85    public WindowGeometry(String preferenceKey, WindowGeometry defaultGeometry) {
     86        try {
     87            initFromPreferences(preferenceKey);
     88        } catch (WindowGeometryException e) {
     89            if (Main.isDebugEnabled()) {
     90                Main.debug(e.getMessage());
     91            }
     92            initFromWindowGeometry(defaultGeometry);
     93        }
     94    }
    2995
    3096    /**
     
    85151     */
    86152    public static class WindowGeometryException extends Exception {
    87         public WindowGeometryException(String message, Throwable cause) {
     153        WindowGeometryException(String message, Throwable cause) {
    88154            super(message, cause);
    89155        }
    90156
    91         public WindowGeometryException(String message) {
     157        WindowGeometryException(String message) {
    92158            super(message);
    93159        }
    94     }
    95 
    96     /** the top left point */
    97     private Point topLeft;
    98     /** the size */
    99     private Dimension extent;
    100 
    101     /**
    102      * Creates a window geometry from a position and dimension
    103      *
    104      * @param topLeft the top left point
    105      * @param extent the extent
    106      */
    107     public WindowGeometry(Point topLeft, Dimension extent) {
    108         this.topLeft = topLeft;
    109         this.extent = extent;
    110     }
    111 
    112     /**
    113      * Creates a window geometry from a rectangle
    114      *
    115      * @param rect the position
    116      */
    117     public WindowGeometry(Rectangle rect) {
    118         this(rect.getLocation(), rect.getSize());
    119     }
    120 
    121     /**
    122      * Creates a window geometry from the position and the size of a window.
    123      *
    124      * @param window the window
    125      */
    126     public WindowGeometry(Window window)  {
    127         this(window.getLocationOnScreen(), window.getSize());
    128160    }
    129161
     
    162194                       "Cannot restore window geometry from preferences.",
    163195                            preferenceKey, field, v), e);
    164         } catch (Exception e) {
     196        } catch (RuntimeException e) {
    165197            throw new WindowGeometryException(
    166198                    tr("Failed to parse field ''{1}'' in preference with key ''{0}''. Exception was: {2}. " +
     
    195227                int w = Integer.parseInt(m.group(1));
    196228                int h = Integer.parseInt(m.group(2));
    197                 int x = screenDimension.x, y = screenDimension.y;
     229                int x = screenDimension.x;
     230                int y = screenDimension.y;
    198231                if (m.group(3) != null) {
    199232                    x = Integer.parseInt(m.group(5));
     
    224257
    225258    /**
    226      * Creates a window geometry from the values kept in the preference store under the
    227      * key <code>preferenceKey</code>
    228      *
    229      * @param preferenceKey the preference key
    230      * @throws WindowGeometryException if no such key exist or if the preference value has
    231      * an illegal format
    232      */
    233     public WindowGeometry(String preferenceKey) throws WindowGeometryException {
    234         initFromPreferences(preferenceKey);
    235     }
    236 
    237     /**
    238      * Creates a window geometry from the values kept in the preference store under the
    239      * key <code>preferenceKey</code>. Falls back to the <code>defaultGeometry</code> if
    240      * something goes wrong.
    241      *
    242      * @param preferenceKey the preference key
    243      * @param defaultGeometry the default geometry
    244      *
    245      */
    246     public WindowGeometry(String preferenceKey, WindowGeometry defaultGeometry) {
    247         try {
    248             initFromPreferences(preferenceKey);
    249         } catch (WindowGeometryException e) {
    250             initFromWindowGeometry(defaultGeometry);
    251         }
    252     }
    253 
    254     /**
    255259     * Remembers a window geometry under a specific preference key
    256260     *
     
    369373        Rectangle virtualBounds = new Rectangle();
    370374        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    371         for (GraphicsDevice gd : ge.getScreenDevices()) {
    372             if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
    373                 virtualBounds = virtualBounds.union(gd.getDefaultConfiguration().getBounds());
     375        if (!GraphicsEnvironment.isHeadless()) {
     376            for (GraphicsDevice gd : ge.getScreenDevices()) {
     377                if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
     378                    virtualBounds = virtualBounds.union(gd.getDefaultConfiguration().getBounds());
     379                }
    374380            }
    375381        }
     
    424430     */
    425431    private static Rectangle getScreenInfo(Rectangle g) {
    426         GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    427         int intersect = 0;
    428432        Rectangle bounds = null;
    429         for (GraphicsDevice gd : gs) {
    430             if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
    431                 Rectangle b = gd.getDefaultConfiguration().getBounds();
    432                 if (b.height > 0 && b.width / b.height >= 3) /* multiscreen with wrong definition */ {
    433                     b.width /= 2;
    434                     Rectangle is = b.intersection(g);
    435                     int s = is.width * is.height;
    436                     if (bounds == null || intersect < s) {
    437                         intersect = s;
    438                         bounds = b;
    439                     }
    440                     b = new Rectangle(b);
    441                     b.x += b.width;
    442                     is = b.intersection(g);
    443                     s = is.width * is.height;
    444                     if (intersect < s) {
    445                         intersect = s;
    446                         bounds = b;
    447                     }
    448                 } else {
    449                     Rectangle is = b.intersection(g);
    450                     int s = is.width * is.height;
    451                     if (bounds == null || intersect < s) {
    452                         intersect = s;
    453                         bounds = b;
     433        if (!GraphicsEnvironment.isHeadless()) {
     434            int intersect = 0;
     435            for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
     436                if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
     437                    Rectangle b = gd.getDefaultConfiguration().getBounds();
     438                    if (b.height > 0 && b.width / b.height >= 3) /* multiscreen with wrong definition */ {
     439                        b.width /= 2;
     440                        Rectangle is = b.intersection(g);
     441                        int s = is.width * is.height;
     442                        if (bounds == null || intersect < s) {
     443                            intersect = s;
     444                            bounds = b;
     445                        }
     446                        b = new Rectangle(b);
     447                        b.x += b.width;
     448                        is = b.intersection(g);
     449                        s = is.width * is.height;
     450                        if (intersect < s) {
     451                            intersect = s;
     452                            bounds = b;
     453                        }
     454                    } else {
     455                        Rectangle is = b.intersection(g);
     456                        int s = is.width * is.height;
     457                        if (bounds == null || intersect < s) {
     458                            intersect = s;
     459                            bounds = b;
     460                        }
    454461                    }
    455462                }
     
    468475
    469476    @Override
     477    public int hashCode() {
     478        final int prime = 31;
     479        int result = 1;
     480        result = prime * result + ((extent == null) ? 0 : extent.hashCode());
     481        result = prime * result + ((topLeft == null) ? 0 : topLeft.hashCode());
     482        return result;
     483    }
     484
     485    @Override
     486    public boolean equals(Object obj) {
     487        if (this == obj)
     488            return true;
     489        if (obj == null || getClass() != obj.getClass())
     490            return false;
     491        WindowGeometry other = (WindowGeometry) obj;
     492        if (extent == null) {
     493            if (other.extent != null)
     494                return false;
     495        } else if (!extent.equals(other.extent))
     496            return false;
     497        if (topLeft == null) {
     498            if (other.topLeft != null)
     499                return false;
     500        } else if (!topLeft.equals(other.topLeft))
     501            return false;
     502        return true;
     503    }
     504
     505    @Override
    470506    public String toString() {
    471507        return "WindowGeometry{topLeft="+topLeft+",extent="+extent+'}';
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r8857 r10200  
    77import static org.junit.Assert.assertTrue;
    88
     9import java.awt.Dimension;
     10import java.awt.Point;
    911import java.util.Collection;
    1012
     13import org.junit.BeforeClass;
    1114import org.junit.Test;
    1215import org.openstreetmap.josm.Main.DownloadParamType;
     16import org.openstreetmap.josm.gui.MainApplication;
     17import org.openstreetmap.josm.tools.WindowGeometry;
    1318
    1419/**
     
    1621 */
    1722public class MainTest {
     23
     24    /**
     25     * Setup test.
     26     */
     27    @BeforeClass
     28    public static void setUp() {
     29        JOSMFixture.createUnitTestFixture().init();
     30    }
    1831
    1932    /**
     
    2841        assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
    2942        assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
     43    }
     44
     45    /**
     46     * Unit test of {@code Main#preConstructorInit}.
     47     */
     48    @Test
     49    public void testPreConstructorInit() {
     50        Main.preConstructorInit(MainApplication.buildCommandLineArgumentMap(new String[0]));
     51        Main.preConstructorInit(MainApplication.buildCommandLineArgumentMap(new String[]{"--geometry=400x300+10+5", "--no-maximize"}));
     52        assertEquals(new WindowGeometry(new Point(10, 5), new Dimension(400, 300)), Main.geometry);
    3053    }
    3154
Note: See TracChangeset for help on using the changeset viewer.