Clover Coverage Report - Liquibase Core 2.0.3-SNAPSHOT
Coverage timestamp: Sat Aug 6 2011 11:33:15 EDT
../../img/srcFileCovDistChart8.png 14% of files have more coverage
27   99   15   3.38
6   70   0.56   8
8     1.88  
1    
 
  ChangeFactory       Line # 19 27 0% 15 10 75.6% 0.75609756
 
  (18)
 
1    package liquibase.change;
2   
3    import java.util.Comparator;
4    import java.util.Map;
5    import java.util.SortedSet;
6    import java.util.TreeSet;
7    import java.util.concurrent.ConcurrentHashMap;
8   
9    import liquibase.exception.UnexpectedLiquibaseException;
10    import liquibase.servicelocator.ServiceLocator;
11   
12    /**
13    * Factory class for constructing the correct liquibase.change.Change implementation based on the tag name. It is
14    * currently implemented by a static array of Change implementations, although that may change in later revisions. The
15    * best way to get an instance of ChangeFactory is off the Liquibase.getChangeFactory() method.
16    *
17    * @see liquibase.change.Change
18    */
 
19    public class ChangeFactory {
20   
21    private static ChangeFactory instance;
22   
23    private Map<String, SortedSet<Class<? extends Change>>> registry = new ConcurrentHashMap<String, SortedSet<Class<? extends Change>>>();
24   
 
25  9 toggle private ChangeFactory() {
26  9 Class<? extends Change>[] classes;
27  9 try {
28  9 classes = ServiceLocator.getInstance().findClasses(Change.class);
29   
30  9 for (Class<? extends Change> clazz : classes) {
31    // noinspection unchecked
32  387 register(clazz);
33    }
34   
35    } catch (Exception e) {
36  0 throw new UnexpectedLiquibaseException(e);
37    }
38   
39    }
40   
41    /**
42    * Return singleton SqlGeneratorFactory
43    */
 
44  38 toggle public static synchronized ChangeFactory getInstance() {
45  38 if (instance == null) {
46  0 instance = new ChangeFactory();
47    }
48  38 return instance;
49    }
50   
 
51  9 toggle public static void reset() {
52  9 instance = new ChangeFactory();
53    }
54   
 
55  394 toggle public void register(Class<? extends Change> changeClass) {
56  394 try {
57  394 String name = changeClass.newInstance().getChangeMetaData().getName();
58  394 if (registry.get(name) == null) {
59  394 registry.put(name, new TreeSet<Class<? extends Change>>(new Comparator<Class<? extends Change>>() {
 
60  0 toggle public int compare(Class<? extends Change> o1, Class<? extends Change> o2) {
61  0 try {
62  0 return -1
63    * new Integer(o1.newInstance().getChangeMetaData().getPriority()).compareTo(o2
64    .newInstance().getChangeMetaData().getPriority());
65    } catch (Exception e) {
66  0 throw new UnexpectedLiquibaseException(e);
67    }
68    }
69    }));
70    }
71  394 registry.get(name).add(changeClass);
72    } catch (Exception e) {
73  0 throw new UnexpectedLiquibaseException(e);
74    }
75    }
76   
 
77  2 toggle public void unregister(String name) {
78  2 registry.remove(name);
79    }
80   
 
81  13 toggle public Map<String, SortedSet<Class<? extends Change>>> getRegistry() {
82  13 return registry;
83    }
84   
 
85  25 toggle public Change create(String name) {
86  25 SortedSet<Class<? extends Change>> classes = registry.get(name);
87   
88  25 if (classes == null) {
89  1 return null;
90    }
91   
92  24 try {
93  24 return classes.iterator().next().newInstance();
94    } catch (Exception e) {
95  0 throw new UnexpectedLiquibaseException(e);
96    }
97    }
98   
99    }