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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! **libwgetj API**
//!
//! Wraps wget with some cookie management to allow downloading Java releases from the command line.
//!
//! # Examples
//!
//! ```
//! use libwgetj::{DownloadConfig, latest};
//! use libwgetj::Arch::*;
//! use libwgetj::Archive::*;
//! use libwgetj::OS::*;
//! use libwgetj::Package::*;
//! use libwgetj::Version::*;
//!
//! // Setup a dry run (don't actually download) for the default Java 8 JDK 64-bit Linux tar.gz
//! // (latest point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//!
//! // Download Java 8 JDK 64-bit Linux tar.gz (older point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.point_release(40).dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//!
//! // Download Java 8 JDK 64-bit Mac OSX dmg (latest point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.archive(DMG).os(Mac).dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//!
//! // Download Java 8 JDK 32-bit Windows exe (latest point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.archive(EXE).os(Windows).arch(I586).dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//!
//! // Download Java 8 JRE 32-bit Linux rpm (latest point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.archive(RPM).arch(I586).package(JRE).dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//!
//! // Download Java 7 JDK 64-bit Linux tar.gz (latest point release).
//! let mut cfg: DownloadConfig = Default::default();
//! cfg.version(Seven).point_release(latest(Seven)).dry_run(true);
//! match cfg.download() {
//!     Ok(res) => assert!(res == 0),
//!     Err(e)  => { println!("{:?}", e); assert!(false) },
//! }
//! ```
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![cfg_attr(feature="clippy", deny(clippy, clippy_pedantic))]
#![deny(missing_docs)]
extern crate commandext;
#[macro_use]
extern crate log;

use commandext::CommandExt;
use cookies::*;
use std::borrow::Borrow;
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};

// Re-exports
pub use errors::LibwgetjError;
pub use types::Arch;
pub use types::Arch::*;
pub use types::Archive;
pub use types::Archive::*;
pub use types::OS;
pub use types::OS::*;
pub use types::Package;
pub use types::Package::*;
pub use types::Version;
pub use types::Version::*;
pub use version::version;

mod cookies;
mod errors;
mod types;
mod version;

/// Base Download URL
static BASEURL: &'static str = "http://download.oracle.com/otn-pub/java/jdk/";

/// The Java download configuration. The download function will read from this configuration and
/// build a url specific to the configuration.
///
/// # Defaults
///
/// * Package: JDK
/// * Version: 8
/// * Point Release: Latest for 8
/// * OS: Linux
/// * CPU Arch: 64-bit
/// * Archive Type: .tar.gz
/// * Download Directory: None
/// * Dry Run: false
///
pub struct DownloadConfig {
    /// Package (JDK or JRE)
    pkg: Package,
    /// Version (Seven or Eight)
    ver: Version,
    /// Point Release
    pr: u8,
    /// OS Type (Linux, Mac OS, or Windows)
    os: OS,
    /// OS Architecture (32 or 64)
    arch: Arch,
    /// Archive Type (tar.gz, or .exe, or .dmg)
    am: Archive,
    /// Directory to save to
    dir: Option<PathBuf>,
    /// Dry Run (don't actually download)
    dr: bool,
}

impl Default for DownloadConfig {
    /// Create a new download configuration with some defaults.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// ```
    fn default() -> DownloadConfig {
        DownloadConfig {
            pkg: JDK,
            ver: Eight,
            pr: latest(Eight),
            os: Linux,
            arch: X64,
            am: TGZ,
            dir: None,
            dr: false,
        }
    }
}

impl DownloadConfig {
    /// Set the package type.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::Package::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.package(JRE);
    /// ```
    pub fn package(&mut self, pkg: Package) -> &mut DownloadConfig {
        self.pkg = pkg;
        debug!("Package Set: {}", pkg);
        self
    }

    /// Set the version.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::Version::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.version(Seven);
    /// ```
    pub fn version(&mut self, ver: Version) -> &mut DownloadConfig {
        self.ver = ver;
        debug!("Version Set: {}", ver);
        self
    }

    /// Set the point release information.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::{DownloadConfig, latest};
    /// use libwgetj::Version::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.version(Seven).point_release(latest(Seven));
    /// ```
    pub fn point_release(&mut self, pr: u8) -> &mut DownloadConfig {
        self.pr = pr;
        self
    }

    /// Set the OS type.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::OS::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.os(Windows);
    /// ```
    pub fn os(&mut self, os: OS) -> &mut DownloadConfig {
        self.os = os;
        self
    }

    /// Set the architecture (32 or 64-bit).
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::Arch::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.arch(I586);
    /// ```
    pub fn arch(&mut self, arch: Arch) -> &mut DownloadConfig {
        self.arch = arch;
        self
    }

    /// Set the archive type.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::Archive::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.archive(EXE);
    /// ```
    pub fn archive(&mut self, archive: Archive) -> &mut DownloadConfig {
        self.am = archive;
        self
    }

    /// Set the target directory for the download.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use std::env;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.dir(Some(env::temp_dir()));
    /// ```
    pub fn dir(&mut self, path: Option<PathBuf>) -> &mut DownloadConfig {
        self.dir = path;
        self
    }

    /// Set the dry run status.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// cfg.dry_run(true);
    /// ```
    pub fn dry_run(&mut self, enable: bool) -> &mut DownloadConfig {
        self.dr = enable;
        self
    }

    /// Check the configuration for invalid combinations.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    /// use libwgetj::Archive::*;
    /// use libwgetj::OS::*;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// // Invalid combination, Mac only has DMG downloads.
    /// cfg.os(Mac).archive(TGZ).dry_run(true);
    /// assert!(cfg.check_config().is_err());
    /// ```
    pub fn check_config(&self) -> Result<(), LibwgetjError> {
        let mut errors = Vec::new();
        let os = self.os;

        match self.arch {
            I586 if os == Mac => {
                errors.push("Java only has 64-bit distributions for Mac");
            }
            _ => {}
        }

        match self.am {
            TGZ if os == Mac || os == Windows => {
                errors.push("Mac/Windows distributions not available in .tar.gz format");
            }
            RPM if os == Mac || os == Windows => {
                errors.push("Mac/Windows distributions not available in .rpm format");
            }
            DMG if os == Linux || os == Windows => {
                errors.push("Linux/Windows distributions not available in .dmg format");
            }
            EXE if os == Linux || os == Mac => {
                errors.push("Linux/Mac distributions not available in .exe format");
            }
            _ => {}
        }

        let pr = self.ver.point_releases();

        if !pr.contains_key(&self.pr) {
            errors.push("Invalid point release for the given major version");
        }

        if errors.is_empty() {
            Ok(())
        } else {
            let mut error_str = String::new();
            for error in &errors {
                error_str.push_str(error);
                error_str.push('\n');
            }

            Err(LibwgetjError::new("Invalid Configuration", error_str))
        }
    }

    /// Generate a command result.
    fn gen_result(&self) -> fn(cmd: &mut Command) -> Result<i32, LibwgetjError> {
        /// Inner Result Function
        fn resfn(cmd: &mut Command) -> Result<i32, LibwgetjError> {
            cmd.stdout(Stdio::inherit());
            cmd.stderr(Stdio::inherit());

            match cmd.spawn() {
                Ok(mut p) => {
                    match p.wait() {
                        Ok(status) => {
                            match status.code() {
                                Some(code) => {
                                    if code == 0 {
                                        Ok(code)
                                    } else {
                                        Err(LibwgetjError::new("Command Failed", code))
                                    }
                                }
                                None => Err(LibwgetjError::new("Command Failed", "Unknown code!")),
                            }
                        }
                        Err(e) => Err(LibwgetjError::new("Command Failed", e)),
                    }
                }
                Err(e) => Err(LibwgetjError::new("Command Failed", e)),
            }
        };
        resfn
    }

    /// Execute the download.
    ///
    /// # Examples
    ///
    /// ```
    /// use libwgetj::DownloadConfig;
    ///
    /// let mut cfg: DownloadConfig = Default::default();
    /// assert!(cfg.dry_run(true).download().is_ok());
    /// ```
    pub fn download(&self) -> Result<i32, LibwgetjError> {
        let url = try!(build_url(self));

        if self.dr {
            info!("Not downloading: {}", url);
            Ok(0)
        } else {
            delete_tmp_files().and(write_tmp_files(self).and_then(|p| {
                let tmp = env::temp_dir();
                let d = match self.dir {
                    Some(ref d) => d,
                    None => &tmp,
                };
                CommandExt::new("wget")
                    .arg("-c")
                    .arg("--load-cookies")
                    .arg(p.to_string_lossy().borrow())
                    .arg("-P")
                    .arg(d.to_string_lossy().borrow())
                    .arg(&url[..])
                    .header(true)
                    .exec(self.gen_result())
                    .and(delete_tmp_files())
            }))
        }
    }
}

/// Produce the latest point release version for each supported major version.
///
/// # Examples
///
/// ```
/// use libwgetj::{DownloadConfig, latest};
/// use libwgetj::Version::*;
///
/// let mut cfg: DownloadConfig = Default::default();
/// cfg.version(Seven).point_release(latest(Seven));
/// ```
pub fn latest(ver: Version) -> u8 {
    if let Some(pr) = ver.point_releases().keys().max() {
        *pr
    } else {
        error!("Unable to determine latest point release!");
        0
    }
}

/// Generate the download URL version.
fn vs(cfg: &DownloadConfig) -> String {
    if cfg.pr == 0 {
        format!("{}", cfg.ver)
    } else {
        format!("{}u{}", cfg.ver, cfg.pr)
    }
}

/// Generate the download URL suffix
fn suffix(cfg: &DownloadConfig) -> String {
    let pr = cfg.ver.point_releases();
    let mut res = String::new();
    if let Some(s) = pr.get(&cfg.pr) {
        res = format!("-b{:02}", s);

        if cfg.ver == Eight && cfg.pr == 121 {
            res.push_str("/e9e7ea248e2c4826b92b3f075a80e441")
        }
    }
    res
}

/// Build the download URL from the component parts
fn build_url(cfg: &DownloadConfig) -> Result<String, LibwgetjError> {
    try!(cfg.check_config());
    let vs = vs(cfg);
    Ok(format!("{}{}{}/{}-{}-{}-{}.{}",
               BASEURL,
               vs,
               suffix(cfg),
               cfg.pkg,
               vs,
               cfg.os,
               cfg.arch,
               cfg.am))
}

#[cfg(test)]
#[cfg_attr(feature="clippy", allow(stutter))]
mod test {
    use super::*;
    use super::build_url;

    static A: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-linux-x64.tar.gz";
    static B: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-linux-i586.tar.gz";
    static C: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-linux-x64.rpm";
    static D: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-linux-i586.rpm";
    static E: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-macosx-x64.dmg";
    static F: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-windows-x64.exe";
    static G: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jdk-8u121-windows-i586.exe";
    static H: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-linux-x64.tar.gz";
    static I: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-linux-i586.tar.gz";
    static J: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-linux-x64.rpm";
    static K: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-linux-i586.rpm";
    static L: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-macosx-x64.dmg";
    static M: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-windows-x64.exe";
    static N: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/\
                              jre-8u121-windows-i586.exe";

    static O: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-x64.tar.gz";
    static P: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-i586.tar.gz";
    static Q: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-x64.rpm";
    static R: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-i586.rpm";
    static S: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-macosx-x64.dmg";
    static T: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-windows-x64.exe";
    static U: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jdk-7u80-windows-i586.exe";
    static V: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jre-7u80-linux-x64.tar.gz";
    static W: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jre-7u80-linux-i586.tar.gz";
    static X: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jre-7u80-linux-x64.rpm";
    static Y: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jre-7u80-linux-i586.rpm";
    static Z: &'static str = "http://download.oracle.\
                              com/otn-pub/java/jdk/7u80-b15/jre-7u80-macosx-x64.dmg";
    static AA: &'static str = "http://download.oracle.\
                               com/otn-pub/java/jdk/7u80-b15/jre-7u80-windows-x64.exe";
    static AB: &'static str = "http://download.oracle.\
                               com/otn-pub/java/jdk/7u80-b15/jre-7u80-windows-i586.exe";

    #[test]
    fn url_suffix_7() {
        let pr = Seven.point_releases();

        // Java 7 mappings
        assert_eq!(Some(&0), pr.get(&0));
        assert_eq!(Some(&8), pr.get(&1));
        assert_eq!(Some(&13), pr.get(&2));
        assert_eq!(Some(&4), pr.get(&3));
        assert_eq!(Some(&20), pr.get(&4));
        assert_eq!(Some(&6), pr.get(&5));
        assert_eq!(Some(&24), pr.get(&6));
        assert_eq!(Some(&10), pr.get(&7));
        assert_eq!(Some(&5), pr.get(&9));
        assert_eq!(Some(&18), pr.get(&10));
        assert_eq!(Some(&21), pr.get(&11));
        assert_eq!(Some(&20), pr.get(&13));
        assert_eq!(Some(&3), pr.get(&15));
        assert_eq!(Some(&2), pr.get(&17));
        assert_eq!(Some(&11), pr.get(&21));
    }

    #[test]
    fn url_suffix_7_1() {
        let pr = Seven.point_releases();

        assert_eq!(Some(&15), pr.get(&25));
        assert_eq!(Some(&43), pr.get(&40));
        assert_eq!(Some(&18), pr.get(&45));
        assert_eq!(Some(&13), pr.get(&51));
        assert_eq!(Some(&13), pr.get(&55));
        assert_eq!(Some(&19), pr.get(&60));
        assert_eq!(Some(&17), pr.get(&65));
        assert_eq!(Some(&1), pr.get(&67));
        assert_eq!(Some(&14), pr.get(&71));
        assert_eq!(Some(&14), pr.get(&72));
        assert_eq!(Some(&13), pr.get(&75));
        assert_eq!(Some(&13), pr.get(&76));
        assert_eq!(Some(&15), pr.get(&79));
        assert_eq!(Some(&15), pr.get(&80));
    }

    #[test]
    fn url_suffix_8() {
        let pr = Eight.point_releases();

        // Java 8 mappings
        assert_eq!(Some(&132), pr.get(&0));
        assert_eq!(Some(&13), pr.get(&5));
        assert_eq!(Some(&12), pr.get(&11));
        assert_eq!(Some(&26), pr.get(&20));
        assert_eq!(Some(&17), pr.get(&25));
        assert_eq!(Some(&13), pr.get(&31));
        assert_eq!(Some(&26), pr.get(&40));
        assert_eq!(Some(&14), pr.get(&45));
        assert_eq!(Some(&16), pr.get(&51));
        assert_eq!(Some(&27), pr.get(&60));
        assert_eq!(Some(&17), pr.get(&65));
        assert_eq!(Some(&17), pr.get(&66));
        assert_eq!(Some(&15), pr.get(&71));
        assert_eq!(Some(&2), pr.get(&74));
        assert_eq!(Some(&3), pr.get(&77));
        assert_eq!(Some(&14), pr.get(&91));
        assert_eq!(Some(&14), pr.get(&92));
        assert_eq!(Some(&13), pr.get(&101));
        assert_eq!(Some(&14), pr.get(&102));
        assert_eq!(Some(&15), pr.get(&112));
    }

    #[test]
    fn jdk8_build_url() {
        let mut cfg: DownloadConfig = Default::default();

        // 8u JDKs
        assert_eq!(Ok(A.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(B.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.archive(Archive::RPM);
        assert_eq!(Ok(C.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(D.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.os(OS::Mac);
        cfg.archive(Archive::DMG);
        assert_eq!(Ok(E.to_owned()), build_url(&cfg));
        cfg.os(OS::Windows);
        cfg.archive(Archive::EXE);
        assert_eq!(Ok(F.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(G.to_owned()), build_url(&cfg));
    }

    #[test]
    fn jre8_build_url() {
        let mut cfg: DownloadConfig = Default::default();

        // 8u JREs
        cfg.package(Package::JRE);
        assert_eq!(Ok(H.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(I.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.archive(Archive::RPM);
        assert_eq!(Ok(J.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(K.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.os(OS::Mac);
        cfg.archive(Archive::DMG);
        assert_eq!(Ok(L.to_owned()), build_url(&cfg));
        cfg.os(OS::Windows);
        cfg.archive(Archive::EXE);
        assert_eq!(Ok(M.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(N.to_owned()), build_url(&cfg));
    }

    #[test]
    fn jdk7_build_url() {
        let mut cfg: DownloadConfig = Default::default();

        // 7u80 JDKs
        cfg.version(Version::Seven);
        cfg.point_release(80);
        assert_eq!(Ok(O.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(P.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.archive(Archive::RPM);
        assert_eq!(Ok(Q.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(R.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.os(OS::Mac);
        cfg.archive(Archive::DMG);
        assert_eq!(Ok(S.to_owned()), build_url(&cfg));
        cfg.os(OS::Windows);
        cfg.archive(Archive::EXE);
        assert_eq!(Ok(T.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(U.to_owned()), build_url(&cfg));
    }

    #[test]
    fn jre7_build_url() {
        let mut cfg: DownloadConfig = Default::default();

        // 7u80 JREs
        cfg.version(Version::Seven);
        cfg.point_release(80);
        cfg.package(Package::JRE);
        assert_eq!(Ok(V.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(W.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.archive(Archive::RPM);
        assert_eq!(Ok(X.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(Y.to_owned()), build_url(&cfg));
        cfg.arch(Arch::X64);
        cfg.os(OS::Mac);
        cfg.archive(Archive::DMG);
        assert_eq!(Ok(Z.to_owned()), build_url(&cfg));
        cfg.os(OS::Windows);
        cfg.archive(Archive::EXE);
        assert_eq!(Ok(AA.to_owned()), build_url(&cfg));
        cfg.arch(Arch::I586);
        assert_eq!(Ok(AB.to_owned()), build_url(&cfg));
    }

    #[test]
    fn bad_archive() {
        let mut cfg: DownloadConfig = Default::default();
        // exe archive not valid for linux.
        cfg.archive(Archive::EXE);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_1() {
        let mut cfg: DownloadConfig = Default::default();
        // dmg archive not valid for linux.
        cfg.archive(Archive::DMG);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_2() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Mac);
        // tar.gz archive not valid for macosx.
        cfg.archive(Archive::TGZ);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_3() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Mac);
        // rpm archive not valid for macosx.
        cfg.archive(Archive::RPM);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_4() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Mac);
        // exe archive not valid for macosx.
        cfg.archive(Archive::EXE);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_5() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Windows);
        // tar.gz archive not valid for windows.
        cfg.archive(Archive::TGZ);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_6() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Windows);
        // rpm archive not valid for windows.
        cfg.archive(Archive::RPM);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_archive_7() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Windows);
        // dmb archive not valid for windows.
        cfg.archive(Archive::DMG);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn bad_arch_mac() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.os(OS::Mac);
        // 32-bit not valid for macosx.
        cfg.arch(Arch::I586);
        match cfg.check_config() {
            Ok(_) => assert!(false),
            Err(_) => assert!(true),
        }
    }

    #[test]
    fn dry_run() {
        let mut cfg: DownloadConfig = Default::default();
        cfg.dry_run(true);

        // dry run
        assert_eq!(Ok(0), cfg.download());
    }
}