Changeset 7030 in josm for trunk/test/unit/org


Ignore:
Timestamp:
2014-04-29T18:06:29+02:00 (10 years ago)
Author:
Don-vip
Message:

see #8465 - use of try-with-resource

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java

    r7005 r7030  
    1818import java.util.List;
    1919import java.util.Map;
    20 import java.util.Map.Entry;
    2120import java.util.Random;
    2221import java.util.Set;
     
    9089
    9190        Random rand = new Random();
    92         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PROJECTION_DATA_FILE), Utils.UTF_8));
    93         out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
    94         out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
    95         for (Entry<String, Projection> e : supportedCodesMap.entrySet()) {
    96         }
    97         for (String code : codesToWrite) {
    98             Projection proj = supportedCodesMap.get(code);
    99             Bounds b = proj.getWorldBoundsLatLon();
    100             double lat, lon;
    101             TestData prev = prevCodesMap.get(proj.toCode());
    102             if (prev != null) {
    103                 lat = prev.ll.lat();
    104                 lon = prev.ll.lon();
    105             } else {
    106                 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
    107                 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
    108             }
    109             EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
    110             LatLon ll2 = proj.eastNorth2latlon(en);
    111             out.write(String.format("%s%n  ll  %s %s%n  en  %s %s%n  ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
    112         }
    113         out.close();
     91        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PROJECTION_DATA_FILE), Utils.UTF_8))) {
     92            out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
     93            out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
     94            for (String code : codesToWrite) {
     95                Projection proj = supportedCodesMap.get(code);
     96                Bounds b = proj.getWorldBoundsLatLon();
     97                double lat, lon;
     98                TestData prev = prevCodesMap.get(proj.toCode());
     99                if (prev != null) {
     100                    lat = prev.ll.lat();
     101                    lon = prev.ll.lon();
     102                } else {
     103                    lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
     104                    lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
     105                }
     106                EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
     107                LatLon ll2 = proj.eastNorth2latlon(en);
     108                out.write(String.format("%s%n  ll  %s %s%n  en  %s %s%n  ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
     109            }
     110        }
    114111    }
    115112
    116113    private static List<TestData> readData() throws IOException, FileNotFoundException {
    117         BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(PROJECTION_DATA_FILE), Utils.UTF_8));
    118         List<TestData> result = new ArrayList<>();
    119         String line;
    120         while ((line = in.readLine()) != null) {
    121             if (line.startsWith("#")) {
    122                 continue;
    123             }
    124             TestData next = new TestData();
    125 
    126             Pair<Double,Double> ll = readLine("ll", in.readLine());
    127             Pair<Double,Double> en = readLine("en", in.readLine());
    128             Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
    129 
    130             next.code = line;
    131             next.ll = new LatLon(ll.a, ll.b);
    132             next.en = new EastNorth(en.a, en.b);
    133             next.ll2 = new LatLon(ll2.a, ll2.b);
    134 
    135             result.add(next);
    136         }
    137         in.close();
    138         return result;
     114        try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(PROJECTION_DATA_FILE), Utils.UTF_8))) {
     115            List<TestData> result = new ArrayList<>();
     116            String line;
     117            while ((line = in.readLine()) != null) {
     118                if (line.startsWith("#")) {
     119                    continue;
     120                }
     121                TestData next = new TestData();
     122   
     123                Pair<Double,Double> ll = readLine("ll", in.readLine());
     124                Pair<Double,Double> en = readLine("en", in.readLine());
     125                Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
     126   
     127                next.code = line;
     128                next.ll = new LatLon(ll.a, ll.b);
     129                next.en = new EastNorth(en.a, en.b);
     130                next.ll2 = new LatLon(ll2.a, ll2.b);
     131   
     132                result.add(next);
     133            }
     134            return result;
     135        }
    139136    }
    140137
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r6920 r7030  
    7979    public void testOpenUrlGzip() throws Exception {
    8080        Main.initApplicationPreferences();
    81         final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/1613906/data"), true);
    82         Assert.assertTrue(x.readLine().startsWith("<?xml version="));
    83         x.close();
     81        try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/1613906/data"), true)) {
     82            Assert.assertTrue(x.readLine().startsWith("<?xml version="));
     83        }
    8484    }
    8585
     
    8787    public void testOpenUrlBzip() throws Exception {
    8888        Main.initApplicationPreferences();
    89         final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/785544/data"), true);
    90         Assert.assertTrue(x.readLine().startsWith("<?xml version="));
    91         x.close();
     89        try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/785544/data"), true)) {
     90            Assert.assertTrue(x.readLine().startsWith("<?xml version="));
     91        }
    9292    }
    9393
Note: See TracChangeset for help on using the changeset viewer.