EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.scriptbuilder]

COVERAGE SUMMARY FOR SOURCE FILE [ScriptBuilder.java]

nameclass, %method, %block, %line, %
ScriptBuilder.java100% (3/3)92%  (11/12)99%  (308/310)98%  (46/47)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ScriptBuilder100% (1/1)88%  (7/8)99%  (256/258)98%  (40/41)
functionDependencies (OsFamily): Iterable 0%   (0/1)0%   (0/2)0%   (0/1)
ScriptBuilder (): void 100% (1/1)100% (26/26)100% (4/4)
accept (StatementVisitor): void 100% (1/1)100% (16/16)100% (3/3)
addEnvironmentVariableScope (String, Map): ScriptBuilder 100% (1/1)100% (12/12)100% (2/2)
addStatement (Statement): ScriptBuilder 100% (1/1)100% (9/9)100% (2/2)
render (OsFamily): String 100% (1/1)100% (147/147)100% (21/21)
resolveFunctionDependencies (Map, OsFamily): void 100% (1/1)100% (37/37)100% (6/6)
unsetEnvironmentVariable (String): ScriptBuilder 100% (1/1)100% (9/9)100% (2/2)
     
class ScriptBuilder$1100% (1/1)100% (2/2)100% (38/38)100% (4/4)
ScriptBuilder$1 (ScriptBuilder, Map): void 100% (1/1)100% (9/9)100% (1/1)
apply (String): String 100% (1/1)100% (29/29)100% (3/3)
     
class ScriptBuilder$2100% (1/1)100% (2/2)100% (14/14)100% (2/2)
ScriptBuilder$2 (ScriptBuilder, OsFamily): void 100% (1/1)100% (9/9)100% (1/1)
apply (Statement): Iterable 100% (1/1)100% (5/5)100% (1/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.scriptbuilder;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.List;
24import java.util.Map;
25import java.util.Map.Entry;
26 
27import org.jclouds.scriptbuilder.domain.AcceptsStatementVisitor;
28import org.jclouds.scriptbuilder.domain.OsFamily;
29import org.jclouds.scriptbuilder.domain.ShellToken;
30import org.jclouds.scriptbuilder.domain.Statement;
31import org.jclouds.scriptbuilder.domain.StatementVisitor;
32import org.jclouds.scriptbuilder.util.Utils;
33 
34import com.google.common.annotations.VisibleForTesting;
35import com.google.common.base.Function;
36import com.google.common.collect.ImmutableSet;
37import com.google.common.collect.Iterables;
38import com.google.common.collect.Lists;
39import com.google.common.collect.Maps;
40 
41/**
42 * Creates a shell script.
43 * 
44 * @author Adrian Cole
45 */
46public class ScriptBuilder implements Statement, AcceptsStatementVisitor {
47 
48   @VisibleForTesting
49   List<Statement> statements = Lists.newArrayList();
50 
51   @VisibleForTesting
52   Map<String, Map<String, String>> variableScopes = Maps.newLinkedHashMap();
53 
54   @VisibleForTesting
55   List<String> variablesToUnset = Lists.newArrayList("path", "javaHome", "libraryPath");
56 
57   public ScriptBuilder addStatement(Statement statement) {
58      statements.add(checkNotNull(statement, "statement"));
59      return this;
60   }
61 
62   /**
63    * Unsets a variable to ensure it is set within the script.
64    */
65   public ScriptBuilder unsetEnvironmentVariable(String name) {
66      variablesToUnset.add(checkNotNull(name, "name"));
67      return this;
68   }
69 
70   /**
71    * Exports a variable inside the script
72    */
73   public ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables) {
74      variableScopes.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
75      return this;
76   }
77 
78   /**
79    * builds the shell script, by adding the following
80    * <ol>
81    * <li>shell declaration line</li>
82    * <li>variable exports</li>
83    * <li>case/switch</li>
84    * </ol>
85    * 
86    * @param osFamily
87    *           whether to write a cmd or bash script.
88    */
89 
90   @Override
91   public String render(OsFamily osFamily) {
92      Map<String, String> functions = Maps.newLinkedHashMap();
93      functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily));
94 
95      for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) {
96         functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry
97                  .getValue())));
98      }
99      final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily);
100      StringBuilder builder = new StringBuilder();
101      builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily));
102      builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform(variablesToUnset,
103               new Function<String, String>() {
104                  @Override
105                  public String apply(String from) {
106                     if (tokenValueMap.containsKey(from + "Variable"))
107                        return Utils.FUNCTION_UPPER_UNDERSCORE_TO_LOWER_CAMEL.apply(tokenValueMap
108                                 .get(from + "Variable"));
109                     return from;
110                  }
111 
112               })), osFamily));
113      resolveFunctionDependencies(functions, osFamily);
114      if (functions.size() > 0) {
115         builder.append(ShellToken.BEGIN_FUNCTIONS.to(osFamily));
116         for (String function : functions.values()) {
117            builder.append(Utils.replaceTokens(function, tokenValueMap));
118         }
119         builder.append(ShellToken.END_FUNCTIONS.to(osFamily));
120      }
121      builder.append(Utils.writeZeroPath(osFamily));
122      StringBuilder statementBuilder = new StringBuilder();
123      for (Statement statement : statements) {
124         statementBuilder.append(statement.render(osFamily));
125      }
126      builder.append(statementBuilder.toString().replaceAll(ShellToken.RETURN.to(osFamily),
127               ShellToken.EXIT.to(osFamily)));
128      builder.append(ShellToken.END_SCRIPT.to(osFamily));
129      return builder.toString();
130   }
131 
132   @VisibleForTesting
133   void resolveFunctionDependencies(Map<String, String> functions, final OsFamily osFamily) {
134      Iterable<String> dependentFunctions = Iterables.concat(Iterables.transform(statements,
135               new Function<Statement, Iterable<String>>() {
136                  @Override
137                  public Iterable<String> apply(Statement from) {
138                     return from.functionDependencies(osFamily);
139                  }
140               }));
141      List<String> unresolvedFunctions = Lists.newArrayList(dependentFunctions);
142      Iterables.removeAll(unresolvedFunctions, functions.keySet());
143      for (String functionName : unresolvedFunctions) {
144         functions.put(functionName, Utils.writeFunctionFromResource(functionName, osFamily));
145      }
146   }
147 
148   @Override
149   public Iterable<String> functionDependencies(OsFamily family) {
150      return ImmutableSet.<String> of();
151   }
152   
153   @Override
154   public void accept(StatementVisitor visitor) {
155      for (Statement statement : statements) {
156         visitor.visit(statement);
157      }
158   }
159}

[all classes][org.jclouds.scriptbuilder]
EMMA 2.0.5312 (C) Vladimir Roubtsov